ListNavigation unstable
Moves an index across a list of items in response to arrow keys, Home and
End, respecting orientation, text direction and looping.
Details
step and target return indexes; the caller moves focus or updates a
highlight such as aria-activedescendant. Filter disabled items with
available before calculating indexes.
Example Navigating a horizontal right-to-left list
import { ListNavigation } from "effect/ui/kit"
const items = [ { value: "a" }, { value: "b", disabled: true }, { value: "c" }].filter(ListNavigation.available)
const next = ListNavigation.target("ArrowLeft", { at: 0, last: items.length - 1, orientation: "horizontal", rtl: true, loop: "wrap"})if (next !== 1 || items[next]?.value !== "c") { throw new Error("ArrowLeft in RTL should move from a to c")}Guards
Returns true when an item is defined and not disabled.
Signature
declare function available<A extends { readonly disabled?: boolean;}>(item: A | undefined): item is AReturns true when a keydown payload is an unmodified, non-composing
navigation key (Home, End, or the arrow keys for orientation).
Details
Use it in a view or parts keydown handler with { preventDefault: true }
to cancel the browser default only for matching keys.
Signature
declare function isKey(event: KeyEvent, orientation: "horizontal" | "vertical"): booleanModels
The KeyboardEvent.key names for moving forward and backward along an axis.
Signature
interface ArrowKeys { readonly next: string; readonly previous: string;}The subset of Html.KeyPayload that isKey inspects.
Signature
type KeyEvent = Pick<Html.KeyPayload, "key" | "alt" | "ctrl" | "meta" | "shift" | "composing">What step and target do at the ends of a list.
Signature
type Loop = typeof Loop.TypeThe axis a list runs along.
Signature
type Orientation = typeof Orientation.TypeOptions for target.
Details
at: the current index, or-1when no item is active.last: the last valid index (items.length - 1), or-1for an empty list.orientation:"vertical"by default.rtl: swapsArrowLeftandArrowRightwhenorientationis"horizontal".falseby default.loop:"wrap"by default.
Signature
interface TargetOptions { readonly at: number; readonly last: number; readonly loop?: "none" | "wrap" | "escape"; readonly orientation?: "horizontal" | "vertical"; readonly rtl?: boolean;}Schemas
What step and target do at the ends of a list.
Details
"none": stay on the first or last item."wrap": jump from the last item to the first and back."escape": return-1past either end, so a highlight can clear before entering the other end on the next press.
Signature
declare const Loop: Literals<readonly ["none", "wrap", "escape"]>Orientation
unstableThe axis a list runs along, which decides which arrow keys move through it.
Signature
declare const Orientation: Literals<readonly ["horizontal", "vertical"]>