Skip to content
Likable Aurora @likable-hair/svelte 5

RadioGroup

Group of radio buttons built from a list, inside a <fieldset> with role="radiogroup": label is its legend, every radio shares a generated name so Tab enters the group and the arrow keys move the selection, and disabled and required work as in HTML forms. Options are RadioItem objects, { value: string | number; label?: string | number; icon?: string; description?: string; disabled?: boolean; data?: Data }, and value holds the value of the selected one (bind:value); onchange reports each choice of the user. card shows every option as a bordered box, and orientation="horizontal" puts them side by side. Its state is exposed as data-state, data-disabled and data-orientation for app CSS.

import { RadioGroup } from '@likable-hair/svelte';

src/lib/components/simple/forms/RadioGroup.svelte · Markdown for LLMs

Basic

A group from a list of items: label is the legend, the radios share a generated name, and bind:value holds the value of the selected item. Items can have an icon, a description and their own disabled.

Preferred contact
We only use it for appointment reminders.
Gender
Code
<script lang="ts">
	import { mdiEmailOutline, mdiMessageTextOutline, mdiPhoneOutline } from '@mdi/js';
	import { RadioGroup } from '@likable-hair/svelte';

	let contact = $state<string | number>('email');
</script>

<div class="demo">
	<RadioGroup
		label="Preferred contact"
		hint="We only use it for appointment reminders."
		bind:value={contact}
		items={[
			{ value: 'email', label: 'Email', icon: mdiEmailOutline },
			{ value: 'sms', label: 'SMS', icon: mdiMessageTextOutline },
			{ value: 'phone', label: 'Phone call', icon: mdiPhoneOutline, disabled: true }
		]}
	/>
	<RadioGroup
		label="Gender"
		orientation="horizontal"
		items={[
			{ value: 'male', label: 'Male' },
			{ value: 'female', label: 'Female' },
			{ value: 'other', label: 'Other' }
		]}
	/>
</div>

<style>
	.demo {
		display: grid;
		gap: 24px;
	}
</style>

Cards

card shows every option as a bordered box and orientation="horizontal" puts them side by side; they wrap when each would be narrower than --radio-group-card-min-width. itemDescriptionSnippet replaces the secondary line.

Plan
Delivery
Code
<script lang="ts">
	import { RadioGroup } from '@likable-hair/svelte';

	let plan = $state<string | number>('yearly');
</script>

<div class="demo">
	<RadioGroup
		label="Plan"
		card
		orientation="horizontal"
		bind:value={plan}
		items={[
			{ value: 'monthly', label: 'Monthly', data: { price: 29, period: 'month' } },
			{ value: 'yearly', label: 'Yearly', data: { price: 290, period: 'year' } }
		]}
	>
		{#snippet itemDescriptionSnippet({ item })}
			€{item.data?.price} / {item.data?.period}
		{/snippet}
	</RadioGroup>
	<RadioGroup
		label="Delivery"
		card
		items={[
			{ value: 'standard', label: 'Standard', description: '3–5 working days, free' },
			{ value: 'express', label: 'Express', description: 'Next working day, €9' },
			{ value: 'pickup', label: 'Pick up in store', description: 'Not available in your area', disabled: true }
		]}
	/>
</div>

<style>
	.demo {
		display: grid;
		gap: 24px;
		width: 100%;
		max-width: 400px;
	}
</style>

In a form

name submits the selected value and required blocks the submission until an option is selected, as with native radios. state and hint show the error; disabled disables the whole group. Here the form data is printed instead of being sent.

Payment method
Invoice
Code
<script lang="ts">
	import { Button, RadioGroup } from '@likable-hair/svelte';

	let payment = $state<string | number>();
	let submitted = $state(false);
	let sent = $state('');

	function submit(event: SubmitEvent & { currentTarget: HTMLFormElement }) {
		event.preventDefault();
		sent = new URLSearchParams(new FormData(event.currentTarget) as never).toString();
	}
</script>

<form class="demo" onsubmit={submit} oninvalidcapture={() => (submitted = true)}>
	<RadioGroup
		label="Payment method"
		name="payment"
		required
		bind:value={payment}
		state={submitted && payment === undefined ? 'error' : undefined}
		hint={submitted && payment === undefined ? 'Choose a payment method' : undefined}
		items={[
			{ value: 'card', label: 'Credit card' },
			{ value: 'transfer', label: 'Bank transfer' },
			{ value: 'cash', label: 'Cash at the salon' }
		]}
	/>
	<RadioGroup
		label="Invoice"
		disabled
		value="email"
		items={[
			{ value: 'email', label: 'By email' },
			{ value: 'paper', label: 'On paper' }
		]}
	/>
	<div class="actions">
		<Button type="submit">Pay</Button>
		{#if sent}<code>{sent}</code>{/if}
	</div>
</form>

<style>
	.demo {
		display: grid;
		gap: 20px;
	}

	.actions {
		display: flex;
		align-items: center;
		gap: 12px;
	}
</style>

Props

NameTypeDefaultDescription
items RadioItem<Data>[][]Options to choose from.
value bindable string | numbervalue of the selected option. undefined while none is selected.
label stringLegend of the group, read as its name.
name stringNative name shared by the radios, sent with forms. Generated when missing, so the radios always form a group.
orientation 'vertical' | 'horizontal''vertical'Puts the options in a column or side by side (wrapping when there is no room).
card booleanfalseShows every option as a bordered box that highlights when selected.
disabled booleanfalseDisables the whole group (native disabled of the fieldset).
required booleanfalseNative required: a form cannot be submitted until an option is selected.
hint stringText below the options. With state it becomes the error or success message.
state 'error' | 'success'Validation state: colors the circles and the hint. error also sets aria-invalid.
class { container?: string; label?: string; items?: string; item?: string; hint?: string; }{}Extra classes for each part: the fieldset, the legend, the list of options, each option and the hint.

It also accepts the native attributes and events of <fieldset> (for example id, title, aria-label, onkeydown), which are forwarded to the element.

Snippets

NameParametersDescription
labelSnippet{ label: string | undefined }Replaces the legend content.
itemSnippet{ item: RadioItem<Data>; index: number; checked: boolean }Replaces the label of each option (icon and text). A click on it still selects the option.
itemDescriptionSnippet{ item: RadioItem<Data>; index: number; checked: boolean }Replaces the secondary text of each option.
hintSnippet{ hint: string | undefined }Replaces the hint below the options.

Events

NameTypeDescription
onchange(args: { value: string | number | undefined; item: RadioItem<Data> | undefined; event: Event }) => voidCalled when the user selects an option, after value changed, with the new value, its item and the native change event.

Types

Exported by the package: import type { RadioItem, Item } from '@likable-hair/svelte';

RadioItem

An option of a RadioGroup: an Item with a secondary line and its own disabled state.

type RadioItem<Data = unknown> = Item<Data> & { … }

NameTypeDescription
description stringSecondary text below the label.
disabled booleanDisables only this option.

Item

An option of a picker (Autocomplete, Select, ...).

type Item<Data = unknown> = { … }

NameTypeDescription
value requiredstring | numberIdentity of the option: two items are the same option when their values are equal (===).
label string | numberText shown and searched. Defaults to value.
icon stringSVG path of an icon shown before the label, for example from @mdi/js.
data DataAny extra data, available in snippets and callbacks.

CSS variables

For a single instance, pass them as style props: <RadioGroup --radio-group-hint-color="tomato">. The --radio-group-default-* defaults apply to the whole app: override them in :root or in a theme.

Per-instance variableDefault
--radio-group-hint-color
--radio-group-default-hint-color var(--global-input-hint-color)
--radio-group-icon-size
--radio-group-default-icon-size 18px
--radio-group-gap
--radio-group-default-gap 10px
--radio-group-error-color
--radio-group-default-error-color var(--global-input-error-color)
--radio-group-success-color
--radio-group-default-success-color var(--global-input-success-color)
--radio-group-label-color
--radio-group-default-label-color var(--global-input-label-color)
--radio-group-label-font-size
--radio-group-default-label-font-size var(--global-input-label-font-size)
--radio-group-label-font-weight
--radio-group-default-label-font-weight var(--global-input-label-font-weight)
--radio-group-items-gap
--radio-group-default-items-gap 10px
--radio-group-default-horizontal-items-gap 10px 20px
--radio-group-card-min-width
--radio-group-default-card-min-width 150px
--radio-group-hint-font-size
--radio-group-default-hint-font-size var(--global-input-hint-font-size)