Typeahead unstable
Matches typed prefixes against enabled items in a labelled list, with repeated-letter cycling. Use it to add type-to-jump navigation to a picker.
Details
Keep Typeahead.State in your component's Model, call Typeahead.step in
update, and use Typeahead.waitReset to send a message that resets the
buffer after a pause.
Example Typing to jump and cycling on repeated letters
import { Typeahead } from "effect/ui/kit"
const items = [ { label: "Apple" }, { label: "Apricot", disabled: true }, { label: "Avocado" }, { label: "Banana" }]
const first = Typeahead.step(Typeahead.initial, "a", items, -1)if (first.index !== 0) throw new Error("should match Apple")
// Repeating the same letter cycles past disabled Apricot to Avocado.const second = Typeahead.step(first.state, "a", items, first.index)if (second.index !== 2) throw new Error("should cycle to Avocado")
// Resetting clears the buffer so typing starts fresh.const cleared = Typeahead.reset(second.state)if (Typeahead.isTyping(cleared)) throw new Error("buffer should be cleared")Commands
Creates a Command that sends onComplete's message after delay
milliseconds (default: 750). Running it again restarts the timer.
Signature
declare function waitReset<Msg>(onComplete: () => Msg, delay?: number): Issued<Msg, never>The reset timer Command. In stories, resolve it with
.resolve(Typeahead.WaitReset, undefined).
Details
Waits delay milliseconds (750 ms when omitted). Each new
run interrupts the previous one in the same component instance.
Signature
declare const WaitReset: Command.Factory<{ readonly delay?: number;}, void>Constants
Guards
isPrintable
unstableReturns true when a keydown payload is a single printable character that
is not part of an IME composition and has no Alt, Ctrl or Meta
modifier.
Signature
declare function isPrintable(event: KeyEvent): booleanReturns true while characters are buffered in state.
Details
Use it to distinguish a Space pressed mid-word (part of typeahead) from a standalone Space (which activates the highlighted item or opens a trigger).
Signature
declare function isTyping(state: { readonly matched: number; readonly typed: string; readonly typedFrom: number;}): booleanMatching
Clears the buffered text so the next key starts a fresh search.
Signature
declare function reset(state: { readonly matched: number; readonly typed: string; readonly typedFrom: number;}): { readonly matched: number; readonly typed: string; readonly typedFrom: number;}Appends key to the buffer and returns the next state and matching index
(-1 if no item matches). A new search starts after from (the start of
items when from is -1).
Details
Matching is case-insensitive and skips disabled items. Pressing the same
single letter repeatedly cycles through items starting with that letter,
unless an enabled item's label begins with that letter doubled (such as
"Aaron"). A space that fails to extend a match is kept in the buffer so
multi-word labels can still match; any other unmatched key clears it.
Signature
declare function step(state: { readonly matched: number; readonly typed: string; readonly typedFrom: number;}, key: string, items: readonly Array<Item>, from: number): StepModels
A labelled item for step. Disabled items are skipped.
Signature
interface Item { readonly disabled?: boolean; readonly label: string;}The subset of Html.KeyPayload that isPrintable inspects.
Signature
type KeyEvent = Pick<Html.KeyPayload, "key" | "alt" | "ctrl" | "meta" | "composing">The typeahead buffer stored in a component's Model.
Signature
type State = typeof State.TypeThe result of step: the next State to store in the Model, and index,
the index of the matched item in items or -1 when nothing matched.
Signature
interface Step { readonly index: number; readonly state: { readonly matched: number; readonly typed: string; readonly typedFrom: number; };}