Skip to content

Native Select

Chooses one option from a list with the browser's own select, styled to match the system.

The dock that signs for the delivery.

Installation

pnpm dlx shadcn@latest add @silkware/native-select

To copy the component by hand, install its dependencies:

pnpm add cn lucide-react

Then copy the source into components/ui/native-select.tsx:

components/ui/native-select.tsx
import * as React from "react"import { cn } from "cn"import { ChevronDownIcon } from "lucide-react"
type NativeSelectProps = Omit<React.ComponentProps<"select">, "size"> & {  size?: "sm" | "default"}
function NativeSelect({  className,  size = "default",  ...props}: NativeSelectProps) {  return (    <div      className={cn(        "group/native-select relative w-fit has-[select:disabled]:opacity-50",        className      )}      data-slot="native-select-wrapper"      data-size={size}    >      <select        data-slot="native-select"        data-size={size}        className="h-9 w-full min-w-0 appearance-none rounded-md border border-input bg-transparent py-1 pr-8 pl-2.5 text-sm shadow-xs transition-[color,box-shadow] outline-none select-none selection:bg-primary selection:text-primary-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-[size=sm]:h-8 dark:bg-input/30 dark:hover:bg-input/50 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40"        {...props}      />      <ChevronDownIcon className="pointer-events-none absolute top-1/2 right-2.5 size-4 -translate-y-1/2 text-muted-foreground select-none" aria-hidden="true" data-slot="native-select-icon" />    </div>  )}
function NativeSelectOption({  className,  ...props}: React.ComponentProps<"option">) {  return (    <option      data-slot="native-select-option"      className={cn("bg-[Canvas] text-[CanvasText]", className)}      {...props}    />  )}
function NativeSelectOptGroup({  className,  ...props}: React.ComponentProps<"optgroup">) {  return (    <optgroup      data-slot="native-select-optgroup"      className={cn("bg-[Canvas] text-[CanvasText]", className)}      {...props}    />  )}
export { NativeSelect, NativeSelectOptGroup, NativeSelectOption }

A native element

Native Select wraps the HTML select element and uses no React Aria component. The browser draws the list of options, which gives you the best behaviour on phones and in long lists, and you cannot style the list. For a list drawn by the page, with your own styling, use Select.

Usage

import {  NativeSelect,  NativeSelectOptGroup,  NativeSelectOption,} from "@/components/ui/native-select"
<NativeSelect id="site" defaultValue="torino">  <NativeSelectOption value="torino">Brevia Torino</NativeSelectOption>  <NativeSelectOption value="brescia">Brevia Brescia</NativeSelectOption></NativeSelect>

Two things catch people out. The className goes to the wrapper, which is only as wide as its content: pass className="w-full" to fill the field. All other props, such as id, name, value, onChange, required, disabled and aria-invalid, go to the select element. Use onChange with an event, the way you do for a plain select, and not onSelectionChange.

Examples

Groups

NativeSelectOptGroup titles a set of options with its label. Use it when a list has natural sections.

Size

default is 36 pixels tall and sm is 32. Use sm in dense toolbars and tables.

Disabled

disabled removes the select from the tab order and dims it. Put data-disabled on the Field so the label dims too.

Locked once the order ships.

Invalid

Set aria-invalid on the select and data-invalid on the Field, and show the message in a FieldError that the select points to with aria-describedby.

In a form with validation

Give the first option an empty value so that nothing is chosen, and treat an empty value as missing on submit. Clear the error when the person chooses.

Accessibility

KeyAction
TabMoves focus to the select, and away from it.
aTyping letters selects the first option that starts with them, without opening the list.

The browser provides:

  • The combobox or listbox semantics of a native select and type-ahead to jump to an option. Space, the arrow keys, Enter and Escape follow the browser and the system, so they differ between systems.
  • A list of options drawn by the system, with its own accessibility and a picker made for touch on phones.
  • Native required validation and form submission.

You provide:

  • A visible FieldLabel that matches the id of the select, or an aria-label.
  • A first option that tells people to choose, with an empty value when no default is right.
  • aria-invalid and a visible error message, linked with aria-describedby.

Guidelines

When to use

  • For a list of about four to fifteen options that people know and choose from.
  • On touch screens, where the system picker beats a custom list.
  • In a long form, where it keeps the page simple and fast.

When not to use

  • For two or three options. Use Radio Group, or a Switch for an on and off setting.
  • For a list that needs search or lets people pick several. Use Combobox.
  • For options with icons, descriptions or custom layout. Use Select.
  • For an action menu. Use Dropdown Menu.

Content

  • Start with a prompt option, "Choose a site", with an empty value and disabled when a choice is required.
  • Write options as plain nouns in sentence case, and sort them in a way people expect: alphabetical, by frequency or by size.
  • Use groups when a list has more than about eight options and natural sections.
  • Do not use a native select for navigation.

Number of options

Do

Use a select for a handful of options that save space on the page.

Don’t

Don’t use a select for a yes or no. Use a switch or a checkbox.

API reference

NativeSelect

Renders a div wrapper with a select and a chevron. It accepts every attribute of an HTML select except size, which is replaced by the prop below. className goes to the wrapper.

PropTypeDefaultDescription
size?"sm" | "default""default"The height of the select. default is 36 pixels and sm is 32.
value?stringNoneThe selected value, for a controlled select. Pair it with onChange.
defaultValue?stringNoneThe value selected at the start, for an uncontrolled select.
onChange?(event: ChangeEvent<HTMLSelectElement>) => voidNoneCalled with the change event when the choice changes.
name?stringNoneThe name used when the form is submitted.
disabled?booleanfalseRemoves the select from the tab order and dims it.
required?booleanfalseAsks the browser to check that a value is chosen on submit.
aria-invalid?booleanNoneMarks the value as wrong and applies the destructive styles.
className?stringNoneClasses for the wrapper element. Use w-full to fill the field.

NativeSelectOption

Renders an option. It accepts every option prop, such as value and disabled.

NativeSelectOptGroup

Renders an optgroup. It accepts every optgroup prop.

PropTypeDefaultDescription
label?stringNoneThe title of the group.
disabled?booleanfalseDisables every option in the group.