Radio Group for Astro
Astro radio choices for one selected value in a related set.
Installation
bunx --bun shadcn@latest add @gnostic/radio-groupnpx shadcn@latest add @gnostic/radio-grouppnpm dlx shadcn@latest add @gnostic/radio-groupComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
Install component packages
bun add @data-slot/radio-groupCopy the files
src/components/ui/radio-group/index.ts
export { default as RadioGroup } from './radio-group.astro'
export { default as RadioGroupItem } from './radio-group-item.astro'
export type { RadioGroupProps } from './types.ts'
src/components/ui/radio-group/radio-group-item.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"span"> & {
value: string
disabled?: boolean
indicatorClass?: string
}
const {
class: className,
value,
disabled,
indicatorClass,
...props
} = Astro.props
---
<span
data-slot="radio-group-item"
data-value={value}
data-disabled={disabled ? "" : undefined}
class={cn("relative inline-flex size-4.5 shrink-0 items-center justify-center rounded-full border border-input bg-background not-dark:bg-clip-padding shadow-xs/5 outline-none transition-shadow before:pointer-events-none before:absolute before:inset-0 before:rounded-full not-data-disabled:not-data-checked:not-aria-invalid:before:shadow-[0_1px_--theme(--color-black/4%)] focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background aria-invalid:border-destructive/36 focus-visible:aria-invalid:border-destructive/64 focus-visible:aria-invalid:ring-destructive/48 data-disabled:cursor-not-allowed data-disabled:opacity-64 sm:size-4 dark:not-data-checked:bg-input/32 dark:aria-invalid:ring-destructive/24 dark:not-data-disabled:not-data-checked:not-aria-invalid:before:shadow-[0_-1px_--theme(--color-white/6%)] [[data-disabled],[data-checked],[aria-invalid=true]]:shadow-none data-checked:border-primary data-checked:bg-primary group/radio-group-item peer", className)}
{...props}
>
<span
aria-hidden="true"
data-slot="radio-group-indicator"
class={cn("absolute -inset-px flex items-center justify-center rounded-full opacity-0 group-data-checked/radio-group-item:opacity-100", indicatorClass)}
>
<span
data-slot="radio-group-indicator-icon"
class="bg-primary-foreground absolute top-1/2 left-1/2 size-2 sm:size-1.5 -translate-x-1/2 -translate-y-1/2 rounded-full"
></span>
</span>
</span>
src/components/ui/radio-group/radio-group.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import type { RadioGroupProps } from "./types.ts"
type Props = HTMLAttributes<"div"> & RadioGroupProps
const {
class: className,
defaultValue,
name,
disabled,
readOnly,
required,
...props
} = Astro.props
---
<div
data-slot="radio-group"
data-default-value={defaultValue}
data-name={name}
data-disabled={disabled}
data-read-only={readOnly}
data-required={required}
class={cn("flex flex-col gap-3 w-full", className)}
{...props}
>
<slot />
</div>
<script>
import { create } from "@data-slot/radio-group"
import type { RadioGroupController } from "@data-slot/radio-group"
// Destroy bindings before a view transition so the next page-load binds once.
const controllers: RadioGroupController[] = []
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/radio-group/types.ts
import type { RadioGroupOptions } from '@data-slot/radio-group'
/**
* Radio group configuration rendered as data attributes.
*/
export type RadioGroupProps = Pick<
RadioGroupOptions,
'defaultValue' | 'name' | 'disabled' | 'readOnly' | 'required'
>
Usage
---
import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
---
<RadioGroup defaultValue="option-one" name="example">
<div class="flex items-center gap-3">
<RadioGroupItem value="option-one" id="option-one" />
<Label for="option-one">Option One</Label>
</div>
<div class="flex items-center gap-3">
<RadioGroupItem value="option-two" id="option-two" />
<Label for="option-two">Option Two</Label>
</div>
</RadioGroup>
Composition
Arrange the parts as shown for a RadioGroup:
RadioGroup
├── RadioGroupItem
└── RadioGroupItem
Examples
With Descriptions
Radio group items with a title and supporting copy using the Field component.
Grid Layout
Wrap the whole row in FieldLabel when you want the entire card to act as the
click target.
With FieldSet
Use FieldSet and FieldLegend to group radio items with a shared label and
description.
Disabled
Use the disabled prop on RadioGroupItem to disable individual items.
API Reference
RadioGroupProps
defaultValue?string
Initial selected value
stringname?string
Form field name used by generated radio inputs
stringdisabled?boolean
Disable user interaction and form submission
booleanreadOnly?boolean
Prevent user interaction while keeping programmatic control
booleanrequired?boolean
Require a selected value for native form validation
booleanSee the data-slot docs for more information on interactivity.
Events
Dispatch these events on the radio group root. The root emits the outbound events.
Outbound events
| Event | Detail |
|---|---|
radio-group:change |
{ value: string | null } |
Inbound events
| Event | Detail |
|---|---|
radio-group:set |
{ value: string | null } |
Read the installed source for the complete prop types.
When to use Radio Group
Use Radio Group when all choices should be visible and only one can be selected. Give the group a name for form data and label each item. Use a fieldset and legend when the choices answer one question.
Keyboard and behavior
The data-slot controller maintains selection and keyboard movement. Test arrow keys and Space, then confirm the submitted form value. Disabled choices should remain understandable rather than disappearing without explanation.
Theme and layout
Selected borders and indicators use the primary token. Check long labels and the spacing between each indicator and its text. Invalid state needs an error message connected to the group.