Repository navigation
fix(Dialog): an explicit autofocus request wins over a header's title, and Lab can read the Dialog context through an internal entry #8798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@astryxdesign/core': patch | ||
| --- | ||
|
|
||
| [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) | ||
| @jiunshinn |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@astryxdesign/core': patch | ||
| --- | ||
|
|
||
| [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) | ||
| @jiunshinn |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| * Fullscreen dialogs add safe-area protection to the default padding fallback | ||
| * while preserving explicit prop/theme padding overrides, and fade in without | ||
| * the centered-dialog translate/scale motion. | ||
| * Initial focus is applied after showModal; an explicit descendant request | ||
| * takes priority over a header's default title request. | ||
| * | ||
| * Under reduced motion the entrance never travels or scales | ||
| * (docs/design/reduced-motion.md): on a touch screen the dialog fades in | ||
|
|
@@ -495,11 +497,6 @@ export function Dialog({ | |
| // would cost at the dialog level. | ||
| const titleId = useId(); | ||
|
|
||
| const dialogContextValue = useMemo( | ||
| () => ({isInline, titleId}), | ||
| [isInline, titleId], | ||
| ); | ||
|
|
||
| // Consumer-provided labels always win over the DialogHeader default. | ||
| const hasConsumerName = | ||
| props['aria-label'] != null || props['aria-labelledby'] != null; | ||
|
|
@@ -526,6 +523,19 @@ export function Dialog({ | |
| [titleId, hasConsumerName], | ||
| ); | ||
|
|
||
| // A composed header whose title renders after the dialog mounted asks the | ||
| // dialog to look for the title again. | ||
| const syncDefaultName = useCallback(() => { | ||
| if (dialogRef.current != null) { | ||
| attachDialog(dialogRef.current); | ||
| } | ||
| }, [attachDialog]); | ||
|
|
||
| const dialogContextValue = useMemo( | ||
| () => ({isInline, titleId, syncDefaultName}), | ||
| [isInline, titleId, syncDefaultName], | ||
| ); | ||
|
|
||
| const mergedDialogRef = useMergedRefs(ref, attachDialog); | ||
|
|
||
| // Capture the element that was focused when the dialog opened, for | ||
|
|
@@ -580,11 +590,27 @@ export function Dialog({ | |
| dialog.showModal(); | ||
| // React's autoFocus calls .focus() during commit, before showModal() | ||
| // makes the dialog visible, so the focus silently fails. | ||
| // Focus the first element with data-autofocus inside the dialog. | ||
| const autofocusTarget = | ||
| dialog.querySelector<HTMLElement>('[data-autofocus]'); | ||
| if (autofocusTarget) { | ||
| focusForLastInput(autofocusTarget); | ||
| // Explicit descendant intent takes priority over a header's default | ||
| // title. Keep the marker private to the focus-owning components. | ||
| const autofocusTargets = [ | ||
| ...dialog.querySelectorAll<HTMLElement>( | ||
| '[data-autofocus]:not([data-autofocus="dialog-title"])', | ||
| ), | ||
| ...dialog.querySelectorAll<HTMLElement>( | ||
| '[data-autofocus="dialog-title"]', | ||
| ), | ||
| ]; | ||
| for (const target of autofocusTargets) { | ||
| if (target.closest('[hidden], [inert], [aria-hidden="true"]')) { | ||
| continue; | ||
| } | ||
| // Disabled and CSS-hidden targets cannot receive focus in the | ||
| // browser. If a request cannot land, try the next eligible request | ||
| // and finally the title; otherwise preserve native initial focus. | ||
| focusForLastInput(target); | ||
| if (document.activeElement === target) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if (target.ownerDocument.activeElement === target) {
break;
} |
||
| break; | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| /** | ||
| * @file dialog-context.test.ts | ||
| * @input The internal `internal/dialog-context` entry and the Dialog module | ||
| * @output Proof that the entry re-exports the same hook Dialog provides, by | ||
| * name, and that the public Dialog barrel names no context | ||
| * @position Unit tests of packages/core/src/internal/dialog-context.ts | ||
| */ | ||
| import {describe, expect, it} from 'vitest'; | ||
|
|
||
| describe('internal/dialog-context', () => { | ||
| it('re-exports the one Dialog context hook, the same value Dialog reads', async () => { | ||
| const entry: Record<string, unknown> = await import('./dialog-context'); | ||
| const context = await import('../Dialog/DialogContext'); | ||
| expect(entry.useDialogContext).toBe(context.useDialogContext); | ||
| // Named re-exports only, nothing else. | ||
| expect(Object.keys(entry).sort()).toEqual(['useDialogContext']); | ||
| }); | ||
|
|
||
| it('the public Dialog barrel exports no context surface', async () => { | ||
| const pub: Record<string, unknown> = await import('../Dialog'); | ||
| expect(pub).not.toHaveProperty('DialogContext'); | ||
| expect(pub).not.toHaveProperty('useDialogContext'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
|
||
| /** | ||
| * @file dialog-context.ts | ||
| * @input Dialog's internal context hook | ||
| * @output `@astryxdesign/core/internal/dialog-context`: `useDialogContext`, | ||
| * for Astryx's own packages | ||
| * @position An internal entry reserved for Astryx's own packages: | ||
| * undocumented, outside the public API and its compatibility promise | ||
| * (spec:AST-017 FR21), versioned in lockstep with Core, and allowed only in | ||
| * Core and Lab by the `@astryx/no-core-internal-import` lint rule | ||
| * (architecture:react-component-runtime INV13). Lab's DialogHeroHeader reads | ||
| * the Dialog's title id and inline state through it, so it names the dialog | ||
| * and requests title focus the way DialogHeader does. Named re-exports only. | ||
| * | ||
| * SYNC: When modified, update dialog-context.test.ts and | ||
| * scripts/sync-exports.js. | ||
| */ | ||
| export {useDialogContext} from '../Dialog/DialogContext'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This walks every ancestor, past the
<dialog>. A modal dialog escapes an ancestor's inertness: Chromium focuses a button inside a modal<dialog>under<div inert>. A Dialog rendered in an inert region therefore skips its own eligible request and falls back to native focus, againstcomponent:DialogFR2. Count a match only when it is inside the dialog: