Components / Selection

Select19

Pick one option from a list. It looks like a field, opens like a menu and submits like a native <select>.

Select · PlaygroundLive
Plan
JSX
<Select label="Plan" placeholder="Choose a plan">
  <SelectItem id="free">Free</SelectItem>
  <SelectItem id="pro">Pro</SelectItem>
  <SelectItem id="team">Team</SelectItem>
</Select>

Usage

TSX
import { Select, SelectItem } from "plonk-ui";

export function Plan() {
  return (
    <Select label="Plan" name="plan" defaultValue="free">
      <SelectItem id="free">Free</SelectItem>
      <SelectItem id="pro">Pro</SelectItem>
    </Select>
  );
}

Each SelectItem has an id; that's the value you get back and the value the form submits under name. For a handful of options that should all be visible, a Radio Group is quicker to scan.

Descriptions

Options can have a description and an icon. The closed field shows only the option's name.

Select · DescriptionsLive
PlanYou can change plans at any time.
TSX
import { Select, SelectItem } from "plonk-ui";

export default function Descriptions() {
  return (
    <Select
      className="w-full max-w-xs"
      label="Plan"
      defaultValue="pro"
      description="You can change plans at any time."
    >
      <SelectItem id="free" description="1 project, 3 members">
        Free
      </SelectItem>
      <SelectItem id="pro" description="Unlimited projects, 10 members">
        Pro
      </SelectItem>
      <SelectItem id="team" description="Everything unlimited">
        Team
      </SelectItem>
      <SelectItem id="enterprise" description="Talk to sales" isDisabled>
        Enterprise
      </SelectItem>
    </Select>
  );
}

Sections

Group long lists with SelectSection.

Select · SectionsLive
Time zone
TSX
import { Select, SelectItem, SelectSection } from "plonk-ui";

export default function Sections() {
  return (
    <Select
      className="w-full max-w-xs"
      label="Time zone"
      placeholder="Pick a city"
    >
      <SelectSection title="Europe">
        <SelectItem id="Europe/Istanbul">Istanbul</SelectItem>
        <SelectItem id="Europe/Berlin">Berlin</SelectItem>
        <SelectItem id="Europe/Lisbon">Lisbon</SelectItem>
      </SelectSection>
      <SelectSection title="Americas">
        <SelectItem id="America/New_York">New York</SelectItem>
        <SelectItem id="America/Mexico_City">Mexico City</SelectItem>
      </SelectSection>
      <SelectSection title="Asia">
        <SelectItem id="Asia/Tokyo">Tokyo</SelectItem>
        <SelectItem id="Asia/Singapore">Singapore</SelectItem>
      </SelectSection>
    </Select>
  );
}

Controlled, in a form

Control the value with value and onChange. Validation works like Input: you decide, then pass the message to error.

Select · FormLive
Role

import { useState, type FormEvent } from "react";
import { Button, Select, SelectItem } from "plonk-ui";

export default function Form() {
  const [role, setRole] = useState<string | null>(null);
  const [submitted, setSubmitted] = useState(false);
  const [sent, setSent] = useState<string>();

  function handleSubmit(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    setSubmitted(true);
    if (!role) return;
    setSent(String(new FormData(event.currentTarget).get("role")));
  }

  return (
    <form
      onSubmit={handleSubmit}
      noValidate
      className="flex w-full max-w-xs flex-col gap-4"
    >
      <Select
        label="Role"
        name="role"
        placeholder="Choose a role"
        isRequired
        value={role}
        onChange={(key) => setRole(key == null ? null : String(key))}
        error={
          submitted && !role ? "Pick a role for the new member." : undefined
        }
      >
        <SelectItem id="viewer">Viewer</SelectItem>
        <SelectItem id="editor">Editor</SelectItem>
        <SelectItem id="admin">Admin</SelectItem>
      </Select>
      <Button type="submit" className="self-start">
        Invite
      </Button>
      <p role="status" className="font-bold empty:hidden">
        {sent ? `Invited as ${sent}.` : ""}
      </p>
    </form>
  );
}

Options from data

Pass items and a render function to build the list from an array. Each item needs an id:

TSX
<Select label="Owner" items={members}>
  {(member) => <SelectItem>{member.name}</SelectItem>}
</Select>;

Personalities

The field lifts on focus like Input, and the list opens like a Menu: from the edge facing the field, without tilting, in each personality's own style. The selected option has a tick (a star in Arcade). The highlight hooks work here too.

Accessibility

  • A button with a listbox: Enter, Space or the arrow keys open it, and typing a letter selects the matching option even while closed.
  • The label, description and error are linked to the button.
  • A hidden native <select> carries the value for forms and autofill.
  • The focus shadow shows for keyboard users only; clicking doesn't leave it behind.

API

Select

PropTypeDefaultDescription
itemsIterable<T>—Options as data, with a render function as children. Each item needs an id.
classNamestring—Extra classes for the outer wrapper.
labelReactNode—Visible label, linked to the field.
descriptionReactNode—Help text under the field.
errorReactNode—Error message. Marks the field invalid and shakes it once. Pass true to mark it without a message.
valueKey | null—Controlled value: the selected option's id, or null.
defaultValueKey | null—Selected option on first render, when uncontrolled.
onChange(value: Key | null) => void—Called with the new option's id.
placeholderstring"Select an item"Shown while nothing is selected. The default is translated for the provider's locale.
namestring—Field name: the selected id is submitted with the form.
isRequiredboolean—Marks the field required, with an asterisk in the label.
isDisabledboolean—Disables the field.
disabledKeysIterable<Key>—Ids of options that can't be selected.

SelectItem

PropTypeDefaultDescription
classNamestring—Extra classes for the row.
iconReactNode—Icon before the label. Hidden from screen readers.
descriptionReactNode—Smaller text under the label, read with it.
idKey—Unique key, used for selection and in callbacks.
isDisabledboolean—Can't be focused or chosen.
textValuestring—Plain text for type-to-search. Set it when the label isn't a string.

SelectSection

PropTypeDefaultDescription
titleReactNode—Group heading, read by screen readers.