---
title: Select for Astro
description: "Astro selection popups with labelled options and data-slot behavior."
seo:
    title: Select for Astro | Shadcn & Tailwind CSS
related:
    - /components/native-select
    - /components/combobox
sidebar:
    label: "Select"
---

```astro
---
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"
---

<Select class="w-[180px]" placeholder="Select a fruit">
  <SelectTrigger>
    <SelectValue />
  </SelectTrigger>
  <SelectContent>
    <SelectGroup>
      <SelectItem value="apple">Apple</SelectItem>
      <SelectItem value="banana">Banana</SelectItem>
      <SelectItem value="blueberry">Blueberry</SelectItem>
      <SelectItem value="grapes">Grapes</SelectItem>
      <SelectItem value="pineapple">Pineapple</SelectItem>
    </SelectGroup>
  </SelectContent>
</Select>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/select
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

Install the component packages:

```sh
bun add @data-slot/select
```

#### src/components/ui/select/index.ts

```ts
export { default as Select } from './select.astro'
export { default as SelectContent } from './select-content.astro'
export { default as SelectGroup } from './select-group.astro'
export { default as SelectItem } from './select-item.astro'
export { default as SelectLabel } from './select-label.astro'
export { default as SelectSeparator } from './select-separator.astro'
export { default as SelectTrigger } from './select-trigger.astro'
export { default as SelectValue } from './select-value.astro'
export type { SelectProps, SelectContentProps } from './types.ts'
```

#### src/components/ui/select/select-content.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import type { SelectContentProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & SelectContentProps

const {
  class: className,
  hidden = true,
  position,
  side,
  align,
  sideOffset,
  alignOffset,
  avoidCollisions,
  collisionPadding,
  ...props
} = Astro.props
---

<div data-slot="select-portal">
  <div
    data-slot="select-positioner"
    data-position={position}
    data-side={side}
    data-align={align}
    data-side-offset={sideOffset}
    data-align-offset={alignOffset}
    data-avoid-collisions={avoidCollisions}
    data-collision-padding={collisionPadding}
    class="isolate z-50"
  >
    <div
      data-slot="select-content"
      data-position={position}
      data-align-trigger={position === "item-aligned" ? "true" : undefined}
      data-side={side}
      data-align={align}
      data-side-offset={sideOffset}
      data-align-offset={alignOffset}
      data-avoid-collisions={avoidCollisions}
      data-collision-padding={collisionPadding}
      hidden={hidden}
      class={cn("relative max-h-(--available-height) min-w-(--anchor-width) max-w-(--available-width) origin-(--transform-origin) overflow-y-auto rounded-lg text-foreground outline-none border bg-popover not-dark:bg-clip-padding shadow-lg/5 before:pointer-events-none before:absolute before:inset-0 before:shadow-[0_1px_--theme(--color-black/4%)] dark:before:shadow-[0_-1px_--theme(--color-white/6%)] before:rounded-[calc(var(--radius-lg)-1px)] transition-[scale,opacity] duration-200 data-ending-style:scale-98 data-starting-style:scale-98 data-ending-style:opacity-0 data-starting-style:opacity-0 data-[align-trigger=true]:transition-none", className)}
      {...props}
    >
      <div
        data-slot="select-viewport"
        data-position={position}
        class="p-1 data-[position=item-aligned]:w-full data-[position=popper]:min-w-36"
      >
        <slot />
      </div>
    </div>
  </div>
</div>
```

#### src/components/ui/select/select-group.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="select-group"
  class={cn("scroll-my-1 p-1", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/select/select-item.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import CheckIcon from "@lucide/astro/icons/check"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & {
  value?: string
  disabled?: boolean
  label?: string
}

const { class: className, value, disabled, label, ...props } = Astro.props
---

<div
  data-slot="select-item"
  data-value={value}
  data-disabled={disabled ? "true" : undefined}
  data-label={label}
  class={cn("grid min-h-8 in-data-[side=none]:min-w-[calc(var(--anchor-width)+1.25rem)] cursor-default grid-cols-[1rem_minmax(0,1fr)] items-center gap-2 rounded-sm py-1 ps-2 pe-4 text-base outline-none data-disabled:pointer-events-none data-highlighted:bg-accent data-highlighted:text-accent-foreground data-disabled:opacity-64 sm:min-h-7 sm:text-sm [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0 relative w-full group/select-item", className)}
  {...props}
>
  <span data-slot="select-item-text" class="col-start-2 row-start-1 flex min-w-0 items-center gap-2 wrap-anywhere">
    <slot />
  </span>
  <span
    data-slot="select-item-indicator"
    aria-hidden="true"
    class="pointer-events-none col-start-1 row-start-1 flex size-4 items-center justify-center opacity-0 group-data-[selected]/select-item:opacity-100"
  >
    <CheckIcon />
  </span>
</div>
```

#### src/components/ui/select/select-label.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="select-label"
  class={cn("px-2 py-1.5 font-medium text-muted-foreground text-xs", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/select/select-separator.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="select-separator"
  class={cn("mx-2 my-1 h-px bg-border", className)}
  {...props}
></div>
```

#### src/components/ui/select/select-trigger.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import ChevronsUpDownIcon from "@lucide/astro/icons/chevrons-up-down"

import { cn } from "cn"

type Props = HTMLAttributes<"button"> & {
  size?: "sm" | "default"
  showIcon?: boolean
}

const {
  class: className,
  type = "button",
  size = "default",
  showIcon = true,
  ...props
} = Astro.props
---

<button
  data-slot="select-trigger"
  data-size={size}
  type={type}
  class={cn("relative inline-flex min-h-9 w-full min-w-36 select-none items-center justify-between gap-2 rounded-lg border border-input bg-background not-dark:bg-clip-padding px-[calc(--spacing(3)-1px)] text-left text-base text-foreground shadow-xs/5 outline-none ring-ring/24 transition-shadow before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] not-data-disabled:not-focus-visible:not-aria-invalid:not-data-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] pointer-coarse:after:absolute pointer-coarse:after:size-full pointer-coarse:after:min-h-11 focus-visible:border-ring focus-visible:ring-[3px] aria-invalid:border-destructive/36 focus-visible:aria-invalid:border-destructive/64 focus-visible:aria-invalid:ring-destructive/16 data-disabled:pointer-events-none data-disabled:opacity-64 sm:min-h-8 sm:text-sm dark:bg-input/32 dark:aria-invalid:ring-destructive/24 dark:not-data-disabled:not-focus-visible:not-aria-invalid:not-data-pressed:before:shadow-[0_-1px_--theme(--color-white/6%)] [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0 [[data-disabled],:focus-visible,[aria-invalid=true],[data-pressed]]:shadow-none data-[size=sm]:min-h-8 data-[size=sm]:gap-1.5 data-[size=sm]:px-[calc(--spacing(2.5)-1px)] sm:data-[size=sm]:min-h-7 disabled:pointer-events-none disabled:opacity-64", className)}
  {...props}
>
  <slot />
  {showIcon && (
    <ChevronsUpDownIcon
      data-slot="select-trigger-icon"
      class="text-muted-foreground pointer-events-none size-4"
    />
  )}
</button>
```

#### src/components/ui/select/select-value.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"span">

const { class: className, ...props } = Astro.props
---

<span
  data-slot="select-value"
  class={cn("flex-1 truncate data-placeholder:text-muted-foreground", className)}
  {...props}
>
  <slot />
</span>
```

#### src/components/ui/select/select.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import type { SelectProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & SelectProps

const {
  class: className,
  defaultValue,
  defaultOpen,
  placeholder,
  mountStrategy,
  disabled,
  required,
  name,
  position,
  side,
  align,
  sideOffset,
  alignOffset,
  avoidCollisions,
  collisionPadding,
  highlightItemOnHover,
  lockScroll,
  ...props
} = Astro.props
---

<div
  data-slot="select"
  data-default-value={defaultValue}
  data-default-open={defaultOpen}
  data-placeholder={placeholder}
  data-mount-strategy={mountStrategy}
  data-disabled={disabled}
  data-required={required}
  data-name={name}
  data-position={position}
  data-side={side}
  data-align={align}
  data-side-offset={sideOffset}
  data-align-offset={alignOffset}
  data-avoid-collisions={avoidCollisions}
  data-collision-padding={collisionPadding}
  data-highlight-item-on-hover={highlightItemOnHover}
  data-lock-scroll={lockScroll}
  class={cn(
    "group/select relative inline-flex w-fit flex-col gap-1.5",
    className,
  )}
  {...props}
>
  <slot />
</div>

<script>
  import { create } from "@data-slot/select"
  import type { SelectController } from "@data-slot/select"
  // Destroy bindings before a view transition so the next page-load binds once.
  const controllers: SelectController[] = []
  const initialize = () => controllers.push(...create())
  initialize()
  document.addEventListener("astro:page-load", initialize)
  document.addEventListener("astro:before-swap", () => {
    for (const controller of controllers) controller.destroy()
    controllers.length = 0
  })
</script>
```

#### src/components/ui/select/types.ts

```ts
import type { SelectOptions } from '@data-slot/select'

/**
 * Select configuration rendered as data attributes.
 */
export type SelectProps = Pick<
    SelectOptions,
    | 'defaultValue'
    | 'defaultOpen'
    | 'placeholder'
    | 'mountStrategy'
    | 'disabled'
    | 'required'
    | 'name'
    | 'position'
    | 'side'
    | 'align'
    | 'sideOffset'
    | 'alignOffset'
    | 'avoidCollisions'
    | 'collisionPadding'
    | 'highlightItemOnHover'
    | 'lockScroll'
>

/** Placement fields for the select popup. */
export type SelectContentProps = Pick<
    SelectOptions,
    | 'position'
    | 'side'
    | 'align'
    | 'sideOffset'
    | 'alignOffset'
    | 'avoidCollisions'
    | 'collisionPadding'
>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```astro
---
import {
  Select,
  SelectContent,
  SelectGroup,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"
---
```

```astro
<Select class="w-[180px]" placeholder="Select a fruit">
  <SelectTrigger>
    <SelectValue />
  </SelectTrigger>
  <SelectContent>
    <SelectGroup>
      <SelectItem value="apple">Apple</SelectItem>
      <SelectItem value="banana">Banana</SelectItem>
      <SelectItem value="blueberry">Blueberry</SelectItem>
      <SelectItem value="grapes">Grapes</SelectItem>
      <SelectItem value="pineapple">Pineapple</SelectItem>
    </SelectGroup>
  </SelectContent>
</Select>
```

## Composition

Arrange the parts as shown for a `Select`:

```text
Select
├── SelectTrigger
│   └── SelectValue
└── SelectContent
    ├── SelectGroup
    │   ├── SelectLabel
    │   ├── SelectItem
    │   └── SelectItem
    ├── SelectSeparator
    └── SelectGroup
        ├── SelectLabel
        ├── SelectItem
        └── SelectItem
```

## Examples

### Item Aligned

Use the `position` prop on `SelectContent` to control alignment. When
`position="item-aligned"` (default), the popup positions so the selected item
appears over the trigger. When `position="popper"`, the popup aligns to the
trigger edge.

```astro
---
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"
---

<div class="grid w-full max-w-2xl gap-6 @md:grid-cols-2">
  <div class="flex flex-col gap-3">
    <p class="text-sm font-medium">Item aligned</p>
    <Select defaultValue="team" class="max-w-xs">
      <SelectTrigger>
        <SelectValue />
      </SelectTrigger>
      <SelectContent position="item-aligned">
        <SelectItem value="starter">Starter workspace</SelectItem>
        <SelectItem value="team">Team workspace</SelectItem>
        <SelectItem value="enterprise">Enterprise workspace</SelectItem>
      </SelectContent>
    </Select>
  </div>

  <div class="flex flex-col gap-3">
    <p class="text-sm font-medium">Popper</p>
    <Select defaultValue="team" class="max-w-xs">
      <SelectTrigger>
        <SelectValue />
      </SelectTrigger>
      <SelectContent position="popper" side="bottom" align="start">
        <SelectItem value="starter">Starter workspace</SelectItem>
        <SelectItem value="team">Team workspace</SelectItem>
        <SelectItem value="enterprise">Enterprise workspace</SelectItem>
      </SelectContent>
    </Select>
  </div>
</div>
```

### With Groups & Labels

Group related choices with `SelectGroup`, give the group a `SelectLabel`, and place `SelectSeparator` between groups when needed.

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

<Select class="max-w-xs" placeholder="Pick produce">
  <SelectTrigger>
    <SelectValue />
  </SelectTrigger>
  <SelectContent>
    <SelectGroup>
      <SelectLabel>Fruits</SelectLabel>
      <SelectItem value="apple">Apple</SelectItem>
      <SelectItem value="banana">Banana</SelectItem>
    </SelectGroup>
    <SelectSeparator />
    <SelectGroup>
      <SelectLabel>Vegetables</SelectLabel>
      <SelectItem value="carrot">Carrot</SelectItem>
      <SelectItem value="spinach">Spinach</SelectItem>
    </SelectGroup>
  </SelectContent>
</Select>
```

### Large List

A select with many items that scrolls.

```astro
---
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"

const timezones = [
  "Pacific/Honolulu",
  "America/Anchorage",
  "America/Los_Angeles",
  "America/Denver",
  "America/Chicago",
  "America/New_York",
  "America/Sao_Paulo",
  "Europe/London",
  "Europe/Amsterdam",
  "Europe/Berlin",
  "Europe/Athens",
  "Asia/Dubai",
  "Asia/Kolkata",
  "Asia/Bangkok",
  "Asia/Tokyo",
  "Australia/Sydney",
]
---

<Select class="max-w-xs" placeholder="Select timezone">
  <SelectTrigger>
    <SelectValue />
  </SelectTrigger>
  <SelectContent class="max-h-56">
    {timezones.map((timezone) => (
      <SelectItem value={timezone}>{timezone}</SelectItem>
    ))}
  </SelectContent>
</Select>
```

### Disabled

Disable the entire select or individual items.

```astro
---
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"
---

<div class="grid w-full max-w-md gap-6 @md:grid-cols-2">
  <div class="flex flex-col gap-3">
    <p class="text-sm font-medium">Disabled item</p>
    <Select defaultValue="pro" class="max-w-xs">
      <SelectTrigger>
        <SelectValue />
      </SelectTrigger>
      <SelectContent>
        <SelectItem value="starter">Starter</SelectItem>
        <SelectItem value="pro">Pro</SelectItem>
        <SelectItem value="enterprise" disabled>
          Enterprise
        </SelectItem>
      </SelectContent>
    </Select>
  </div>

  <div class="flex flex-col gap-3">
    <p class="text-sm font-medium">Disabled select</p>
    <Select disabled class="max-w-xs" placeholder="Unavailable">
      <SelectTrigger>
        <SelectValue />
      </SelectTrigger>
      <SelectContent>
        <SelectItem value="starter">Starter</SelectItem>
      </SelectContent>
    </Select>
  </div>
</div>
```

## API Reference

### SelectProps

<AutoTypeTable path="../../packages/ui/src/components/ui/select/types.ts" name="SelectProps" />

### SelectContentProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/select/types.ts"
    name="SelectContentProps"
/>

See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/select/README.md) for more information on interactivity.

### Events

Dispatch these events on the select root. The root emits the outbound events.

#### Outbound events

| Event                | Detail                      |
| -------------------- | --------------------------- |
| `select:change`      | `{ value: string \| null }` |
| `select:open-change` | `{ open: boolean }`         |

#### Inbound events

| Event        | Detail                                       |
| ------------ | -------------------------------------------- |
| `select:set` | `{ value?: string \| null, open?: boolean }` |

Read the [installed source](/r/select.json) for the complete prop types.

## When to use Select

Use Select for one choice from a known set. Compose trigger, value, content, and items under one root. Give items stable values and provide a visible label for the field.

## Keyboard and behavior

The data-slot controller handles the popup and keyboard selection. Check arrow keys, Enter, Escape, and the submitted value. The existing SelectLabel part styles a group label inside the list; it does not replace the field’s external label.

## Theme and layout

The trigger and popup share input and popover tokens. Test long option labels and disabled states. This Astro API retains its current size options and DOM events instead of adopting React callbacks.
