React (@fiveway/react)
Re-exports everything from @fiveway/core and @fiveway/core/dom, plus React bindings.
import {
NavigationProvider,
useNavigationContext,
useNavigationNode,
NavigationNode,
useIsFocused,
useOnFocus,
useOnBlur,
useOnFocusChange,
useFocusedId,
useFocus,
useSelect,
useElementHandler,
useDispatchOnEvent,
useFocusSync,
} from "@fiveway/react";Context
NavigationContext (type)
type NavigationContext = {
tree: NavigationTree;
parentNode: NodeId;
};NavigationContext (value)
const NavigationContext: React.Context<NavigationContext | null>;NavigationProvider
function NavigationProvider(props: { tree: NavigationTree; children?: ReactNode }): ReactNode;Supplies tree and sets parentNode to "#".
useNavigationContext
function useNavigationContext(): NavigationContext;Throws if used outside a provider.
Nodes
NavigationNodeOptions
type NavigationNodeOptions = {
id: NodeId;
parent?: NodeId;
order?: number;
handler?: NavigationHandler;
};NavigationNodeHandle
type NavigationNodeHandle = {
id: NodeId;
isFocused: () => boolean;
focus: (nodeId?: NodeId, options?: FocusNodeOptions) => void;
select: (nodeId?: NodeId, options?: SelectNodeOptions) => void;
Context: React.FunctionComponent<{ children: ReactNode }>;
};FocusNodeOptions and SelectNodeOptions are from @fiveway/core.
useNavigationNode
function useNavigationNode(options: NavigationNodeOptions): NavigationNodeHandle;Creates/updates a node, inserts it on mount, removes on unmount, and provides a Context component that scopes children to this node’s id.
NavigationNodeProps / NavigationNode
type NavigationNodeProps = NavigationNodeOptions & {
children?: ReactNode | ((props: Omit<NavigationNodeHandle, "Context">) => ReactNode);
};
function NavigationNode(props: NavigationNodeProps): ReactNode;Declarative wrapper around useNavigationNode with optional render-prop children.
Hooks
useIsFocused
function useIsFocused(nodeId: NodeId): boolean;Reactive isFocused for a node id relative to the current context parent (uses joinId).
useOnFocus
function useOnFocus(nodeId: NodeId, handler: () => void): void;Invokes the handler when focus enters this node’s subtree (from unfocused).
useOnBlur
function useOnBlur(nodeId: NodeId, handler: () => void): void;Invokes the handler when focus leaves this node’s subtree.
useOnFocusChange
function useOnFocusChange(nodeId: NodeId, handler: (id: NodeId | null) => void): void;Invokes the handler with the current focused id under this subtree, or null when unfocused. Also runs once with the initial value.
useFocusedId
function useFocusedId(scope: NodeId): NodeId | null;While scope contains focus, returns tree.focus; otherwise null.
useFocus
function useFocus(scope?: NodeId): (nodeId: NodeId, options?: FocusNodeOptions) => boolean;Returns a stable function that calls focusNode with ids joined under scope (default: context parent).
useSelect
function useSelect(scope?: NodeId): (nodeId: NodeId, options?: SelectNodeOptions) => void;Calls selectNode with ids under scope.
DOM integration
ElementHandler
type ElementHandler = ChainedHandler & {
register: (e: HTMLElement | null) => void;
};useElementHandler
function useElementHandler(): ElementHandler;Memoized handler combining element and spatial item behavior with a register ref callback.
DispatchOnEventOptions
type DispatchOnEventOptions = {
target?: EventTarget;
event?: string;
eventToAction?: (e: Event) => NavigationAction | null;
};useDispatchOnEvent
function useDispatchOnEvent(tree: NavigationTree, options?: DispatchOnEventOptions): void;Subscribes to DOM events on target (default window), event type default keydown, maps them with eventToAction (default defaultEventMapping), and forwards to dispatchAction.
useFocusSync
function useFocusSync(tree: NavigationTree): void;On focus change, focuses the DOM element registered for the focused node, if any.