Native Select for Astro
Styled native HTML select controls for Astro forms.
Installation
bunx --bun shadcn@latest add @gnostic/native-selectnpx shadcn@latest add @gnostic/native-selectpnpm dlx shadcn@latest add @gnostic/native-selectComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
No extra packages are required.
Copy the files
src/components/ui/native-select/index.ts
export { default as NativeSelect } from "./native-select.astro"
export { default as NativeSelectOption } from "./native-select-option.astro"
export { default as NativeSelectOptGroup } from "./native-select-optgroup.astro"
src/components/ui/native-select/native-select-optgroup.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"optgroup">
const { class: className, ...props } = Astro.props
// Canvas and CanvasText are inherited by the OS, otherwise optgroup would often not get applied by the browser.
---
<optgroup
data-slot="native-select-optgroup"
class={cn("bg-[Canvas] text-[CanvasText]", className)}
{...props}
>
<slot />
</optgroup>
src/components/ui/native-select/native-select-option.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"option">
const { class: className, ...props } = Astro.props
// Canvas and CanvasText are inherited by the OS, otherwise option would often not get applied by the browser.
---
<option
data-slot="native-select-option"
class={cn("bg-[Canvas] text-[CanvasText]", className)}
{...props}
>
<slot />
</option>
src/components/ui/native-select/native-select.astro
---
import type { HTMLAttributes } from "astro/types"
import ChevronDownIcon from "@lucide/astro/icons/chevron-down"
import { cn } from "cn"
type Props = Omit<HTMLAttributes<"select">, "size"> & {
size?: "sm" | "default"
}
const { class: className, size = "default", ...props } = Astro.props
---
<div
class={cn(
"group/native-select relative w-fit has-[select:disabled]:opacity-50",
className,
)}
data-slot="native-select-wrapper"
data-size={size}
>
<select
data-slot="native-select"
data-size={size}
class="border-input placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 dark:hover:bg-input/50 focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:aria-invalid:border-destructive/50 h-9 w-full min-w-0 appearance-none rounded-md border bg-transparent py-1 pr-8 pl-2.5 text-sm shadow-xs transition-[color,box-shadow] outline-none select-none focus-visible:ring-3 disabled:pointer-events-none disabled:cursor-not-allowed aria-invalid:ring-3 data-[size=sm]:h-8"
{...props}
>
<slot />
</select>
<ChevronDownIcon
class="text-muted-foreground pointer-events-none absolute top-1/2 right-2.5 size-4 -translate-y-1/2 select-none"
aria-hidden="true"
data-slot="native-select-icon"
/>
</div>
Usage
import {
NativeSelect,
NativeSelectOptGroup,
NativeSelectOption,
} from "@/components/ui/native-select"
<NativeSelect>
<NativeSelectOption value="">Select a fruit</NativeSelectOption>
<NativeSelectOption value="apple">Apple</NativeSelectOption>
<NativeSelectOption value="banana">Banana</NativeSelectOption>
<NativeSelectOption value="blueberry">Blueberry</NativeSelectOption>
<NativeSelectOption value="grapes" disabled> Grapes </NativeSelectOption>
<NativeSelectOption value="pineapple">Pineapple</NativeSelectOption>
</NativeSelect>
Composition
Use options directly under NativeSelect for a simple list:
NativeSelect
├── NativeSelectOption
├── NativeSelectOption
├── NativeSelectOption
└── NativeSelectOption
Use NativeSelectOptGroup to organize options into categories:
NativeSelect
├── NativeSelectOptGroup
│ ├── NativeSelectOption
│ └── NativeSelectOption
└── NativeSelectOptGroup
├── NativeSelectOption
└── NativeSelectOption
Examples
With Groups
Organize options using NativeSelectOptGroup for better categorization.
Disabled State
Disable individual options or the entire select component.
Invalid State
Show validation errors with the aria-invalid attribute and error styling.
Accessibility
- The component maintains all native HTML select accessibility features.
- Screen readers can navigate through options using arrow keys.
- The chevron icon is marked as
aria-hidden="true"to avoid duplication. - Use
aria-labeloraria-labelledbyfor additional context when needed.
<NativeSelect aria-label="Choose your preferred language">
<NativeSelectOption value="en">English</NativeSelectOption>
<NativeSelectOption value="es">Spanish</NativeSelectOption>
<NativeSelectOption value="fr">French</NativeSelectOption>
</NativeSelect>
API Reference
Read the installed source for the complete prop types.
When to use Native Select
Use NativeSelect when a standard browser selection control fits the task. Put native option and optgroup elements inside it, then set name, ID, and a linked label.
Keyboard and behavior
The browser owns keyboard navigation, touch pickers, form submission, and disabled behavior. These details can differ across platforms. Keep option labels clear without depending on custom icons or markup.
Theme and layout
The size prop changes the component’s visual size. Native option popup colors and spacing are not fully controlled by the page theme. Test the closed control and the open picker on target platforms.