Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Lightbox): a viewer opened before its first item arrives opens when it does (#8741)

Christopher Chedeau1ffd3234b842
diff --git a/.changeset/lightbox-open-before-items.md b/.changeset/lightbox-open-before-items.md
new file mode 100644
index 0000000000..3fe7f1725e
--- /dev/null
+++ b/.changeset/lightbox-open-before-items.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Lightbox: a viewer opened before its first item arrives now opens when that item does. It used to stay closed, its dialog never shown.
+
+@vjeux
diff --git a/packages/core/src/Lightbox/Lightbox.test.tsx b/packages/core/src/Lightbox/Lightbox.test.tsx
index a1030b6751..f0dc0b3a67 100644
--- a/packages/core/src/Lightbox/Lightbox.test.tsx
+++ b/packages/core/src/Lightbox/Lightbox.test.tsx
@@ -53,6 +53,21 @@ describe('Lightbox', () => {
expect(dialog).toBeInTheDocument();
});
+ it('opens once an item arrives when opened with none', () => {
+ const {rerender} = render(
+ <Lightbox isOpen={true} onOpenChange={() => {}} media={[]} />,
+ );
+ expect(document.querySelector('dialog')).toBeNull();
+ rerender(
+ <Lightbox
+ isOpen={true}
+ onOpenChange={() => {}}
+ media={[{src: '/photo.jpg', alt: 'Photo'}]}
+ />,
+ );
+ expect(document.querySelector('dialog')).toHaveAttribute('open');
+ });
+
it('calls showModal when isOpen becomes true', () => {
render(
<Lightbox
diff --git a/packages/core/src/Lightbox/Lightbox.tsx b/packages/core/src/Lightbox/Lightbox.tsx
index 7dd00a434f..476487c352 100644
--- a/packages/core/src/Lightbox/Lightbox.tsx
+++ b/packages/core/src/Lightbox/Lightbox.tsx
@@ -417,10 +417,12 @@ export function Lightbox({
);
}, [index, isOpen, announce, mediaArray, t]);
- // Open/close dialog
+ // Open/close dialog. The dialog renders only with an item, so a viewer
+ // opened before its first item arrives opens when that item does.
+ const hasItem = currentItem != null;
useIsomorphicLayoutEffect(() => {
const dialog = dialogRef.current;
- if (!dialog) {
+ if (!dialog || !hasItem) {
return;
}
@@ -433,7 +435,7 @@ export function Lightbox({
triggerElementRef.current.focus();
}
}
- }, [isOpen]);
+ }, [isOpen, hasItem]);
const handleClose = useCallback(() => {
onOpenChange(false);