Skip to content

Commit fad9909

Browse files
cixzhangjiunshinn
andcommitted
fix(Dialog): only hiding inside the dialog skips an initial-focus request, and the dialog's own document reports its focus
A Dialog rendered inside an inert region of the page no longer skips its own eligible request, and in an iframe or Picture-in-Picture window the first request that takes focus wins, so the title request no longer overrides an explicit one. The changesets name the aria-hidden skip and syncDefaultName(). Co-authored-by: jiunshinn <jiun.dev@gmail.com>
1 parent 7c0f6d2 commit fad9909

4 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎.changeset/dialog-when-the-opening-dialog-s-first-data-auto-wr66.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
'@astryxdesign/core': patch
33
---
44

5-
[fix] Dialog: when the opening Dialog's first data-autofocus request is hidden, inert, or cannot take focus, the next request takes it; a header's default title request comes after every explicit request (#4182)
5+
[fix] Dialog: when the opening Dialog's first data-autofocus request is hidden, inert, or `aria-hidden` inside the dialog, or cannot take focus, the next request takes it; a header's default title request comes after every explicit request. This also holds for a Dialog rendered inside an inert region of the page or in an iframe or Picture-in-Picture window (#4182)
66
@jiunshinn

‎.changeset/internal-lab-s-dialogheroheader-reads-the-dialog-ctse.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
'@astryxdesign/core': patch
33
---
44

5-
[chore] Internal: an internal entry reserved for Astryx's own packages exposes the Dialog's title id and inline state, so Lab's upcoming DialogHeroHeader can name the dialog and request title focus the way DialogHeader does; nothing a consumer sees changes (#4182)
5+
[chore] Internal: an internal entry reserved for Astryx's own packages exposes the Dialog's title id, inline state and `syncDefaultName()` (a header whose title renders after the dialog mounted asks the dialog to look for its name again), so Lab's upcoming DialogHeroHeader can name the dialog and request title focus the way DialogHeader does; nothing a consumer sees changes (#4182)
66
@jiunshinn

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,22 @@ describe('Dialog', () => {
524524
expect(screen.getByRole('button', {name: 'Continue'})).toHaveFocus();
525525
});
526526

527+
it('honors its own initial-focus request inside an inert region of the page', () => {
528+
render(
529+
<div inert>
530+
<Dialog isOpen={true} onOpenChange={() => {}}>
531+
<button type="button" data-autofocus>
532+
Continue
533+
</button>
534+
</Dialog>
535+
</div>,
536+
);
537+
538+
expect(
539+
screen.getByRole('button', {name: 'Continue', hidden: true}),
540+
).toHaveFocus();
541+
});
542+
527543
it('falls through an initial-focus request that cannot take focus', () => {
528544
render(
529545
<Dialog isOpen={true} onOpenChange={() => {}}>
@@ -975,6 +991,25 @@ describe('Dialog', () => {
975991
);
976992
}
977993

994+
it("honors an explicit request before a header's title request", () => {
995+
render(
996+
createPortal(
997+
<Dialog isOpen={true} onOpenChange={() => {}} aria-label="Review">
998+
<h2 tabIndex={-1} data-autofocus="dialog-title">
999+
Hero title
1000+
</h2>
1001+
<button type="button" data-autofocus>
1002+
Continue
1003+
</button>
1004+
</Dialog>,
1005+
secondDocument.body,
1006+
),
1007+
);
1008+
1009+
const continueButton = secondDocument.querySelector('button')!;
1010+
expect(secondDocument.activeElement).toBe(continueButton);
1011+
});
1012+
9781013
function SecondDocumentFieldDialog({isOpen}: {isOpen: boolean}) {
9791014
return createPortal(
9801015
<>

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,14 +663,21 @@ export function Dialog({
663663
),
664664
];
665665
for (const target of autofocusTargets) {
666-
if (target.closest('[hidden], [inert], [aria-hidden="true"]')) {
666+
// Only hiding inside the dialog makes a request ineligible: the
667+
// modal dialog is not part of an inert region around it.
668+
const hiddenAncestor = target.closest(
669+
'[hidden], [inert], [aria-hidden="true"]',
670+
);
671+
if (hiddenAncestor != null && dialog.contains(hiddenAncestor)) {
667672
continue;
668673
}
669674
// Disabled and CSS-hidden targets cannot receive focus in the
670675
// browser. If a request cannot land, try the next eligible request
671676
// and finally the title; otherwise preserve native initial focus.
677+
// The dialog's own document reports its focus, also when the dialog
678+
// renders in an iframe or a Picture-in-Picture window.
672679
focusForLastInput(target);
673-
if (document.activeElement === target) {
680+
if (target.ownerDocument.activeElement === target) {
674681
break;
675682
}
676683
}

0 commit comments

Comments
 (0)