NativeSelect
A styled native select element
Usage
import {NativeSelect} from "@peakui-pro/react";Anatomy
import {Description, FieldError, Label} from "@peakui/react";
import {NativeSelect} from "@peakui-pro/react";
// A core `Label`, `Description` and `FieldError` placed inside the root name and describe the
// select. `Trigger` takes every native `select` attribute; its options go inside it, and an
// `Indicator` among them is drawn beside the select rather than inside it.
export default () => (
<NativeSelect>
<Label />
<NativeSelect.Trigger>
<NativeSelect.OptGroup>
<NativeSelect.Option />
</NativeSelect.OptGroup>
<NativeSelect.Indicator />
</NativeSelect.Trigger>
<Description />
<FieldError />
</NativeSelect>
);NativeSelect is a real <select>: the browser draws the list, handles the keyboard and shows its
own picker on touch. It imports no Select from the core. Without an Indicator among the
trigger's children it draws a chevron.
Only direct children of Trigger are told apart. An Indicator wrapped in a fragment or another
component stays inside the select, where it is not valid; write it as a direct child.
Examples
Variants
With Groups
With Label
With Description
Disabled Select
Invalid State
Mark the field invalid on the root with data-invalid="true" (or aria-invalid="true"). The root
sets data-invalid, the select gets aria-invalid, the Description steps aside, and a FieldError
placed inside the root renders its message and describes the select.
Full Width
Controlled
With Disabled Options
Custom Indicator
Form Example
Customization
Tailwind CSS
<NativeSelect className="[--native-select-bg:var(--color-surface)]" variant="secondary">
<NativeSelect.Trigger className="text-sm" wrapperClassName="w-48" />
</NativeSelect>Global CSS
@layer components {
.native-select__select {
border-radius: var(--radius-xl);
}
.native-select__indicator {
color: var(--color-accent);
}
}Styling Reference
PeakUI follows the BEM methodology, so variants and states are plain class names you can target.
CSS Classes
The NativeSelect component uses these CSS classes (view source styles):
Base & Layout Classes
.native-select- The root: the label, the control and the description.native-select__trigger- The positioned box around the select and the indicator.native-select__select- The nativeselectelement.native-select__indicator- The chevron
Variant Classes
.native-select--secondary- Neutral fill, no shadow
Modifier Classes
.native-select--full-width- The root takes the full width.native-select__trigger--full-width- The trigger takes the full width
Slots
[data-slot="native-select"]- The root[data-slot="native-select-trigger"]- The positioned box[data-slot="native-select-select"]- The select[data-slot="native-select-indicator"]- The indicator[data-slot="native-select-optgroup"]- A group[data-slot="native-select-option"]- An option
Interactive States
The select supports both CSS pseudo-classes and data attributes:
- Hover:
:hoveror[data-hovered="true"] - Focus visible:
:focus-visibleor[data-focus-visible="true"] - Invalid:
[data-invalid="true"]on.native-select, or[aria-invalid="true"]on the select - Disabled:
:disabledor[aria-disabled="true"]
CSS Variables
| Variable | Purpose |
|---|---|
--native-select-bg | Resting fill of the select; set by the variant |
--native-select-bg-hover | Fill while hovered |
--native-select-bg-focus | Fill while focused or invalid |
--native-select-shadow | The select's own shadow, beside the focus ring |
API Reference
NativeSelect
| Prop | Type | Default | Description |
|---|---|---|---|
variant | 'primary' | 'secondary' | 'primary' | The surface: the field's own, or a neutral fill without a shadow |
fullWidth | boolean | false | Takes the full width of its container |
data-invalid | 'true' | - | Marks the field invalid; aria-invalid="true" does the same |
className | string | - | Additional classes |
render | (props, state) => ReactElement | - | Replaces the root element |
...rest | ComponentProps<'div'> | - | Native <div> attributes |
NativeSelect.Trigger
| Prop | Type | Default | Description |
|---|---|---|---|
wrapperClassName | string | - | Classes for the box around the select and the indicator |
className | string | - | Classes for the select element |
children | ReactNode | - | Option, OptGroup and an optional Indicator |
...rest | ComponentProps<'select'> | - | Native <select> attributes: value, onChange, disabled, name, required |
NativeSelect.Indicator
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Replaces the default chevron |
className | string | - | Additional classes |
...rest | ComponentProps<'span'> | - | Native <span> attributes |
NativeSelect.OptGroup
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | - | The group's heading |
...rest | ComponentProps<'optgroup'> | - | Native <optgroup> attributes |
NativeSelect.Option
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | - | The option's value |
disabled | boolean | false | Makes the option unselectable |
...rest | ComponentProps<'option'> | - | Native <option> attributes |