Radio Group
A set of checkable buttons—known as radio buttons—where no more than one of the buttons can be checked at a time.
<script lang="ts">
import * as RadioGroup from "$lib/components/ui/radio-group/index.js";
import { Label } from "$lib/components/ui/label/index.js";
</script>
<RadioGroup.Root value="comfortable">
<div class="flex items-center space-x-2">
<RadioGroup.Item value="default" id="r1" />
<Label for="r1">Default</Label>
</div>
<div class="flex items-center space-x-2">
<RadioGroup.Item value="comfortable" id="r2" />
<Label for="r2">Comfortable</Label>
</div>
<div class="flex items-center space-x-2">
<RadioGroup.Item value="compact" id="r3" />
<Label for="r3">Compact</Label>
</div>
</RadioGroup.Root> Installation
pnpm dlx shadcn-svelte@latest add radio-group Install bits-ui:
pnpm add bits-ui -D Copy and paste the following code into your project.
import Root from './radio-group.svelte';
import Item from './radio-group-item.svelte';
export {
Root,
Item,
//
Root as RadioGroup,
Item as RadioGroupItem
};
<script lang="ts">
import { RadioGroup as RadioGroupPrimitive } from 'bits-ui';
import CircleIcon from 'phosphor-svelte/lib/Circle';
import { cn, type WithoutChildrenOrChild } from '$UTILS$.js';
let {
ref = $bindable(null),
class: className,
...restProps
}: WithoutChildrenOrChild<RadioGroupPrimitive.ItemProps> = $props();
</script>
<RadioGroupPrimitive.Item
bind:ref
data-slot="radio-group-item"
class={cn(
'border-zinc-700 bg-background data-checked:border-[#b9d765] aria-invalid:aria-checked:border-[#b9d765] aria-invalid:border-destructive focus-visible:border-zinc-300 focus-visible:ring-zinc-300/30 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 dark:aria-invalid:border-destructive/50 flex size-4.5 rounded-full border focus-visible:ring-2 aria-invalid:ring-2 group/radio-group-item peer relative aspect-square shrink-0 outline-none after:absolute after:-inset-x-3 after:-inset-y-2 disabled:cursor-not-allowed disabled:opacity-50',
className
)}
{...restProps}
>
{#snippet children({ checked })}
<div data-slot="radio-group-indicator" class="flex size-4.5 items-center justify-center">
{#if checked}
<CircleIcon
class="bg-[#d0e891] text-[#d0e891] absolute top-1/2 left-1/2 size-2 -translate-x-1/2 -translate-y-1/2 rounded-full"
/>
{/if}
</div>
{/snippet}
</RadioGroupPrimitive.Item>
<script lang="ts">
import { RadioGroup as RadioGroupPrimitive } from 'bits-ui';
import { cn } from '$UTILS$.js';
let {
ref = $bindable(null),
class: className,
value = $bindable(''),
...restProps
}: RadioGroupPrimitive.RootProps = $props();
</script>
<RadioGroupPrimitive.Root
bind:ref
bind:value
data-slot="radio-group"
class={cn('grid w-full gap-3 text-zinc-300', className)}
{...restProps}
/>
Usage
<script lang="ts">
import { Label } from '$lib/components/ui/label/index.js';
import * as RadioGroup from '$lib/components/ui/radio-group/index.js';
</script> <RadioGroup.Root value="option-one">
<div class="flex items-center space-x-2">
<RadioGroup.Item value="option-one" id="option-one" />
<Label for="option-one">Option One</Label>
</div>
<div class="flex items-center space-x-2">
<RadioGroup.Item value="option-two" id="option-two" />
<Label for="option-two">Option Two</Label>
</div>
</RadioGroup.Root>