Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 56 additions & 25 deletions packages/@react-spectrum/ai/src/Lightbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ChevronRight from '@react-spectrum/s2/icons/ChevronRight';
import {Children, ReactNode, useCallback, useContext, useRef} from 'react';
import {CloseButton} from '@react-spectrum/s2/CloseButton';
import {Dialog, Modal, ModalOverlay} from 'react-aria-components/Modal';
import {filterDOMProps} from 'react-aria/filterDOMProps';
import {flushSync} from 'react-dom';
import {FocusableElement, Key, LinkDOMProps} from '@react-types/shared';
import {focusSafely} from 'react-aria/private/interactions/focusSafely';
Expand Down Expand Up @@ -58,6 +59,7 @@ export interface LightboxCarouselProps extends Omit<
| 'children'
| 'orientation'
| 'keyboardActivation'
| 'shouldFocusWrap'
| 'isDisabled'
| 'disabledKeys'
// TODO: Expose selection props when LightboxTrigger supports controlled selection and opening.
Expand Down Expand Up @@ -260,23 +262,27 @@ export function LightboxCarousel(props: LightboxCarouselProps) {
})}>
{({close}) => (
<>
<RACTabs
{...props}
id={undefined}
shouldFocusWrap={false}
selectedKey={ctx?.activeKey}
onSelectionChange={chain(props.onSelectionChange, ctx?.setActiveKey)}
className={carouselStyles}
/>
<CloseButton
staticColor="white"
onPress={close}
styles={style({
position: 'absolute',
top: 8,
top: {
default: 16,
[smallTouchScreen]: 16
},
insetEnd: 16,
zIndex: 1
})}
/>
<RACTabs
{...props}
id={undefined}
selectedKey={ctx?.activeKey}
onSelectionChange={chain(props.onSelectionChange, ctx?.setActiveKey)}
className={carouselStyles}
/>
</>
)}
</Dialog>
Expand All @@ -291,7 +297,7 @@ export function LightboxCarousel(props: LightboxCarouselProps) {
{...labelProps}
aria-roledescription={stringFormatter.format('lightbox.carousel')}
style={{width: '100%', height: '100%'}}>
<RACTabs {...props} id={undefined} className={carouselStyles} />
<RACTabs {...props} id={undefined} shouldFocusWrap={false} className={carouselStyles} />
</div>
);
}
Expand All @@ -307,10 +313,11 @@ export function LightboxPicker<T>(props: LightboxPickerProps<T>) {
let selectedKey = state?.selectedKey;
let manager = state?.selectionManager;
let focusedKey = manager?.focusedKey;
let isHidden = state != null && state.collection.size <= 1;

useLayoutEffect(() => {
let tabList = ref.current;
if (!tabList || selectedKey == null || !manager) {
if (isHidden || !tabList || selectedKey == null || !manager) {
return;
}

Expand Down Expand Up @@ -344,7 +351,11 @@ export function LightboxPicker<T>(props: LightboxPickerProps<T>) {
}

scrollIntoView(tabList, selectedTab, {inline: 'center'});
}, [selectedKey, focusedKey, manager, ctx?.isOpen]);
}, [isHidden, selectedKey, focusedKey, manager, ctx?.isOpen]);

if (isHidden) {
return null;
}

return (
<div className={style({display: 'flex', justifyContent: 'center', order: 1})}>
Expand Down Expand Up @@ -610,20 +621,24 @@ function FiniteLightboxItems(props: FiniteLightboxItemsProps) {
}}
/>
</div>
<CarouselNavButton
onPress={() => {
selectIndex(selectedIndex - 1);
}}
side="start"
aria-disabled={selectedIndex === 0}
/>
<CarouselNavButton
onPress={() => {
selectIndex(selectedIndex + 1);
}}
side="end"
aria-disabled={selectedIndex === size - 1}
/>
{size > 1 && (
<>
<CarouselNavButton
onPress={() => {
selectIndex(selectedIndex - 1);
}}
side="start"
aria-disabled={selectedIndex === 0}
/>
<CarouselNavButton
onPress={() => {
selectIndex(selectedIndex + 1);
}}
side="end"
aria-disabled={selectedIndex === size - 1}
/>
</>
)}
</>
);
}
Expand Down Expand Up @@ -754,6 +769,22 @@ const innerImageStyles = style({
* Displays the content for an individual item in a lightbox carousel.
*/
export function LightboxItem(props: LightboxItemProps) {
let state = useContext(TabListStateContext);
if (state && state.collection.size <= 1) {
let DOMProps = filterDOMProps({...props, id: undefined}, {labelable: true, global: true});
return (
<div {...DOMProps} className={itemStyles}>
<ImageContext.Provider
value={{
styles: imageStyles({isFocusVisible: false}),
UNSTABLE_imgStyles: innerImageStyles
}}>
{props.children}
</ImageContext.Provider>
</div>
);
}

return (
<RACTabPanel {...props} shouldForceMount className={itemStyles}>
{renderProps => (
Expand Down
36 changes: 36 additions & 0 deletions packages/@react-spectrum/ai/stories/Lightbox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,39 @@ export const AttachmentGridDialog: StoryObj<typeof AttachmentGridDialogExample>
showCardContent: {control: 'boolean'}
}
};

export const SingleItemDialog: Story = {
render: () => (
<LightboxTrigger>
<div className={style({display: 'flex', flexDirection: 'column', gap: 16, width: 400})}>
<AttachmentList aria-label="Attachments">
<Attachment id="amsterdam" textValue="photo.png">
<AttachmentPreview
mimeType="image/jpg"
slot="thumbnail"
src="https://www.w3.org/WAI/content-images/wai-aria-practices/patterns/carousel/examples/images/amsterdamslide__800x600.jpg"
/>
</Attachment>
</AttachmentList>
</div>
<LightboxCarousel>
<LightboxPicker aria-label="Choose photo">
<LightboxPickerItem id="amsterdam" aria-label="Amsterdam">
<Image
src="https://www.w3.org/WAI/content-images/wai-aria-practices/patterns/carousel/examples/images/amsterdamslide__800x600.jpg"
alt=""
/>
</LightboxPickerItem>
</LightboxPicker>
<LightboxItems>
<LightboxItem id="amsterdam">
<Image
src="https://www.w3.org/WAI/content-images/wai-aria-practices/patterns/carousel/examples/images/amsterdamslide__800x600.jpg"
alt="Amsterdam canal"
/>
</LightboxItem>
</LightboxItems>
</LightboxCarousel>
</LightboxTrigger>
)
};
36 changes: 23 additions & 13 deletions packages/@react-spectrum/ai/test/Lightbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,14 @@ describeOrSkip('Lightbox', () => {
}
);

it('disables both navigation actions when there is only one slide', async () => {
let onSelectionChange = jest.fn();
let {getByRole} = render(
<Carousel itemIds={['one']} onSelectionChange={onSelectionChange} />
);
onSelectionChange.mockClear();
for (let name of ['Previous attachment', 'Next attachment']) {
let button = getByRole('button', {name});
expect(button).toHaveAttribute('aria-disabled', 'true');
await user.click(button);
}
expect(within(getByRole('tabpanel')).getByRole('img')).toHaveAccessibleName('Photo one');
expect(onSelectionChange).not.toHaveBeenCalled();
it('hides the picker, navigation buttons, and tab roles when there is only one slide', () => {
let {getByRole, queryByRole} = render(<Carousel itemIds={['one']} />);
expect(queryByRole('tablist')).not.toBeInTheDocument();
expect(queryByRole('tab')).not.toBeInTheDocument();
expect(queryByRole('tabpanel')).not.toBeInTheDocument();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we were leaving in the single tab, just hiding next/prev?

@yihuiliao yihuiliao Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think it makes much sense to have a tabpanel when the tablist isn't there. now it's just a regular div (no role) with an image inside of it. my thought process was that if it's a single image, it doesn't follow the tab pattern

expect(queryByRole('button', {name: 'Previous attachment'})).not.toBeInTheDocument();
expect(queryByRole('button', {name: 'Next attachment'})).not.toBeInTheDocument();
expect(getByRole('img', {name: 'Photo one'})).toBeInTheDocument();
});
});

Expand Down Expand Up @@ -416,6 +411,21 @@ describeOrSkip('Lightbox', () => {
expect(within(getByRole('tabpanel')).getByRole('img')).toHaveAccessibleName('Photo one');
expect(previous).toHaveFocus();
});

it('does not wrap picker focus at the boundaries', async () => {
let {getByRole} = render(<Carousel itemIds={['one', 'two']} />);
let firstTab = getByRole('tab', {name: 'Photo one'});
let lastTab = getByRole('tab', {name: 'Photo two'});

await user.tab();
expect(firstTab).toHaveFocus();
await user.keyboard('{ArrowLeft}');
expect(firstTab).toHaveFocus();
await user.keyboard('{ArrowRight}');
expect(lastTab).toHaveFocus();
await user.keyboard('{ArrowRight}');
expect(lastTab).toHaveFocus();
});
});

it('updates the mounted slide window, keeping only selected and neighboring slides mounted and neighboring wrappers inert', async () => {
Expand Down