labs

command palette

2026-08-31

who owns the selection

the half nobody handles is who owns the selection. a cursor resting over row three steals it the instant the list scrolls under it while you are arrowing, because the browser fires pointerover at a stationary cursor whenever the element beneath it changes, and a palette that loses your place mid keystroke is worse than one with no mouse support at all.

so hover is ignored until a real pointermove says the mouse actually moved, and every arrow key disarms it again.

one travelling highlight

the highlight is the other half: one element that travels between rows rather than a class that jumps, and it has to be placed without travelling on the very first render or the palette opens with a bar sliding in from nowhere.

take it with you

three steps, and the stage above runs in your own project.

  1. install

    nothing to add. it runs on react alone, 18 or newer.

  2. copy

    3 files, kept together in one folder, command-palette/. the styles travel with them.

    "use client";
    
    import {
      useEffect,
      useLayoutEffect,
      useRef,
      useState,
      type KeyboardEvent,
      type CSSProperties,
    } from "react";
    import { match, nextIndex, type Ranges } from "./filter";
    import "./command-palette.css";
    
    export interface Item {
      id: string;
      label: string;
      hint: string;
    }
    
    export interface PaletteProps {
      items: Item[];
      // rows shown before the list scrolls
      max?: number;
      // ms the highlight takes to glide between rows
      travel?: number;
      placeholder?: string;
    }
    
    function Marked({ text, ranges }: { text: string; ranges: Ranges }) {
      if (ranges.length === 0) return <>{text}</>;
      const out = [];
      let at = 0;
      for (const [start, end] of ranges) {
        if (start > at) out.push(text.slice(at, start));
        out.push(<mark key={start}>{text.slice(start, end)}</mark>);
        at = end;
      }
      if (at < text.length) out.push(text.slice(at));
      return <>{out}</>;
    }
    
    export default function CommandPalette({
      items,
      max = 9,
      travel = 120,
      placeholder = "type a command",
    }: PaletteProps) {
      const [query, setQuery] = useState("");
      const [selected, setSelected] = useState(0);
      const listRef = useRef<HTMLUListElement>(null);
      const barRef = useRef<HTMLDivElement>(null);
      const hoverArmed = useRef(false);
      const armed = useRef(false);
      const [armedState, setArmedState] = useState(false);
    
      const results = items
        .map((item) => ({ item, ranges: match(query, item.label) }))
        .filter((r): r is { item: Item; ranges: Ranges } => r.ranges !== null)
        .slice(0, max);
    
      useEffect(() => {
        setSelected(0);
      }, [query]);
    
      useLayoutEffect(() => {
        const row = listRef.current?.children[selected] as HTMLElement | undefined;
        const bar = barRef.current;
        if (!row || !bar) return;
        bar.style.transform = `translateY(${row.offsetTop}px)`;
        bar.style.height = `${row.offsetHeight}px`;
        const wrap = row.closest(".cp-list-wrap") as HTMLElement | null;
        if (wrap) {
          const top = row.offsetTop;
          const bottom = top + row.offsetHeight;
          if (top < wrap.scrollTop) wrap.scrollTop = row.previousElementSibling ? top : 0;
          else if (bottom > wrap.scrollTop + wrap.clientHeight) wrap.scrollTop = bottom - wrap.clientHeight;
        }
        if (!armed.current) {
          armed.current = true;
          requestAnimationFrame(() => setArmedState(true));
        }
      }, [selected, results.length]);
    
      const onKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
        if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return;
        e.preventDefault();
        hoverArmed.current = false;
        setSelected((s) => nextIndex(s, e.key === "ArrowDown" ? 1 : -1, results.length));
      };
    
      const activeId = results[selected] ? `cp-${results[selected]!.item.id}` : undefined;
    
      return (
        <div
          className="cp"
          onPointerMove={() => {
            hoverArmed.current = true;
          }}
        >
          <input
            className="cp-input"
            type="text"
            role="combobox"
            aria-expanded="true"
            aria-controls="cp-list"
            aria-activedescendant={activeId}
            aria-label="search"
            placeholder={placeholder}
            value={query}
            onChange={(e) => setQuery(e.target.value)}
            onKeyDown={onKeyDown}
          />
    
          <div className="cp-list-wrap" style={{ "--cp-travel": `${travel}ms` } as CSSProperties}>
            <div className="cp-bar" ref={barRef} data-armed={String(armedState)} aria-hidden="true" />
            <ul className="cp-list" id="cp-list" role="listbox" aria-label="results" ref={listRef}>
              {results.map(({ item, ranges }, i) => (
                <li
                  key={item.id}
                  id={`cp-${item.id}`}
                  role="option"
                  aria-selected={i === selected}
                  className="cp-row"
                  data-on={String(i === selected)}
                  onPointerEnter={() => {
                    if (hoverArmed.current) setSelected(i);
                  }}
                >
                  <span>
                    <Marked text={item.label} ranges={ranges} />
                  </span>
                  <span className="cp-hint">{item.hint}</span>
                </li>
              ))}
              {results.length === 0 && <li className="cp-empty">nothing matches.</li>}
            </ul>
          </div>
        </div>
      );
    }
    
  3. use

    the stage above, exactly as it runs here. the comments mark what to change.

    example.tsx
    "use client";
    
    import CommandPalette, { type Item } from "./command-palette/CommandPalette";
    
    const ITEMS: Item[] = [
      { id: "home", label: "go home", hint: "route" },
      { id: "projects", label: "open projects", hint: "route" },
      { id: "labs", label: "open labs", hint: "route" },
      { id: "theme", label: "switch theme", hint: "action" },
      { id: "copy", label: "copy page link", hint: "action" },
      { id: "mail", label: "write an email", hint: "contact" },
      { id: "resume", label: "download résumé", hint: "file" },
      { id: "source", label: "view source", hint: "link" },
      { id: "top", label: "back to top", hint: "action" },
      { id: "search", label: "search the docs", hint: "link" },
    ];
    
    export default function CommandPaletteExample({ items = ITEMS }: { items?: Item[] }) {
      return (
        <div className="cp-lab">
          {/* the arrow keys move the highlight; the pointer only takes over once it moves */}
          <CommandPalette items={items} max={9} travel={120} />
        </div>
      );
    }