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/selectInstall component packages
bun add @data-slot/selectnpm install @data-slot/selectpnpm add @data-slot/selectCopy the files
src/components/ui/select/index.tssrc/components/ui/select/select-content.astrosrc/components/ui/select/select-group.astrosrc/components/ui/select/select-item.astrosrc/components/ui/select/select-label.astrosrc/components/ui/select/select-separator.astrosrc/components/ui/select/select-trigger.astrosrc/components/ui/select/select-value.astrosrc/components/ui/select/select.astrosrc/components/ui/select/types.ts
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.