Select

A list that hangs from its field.

    Installation

    Terminal
    npx shadcn@latest add https://fabrickit.org/r/select.json

    This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

    Code

    components/fabricui/select.tsx
    "use client";
    
    import * as React from "react";
    import { mount } from "@/lib/fabricui/mount";
    import { Select as SelectEngine } from "@/lib/fabricui/select-engine";
    import "./select.css";
    
    const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
    
    /** One choice: its value, what it says, and a dim note on the right (a price, a count). */
    export type SelectOption = { value: string; label: string; meta?: string; sep?: false };
    /** A thin line between groups of options. */
    export type SelectSeparator = { sep: true };
    
    export type SelectProps = {
      /** The choices, in order; `{ sep: true }` draws a line between groups. */
    components/fabricui/select.css
    /*
     * select.css — Select (select.js). Moved out of select.html.
     */
    /*
     * Select. The calendar's trigger + overlay panel, holding a list of
     * options. The list drops open as cloth (cloth-drop.js, overlay mode) and
     * collapses plainly.
     */
    
    .sel:where(.fui-select, .fui-select *) {
      position: relative;
      width: 299px;
      height: 38px; /* the slot keeps its closed height; the panel overlays below */
    }
    .sel__panel:where(.fui-select, .fui-select *) { position: absolute; inset: 0 0 auto 0; z-index: 10; }
    
    lib/fabricui/select-engine.js
    /*
     * select-engine.js — Select (select.css). Moved out of select.html so it can be used anywhere.
     * (Fabric Kit: components/fabricui/select.tsx renders the markup and starts it.)
     */
    import { ClothDrop } from "./cloth-drop";
    
    /**
     * Select: a single choice from a list. Opens on click / Enter / Space /
     * ArrowDown; arrows + Home/End move, Enter/Space picks, Escape closes,
     * typing jumps to the next option starting with that letter.
     */
    export class Select {
      /**
       * @param {HTMLElement} root the .sel element
       * @param {{ options: ({ value: string, label: string, meta?: string, sep?: false } | { sep: true })[], value?: string, onChange?: (value: string) => void }} options
       */

    Usage

    Usage
    "use client";
    
    import * as React from "react";
    import { Select } from "@/components/fabricui/select";
    
    const PLANS = [
      { value: "starter", label: "Starter", meta: "Free" },
      { value: "pro", label: "Pro", meta: "$12/mo" },
      { sep: true as const },
      { value: "enterprise", label: "Enterprise", meta: "Custom" },
    ];
    
    export function Plan() {
      const [plan, setPlan] = React.useState("pro");
      return <Select options={PLANS} value={plan} onValueChange={setPlan} label="Plan" />;
    }

    Props

    PropTypeDefault
    options

    The choices, in order. meta is a dim note on the right (a price, a count); { sep: true } draws a line between groups.

    ({ value: string; label: string; meta?: string } | { sep: true })[]—
    value

    The chosen value, if you control it.

    string—
    defaultValue

    The starting value when you don't (the first option, if left out).

    string—
    onValueChange

    Called with the new value when another option is picked.

    (value: string) => void—
    label

    The list's accessible name.

    string—
    aria-labelledby

    Ids of what names the field, given to the trigger.

    string—
    className

    Added to the root.

    string—