Select
Dropdown for a known set of options. Uses a native select, so it gets the platform's picker on mobile for free.
Playground
Change a prop and watch it. The code below follows along.
Live
Open full cardInstall and import
Select is in @dovetail-ds/react. Install it with React, if you haven't:
npm install @dovetail-ds/react react react-dom
import { Select } from "@dovetail-ds/react";
Import @dovetail-ds/react/styles.css once at your app's root, then your theme after it. Setup covers the stylesheets, fonts and dark mode.
Guidelines
Dropdown for a known set of options. Uses a native select, so it gets the platform's picker on mobile for free.
Use it when
- Five or more options, one answer.
Don't use it when
- Two or three options. Use Radio; visible options are faster than hidden ones.
- The list is long enough to need search. That is a Combobox, which arrives in Phase 3.
- Multiple answers. Use Checkbox.
Example
``jsx <Select label="Plan" placeholder="Choose a plan" options={["Starter", "Team", "Enterprise"]} /> ``
Tokens
--dt-input-*, shared with Input and Textarea.
Content
Placeholder is an instruction, not a fake value: "Choose a plan". Options are sentence case and parallel in structure.
Props
The typed contract, from Select.d.ts.
import * as React from "react";
/** Dropdown for a known set of options. Native select underneath. */
export interface SelectProps extends Omit<React.SelectHTMLAttributes<HTMLSelectElement>, "size"> {
label?: string;
hint?: string;
error?: string;
required?: boolean;
/** Strings or {value,label} pairs. */
options?: Array<string | { value: string; label: string }>;
/** Empty-value first option, e.g. "Choose a plan". */
placeholder?: string;
/** @default "md" */
size?: "sm" | "md" | "lg";
}
export declare function Select(props: SelectProps): React.JSX.Element;
Tokens it reads
Every custom property Select.jsx resolves, read from the source: 15 component, 2 semantic. Component tier first, because that is the one to reach for.
- component Its own tier. Override one of these and only this component moves.
- semantic Shared roles. Override one and everything using that role moves with it.
| Token | Tier | Declared as |
|---|---|---|
--dt-input-bg | component | var(--dt-surface-base) |
--dt-input-bg-disabled | component | var(--dt-surface-disabled) |
--dt-input-border | component | var(--dt-border-default) |
--dt-input-border-error | component | var(--dt-surface-danger) |
--dt-input-border-focus | component | var(--dt-focus-ring-color) |
--dt-input-border-width | component | var(--dt-border-width-default) |
--dt-input-fg | component | var(--dt-text-primary) |
--dt-input-font-family | component | var(--dt-text-body-md-family) |
--dt-input-font-size | component | var(--dt-font-size-sm) |
--dt-input-height-lg | component | var(--dt-size-control-lg) |
--dt-input-height-md | component | var(--dt-size-control-md) |
--dt-input-height-sm | component | var(--dt-size-control-sm) |
--dt-input-padding-x | component | var(--dt-space-inset-sm) |
--dt-input-radius | component | var(--dt-radius-control) |
--dt-input-transition | component | var(--dt-motion-micro) |
--dt-size-icon-sm | semantic | var(--dt-dim-4) |
--dt-text-secondary | semantic | var(--dt-color-neutral-600) |
Source
React only, no dependencies, every value a token.
import React from "react";
import { Field } from "./Field.jsx";
export function Select({ label, hint, error, required, options = [], placeholder, size = "md", id, style, ...rest }) {
const [focus, setFocus] = React.useState(false);
const auto = React.useId();
const selectId = id || auto;
const border = error ? "var(--dt-input-border-error)" : focus ? "var(--dt-input-border-focus)" : "var(--dt-input-border)";
const control = (
<div style={{ position: "relative", display: "flex", alignItems: "center" }}>
<select
id={selectId}
required={required}
aria-invalid={error ? true : undefined}
onFocus={() => setFocus(true)}
onBlur={() => setFocus(false)}
style={{
width: "100%",
height: `var(--dt-input-height-${size})`,
padding: "0 calc(var(--dt-input-padding-x) * 2 + var(--dt-size-icon-sm)) 0 var(--dt-input-padding-x)",
fontFamily: "var(--dt-input-font-family)",
fontSize: "var(--dt-input-font-size)",
color: "var(--dt-input-fg)",
background: rest.disabled ? "var(--dt-input-bg-disabled)" : "var(--dt-input-bg)",
border: `var(--dt-input-border-width) solid ${border}`,
borderRadius: "var(--dt-input-radius)",
outline: "none",
appearance: "none",
cursor: "pointer",
transition: "border-color var(--dt-input-transition)",
boxSizing: "border-box",
}}
{...rest}
>
{placeholder && <option value="">{placeholder}</option>}
{options.map((o) => {
const opt = typeof o === "string" ? { value: o, label: o } : o;
return <option key={opt.value} value={opt.value}>{opt.label}</option>;
})}
</select>
<svg
aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"
style={{ position: "absolute", right: "var(--dt-input-padding-x)", width: "var(--dt-size-icon-sm)", height: "var(--dt-size-icon-sm)", pointerEvents: "none", color: "var(--dt-text-secondary)" }}
>
<path d="m6 9 6 6 6-6" />
</svg>
</div>
);
if (!label && !hint && !error) return <div style={style}>{control}</div>;
return <Field label={label} hint={hint} error={error} required={required} htmlFor={selectId} style={style}>{control}</Field>;
}
In context
Select also appears on the forms reference cards, alongside the rest of its family.