#149
focusTrap
A button that opens a box where Tab stays trapped among three buttons until Close is pressed.
Hand this to your AI. It names the exact library parts, timings and accessibility promises of this demo.
Build the "focusTrap" pattern (@codeatlasdev/ui demo #149, navigation/actions) in my app.
Call get_demo({ ids: ["#149"] }) first and keep its structure. It needs @codeatlasdev/[email protected].
Use: Modal-like surfaces such as dialogs, drawers and inline panels that must keep keyboard focus inside.
Keep these library parts, with the props and tokens the demo uses:
- Button from "@codeatlasdev/ui"
- focusTrap from "@codeatlasdev/ui/actions"
Behaviour to keep:
- `use:focusTrap={open}` imported from `@codeatlasdev/ui/actions` on a `div` with `class="trap"`
- `let open = $state(false)` toggled by `<Button onclick={() => (open = true)}>Open trap</Button>`
- Trap is rendered inside `{#if open}` and holds Button A and Button B with `variant="outlined"`
- Close button uses `onclick={() => (open = false)}` to release the trap
Reduced motion: the demo handles it; keep that branch.
Keyboard: operable; keep the focus order and key handling.
Change only the copy and the data. Use the theme tokens (--ca-*), never raw colours. Library parts
- Button
@codeatlasdev/ui
How it is built
- `use:focusTrap={open}` imported from `@codeatlasdev/ui/actions` on a `div` with `class="trap"`
- `let open = $state(false)` toggled by `<Button onclick={() => (open = true)}>Open trap</Button>`
- Trap is rendered inside `{#if open}` and holds Button A and Button B with `variant="outlined"`
- Close button uses `onclick={() => (open = false)}` to release the trap
Related
Loading source.