suiss/ui · Component
Native Select
A styled native select element for choosing one option from a list.
Native Select is an open-source component from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.
NativeSelect wraps the browser's select element with the library's control styling and a trailing up-down caret. Add choices with NativeSelectOption, and group them under headings with NativeSelectOptGroup.
Because it uses the platform picker, it works well on touch devices and in plain HTML forms. Use Select when you need a custom-styled popup, rich option content or a searchable list.
Installation
CLI
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
Manual
- Install the dependencies and add the
cnhelper:
- Copy the source into
components/ui/native-select.tsx:
- Update the import paths to match your project.
Usage
All standard select attributes, such as value, defaultValue, onChange, name, disabled and required, pass through to the select element. className is applied to the wrapper, not the select.
To show a placeholder, add a first option with value="" and disabled; while it is selected, the text uses text-muted-foreground.
Examples
Sizes
The sm and default sizes side by side.
With groups
Options organized under labelled NativeSelectOptGroup headings.
Disabled
A select with disabled set, dimmed to 50% opacity.
Invalid
A select with aria-invalid set, shown with the destructive border and ring.
API Reference
NativeSelect
The wrapper and native select element, with a decorative caret icon.
| Prop | Type | Default | Description |
|---|---|---|---|
size | "sm" | "default" | "default" | The control height. |
Also accepts the props of NativeSelectProps.
NativeSelectOption
A single choice, rendered as an option element.
Also accepts the props of React.ComponentProps<"option">.
NativeSelectOptGroup
A labelled group of options, rendered as an optgroup element.
Also accepts the props of React.ComponentProps<"optgroup">.
Accessibility
- Renders a native
select, exposed as acomboboxwith native keyboard and picker behavior. - Provide an accessible name with a
Labellinked throughhtmlFor, or witharia-label. NativeSelectOptGroupexposes itslabelas a namedgroup.- Set
aria-invalidto communicate a validation error; it also applies the invalid styling. - The caret icon is
aria-hidden.
Notes
- Styling hooks:
data-slotvaluesnative-select-wrapper,native-select,native-select-icon,native-select-optionandnative-select-optgroup. The wrapper andselectboth exposedata-size. - The
sizeprop acceptsdefault(44px tall) orsm(36px tall) and replaces the nativesizeattribute. - Options and groups use the
CanvasandCanvasTextsystem colors so the native popup stays legible in light and dark modes. - Uses the
secondary,destructiveandmuted-foregroundcolors, andopacity-50when disabled.