An open source design system that's fully customizable and agent ready
TypeScript
fix(Dialog): a header that mounts with the opening commit takes focus only after the dialog is modal (#10408)
Cindy Zhange07291680026
diff --git a/.changeset/dialog-title-focus-after-modal.md b/.changeset/dialog-title-focus-after-modal.mdnew file mode 100644index 0000000000..7ca33c5dbf--- /dev/null+++ b/.changeset/dialog-title-focus-after-modal.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `Dialog`: a `DialogHeader` title now takes focus only after the dialog is modal, also when the header mounts in the render that opens the dialog. When no other control asks for focus, initial focus now starts on the title in every browser. Before, a header with a start control, and any header in Firefox, started on the first control.++@cixzhangdiff --git a/packages/core/src/Dialog/Dialog.test.tsx b/packages/core/src/Dialog/Dialog.test.tsxindex a331844720..97edfbd56a 100644--- a/packages/core/src/Dialog/Dialog.test.tsx+++ b/packages/core/src/Dialog/Dialog.test.tsx@@ -405,6 +405,35 @@ describe('Dialog', () => {);});+ it('focuses the title of a header that mounts with the opening commit only after the dialog is modal', () => {+ const order: string[] = [];+ const showModal = HTMLDialogElement.prototype.showModal;+ HTMLDialogElement.prototype.showModal = vi.fn(function (+ this: HTMLDialogElement,+ ) {+ order.push(`showModal open=${this.open}`);+ showModal.call(this);+ });+ const focus = vi+ .spyOn(HTMLElement.prototype, 'focus')+ .mockImplementation(function (this: HTMLElement) {+ if (this.tagName === 'H2') {+ order.push(`title focus open=${this.closest('dialog')?.open}`);+ }+ });+ function Harness({open}: {open: boolean}) {+ return (+ <Dialog isOpen={open} onOpenChange={() => {}}>+ {open ? <DialogHeader title="Review" /> : null}+ </Dialog>+ );+ }+ const harness = render(<Harness open={false} />);+ harness.rerender(<Harness open />);+ focus.mockRestore();+ expect(order).toEqual(['showModal open=false', 'title focus open=true']);+ });+it('restores focus to the opener when content mounts in the open commit', () => {function Harness({open,diff --git a/packages/core/src/Dialog/DialogHeader.tsx b/packages/core/src/Dialog/DialogHeader.tsxindex de93243e42..71670ee08e 100644--- a/packages/core/src/Dialog/DialogHeader.tsx+++ b/packages/core/src/Dialog/DialogHeader.tsx@@ -190,9 +190,16 @@ export function DialogHeader({// avoid stealing scroll position from the surrounding page.// The parent Dialog detects this title (by `titleId`) via a callback ref to// set its default aria-labelledby — no registration handshake needed here.+ // The native dialog is not modal yet when the header mounts in its opening+ // render. Dialog focuses the title after showModal().useEffect(() => {- if (shouldAutoFocus && titleRef.current) {- titleRef.current.focus();+ const title = titleRef.current;+ if (!shouldAutoFocus || title == null) {+ return;+ }+ const nativeDialog = title.closest('dialog');+ if (nativeDialog == null || nativeDialog.open) {+ title.focus();}}, [shouldAutoFocus]);@@ -228,6 +235,7 @@ export function DialogHeader({id={titleId}level={2}tabIndex={-1}+ data-autofocus={shouldAutoFocus ? 'dialog-title' : undefined}xstyle={styles.titleFocusable}>{title}</Heading>diff --git a/packages/core/src/Dialog/__tests__/Dialog.a11y.known-failures.ts b/packages/core/src/Dialog/__tests__/Dialog.a11y.known-failures.tsindex 69cbde05cc..cd5d071ea5 100644--- a/packages/core/src/Dialog/__tests__/Dialog.a11y.known-failures.ts+++ b/packages/core/src/Dialog/__tests__/Dialog.a11y.known-failures.ts@@ -10,19 +10,4 @@import type {KnownFailure} from '@astryxdesign/a11y-spec';-export const DIALOG_MODAL_KNOWN_FAILURES: ReadonlyArray<KnownFailure> = [- {- expectation: 'modal-dialog.focus.enters-after-modal',- binding: 'Dialog',- state: 'conditional-content-focus-restoration',- evidenceLayer: 'real-browser',- failureEquals:- "focus entered the dialog before it reached the browser's native modal state",- standardsReference:- 'Astryx component:Dialog FR1 — native Dialog must become visibly modal before Dialog applies its component-owned initial-focus request.',- userImpact:- 'When focus-requesting content mounts only while opening, focus enters the task before the browser has established the modal boundary that blocks the page behind it.',- reason:- 'This migration records the existing focus-order violation without changing the overlapping Dialog implementation path.',- },-];+export const DIALOG_MODAL_KNOWN_FAILURES: ReadonlyArray<KnownFailure> = [];