仕組み
The panel is not portaled: the root is relative and the panel hangs under it at the trigger's width, so it stays put inside scaled containers, scroll areas and dialogs. On open it measures the space and flips up if there is no room below. Long lists scroll with a 16px fade at the edge that has more. When searchable, the closed field shows the selected option, fully selected so typing replaces it; IME composition does not filter until committed.
向いている場面
- Picking one value from five or more fixed options in a form: destination, country, team.
- Filters at the top of a list page (size="sm"), applied as soon as a value is chosen.
- Dozens or hundreds of options, or options from an API: turn on searchable and use onSearch with loading.
- Unlike a dropdown menu, the value stays in the trigger after choosing instead of running an action.
向かない場面
- Actions such as edit, delete or export: use a dropdown menu.
- Two to four short options: use radio buttons or a segmented control.
- Choosing several values at once: use a multi-select.
- An ancestor with overflow: hidden and no room above or below: the panel can be clipped; leave height or lower listHeight.
使い方
import { useState } from 'react'
import { Select } from './Select.jsx'
const cities = [
{ value: 'lis', label: 'Lisbon', description: '12 flights a week', group: 'Europe' },
{ value: 'rom', label: 'Rome', description: '9 flights a week', group: 'Europe' },
{ value: 'tyo', label: 'Tokyo', description: '14 flights a week', group: 'Asia' },
{ value: 'bkk', label: 'Bangkok', description: 'No service yet', group: 'Asia', disabled: true },
]
export function DestinationField() {
const [city, setCity] = useState(null)
const [touched, setTouched] = useState(false)
const invalid = touched && city === null
return (
<div>
<label htmlFor="city">Destination</label>
<Select
id="city"
options={cities}
value={city}
onChange={setCity}
onBlur={() => setTouched(true)}
searchable
clearable
required
invalid={invalid}
placeholder="Type a city to search"
aria-describedby="city-msg"
/>
<p id="city-msg">{invalid ? 'Please choose a destination' : ''}</p>
</div>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
options | { value, label, description?, disabled?, group? }[] | — | The options. value is a string or number and must be unique; adjacent options with the same group form one group. |
value | string | number | null | — | Controlled value; null means nothing selected. |
defaultValue | string | number | null | null | Initial value when uncontrolled. |
onChange | (value, option) => void | — | Called when an option is chosen or the value is cleared (both arguments null). Choosing the selected option again does not fire it. |
open | boolean | — | Controlled: whether the panel is open. Use with onOpenChange. |
defaultOpen | boolean | false | Uncontrolled: initially open (does not take focus). |
onOpenChange | (open: boolean) => void | — | Called when the open state changes. |
size | 'sm' | 'md' | 'lg' | 'md' | Trigger 36 / 44 / 52 high with 10 / 12 / 14px corners; option rows 36 / 40 / 44. Use lg on mobile. |
placeholder | string | 'Select' | Hint shown when nothing is selected. |
disabled | boolean | false | Disabled; an open panel closes. |
invalid | boolean | false | Validation failed: danger hairline and tint, and aria-invalid on the trigger. Write the message yourself and link it with aria-describedby. |
required | boolean | false | Sets aria-required on the trigger; draw any “*” beside the label yourself. |
searchable | boolean | false | The trigger becomes an input that filters as you type; matches are highlighted. |
onSearch | (query: string) => void | — | Called when the search term changes (after IME commit; an empty string on close). Send remote searches from here. |
filterOption | false | ((query, option) => boolean) | — | How to filter. Default: label contains the term, case-insensitive. A function allows aliases; false means options are already the results. |
clearable | boolean | false | Shows a clear button when there is a value (on hover or focus; always on touch). Backspace / Delete also clear. |
loading | boolean | false | Options are loading: the panel shows only the loading text. |
renderOption | (option, { selected, active, query }) => ReactNode | — | Custom content for the left of an option, such as an avatar or price. The check mark on the right is still drawn by the component. |
listHeight | number | 240 | Maximum list height in px; longer lists scroll inside the panel. |
emptyText | string | — | Text when there are no options. Defaults to the noMatch label while searching, otherwise noOptions. |
labels | object | — | Replaces built-in text (clear, options, loading, noMatch, noOptions). Shallow-merged over the English defaults. |
name | string | — | Renders a hidden <input name> so native form submission includes the value. |
ref | Ref<HTMLButtonElement | HTMLInputElement> | — | Forwarded to the focusable element in the trigger (a <button>, or an <input> when searchable). |
...rest | HTMLAttributes<HTMLDivElement> | — | className, style and onBlur go to the root. id, autoFocus, aria-label, aria-labelledby and aria-describedby go to the trigger so <label htmlFor> works. Panel layer: --ds-select-z (default 30). |
アクセシビリティ
- Follows the WAI-ARIA combobox + listbox pattern: role="combobox" with aria-expanded and aria-controls, options with aria-selected, groups labelled by their headings.
- Focus stays on the trigger and the highlighted option is announced through aria-activedescendant.
- Keyboard: Down / Up open and cycle (skipping disabled), Home / End, PageUp / PageDown jump 10, Enter selects, Esc closes only the panel, Tab closes and moves on.
- When not searchable, typing a letter jumps to a matching option; turn on searchable for long lists.
- invalid sets aria-invalid, required sets aria-required; loading and empty text are in role="status". On touch, options are at least 44px high.
モーション
The panel enters in 220ms ease-out (from 6px toward the trigger and scale 0.96, with a fade, growing from the edge that touches the trigger) and exits in 140ms. The trigger turns white and its accent halo grows in over 220ms; the chevron turns in 240ms. The check mark pops in with a 320ms spring. The highlight has no transition.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
Select.jsxselect.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。





