Number 51: Select with a gliding highlight
Specification
- Catalogue
- No. 51 of 110
- Section
- Forms and inputs
- Interaction
- Hover
- Element
- Input, Picker
- Preview
- Live component
- Imports
- react 19.3.1, @number-flow/react 0.6.2
- Added
Notes
One highlight moves between the options as the pointer or the arrow keys move, like the gliding menu.
The eye follows one moving highlight instead of rows blinking. It opens a little slower than it closes, and on a phone it becomes the phone's own picker.
Highlight glides 180ms cubic-bezier(.22, 1, .36, 1). Opens 150ms, closes 110ms (the popover recipe); Escape returns focus to the button. Listbox with arrows, Home, End, Enter; native select on touch screens.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useEffect, useId, useRef, useState, type KeyboardEvent } from "react";
import { useMounted } from "@/components/pieces/shared";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "@/components/pieces/kit/pop.css";
import "./glide-select.css";
const SIZES = [
{ id: "6", label: "6 inch", note: "Serves 8", price: "£32" },
{ id: "8", label: "8 inch", note: "Serves 14", price: "£45" },
{ id: "10", label: "10 inch", note: "Serves 22", price: "£60" },
{ id: "tier", label: "Two tiers", note: "Serves 40", price: "£110" },
];
const OPT_H = 44;
function GlideSelectDemo() {
const [value, setValue] = useState(1);
const [open, setOpen] = useState(false);
const [active, setActive] = useState(1);
const [coarse, setCoarse] = useState(false);
const mounted = useMounted();
const trigger = useRef<HTMLButtonElement>(null);
const list = useRef<HTMLUListElement>(null);
const wrap = useRef<HTMLDivElement>(null);
const uid = useId();
useEffect(() => {
const mq = window.matchMedia("(pointer: coarse)");
const on = () => setCoarse(mq.matches);
on();
mq.addEventListener("change", on);
return () => mq.removeEventListener("change", on);
}, []);
useEffect(() => {
if (!open) return;
list.current?.focus();
const onDown = (e: PointerEvent) => {
if (wrap.current && e.target instanceof Node && !wrap.current.contains(e.target)) setOpen(false);
};
document.addEventListener("pointerdown", onDown);
return () => document.removeEventListener("pointerdown", onDown);
}, [open]);
const choose = (i: number) => {
setValue(i);
setOpen(false);
trigger.current?.focus();
};
const onListKey = (e: KeyboardEvent) => {
const n = SIZES.length;
const map: Record<string, number> = { ArrowDown: Math.min(n - 1, active + 1), ArrowUp: Math.max(0, active - 1), Home: 0, End: n - 1 };
if (e.key in map) {
e.preventDefault();
setActive(map[e.key] ?? active);
} else if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
choose(active);
} else if (e.key === "Escape" || e.key === "Tab") {
if (e.key === "Escape") e.preventDefault();
setOpen(false);
if (e.key === "Escape") trigger.current?.focus();
}
};
const v = SIZES[value]!;
return (
<div className="dm" style={{ alignContent: "start", paddingTop: 40 }}>
<div className="gs" ref={wrap}>
<span className="gs-label" id={`${uid}-l`}>
Cake size
</span>
{mounted && coarse ? (
<select className="gs-native" aria-labelledby={`${uid}-l`} value={value} onChange={(e) => setValue(Number(e.target.value))}>
{SIZES.map((s, i) => (
<option key={s.id} value={i}>
{s.label}, {s.note.toLowerCase()}, {s.price}
</option>
))}
</select>
) : (
<>
<button
ref={trigger}
type="button"
className="gs-trigger"
aria-haspopup="listbox"
aria-expanded={open}
aria-labelledby={`${uid}-l ${uid}-v`}
onClick={() => {
setActive(value);
setOpen((o) => !o);
}}
onKeyDown={(e) => {
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
e.preventDefault();
setActive(value);
setOpen(true);
}
}}
>
<span id={`${uid}-v`}>
{v.label} <span className="gs-note">{v.note}</span>
</span>
<span className="gs-price">{v.price}</span>
<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
<path d="m7 10 5 5 5-5" />
</svg>
</button>
<ul
ref={list}
className="gs-list pop"
role="listbox"
tabIndex={-1}
aria-labelledby={`${uid}-l`}
aria-activedescendant={`${uid}-o${active}`}
data-closed={!open || undefined}
inert={!open || undefined}
onKeyDown={onListKey}
>
<span className="gs-hl" aria-hidden="true" style={{ translate: `0 ${active * OPT_H}px` }} />
{SIZES.map((s, i) => (
<li
key={s.id}
id={`${uid}-o${i}`}
role="option"
aria-selected={i === value}
className="gs-opt"
onPointerEnter={() => setActive(i)}
onPointerUp={() => choose(i)}
>
<span>
{s.label} <span className="gs-note">{s.note}</span>
</span>
<span className="gs-price">{s.price}</span>
</li>
))}
</ul>
</>
)}
</div>
</div>
);
}
// What a hovered card shows: the list opened and walked with the keyboard.
// On a touch screen the piece uses the native select, which a card leaves alone.
const press = (root: HTMLElement, key: string) =>
root.querySelector(".gs-list")?.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true }));
const TOUR: Tour = [
{ run: (root) => root.querySelector<HTMLElement>(".gs-trigger")?.click(), wait: 700 },
{ run: (root) => press(root, "ArrowDown"), wait: 450 },
{ run: (root) => press(root, "ArrowDown"), wait: 650 },
{ run: (root) => press(root, "Enter"), wait: 1200 },
];
export default function GlideSelectEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Select with a gliding highlight" sample tour={TOUR}>
<GlideSelectDemo />
</PieceStage>
);
}