-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: add AttachmentGrid component #10561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DPandyan
wants to merge
7
commits into
main
Choose a base branch
from
attachmentgrid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+324
−3
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6c15536
add: AttachmentGrid component
a130824
fix: remove extra focus
230b313
fix: remove padding on story
a9f231b
fix: add scrollbar + try gap fixes
aea82f2
fix: lint + remove custom scroll bars
c9651b5
fix: align + remove UserMessage story
6edbbf8
fix: remove renderprops
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import {AriaLabelingProps, DOMProps, DOMRef, forwardRefType} from '@react-types/shared'; | ||
| import {AttachmentCard, AttachmentPreviewContext, isAttachmentLoading} from './AttachmentList'; | ||
| import {filterDOMProps} from 'react-aria/filterDOMProps'; | ||
| import {focusRing, style} from '@react-spectrum/s2/style' with {type: 'macro'}; | ||
| import {forwardRef, ReactNode} from 'react'; | ||
| import {ListBox, ListBoxItem, ListBoxItemProps, ListBoxProps} from 'react-aria-components/ListBox'; | ||
| import {mergeStyles} from '@react-spectrum/s2/mergeStyles'; | ||
| import {scrollFade} from './tokens.macro' with {type: 'macro'}; | ||
| import {StyleString} from '@react-spectrum/s2/style' with {type: 'macro'}; | ||
| import {useDOMRef} from './useDOMRef'; | ||
|
|
||
| export interface AttachmentGridProps<T> | ||
| extends | ||
| DOMProps, | ||
| AriaLabelingProps, | ||
| Pick<ListBoxProps<T>, 'items' | 'children' | 'dependencies'> { | ||
| /** | ||
| * The alignment of attachments within the grid. | ||
| * | ||
| * @default 'start' | ||
| */ | ||
| align?: 'start' | 'center' | 'end'; | ||
| /** | ||
| * Spectrum-defined styles, returned by the `style()` macro. | ||
| */ | ||
| styles?: StyleString; | ||
| } | ||
|
|
||
| // Grid items are expected to be either all thumbnails or cards and the same size. | ||
| const hasContent = ':has([data-slot=content])'; | ||
|
|
||
| const gridStyles = style({ | ||
| display: { | ||
| default: 'flex', | ||
| [hasContent]: 'grid' | ||
| }, | ||
| flexWrap: 'wrap', | ||
| alignItems: 'start', | ||
| gridTemplateColumns: { | ||
| [hasContent]: 'repeat(auto-fill, minmax(240px, 1fr))' | ||
| }, | ||
| justifyContent: { | ||
| align: { | ||
| start: 'normal', | ||
| center: 'center', | ||
| end: 'end' | ||
| } | ||
| }, | ||
| gap: 8, | ||
| maxHeight: 240, | ||
| overflowY: 'auto', | ||
| overflowX: 'clip', | ||
| boxSizing: 'border-box', | ||
| ...focusRing() | ||
| }); | ||
|
|
||
| /** | ||
| * An AttachmentGrid displays file attachments as a wrapping, vertically-scrolling grid of | ||
| * thumbnails. Unlike AttachmentList, it is display-only and does not support selection or removal. | ||
| * Every attachment is disabled, so the grid itself becomes the sole tab stop, keeping the | ||
| * overflow area keyboard-scrollable without letting individual attachments be focused or actioned. | ||
| */ | ||
| export const AttachmentGrid = (forwardRef as forwardRefType)(function AttachmentGrid<T>( | ||
| props: AttachmentGridProps<T>, | ||
| ref: DOMRef<HTMLDivElement> | ||
| ) { | ||
| let {styles, items, children, dependencies, align = 'start', ...otherProps} = props; | ||
| let domRef = useDOMRef(ref); | ||
|
|
||
| return ( | ||
| <ListBox | ||
| {...otherProps} | ||
| layout="grid" | ||
| items={items} | ||
| dependencies={dependencies} | ||
| ref={domRef} | ||
| className={renderProps => | ||
| mergeStyles(gridStyles({...renderProps, align}), styles) + ' ' + scrollFade({y: 36}) | ||
| }> | ||
| {children} | ||
| </ListBox> | ||
| ); | ||
| }); | ||
|
|
||
| export interface AttachmentGridItemProps | ||
| extends AriaLabelingProps, Pick<ListBoxItemProps, 'id' | 'textValue'> { | ||
| /** The size of the Card. */ | ||
| size?: 'XS' | 'S' | 'M' | 'L' | 'XL'; | ||
| /** Whether the attachment has an error. */ | ||
| isInvalid?: boolean; | ||
| uploadProgress?: number; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing description |
||
| /** The children of the AttachmentGridItem. */ | ||
| children: ReactNode; | ||
| /** | ||
| * Spectrum-defined styles, returned by the `style()` macro. | ||
| */ | ||
| styles?: StyleString; | ||
| } | ||
|
|
||
| const itemStyles = style({ | ||
| flexShrink: 0, | ||
| flexGrow: 0, | ||
| position: 'relative', | ||
| borderRadius: 'lg' | ||
| }); | ||
|
|
||
| /** | ||
| * AttachmentGridItem displays an individual file attachment thumbnail within an AttachmentGrid. | ||
| */ | ||
| export const AttachmentGridItem = forwardRef(function AttachmentGridItem( | ||
| props: AttachmentGridItemProps, | ||
| ref: DOMRef<HTMLDivElement> | ||
| ) { | ||
| let {id, textValue, styles, isInvalid, size = 'M', children, ...otherProps} = props; | ||
| let domRef = useDOMRef(ref); | ||
| let isLoading = isAttachmentLoading(props.uploadProgress); | ||
|
|
||
| return ( | ||
| <ListBoxItem | ||
| id={id} | ||
| {...filterDOMProps(otherProps, {labelable: true})} | ||
| textValue={textValue} | ||
| isDisabled | ||
| ref={domRef} | ||
| className={mergeStyles(itemStyles, styles)}> | ||
| <AttachmentCard size={size} isInvalid={isInvalid} isLoading={isLoading}> | ||
| <AttachmentPreviewContext.Provider | ||
| value={{isInvalid: !!isInvalid, uploadProgress: props.uploadProgress ?? 100, size}}> | ||
| {children} | ||
| </AttachmentPreviewContext.Provider> | ||
| </AttachmentCard> | ||
| </ListBoxItem> | ||
| ); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/@react-spectrum/ai/stories/AttachmentGrid.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import { | ||
| AttachmentGrid, | ||
| AttachmentGridItem, | ||
| AttachmentGridItemProps, | ||
| AttachmentGridProps | ||
| } from '../src/AttachmentGrid'; | ||
| import {AttachmentPreview} from '../src/AttachmentList'; | ||
| import {Content} from '@react-spectrum/s2/Content'; | ||
| import type {Meta, StoryObj} from '@storybook/react'; | ||
| import {style} from '@react-spectrum/s2/style' with {type: 'macro'}; | ||
| import {Text} from '@react-spectrum/s2/Text'; | ||
|
|
||
| interface AttachmentGridDemoProps | ||
| extends | ||
| Pick<AttachmentGridItemProps, 'isInvalid' | 'uploadProgress' | 'size'>, | ||
| Pick<AttachmentGridProps<unknown>, 'align'> { | ||
| /** Number of demo attachments to render. */ | ||
| count: number; | ||
| /** Whether to show title/description content below the thumbnail. */ | ||
| showCardContent?: boolean; | ||
| } | ||
|
|
||
| function AttachmentGridDemo({ | ||
| count, | ||
| isInvalid, | ||
| uploadProgress, | ||
| showCardContent, | ||
| size, | ||
| align | ||
| }: AttachmentGridDemoProps) { | ||
| return ( | ||
| <AttachmentGrid aria-label="Uploaded files" align={align} styles={style({width: 'full'})}> | ||
| {Array.from({length: count}, (_, i) => ( | ||
| <AttachmentGridItem | ||
| key={i} | ||
| uploadProgress={uploadProgress} | ||
| isInvalid={isInvalid} | ||
| size={size} | ||
| aria-label={`file-${i + 1}.pdf`}> | ||
| <AttachmentPreview | ||
| mimeType="application/pdf" | ||
| slot="thumbnail" | ||
| src={new URL('../../s2/stories/assets/placeholder.png', import.meta.url).toString()} | ||
| /> | ||
| {showCardContent && ( | ||
| <Content> | ||
| <Text slot="title">{`file-${i + 1}.pdf`}</Text> | ||
| <Text slot="description">PDF</Text> | ||
| </Content> | ||
| )} | ||
| </AttachmentGridItem> | ||
| ))} | ||
| </AttachmentGrid> | ||
| ); | ||
| } | ||
|
|
||
| const meta: Meta<typeof AttachmentGridDemo> = { | ||
| component: AttachmentGridDemo, | ||
| subcomponents: {AttachmentGrid, AttachmentGridItem}, | ||
| parameters: { | ||
| layout: 'centered' | ||
| }, | ||
| tags: ['autodocs'], | ||
| argTypes: { | ||
| count: {table: {disable: true}}, | ||
| isInvalid: {control: 'boolean'}, | ||
| uploadProgress: {control: 'number', min: 0, max: 100}, | ||
| showCardContent: {control: 'boolean'}, | ||
| size: {control: 'select', options: ['XS', 'S', 'M', 'L', 'XL']}, | ||
| align: {control: 'select', options: ['start', 'center', 'end']} | ||
| }, | ||
| args: {isInvalid: false, showCardContent: false, size: 'M', align: 'start'}, | ||
| title: 'AI/AttachmentGrid' | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof AttachmentGridDemo>; | ||
|
|
||
| export const AIAttachmentGrid: Story = { | ||
| render: args => ( | ||
| <div style={{width: 320}}> | ||
| <AttachmentGridDemo {...args} count={5} /> | ||
| </div> | ||
| ) | ||
| }; | ||
|
|
||
| export const Overflow: Story = { | ||
| name: 'Overflow (vertical scroll fade)', | ||
| render: args => ( | ||
| <div style={{width: 404, resize: 'horizontal', overflow: 'hidden'}}> | ||
| <AttachmentGridDemo {...args} count={20} /> | ||
| </div> | ||
| ) | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import {act, fireEvent, render} from '@react-spectrum/test-utils-internal'; | ||
| import {AttachmentGrid, AttachmentGridItem} from '@react-spectrum/ai'; | ||
| import {Image} from '@react-spectrum/s2/Image'; | ||
| import React from 'react'; | ||
|
|
||
| // Conditionally skip the suite | ||
| const describeOrSkip = parseInt(React.version, 10) < 19 ? describe.skip : describe; | ||
| describeOrSkip('AttachmentGrid', () => { | ||
| it('should render as a non-interactive grid whose items are not focusable', () => { | ||
| let {getByRole, getAllByRole} = render( | ||
| <AttachmentGrid aria-label="Uploaded files"> | ||
| <AttachmentGridItem aria-label="one.pdf" textValue="one.pdf"> | ||
| <Image slot="thumbnail" src="https://example.com/image.png" /> | ||
| </AttachmentGridItem> | ||
| <AttachmentGridItem aria-label="two.pdf" textValue="two.pdf"> | ||
| <Image slot="thumbnail" src="https://example.com/image.png" /> | ||
| </AttachmentGridItem> | ||
| </AttachmentGrid> | ||
| ); | ||
|
|
||
| // All options are disabled, so the grid itself becomes the sole tab stop, keeping the | ||
| // overflow area keyboard-scrollable even though no individual attachment is focusable. | ||
| let grid = getByRole('listbox'); | ||
| expect(grid).toBeInTheDocument(); | ||
| expect(grid).toHaveAttribute('tabIndex', '0'); | ||
| let options = getAllByRole('option'); | ||
| expect(options).toHaveLength(2); | ||
| for (let option of options) { | ||
| expect(option).toHaveAttribute('aria-disabled', 'true'); | ||
| expect(option).not.toHaveAttribute('tabIndex'); | ||
| } | ||
| expect(grid).not.toHaveAttribute('aria-multiselectable'); | ||
| }); | ||
|
|
||
| it('should not intercept arrow keys, so the browser can natively scroll the grid', () => { | ||
| let {getByRole} = render( | ||
| <AttachmentGrid aria-label="Uploaded files"> | ||
| <AttachmentGridItem aria-label="one.pdf" textValue="one.pdf"> | ||
| <Image slot="thumbnail" src="https://example.com/image.png" /> | ||
| </AttachmentGridItem> | ||
| </AttachmentGrid> | ||
| ); | ||
|
|
||
| let grid = getByRole('listbox'); | ||
| act(() => grid.focus()); | ||
| expect(fireEvent.keyDown(grid, {key: 'ArrowDown'})).toBe(true); | ||
| expect(fireEvent.keyDown(grid, {key: 'ArrowUp'})).toBe(true); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should the container have a focus ring? Or are the items focusable its hard to tell
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the individual items should be focusable/interactable but perhaps the grid/container itself should have a focus ring. You are supposed to be able to scroll it with keyboard when focused.