Repository navigation
fix(core): focus traps and the keyboard model are one per page across swizzled copies (AST-110 FR3) #10831
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
Merged
Merged
fix(core): focus traps and the keyboard model are one per page across swizzled copies (AST-110 FR3) #10831
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8dd1933
test(core): each shared-realm shape gives the package and a copy one …
cixzhang 6392e53
feat(core): shared-realm helpers hold every kind of cross-module state
cixzhang 460632c
chore(changeset): note the internal shared-realm helpers
cixzhang 1677454
feat(core): sharedForReact keeps a React-bound shared value one per R…
cixzhang 9aca48d
fix(core): reset shared state without a dynamic delete
cixzhang 53b6023
fix(core): a shared brand's type member is required
cixzhang 3915114
spec(AST-110): FR3 states the observable slot guarantee; the mechanis…
cixzhang be43594
test(core): a scroll lock from a swizzled copy keeps the package's pa…
cixzhang 4446e5e
fix(core): the page scroll lock is one per page across swizzled copies
cixzhang 2d7d70c
fix(core): type the shared scroll-claim stacks
cixzhang 3b00d66
test(core): import the scroll-lock module types
cixzhang 4cbecd6
chore(changeset): describe the shared scroll and selection locks accu…
cixzhang 4d09efd
test(core): focus traps and the keyboard are one set across a swizzle…
cixzhang f4b7b9b
fix(core): focus traps and the keyboard model are one per page across…
cixzhang 6db7a2d
test(core): a copy's focus region nested in a package region answers …
cixzhang 63307d2
Merge remote-tracking branch 'origin/main' into fix/focus-keyboard-sh…
cixzhang 7ea1f91
chore(changeset): drop the two notes 0.7.12 already shipped; tighten …
cixzhang 1d1d1af
Merge branch 'main' into fix/focus-keyboard-shared
cixzhang 14cf8df
Merge remote-tracking branch 'origin/main' into fix/focus-keyboard-sh…
cixzhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@astryxdesign/core': patch | ||
| --- | ||
|
|
||
| [fix] A swizzled overlay and the package's components now share one set of focus regions and one mobile keyboard model: nested focus regions from the package and a copy no longer both answer the same Tab, and package and copied fields agree on the keyboard's height, its layout guide and which elements ride it. No API changes. | ||
|
|
||
| @cixzhang | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/core/src/keyboard/focusKeyboard.shared.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
|
||
| /** | ||
| * @file focusKeyboard.shared.test.tsx | ||
| * @input Two loads of the focus-trap and keyboard modules, as the package and | ||
| * a swizzled copy each carry one | ||
| * @output Proves both loads see one set of focus traps (a copy's region | ||
| * nested in a package region answers its own Tab), one keyboard layout | ||
| * guide per window, and one set of keyboard targets | ||
| * @position Shared-state tests for useFocusTrap.ts and the keyboard modules | ||
| * (spec:AST-110 FR3) | ||
| */ | ||
|
|
||
| import {act, cleanup, render, renderHook} from '@testing-library/react'; | ||
| import {useRef, useState, type ReactNode} from 'react'; | ||
| import {afterEach, describe, expect, it, vi} from 'vitest'; | ||
|
|
||
| /** One load of a module, as the package or a copy has. */ | ||
| async function load<T>(importModule: () => Promise<T>): Promise<T> { | ||
| vi.resetModules(); | ||
| return importModule(); | ||
| } | ||
|
|
||
| afterEach(async () => { | ||
| cleanup(); | ||
| (await import('../utils/sharedRealm')).resetSharedStateForTest('keyboard/'); | ||
| (await import('../utils/sharedRealm')).resetSharedStateForTest('hooks/'); | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| describe('focus traps and the keyboard, shared by the package and a copy', () => { | ||
|
astracat-bot[bot] marked this conversation as resolved.
Resolved
astracat-bot[bot] marked this conversation as resolved.
Resolved
|
||
| it("reports a copy's active Escape trap to the package", async () => { | ||
| const pkg = await load(async () => import('../hooks/useFocusTrap')); | ||
| const copy = await load(async () => import('../hooks/useFocusTrap')); | ||
| const trap = renderHook(() => | ||
| copy.useFocusTrapEscapeCompatibilitySignal(true), | ||
| ); | ||
| expect(pkg.hasActiveFocusTrapEscape()).toBe(true); | ||
| trap.unmount(); | ||
| expect(pkg.hasActiveFocusTrapEscape()).toBe(false); | ||
| }); | ||
|
|
||
| it("wraps Tab inside a copy's region nested in a package region", async () => { | ||
| const pkg = await load(async () => import('../hooks/useFocusTrap')); | ||
| const copy = await load(async () => import('../hooks/useFocusTrap')); | ||
| function Region({mod, children}: {mod: typeof pkg; children: ReactNode}) { | ||
| const ref = useRef<HTMLDivElement>(null); | ||
| mod.useFocusRegionContainment(ref, true); | ||
| return <div ref={ref}>{children}</div>; | ||
| } | ||
| let open: () => void = () => {}; | ||
| function Page() { | ||
| const [isOpen, setOpen] = useState(false); | ||
| open = () => setOpen(true); | ||
| return ( | ||
| <Region mod={pkg}> | ||
| <button type="button">A</button> | ||
| {isOpen && ( | ||
| <Region mod={copy}> | ||
| <button type="button">B1</button> | ||
| <button type="button">B2</button> | ||
| </Region> | ||
| )} | ||
| </Region> | ||
| ); | ||
| } | ||
| const {getByText} = render(<Page />); | ||
| // The copy's region opens after the package's, as an overlay opened from | ||
| // a page region does, so the package's Tab handler runs first. | ||
| act(() => open()); | ||
| getByText('B2').focus(); | ||
| act(() => { | ||
| document.dispatchEvent( | ||
| new KeyboardEvent('keydown', { | ||
| key: 'Tab', | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }), | ||
| ); | ||
| }); | ||
| expect(document.activeElement?.textContent).toBe('B1'); | ||
| }); | ||
|
|
||
| it('gives both loads one keyboard layout guide per window', async () => { | ||
| const pkg = await load(async () => import('./keyboardLayoutGuide')); | ||
| const copy = await load(async () => import('./keyboardLayoutGuide')); | ||
| expect(copy.getKeyboardLayoutGuide(window)).toBe( | ||
| pkg.getKeyboardLayoutGuide(window), | ||
| ); | ||
| }); | ||
|
|
||
| it('recognizes a keyboard target a copy registered', async () => { | ||
| const pkg = await load(async () => import('./keyboardTarget')); | ||
| const copy = await load(async () => import('./keyboardTarget')); | ||
| const target = document.createElement('div'); | ||
| const field = document.createElement('input'); | ||
| target.append(field); | ||
| document.body.append(target); | ||
| const release = copy.registerKeyboardTarget(target); | ||
| expect(pkg.isInsideKeyboardTarget(field)).toBe(true); | ||
| release(); | ||
| target.remove(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Optional wording: "nested focus regions from the package and a copy no longer both answer the same Tab" names a symptom the base does not show. Each region's Tab handler returns once another has called
preventDefault(useFocusTrap.ts:402), so on the base only the outer region answers and focus leaves the inner overlay (the new case lands onA). Suggest: "a Tab inside a copy's focus region nested in a package region (or the reverse) now wraps inside the inner region instead of moving to the outer region's first control".