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
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a NativeSelect:
Examples
Sizes
Use the size prop to set the height: lg is 54px tall, default is 44px tall and sm is 36px tall.
With groups
Options organized under labelled NativeSelectOptGroup headings.
Disabled
Set disabled to dim the select to 50% opacity and block input.
Invalid
Set aria-invalid to show the destructive border and ring.
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.
API Reference
NativeSelect
The wrapper and native select element, with a decorative caret icon.
| Prop | Type | Default |
|---|---|---|
size | "sm" | "default" | "lg" | "default" |
Also accepts the props of Omit<React.ComponentProps<"select">, "size">. They go to the select element, except className, which goes to the wrapper.
Standard attributes such as value, defaultValue, onChange, name and required work as on a plain select. The size prop replaces the native size attribute. To show a placeholder, add a first option with value="" and disabled; while it is selected, the text uses text-muted-foreground.
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">.
Options and groups use the Canvas and CanvasText system colors so the native popup stays legible in light and dark modes. Each part sets a data-slot, such as native-select-wrapper or native-select-option, for styling. The wrapper and select both set data-size.