Skip to content

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

TypeScript

fix(lightbox): dismiss on backdrop click around the media (#3894)

Shahrier Islam Arham068bda99c5de
diff --git a/.changeset/lightbox-backdrop-dismiss.md b/.changeset/lightbox-backdrop-dismiss.md
new file mode 100644
index 0000000000..bb2e930bf1
--- /dev/null
+++ b/.changeset/lightbox-backdrop-dismiss.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Lightbox: make backdrop click dismissal actually reachable
+
+The dismiss check only matched clicks on the dialog element itself, but the layout container fills the entire transparent dialog, so clicks on the dark area around the media always landed on the container and never closed the lightbox. Clicks on the container now dismiss too, and a pan drag that ends over the backdrop is ignored.
+@arham766
diff --git a/packages/core/src/Lightbox/Lightbox.test.tsx b/packages/core/src/Lightbox/Lightbox.test.tsx
index 4fa3cbca8e..3d41ce6f67 100644
--- a/packages/core/src/Lightbox/Lightbox.test.tsx
+++ b/packages/core/src/Lightbox/Lightbox.test.tsx
@@ -443,4 +443,37 @@ describe('Lightbox', () => {
);
expect(container.querySelector('dialog')).not.toBeInTheDocument();
});
+
+ describe('backdrop dismiss', () => {
+ it('calls onOpenChange(false) when the dark area around the media is clicked', () => {
+ const onOpenChange = vi.fn();
+ render(
+ <Lightbox
+ isOpen={true}
+ onOpenChange={onOpenChange}
+ media={{src: '/photo.jpg', alt: 'Photo'}}
+ />,
+ );
+ // The container fills the whole dialog, so a click on the visual
+ // backdrop (the dark area around the media) lands on it — not on the
+ // dialog element itself.
+ const dialog = document.querySelector('dialog')!;
+ const container = dialog.firstElementChild!;
+ fireEvent.click(container);
+ expect(onOpenChange).toHaveBeenCalledWith(false);
+ });
+
+ it('does not close when the media itself is clicked', () => {
+ const onOpenChange = vi.fn();
+ render(
+ <Lightbox
+ isOpen={true}
+ onOpenChange={onOpenChange}
+ media={{src: '/photo.jpg', alt: 'Photo'}}
+ />,
+ );
+ fireEvent.click(screen.getByRole('img', {hidden: true}));
+ expect(onOpenChange).not.toHaveBeenCalled();
+ });
+ });
});
diff --git a/packages/core/src/Lightbox/Lightbox.tsx b/packages/core/src/Lightbox/Lightbox.tsx
index ebb2beeacb..68345cee54 100644
--- a/packages/core/src/Lightbox/Lightbox.tsx
+++ b/packages/core/src/Lightbox/Lightbox.tsx
@@ -285,6 +285,7 @@ export function Lightbox({
}: LightboxProps) {
const t = useTranslator();
const dialogRef = useRef<HTMLDialogElement>(null);
+ const containerRef = useRef<HTMLDivElement>(null);
const imageWrapperRef = useRef<HTMLDivElement>(null);
const triggerElementRef = useRef<Element | null>(null);
@@ -390,10 +391,19 @@ export function Lightbox({
[handleClose],
);
- // Backdrop click
+ // Backdrop click. The layout container fills the whole transparent dialog,
+ // so clicks on the visual backdrop (the dark area around the media) land on
+ // the container, never on the dialog element itself — treat both as the
+ // backdrop. A pan drag that ends over the backdrop still fires a click on
+ // the common ancestor; ignore it so releasing a drag doesn't dismiss.
+ const didDragRef = useRef(false);
const handleBackdropClick = useCallback(
(e: ReactMouseEvent<HTMLDialogElement>) => {
- if (e.target === e.currentTarget) {
+ if (didDragRef.current) {
+ didDragRef.current = false;
+ return;
+ }
+ if (e.target === e.currentTarget || e.target === containerRef.current) {
handleClose();
}
},
@@ -448,6 +458,7 @@ export function Lightbox({
return;
}
setIsDragging(true);
+ didDragRef.current = false;
dragStartRef.current = {
x: e.clientX,
y: e.clientY,
@@ -464,6 +475,7 @@ export function Lightbox({
}
const handlePointerMove = (e: PointerEvent) => {
+ didDragRef.current = true;
const dx = e.clientX - dragStartRef.current.x;
const dy = e.clientY - dragStartRef.current.y;
setPan({
@@ -514,7 +526,7 @@ export function Lightbox({
style,
)}
{...props}>
- <div {...stylex.props(styles.container)}>
+ <div ref={containerRef} {...stylex.props(styles.container)}>
{/* Close button */}
<div {...stylex.props(styles.closeButton)}>
<IconButton