Focus system
Focus decides which of several components in the same layer answers a key. Each focus target has a unique focusId, and a group has at most one active target. Only bindings on the active target are tried.
Prerequisites
| Chapter | Why you need it |
|---|---|
| Basic Binding | focusId is a binding option |
Focus targets
Adding focusId scopes a binding to a target instead of the whole layer:
boundKeyboard(['up', 'down'], handleUpDown, { focusId: 'select-a' })- A target is created lazily on the first binding registered with that
focusId. - The first target created in a group is activated automatically; later ones wait for an explicit switch.
- Unbinding does not unregister the target; it still occupies its place in the order.
- Leaving a screen destroys its keyboard layer and all focus targets with it.
Control methods
| Method | Signature | Effect |
|---|---|---|
focusSet | (focusId, groupOrOptions?) | Force-activate a target |
focusNext | (groupOrOptions?) | Move forward in registration order, wrapping |
focusPrev | (groupOrOptions?) | Move backward |
focusCurrent | (groupOrOptions?) | Read the active target without rendering |
focusUnregister | (focusId, groupOrOptions?) | Unregister a target |
useFocusState | (focusId, group?) | Subscribe to whether this target is active |
groupOrOptions is a group name or FocusSetOptions:
type FocusSetOptions = { group?: string; element?: string }const { focusSet, focusNext, focusPrev, focusCurrent, focusUnregister } = useKeyboard()
focusSet('select-b')
focusNext()
focusPrev()
const { result, noFound } = focusCurrent()
focusUnregister('select-b')
// Standalone hook, imported from 'ink-cartridge' rather than useKeyboard()
const focused = useFocusState('select-b')focusCurrent returns:
{
result?: { id: string; fromGroup: string | symbol }
noFound?: boolean // no active target in the group
noOwner?: boolean // no page or layer
noLayer?: boolean // the current owner has no keyboard layer
}fromGroup is the named group, or defaultTargetsSymbol for the default group; the symbol is exported from ink-cartridge.
focusNext / focusPrev do nothing when a group has no active target; focusSet can activate any registered target directly. After focusUnregister removes the active target, focus moves to the first remaining target in registration order.
focusSet, focusNext, and focusPrev return true when the active focus changed; focusUnregister returns true when a target was actually removed (it need not be the active one). Every no-op path returns false, and none of them throw.
Which method?
| Goal | Method |
|---|---|
| Switch to a known target | focusSet |
| Move one step, wrapping | focusNext / focusPrev |
| Read the active target | focusCurrent |
| Re-render on focus changes | useFocusState / subscribeFocus |
| Remove a target | focusUnregister |
autoTab
<KeyboardProvider autoTab>The engine intercepts Tab / Shift+Tab and calls focusNext / focusPrev. It applies to the default group only. For custom Tab behavior, disable autoTab and bind manually:
boundKeyboard('tab', () => focusNext())
boundKeyboard('shift+tab', () => focusPrev())Non-React hosts can set tabKey to the bare forward key name; reverse rotation matches shift+<tabKey>.
releaseFocusWhenEmpty
<KeyboardProvider releaseFocusWhenEmpty>Off by default. When enabled, bindings hold a reference to their focus target and the last release removes it. Registering the same focusId again recreates it. Focus moves to the group's first remaining target, or the group slot is cleared when none remains. If the group was kicked, only a newly created target reclaims its focus; re-registering an existing target does not.
Subscribing to focus changes
const { subscribeFocus } = useKeyboard()
useEffect(() => subscribeFocus(() => {
setCurrent(focusCurrent().result?.id)
}), [subscribeFocus, focusCurrent])useFocusState is the usual declarative wrapper. subscribeFocus fits code outside a component or reads across several groups.
Boundaries
focusSeton an unknown target returnsfalseand leaves the current focus unchanged.useFocusStatereads during render, so a component reused with a differentfocusIdreflects the new target immediately.- Component unmount and screen leave differ: unmount only unbinds; the target stays on the screen. Call
focusUnregisterin the cleanup function to remove it. - Named groups and multi-group routing are covered in Default and named groups.
