FocusTrap
Keyboard focus containment helper for overlays.
Preview
A live, theme-aware example rendered with the published component.
Loading preview…
Import
Import from the focused entry point below, or use the same named export from @tavojs/ui. Both are public APIs; measure the built application when comparing their bundle cost.
tsximport { FocusTrap } from "@tavojs/ui/focus-trap";The component import loads compiled component CSS. Enable tavoUi() for your project theme variables, or import @tavojs/ui/theme.css once for the package default theme. The equivalent CSS-safe component alias is @tavojs/ui/css/focus-trap.
When to use
Use it when
Choose something else when
Examples
These examples include imports and local state where interaction requires it. Render the exported component in a page after completing UI installation. Demo state stays in the mounted component; connect file handling, persistence, and data loading to your application as needed.
FocusTrap example
tsximport { createTavo } from "@tavojs/core";
import { FocusTrap } from "@tavojs/ui/focus-trap";
import { Button, Link, Text } from "@tavojs/ui";
type State = { active: boolean };
export const FocusTrapExample1 = createTavo<Record<string, never>, State>({
model: () => ({ active: false }),
view: ({ state, model }) => (
<>
<Button onClick={() => model.set("active", true)}>Enable focus trap</Button>
<FocusTrap active={state.active}>
<Link href="/docs">Documentation</Link>
<Button onClick={() => model.set("active", false)}>Release focus trap</Button>
</FocusTrap>
<Text>Focus trap: {state.active ? "active" : "inactive"}</Text>
</>
)
});Common props and defaults
A curated starting point. The complete TypeScript API below includes additional props and compound member contracts.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Optional class hook applied to the public root element owned by the component. |
active | boolean | — | Configures active for this component. |
Complete TypeScript API
Import from @tavojs/ui/focus-trap. Published exports: FocusTrap, FocusTrapProps.
FocusTrapProps
| Property | Type | Required | Description |
|---|---|---|---|
children | Child | No | Inherited children attribute accepted by FocusTrap. |
className | string | No | Optional class hook applied to the public root element owned by the component. |
sx | Sx | No | Inherited sx attribute accepted by FocusTrap. |
id | string | No | Inherited id attribute accepted by FocusTrap. |
role | string | No | Inherited role attribute accepted by FocusTrap. |
style | Record<string, unknown> | No | Inherited style attribute accepted by FocusTrap. |
title | string | No | Inherited title attribute accepted by FocusTrap. |
tabIndex | number | No | Inherited tabIndex attribute accepted by FocusTrap. |
hidden | boolean | No | Inherited hidden attribute accepted by FocusTrap. |
disabled | boolean | No | Inherited disabled attribute accepted by FocusTrap. |
onClick | TavoEventHandler<MouseEvent> | No | Inherited onClick attribute accepted by FocusTrap. |
onChange | TavoEventHandler<Event> | No | Inherited onChange attribute accepted by FocusTrap. |
onInput | TavoEventHandler<Event> | No | Inherited onInput attribute accepted by FocusTrap. |
onKeyDown | TavoEventHandler<KeyboardEvent> | No | Inherited onKeyDown attribute accepted by FocusTrap. |
onFocus | TavoEventHandler<FocusEvent> | No | Inherited onFocus attribute accepted by FocusTrap. |
onBlur | TavoEventHandler<FocusEvent> | No | Inherited onBlur attribute accepted by FocusTrap. |
active | boolean | No | Configures active for this component. |
State ownership and DOM target
| Concern | Contract |
|---|---|
| State | No component state contract is exposed; the application owns the rendered content and surrounding behavior. |
| DOM target | Public passthrough props target the component root. Treat nested elements and private class names as implementation details. |
Anatomy and related components
Limitations
Avoid FocusTrap when using feedback components as primary page layout.
Use only the documented props, entry points, and compound members. Internal DOM nesting and CSS class names can change without becoming part of the public component contract.
Accessibility
Traps `Tab` and `Shift+Tab` within focusable descendants.
Traps `Tab` and `Shift+Tab` within focusable descendants.
Component status
| Contract | Current value |
|---|---|
| Maturity | stable |
| Category | feedback |
| Component import | @tavojs/ui/focus-trap |
| Explicit CSS import | @tavojs/ui/css/focus-trap |
| Preview | Interactive preview available on this page |