Skip to content

Commit e072916

Browse files
authored
fix(Dialog): a header that mounts with the opening commit takes focus only after the dialog is modal (#10408)
* test(Dialog): a header that mounts with the opening commit takes focus only after the dialog is modal (red) * fix(Dialog): a header that mounts with the opening commit leaves its title's focus to the modal dialog DialogHeader focused its title in its own mount effect. When the header mounted in the commit that opens the dialog, that effect ran before Dialog's showModal(), so focus entered the dialog before it was modal. The title now carries the dialog-title focus marker, which Dialog already focuses after showModal() when no other request is eligible, and the header focuses itself only when its native dialog is already open. * Merge branch 'main' into fix/dialog-focus-after-modal * docs(Dialog): the release note states the title's focus change in every browser; a shorter Effect comment
1 parent 619ffeb commit e072916

4 files changed

Lines changed: 47 additions & 18 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[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.
6+
7+
@cixzhang

‎packages/core/src/Dialog/Dialog.test.tsx‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,35 @@ describe('Dialog', () => {
405405
);
406406
});
407407

408+
it('focuses the title of a header that mounts with the opening commit only after the dialog is modal', () => {
409+
const order: string[] = [];
410+
const showModal = HTMLDialogElement.prototype.showModal;
411+
HTMLDialogElement.prototype.showModal = vi.fn(function (
412+
this: HTMLDialogElement,
413+
) {
414+
order.push(`showModal open=${this.open}`);
415+
showModal.call(this);
416+
});
417+
const focus = vi
418+
.spyOn(HTMLElement.prototype, 'focus')
419+
.mockImplementation(function (this: HTMLElement) {
420+
if (this.tagName === 'H2') {
421+
order.push(`title focus open=${this.closest('dialog')?.open}`);
422+
}
423+
});
424+
function Harness({open}: {open: boolean}) {
425+
return (
426+
<Dialog isOpen={open} onOpenChange={() => {}}>
427+
{open ? <DialogHeader title="Review" /> : null}
428+
</Dialog>
429+
);
430+
}
431+
const harness = render(<Harness open={false} />);
432+
harness.rerender(<Harness open />);
433+
focus.mockRestore();
434+
expect(order).toEqual(['showModal open=false', 'title focus open=true']);
435+
});
436+
408437
it('restores focus to the opener when content mounts in the open commit', () => {
409438
function Harness({
410439
open,

‎packages/core/src/Dialog/DialogHeader.tsx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,16 @@ export function DialogHeader({
190190
// avoid stealing scroll position from the surrounding page.
191191
// The parent Dialog detects this title (by `titleId`) via a callback ref to
192192
// set its default aria-labelledby — no registration handshake needed here.
193+
// The native dialog is not modal yet when the header mounts in its opening
194+
// render. Dialog focuses the title after showModal().
193195
useEffect(() => {
194-
if (shouldAutoFocus && titleRef.current) {
195-
titleRef.current.focus();
196+
const title = titleRef.current;
197+
if (!shouldAutoFocus || title == null) {
198+
return;
199+
}
200+
const nativeDialog = title.closest('dialog');
201+
if (nativeDialog == null || nativeDialog.open) {
202+
title.focus();
196203
}
197204
}, [shouldAutoFocus]);
198205

@@ -228,6 +235,7 @@ export function DialogHeader({
228235
id={titleId}
229236
level={2}
230237
tabIndex={-1}
238+
data-autofocus={shouldAutoFocus ? 'dialog-title' : undefined}
231239
xstyle={styles.titleFocusable}>
232240
{title}
233241
</Heading>

‎packages/core/src/Dialog/__tests__/Dialog.a11y.known-failures.ts‎

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,4 @@
1010

1111
import type {KnownFailure} from '@astryxdesign/a11y-spec';
1212

13-
export const DIALOG_MODAL_KNOWN_FAILURES: ReadonlyArray<KnownFailure> = [
14-
{
15-
expectation: 'modal-dialog.focus.enters-after-modal',
16-
binding: 'Dialog',
17-
state: 'conditional-content-focus-restoration',
18-
evidenceLayer: 'real-browser',
19-
failureEquals:
20-
"focus entered the dialog before it reached the browser's native modal state",
21-
standardsReference:
22-
'Astryx component:Dialog FR1 — native Dialog must become visibly modal before Dialog applies its component-owned initial-focus request.',
23-
userImpact:
24-
'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.',
25-
reason:
26-
'This migration records the existing focus-order violation without changing the overlapping Dialog implementation path.',
27-
},
28-
];
13+
export const DIALOG_MODAL_KNOWN_FAILURES: ReadonlyArray<KnownFailure> = [];

0 commit comments

Comments
 (0)