PeakUIPro

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 native select element
  • .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: :hover or [data-hovered="true"]
  • Focus visible: :focus-visible or [data-focus-visible="true"]
  • Invalid: [data-invalid="true"] on .native-select, or [aria-invalid="true"] on the select
  • Disabled: :disabled or [aria-disabled="true"]

CSS Variables

VariablePurpose
--native-select-bgResting fill of the select; set by the variant
--native-select-bg-hoverFill while hovered
--native-select-bg-focusFill while focused or invalid
--native-select-shadowThe select's own shadow, beside the focus ring

API Reference

NativeSelect

PropTypeDefaultDescription
variant'primary' | 'secondary''primary'The surface: the field's own, or a neutral fill without a shadow
fullWidthbooleanfalseTakes the full width of its container
data-invalid'true'-Marks the field invalid; aria-invalid="true" does the same
classNamestring-Additional classes
render(props, state) => ReactElement-Replaces the root element
...restComponentProps<'div'>-Native <div> attributes

NativeSelect.Trigger

PropTypeDefaultDescription
wrapperClassNamestring-Classes for the box around the select and the indicator
classNamestring-Classes for the select element
childrenReactNode-Option, OptGroup and an optional Indicator
...restComponentProps<'select'>-Native <select> attributes: value, onChange, disabled, name, required

NativeSelect.Indicator

PropTypeDefaultDescription
childrenReactNode-Replaces the default chevron
classNamestring-Additional classes
...restComponentProps<'span'>-Native <span> attributes

NativeSelect.OptGroup

PropTypeDefaultDescription
labelstring-The group's heading
...restComponentProps<'optgroup'>-Native <optgroup> attributes

NativeSelect.Option

PropTypeDefaultDescription
valuestring-The option's value
disabledbooleanfalseMakes the option unselectable
...restComponentProps<'option'>-Native <option> attributes

On this page