hono-preact
Overview
Quick Start
The Route Table
Layouts & Nesting
Adding Pages
Active Links
Server Loaders
Loading States
Reloading Data
Prefetching
Streaming
Live Loaders
Realtime Channels
Server Actions
Validation
Optimistic UI
View Transitions
Middleware
CSRF Protection
CLI
Vite Config
Project Structure
Composing Hono Middleware
WebSockets
Rooms & Presence
renderPage
Link Prefetch
Build & Deploy
Overview
Dialog
Popover
Tooltip
Menu
Context Menu
Select
Combobox
Toast
renderElement
useControllableState
mergeRefs
useListNavigation
useTypeahead
useListboxSelection
usePosition
usePositioner
useDismiss
useFocusReturn
useSafeArea
usePresence

useTypeahead#

useTypeahead is the type-to-select hook useListNavigation uses internally, exported for composing your own keyboard navigation. It returns an onChar(char) callback that accumulates printable characters into a query string and returns the current query; the buffer resets after a short idle gap. The caller matches the returned query against its item labels and moves the active item.

Demo#

  • Argon
  • Boron
  • Calcium
  • Carbon
  • Cobalt
  • Neon

buffer: (empty)focus the list and type; it resets after 500ms idle

Example#

import { useTypeahead } from 'hono-preact-ui';
import { useState } from 'preact/hooks';

const ITEMS = ['Argon', 'Boron', 'Calcium', 'Carbon'];

function Typeahead() {
  const onChar = useTypeahead();
  const [active, setActive] = useState(0);

  return (
    <ul
      tabIndex={0}
      role="listbox"
      onKeyDown={(e) => {
        if (e.key.length !== 1) return; // printable characters only
        const query = onChar(e.key);
        const idx = ITEMS.findIndex((it) =>
          it.toLowerCase().startsWith(query.toLowerCase())
        );
        if (idx >= 0) setActive(idx);
      }}
    >
      {ITEMS.map((it, i) => (
        <li key={it} role="option" aria-selected={i === active}>
          {it}
        </li>
      ))}
    </ul>
  );
}

See also: useListNavigation, which folds typeahead together with arrow-key and Home/End navigation.

Signature#

import { useTypeahead } from 'hono-preact-ui';

function useTypeahead(opts?: UseTypeaheadOptions): (char: string) => string;

interface UseTypeaheadOptions {
  idleMs?: number; // reset the buffer after this idle gap, default 500
}

Call the returned function with each printable character (for example from a keydown handler, skipping non-printable keys); it appends the character to the buffer, schedules a reset after idleMs of no input, and returns the current query.

Options#

OptionTypeDefaultNotes
idleMsnumber500Reset the accumulated query after this idle gap.

Returns (char: string) => string: a stable callback that accumulates char into the query and returns it.