An open source design system that's fully customizable and agent ready
TypeScript
fix(Dialog): capture the opener only on the closed-to-open edge (#5642)
Ojas Mutreja7fefc8a66223
diff --git a/.changeset/dialog-strictmode-opener-capture.md b/.changeset/dialog-strictmode-opener-capture.mdnew file mode 100644index 0000000000..36ed418c9f--- /dev/null+++ b/.changeset/dialog-strictmode-opener-capture.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Dialog: under React StrictMode, a `Dialog` with a `DialogHeader` that is first mounted open and then removed while open (rendered only while open) now returns focus to the element that opened it, instead of leaving focus on the page body. The opener is captured only on the closed-to-open edge.++@HelloOjasMutreja @cixzhangdiff --git a/packages/core/src/Dialog/Dialog.test.tsx b/packages/core/src/Dialog/Dialog.test.tsxindex 97edfbd56a..6363ff6bc3 100644--- a/packages/core/src/Dialog/Dialog.test.tsx+++ b/packages/core/src/Dialog/Dialog.test.tsx@@ -480,6 +480,65 @@ describe('Dialog', () => {expect(document.activeElement).toBe(opener);});+ it('keeps the opener when StrictMode re-runs the capture on a dialog first mounted open', () => {+ // StrictMode simulates an unmount/remount after the first commit, which+ // re-runs the layout effect AFTER DialogHeader's own passive effect has+ // already focused the title. Capturing on every run would take the title+ // as the opener, and closing would return focus to a node about to unmount.+ const opener = document.createElement('button');+ opener.type = 'button';+ document.body.appendChild(opener);+ opener.focus();++ const {rerender} = render(+ <StrictMode>+ <Dialog isOpen={true} onOpenChange={() => {}}>+ <DialogHeader title="Review" />+ </Dialog>+ </StrictMode>,+ );+ expect(document.activeElement).toBe(screen.getByText('Review'));++ rerender(+ <StrictMode>+ <Dialog isOpen={false} onOpenChange={() => {}}>+ {null}+ </Dialog>+ </StrictMode>,+ );+ expect(document.activeElement).toBe(opener);+ opener.remove();+ });++ it('captures a fresh opener on each open, not only the first', () => {+ const first = document.createElement('button');+ const second = document.createElement('button');+ for (const b of [first, second]) {+ b.type = 'button';+ document.body.appendChild(b);+ }+ // Content mounts only while open, as it does in the other opener test.+ const dialog = (isOpen: boolean) => (+ <Dialog isOpen={isOpen} onOpenChange={() => {}}>+ {isOpen ? <DialogHeader title="Review" /> : null}+ </Dialog>+ );++ first.focus();+ const {rerender} = render(dialog(false));+ rerender(dialog(true));+ rerender(dialog(false));+ expect(document.activeElement).toBe(first);++ second.focus();+ rerender(dialog(true));+ rerender(dialog(false));+ expect(document.activeElement).toBe(second);++ first.remove();+ second.remove();+ });+it('renders when isOpen is true', () => {render(<Dialog isOpen={true} onOpenChange={() => {}}>diff --git a/packages/core/src/Dialog/Dialog.tsx b/packages/core/src/Dialog/Dialog.tsxindex d69825f2a5..02ce922057 100644--- a/packages/core/src/Dialog/Dialog.tsx+++ b/packages/core/src/Dialog/Dialog.tsx@@ -611,19 +611,27 @@ export function Dialog({// own title on mount — would otherwise move document.activeElement inside// the dialog before the capture ran, and closing would then restore focus// to a detached node, stranding the user's focus on <body>.+ //+ // Only the closed-to-open edge captures. StrictMode replays this effect+ // after the open commit's passive effects. By then Dialog has focused the+ // title, so a replayed capture would take the title as the opener. The ref+ // survives the replay. It starts false, so a dialog first mounted open still+ // captures.+ const wasOpenRef = useRef(false);useLayoutEffect(() => {- if (isInline || !isOpen) {- return;+ const isOpenNow = !isInline && isOpen;+ if (isOpenNow && !wasOpenRef.current) {+ // The dialog's own document while it holds focus: an app can render a+ // dialog into a second document (a same-origin iframe, a Document+ // Picture-in-Picture window) through a portal, and a control there that+ // opens it is focused in that document, which the page's document reports+ // only as the iframe element, or not at all. A control in the page that+ // opens such a dialog leaves focus in the page.+ const dialogDocument = dialogRef.current?.ownerDocument ?? document;+ const doc = dialogDocument.hasFocus() ? dialogDocument : document;+ triggerElementRef.current = doc.activeElement as HTMLElement | null;}- // The dialog's own document while it holds focus: an app can render a- // dialog into a second document (a same-origin iframe, a Document- // Picture-in-Picture window) through a portal, and a control there that- // opens it is focused in that document, which the page's document reports- // only as the iframe element, or not at all. A control in the page that- // opens such a dialog leaves focus in the page.- const dialogDocument = dialogRef.current?.ownerDocument ?? document;- const doc = dialogDocument.hasFocus() ? dialogDocument : document;- triggerElementRef.current = doc.activeElement as HTMLElement | null;+ wasOpenRef.current = isOpenNow;}, [isOpen, isInline]);// Derive dismissal behavior from purpose