Select for Astro
Astro selection popups with labelled options and data-slot behavior.
Installation
bunx --bun shadcn@latest add @gnostic/selectnpx shadcn@latest add @gnostic/selectpnpm dlx shadcn@latest add @gnostic/selectComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
Install component packages
bun add @data-slot/selectCopy the files
src/components/ui/select/index.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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
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'
>
Usage
---
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>
Composition
Arrange the parts as shown for a Select:
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.
With Groups & Labels
Group related choices with SelectGroup, give the group a SelectLabel, and place SelectSeparator between groups when needed.
Large List
A select with many items that scrolls.
Disabled
Disable the entire select or individual items.
API Reference
SelectProps
defaultValue?string
Initial selected value
stringdefaultOpen?boolean
Initial open state
booleanplaceholder?string
Placeholder text when no value selected
stringmountStrategy?MountStrategy
Detach closed content by default; eager keeps it connected and hidden.
MountStrategydisabled?boolean
Disable interaction
booleanrequired?boolean
Form validation required
booleanname?string
Form field name (auto-creates an internal form control)
stringposition?Position
Positioning mode for the content. - "item-aligned": Positions content so selected item aligns with trigger (like native select) - "popper": Positions content below/above trigger like a dropdown
Position"item-aligned"side?Side
The preferred side of the trigger to render against. Will be reversed when collisions occur and `avoidCollisions` is enabled.
Side"bottom"align?Align
The preferred alignment against the trigger. May change when collisions occur.
Align"start"sideOffset?number
The distance in pixels from the trigger.
number4alignOffset?number
An offset in pixels from the "start" or "end" alignment options.
number0avoidCollisions?boolean
When true, overrides side/align preferences to prevent collisions with viewport edges.
booleantruecollisionPadding?number
The padding between the content and the viewport edges when avoiding collisions.
number8highlightItemOnHover?boolean
Whether moving the pointer over items should highlight and focus them.
booleantruelockScroll?boolean
Lock body scroll when open.
booleantrueSelectContentProps
position?Position
Positioning mode for the content. - "item-aligned": Positions content so selected item aligns with trigger (like native select) - "popper": Positions content below/above trigger like a dropdown
Position"item-aligned"side?Side
The preferred side of the trigger to render against. Will be reversed when collisions occur and `avoidCollisions` is enabled.
Side"bottom"align?Align
The preferred alignment against the trigger. May change when collisions occur.
Align"start"sideOffset?number
The distance in pixels from the trigger.
number4alignOffset?number
An offset in pixels from the "start" or "end" alignment options.
number0avoidCollisions?boolean
When true, overrides side/align preferences to prevent collisions with viewport edges.
booleantruecollisionPadding?number
The padding between the content and the viewport edges when avoiding collisions.
number8See the data-slot docs 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 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.