# Native Select

A native select in the sunken field style with a pixel chevron.

Source: https://www.ui95.dev/ui/native-select

## Examples

```tsx
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select"

import type { ShowcaseDemo } from "../types"

function renderSelect(size: "default" | "sm") {
  return (
    <NativeSelect size={size}>
      <NativeSelectOption value="a">Apple</NativeSelectOption>
      <NativeSelectOption value="b">Banana</NativeSelectOption>
    </NativeSelect>
  )
}

export const demo: ShowcaseDemo = {
  span: "single",
  variants: [
    { label: "default", render: () => renderSelect("default") },
    { label: "sm", render: () => renderSelect("sm") },
    {
      label: "disabled",
      render: () => (
        <NativeSelect disabled>
          <NativeSelectOption value="a">Apple</NativeSelectOption>
        </NativeSelect>
      ),
    },
    {
      label: "invalid",
      render: () => (
        <NativeSelect aria-invalid>
          <NativeSelectOption value="a">Apple</NativeSelectOption>
        </NativeSelect>
      ),
    },
  ],
}
```

## Installation

```bash
npx shadcn@latest add https://www.ui95.dev/r/native-select.json
```

Run it in a Radix shadcn project that already has the UI95 theme.

Dependencies:

```bash
npm install cn
```

Add these items first:

- [pixel-glyphs](https://www.ui95.dev/ui/pixel-glyphs)

`components/ui/native-select.tsx`

```tsx
import { cn } from "cn"
import * as React from "react"

import { ChevronGlyph } from "@/components/ui/pixel-glyphs"

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]:text-muted-foreground",
        className
      )}
      data-slot="native-select-wrapper"
      data-size={size}
    >
      <select
        data-slot="native-select"
        data-size={size}
        className="h-10 w-full min-w-0 appearance-none pixel-border border-border well bg-input text-foreground py-1 pr-9 pl-2 text-xl leading-none outline-none select-none placeholder:text-well-muted disabled:cursor-not-allowed disabled:bg-muted disabled:text-muted-foreground aria-invalid:border-dashed aria-invalid:border-destructive data-[size=sm]:h-8 data-[size=sm]:py-0.5"
        {...props}
      />
      <ChevronGlyph
        direction="down"
        className="pointer-events-none absolute top-1/2 right-2.5 size-4 -translate-y-1/2 text-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 }
```

## API reference

### NativeSelect

| Prop | Type | Default |
| --- | --- | --- |
| `size` | `"sm" \| "default"` | `"default"` |

### NativeSelectOptGroup

No props of its own. It forwards the props of the element or primitive it wraps.

### NativeSelectOption

No props of its own. It forwards the props of the element or primitive it wraps.
