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.
default
comfortable
compact
Props
Features
- 🎹 Keyboard navigation
- 🧠 Smart focus management
- 🔄 Horizontal and vertical orientation
🌐 RTL support(incoming!)
Usage
<script lang="ts"> import { RadioGroup } from "melt/builders";
const group = new RadioGroup(); const items = ['a', 'b', 'c'];</script>
<div {...group.root}> <!-- svelte-ignore a11y_label_has_associated_control -- https://github.com/sveltejs/svelte/issues/15067 --> <label {...group.label}>Label</label> {#each items as i} {@const item = group.getItem(i)} <div {...item.attrs}> {#if item.checked} ✅ {/if} </div> {/each} <input {...group.hiddenInput} /></div>
<script lang="ts"> import { RadioGroup } from "melt/components"; const items = ['a', 'b', 'c'];</script>
<RadioGroup> {#snippet children(group)} <div {...group.root}> <!-- svelte-ignore a11y_label_has_associated_control -- https://github.com/sveltejs/svelte/issues/15067 --> <label {...group.label}>Label</label> {#each items as i} {@const item = group.getItem(i)} <div {...item.attrs}> {#if item.checked} ✅ {/if} </div> {/each} <input {...group.hiddenInput} /> </div> {/snippet}</RadioGroup>
API Reference
Constructor Props
The props that are passed when calling
new RadioGroup()
new RadioGroup()
export type RadioGroupProps = { /** * If `true`, prevents the user from interacting with the group. * * @default false */ disabled?: MaybeGetter<boolean | undefined>; /** * If `true`, indicates that the user must select a radio button before * the owning form can be submitted. * * @default false */ required?: MaybeGetter<boolean | undefined>; /** * If the the button selection should loop when navigating with the arrow keys. * * @default true */ loop?: MaybeGetter<boolean | undefined>; /** * If `true`, the value will be changed whenever a button is focused. * * @default true */ selectWhenFocused?: MaybeGetter<boolean | undefined>; /** * The orientation of the slider. * * @default "vertical" */ orientation?: MaybeGetter<"horizontal" | "vertical" | undefined>; /** * Input name for radio group. */ name?: MaybeGetter<string | undefined>; /** * Default value for radio group. * * @default "" */ value?: MaybeGetter<string | undefined>; /** * Called when the radio button is clicked. */ onValueChange?: (active: string) => void;};
export type RadioGroupProps = { /** * If `true`, prevents the user from interacting with the group. * * @default false */ disabled?: MaybeGetter<boolean | undefined>; /** * If `true`, indicates that the user must select a radio button before * the owning form can be submitted. * * @default false */ required?: MaybeGetter<boolean | undefined>; /** * If the the button selection should loop when navigating with the arrow keys. * * @default true */ loop?: MaybeGetter<boolean | undefined>; /** * If `true`, the value will be changed whenever a button is focused. * * @default true */ selectWhenFocused?: MaybeGetter<boolean | undefined>; /** * The orientation of the slider. * * @default "vertical" */ orientation?: MaybeGetter<"horizontal" | "vertical" | undefined>; /** * Input name for radio group. */ name?: MaybeGetter<string | undefined>; /** * Default value for radio group. * * @default "" */ value?: MaybeGetter<string | undefined>; /** * Called when the radio button is clicked. */ onValueChange?: (active: string) => void;};
Methods
The methods returned from
new RadioGroup()
new RadioGroup()
-
getItem
(item: string) => RadioItem(item: string) => RadioItem -
select
(item: string) => void(item: string) => void
Properties
The properties returned from
new RadioGroup()
new RadioGroup()
-
disabled
booleanboolean -
required
booleanboolean -
loop
booleanboolean -
selectWhenFocused
booleanboolean -
orientation
"horizontal" | "vertical""horizontal" | "vertical" -
value
stringstring -
root
{readonly "data-melt-radio-group-root": ""readonly id: stringreadonly role: "radiogroup"readonly "aria-required": booleanreadonly "aria-labelledby": stringreadonly "data-orientation": "horizontal" | "vertical"readonly "data-disabled": true | undefinedreadonly "data-value": string}{readonly "data-melt-radio-group-root": ""readonly id: stringreadonly role: "radiogroup"readonly "aria-required": booleanreadonly "aria-labelledby": stringreadonly "data-orientation": "horizontal" | "vertical"readonly "data-disabled": true | undefinedreadonly "data-value": string} -
label
{readonly "data-melt-radio-group-label": ""readonly id: stringreadonly for: stringreadonly onclick: (e: MouseEvent & { currentTarget: EventTarget & HTMLLabelElement },) => voidreadonly "data-orientation": "horizontal" | "vertical"readonly "data-disabled": true | undefinedreadonly "data-value": string}{readonly "data-melt-radio-group-label": ""readonly id: stringreadonly for: stringreadonly onclick: (e: MouseEvent & { currentTarget: EventTarget & HTMLLabelElement },) => voidreadonly "data-orientation": "horizontal" | "vertical"readonly "data-disabled": true | undefinedreadonly "data-value": string} -
hiddenInput
{readonly "data-melt-radio-group-hidden-input": ""readonly disabled: booleanreadonly required: booleanreadonly hidden: truereadonly "aria-hidden": truereadonly tabindex: -1readonly value: stringreadonly name: string | undefined}{readonly "data-melt-radio-group-hidden-input": ""readonly disabled: booleanreadonly required: booleanreadonly hidden: truereadonly "aria-hidden": truereadonly tabindex: -1readonly value: stringreadonly name: string | undefined}