Utilities
Helper functions for working with @diffgazer/keys — keys, navigation item utilities, composed-tree and reachability predicates, DOM focus restore utilities, and keyboard context hooks.
Navigation item utilities
DOM helpers shared by useNavigation and composite widgets. These are package exports, not standalone shadcn registry items.
Use getNavigationItemProps(type, value) to apply the public data contract:
Signature
The query helpers read data-diffgazer-navigation-item, role selectors, and native radio/checkbox/button controls. Navigation items should expose a stable data-value. Typed data-contract markers only match the requested item type, which keeps mixed widgets separated inside the same subtree.
Focusable helpers use the same DOM contract as focus traps and overlays. getFocusableElements() includes programmatic focus targets such as tabIndex={-1} and traverses nested open shadow roots in composed order. getTabbableElements() applies browser Tab ordering across those light- and shadow-DOM descendants and collapses native radio groups to one Tab stop. Closed shadow roots remain opaque.
canonicalizeHotkey, isEditableElement, isInputElement, isListNavigationKey, getVerticalArrowDirection, and toVerticalBoundaryDirection expose the same parsing and guard helpers used internally by the hooks. clampIndex and moveHighlight are pure list-navigation helpers for code that already owns its item model.
DOM focus restore utilities
Plain DOM helpers used by useFocusRestore. Use these when you are writing non-hook code that still needs the same focus target contract.
Signature
getRestorableFocusTarget() ignores body, documentElement, disconnected nodes, and missing DOM. restoreFocus() focuses a connected target and returns whether focus moved there.
For React components, prefer useFocusRestore. The hook owns the nested overlay stack and the cleanup behavior.
keys
Utility to create a Record<string, KeyHandler> from an array of hotkeys and a single handler. Useful with the key-map overload of useKey.
Signature
Handlers created through keys() follow the same KeyHandler contract as any other handler: returning false declines the match and lets the next lower-priority handler in the active scope run.
Example
Provider-aware hooks
Low-level hooks to access the KeyboardProvider context. useKeyboardContext throws when no provider is present. useOptionalKeyboardContext returns null.
Signature
KeyboardContextValue
Behavior
useKeyboardContextreturns the active keyboard context and throws ifKeyboardProvideris missing.useOptionalKeyboardContextreturns the active keyboard context ornullifKeyboardProvideris missing.- This is what
useKeyuses internally. You probably don't need this directly unless you're building a custom hook on top of@diffgazer/keys.
isEditableElement and isInputElement
Predicate functions for classifying event targets. Used internally by KeyboardProvider to skip non-allowInInput handlers, and useful when building custom keyboard hooks.
Signature
Behavior
isInputElementreturnstruefor<input>,<textarea>,<select>, andcontenteditableelements.isEditableElementis stricter: returnstrueonly for elements that accept text editing keys (text-like inputs, textarea, contenteditable). Returnsfalsefor checkboxes, radios, selects, buttons, disabled, and readonly inputs.
Composed-tree and reachability predicates
DOM predicates shared by the focus trap, focus zones, and navigation discovery. Use them when your own code has to make the same "is this element reachable" or "is this target inside my container" decision across shadow boundaries.
Signature
Behavior
composedContainswalks out through every open shadow root betweentargetandcontainer, so a target inside a nested shadow tree still reports as contained. NativeNode.containsstops at the first shadow boundary.composedClosestisElement.closestcontinued across shadow hosts: when no ancestor inside the current root matches, it hops to the host and keeps looking.isReachablereturnsfalsewhen a hidden,inert,aria-hidden="true", or closed-<details>self-or-ancestor removes the element from keyboard reach. Focus and navigation discovery skip the same elements.isInsideDisabledFieldsetreturnstruefor controls disabled by an ancestor<fieldset disabled>, honoring the spec exemption for descendants of that fieldset's first<legend>.
canonicalizeHotkey
Normalizes a hotkey string so that aliases and modifier orderings collapse to a single canonical form. Useful when building registries or deduplicating hotkey bindings.
Signature
Behavior
Resolves key aliases (esc to escape, up to arrowup, question to ?), normalizes mod to meta or ctrl, and sorts modifiers alphabetically. Two hotkey strings that match the same physical key event will produce the same canonical string.