# Select

shadcn select as a Win95 combo box that drops a menu with no animation.

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

## Examples

```tsx
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectLabel,
  SelectSeparator,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"

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

type SelectSetting = {
  label: string
  size: "sm" | "default"
  defaultValue?: string
  isDisabled?: boolean
  isInvalid?: boolean
  isGrouped?: boolean
}

const SETTINGS: readonly SelectSetting[] = [
  { label: "default", size: "default", defaultValue: "vga" },
  { label: "placeholder", size: "default" },
  { label: "sm", size: "sm", defaultValue: "ega" },
  { label: "groups, disabled item", size: "default", defaultValue: "svga", isGrouped: true },
  { label: "invalid", size: "default", defaultValue: "cga", isInvalid: true },
  { label: "disabled", size: "default", defaultValue: "vga", isDisabled: true },
]

function renderFlatItems() {
  return (
    <>
      <SelectItem value="cga">CGA 4 colors</SelectItem>
      <SelectItem value="ega">EGA 16 colors</SelectItem>
      <SelectItem value="vga">VGA 256 colors</SelectItem>
      <SelectItem value="svga">SVGA 800x600</SelectItem>
    </>
  )
}

function renderGroupedItems() {
  return (
    <>
      <SelectGroup>
        <SelectLabel>Color</SelectLabel>
        <SelectItem value="cga">CGA 4 colors</SelectItem>
        <SelectItem value="ega">EGA 16 colors</SelectItem>
      </SelectGroup>
      <SelectSeparator />
      <SelectGroup>
        <SelectLabel>Resolution</SelectLabel>
        <SelectItem value="svga">SVGA 800x600</SelectItem>
        <SelectItem value="xga" disabled>
          XGA 1024x768
        </SelectItem>
      </SelectGroup>
    </>
  )
}

function renderSelect({ label, size, defaultValue, isDisabled, isInvalid, isGrouped }: SelectSetting) {
  return (
    <Select defaultValue={defaultValue} disabled={isDisabled}>
      <SelectTrigger size={size} aria-label={`Display, ${label}`} aria-invalid={isInvalid} className="w-full">
        <SelectValue placeholder="Pick a display" />
      </SelectTrigger>
      <SelectContent>{isGrouped ? renderGroupedItems() : renderFlatItems()}</SelectContent>
    </Select>
  )
}

export const demo: ShowcaseDemo = {
  span: "wide",
  variants: SETTINGS.map((setting) => ({ label: setting.label, render: () => renderSelect(setting) })),
}
```

## Installation

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

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

Dependencies:

```bash
npm install cn radix-ui
```

Add these items first:

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

`components/ui/select.tsx`

```tsx
"use client"

import { cn } from "cn"
import { Select as SelectPrimitive } from "radix-ui"
import * as React from "react"

import { ArrowGlyph, CheckGlyph } from "@/components/ui/pixel-glyphs"

function Select({
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Root>) {
  return <SelectPrimitive.Root data-slot="select" {...props} />
}

function SelectGroup({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Group>) {
  return (
    <SelectPrimitive.Group
      data-slot="select-group"
      className={cn("scroll-my-1", className)}
      {...props}
    />
  )
}

function SelectValue({
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Value>) {
  return <SelectPrimitive.Value data-slot="select-value" {...props} />
}

function SelectTrigger({
  className,
  size = "default",
  children,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Trigger> & {
  size?: "sm" | "default"
}) {
  return (
    <SelectPrimitive.Trigger
      data-slot="select-trigger"
      data-size={size}
      className={cn(
        "group/select-trigger flex w-fit min-w-40 items-stretch justify-between gap-2 pixel-border border-border well bg-input text-foreground pl-2 text-xl leading-none whitespace-nowrap select-none disabled:cursor-not-allowed disabled:bg-muted disabled:text-muted-foreground aria-invalid:border-dashed aria-invalid:border-destructive data-placeholder:text-well-muted data-[size=default]:h-10 data-[size=sm]:h-8 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-2 *:data-[slot=select-value]:self-center [&_svg]:pointer-events-none [&_svg]:shrink-0",
        className
      )}
      {...props}
    >
      {children}
      <SelectPrimitive.Icon asChild>
        <span className="flex aspect-square items-center justify-center border-l-(length:--pixel) border-border bg-secondary text-secondary-foreground bevel-raised offset-shadow-none group-disabled/select-trigger:text-muted-foreground group-data-[state=open]/select-trigger:bevel-sunken">
          <ArrowGlyph />
        </span>
      </SelectPrimitive.Icon>
    </SelectPrimitive.Trigger>
  )
}

function SelectContent({
  className,
  children,
  position = "popper",
  align = "start",
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Content>) {
  return (
    <SelectPrimitive.Portal>
      <SelectPrimitive.Content
        data-slot="select-content"
        data-align-trigger={position === "item-aligned"}
        className={cn(
          "relative z-50 max-h-(--radix-select-content-available-height) min-w-36 overflow-x-hidden overflow-y-auto pixel-border border-border bg-popover p-(--pixel) text-popover-foreground offset-shadow-2",
          className
        )}
        position={position}
        align={align}
        {...props}
      >
        <SelectScrollUpButton />
        <SelectPrimitive.Viewport
          data-position={position}
          className="data-[position=popper]:h-(--radix-select-trigger-height) data-[position=popper]:w-full data-[position=popper]:min-w-(--radix-select-trigger-width)"
        >
          {children}
        </SelectPrimitive.Viewport>
        <SelectScrollDownButton />
      </SelectPrimitive.Content>
    </SelectPrimitive.Portal>
  )
}

function SelectLabel({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Label>) {
  return (
    <SelectPrimitive.Label
      data-slot="select-label"
      className={cn(
        "px-2 pt-2 pb-1 font-pixel text-sm leading-none text-muted-foreground uppercase",
        className
      )}
      {...props}
    />
  )
}

function SelectItem({
  className,
  children,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Item>) {
  return (
    <SelectPrimitive.Item
      data-slot="select-item"
      className={cn(
        "relative flex h-8 w-full items-center gap-2 pr-2 pl-7 text-xl leading-none outline-hidden select-none focus:bg-foreground focus:text-popover data-disabled:pointer-events-none data-disabled:text-muted-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
        className
      )}
      {...props}
    >
      <span className="pointer-events-none absolute left-2 flex size-3.5 items-center justify-center">
        <SelectPrimitive.ItemIndicator>
          <CheckGlyph />
        </SelectPrimitive.ItemIndicator>
      </span>
      <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
    </SelectPrimitive.Item>
  )
}

function SelectSeparator({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.Separator>) {
  return (
    <SelectPrimitive.Separator
      data-slot="select-separator"
      className={cn(
        "pointer-events-none mx-1 my-1 border-t-(length:--pixel) border-b-(length:--pixel) border-t-bevel-lo border-b-bevel-hi",
        className
      )}
      {...props}
    />
  )
}

function SelectScrollUpButton({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>) {
  return (
    <SelectPrimitive.ScrollUpButton
      data-slot="select-scroll-up-button"
      className={cn(
        "z-10 flex h-6 items-center justify-center bg-popover",
        className
      )}
      {...props}
    >
      <ArrowGlyph direction="up" />
    </SelectPrimitive.ScrollUpButton>
  )
}

function SelectScrollDownButton({
  className,
  ...props
}: React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>) {
  return (
    <SelectPrimitive.ScrollDownButton
      data-slot="select-scroll-down-button"
      className={cn(
        "z-10 flex h-6 items-center justify-center bg-popover",
        className
      )}
      {...props}
    >
      <ArrowGlyph />
    </SelectPrimitive.ScrollDownButton>
  )
}

export {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectLabel,
  SelectScrollDownButton,
  SelectScrollUpButton,
  SelectSeparator,
  SelectTrigger,
  SelectValue,
}
```

## API reference

### Select

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

### SelectContent

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

### SelectGroup

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

### SelectItem

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

### SelectLabel

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

### SelectScrollDownButton

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

### SelectScrollUpButton

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

### SelectSeparator

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

### SelectTrigger

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

### SelectValue

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

## Sounds

- `button.press`
- `button.release`
- `menu.select`
