<!--
Source: https://www.theui.dev/docs/radio-button
Part of the theui-svelte documentation. Index: https://www.theui.dev/llms.txt
-->

# Radio Button

A styled radio input that takes its size, animation speed and label classes from the Form or Fieldset around it.

## About

It is a native `<input type="radio">` underneath, so the browser handles the one-per-group behavior. What the component adds is the styling, the linked label, and the settings it picks up from its parent.

## Example

Here is a basic example of using the Radio Button component:

```html
<script>
  import { Radio } from "theui-svelte";
</script>

<Radio name="gender">Male</Radio>
<Radio name="gender">Female</Radio>
```

## Name and Value

The `name` and `value` props are what group the radio buttons. The `name` ensures that only one radio button from the group can be selected at a time, while the `value` represents the option this radio button stands for. To read which option is selected, use the `group` prop.

```html
<Radio name="myRadio" value="credit-card">Credit Card</Radio>
<Radio name="myRadio" value="paypal">Paypal</Radio>
```

## Selected Option

The `group` prop holds the value of the selected radio button. Give every radio button of the group the same `name` and bind the same variable with `bind:group`. The radio button whose `value` matches the bound variable is selected, so you can also preselect an option by setting that variable.

```html
<script>
  let plan = $state("free");
</script>

<Radio name="plan" value="free" bind:group={plan}>Free plan</Radio>
<Radio name="plan" value="pro" bind:group={plan}>Pro plan</Radio>

<p>Selected plan: {plan}</p>
```

## Radio Button Sizes

The `size` prop controls the size of the radio button, allowing flexibility for different UI layouts. Available sizes include `"sm"`, `"md"` (default), `"lg"` and `"xl"`.

```html
<Radio name="myRadio" size="sm">Small (sm) radio button</Radio>
<Radio name="myRadio" size="md">Medium (md) radio button</Radio>
<Radio name="myRadio" size="lg">Large (lg) radio button</Radio>
<Radio name="myRadio" size="xl">Extra Large (xl) radio button</Radio>
```

## Animation

The `animationSpeed` prop defines the speed of the radio button's selection transition. This improves user interaction by adding smooth effects when selecting options. The available values are `"none"`, `"slower"`, `"slow"` `"normal"` (default), `"fast"` and `"faster"`.

Setting it to `"none"`disables animations, while other values changes the transition speed.

```html
<Radio name="myRadio" animationSpeed="slower">Slower animation</Radio>
<Radio name="myRadio" animationSpeed="slow">Slow animation</Radio>
<Radio name="myRadio" animationSpeed="normal">Normal animation</Radio>
<Radio name="myRadio" animationSpeed="fast">Fast animation</Radio>
<Radio name="myRadio" animationSpeed="faster">Faster animation</Radio>
<Radio name="myRadio" animationSpeed="none">No animation</Radio>
```

## Resetting Style

The `reset` prop removes all default styles from the Radio Button component, leaving only the applied custom styles. This is useful for complete control over styling.

```html
<Radio name="myRadio" reset={true} class="checked:bg-green-500 focus:ring-green-200 focus:border-green-200">Option 1</Radio>
<Radio name="myRadio" reset={true} class="checked:bg-green-500 focus:ring-green-200 focus:border-green-200">Option 2</Radio>
```

## Disabled Radio Button

Add the native `disabled` or `readonly` attribute to prevent changes. The radio button and its label are dimmed and can't be clicked.

```html
<Radio name="myRadio" value="1" disabled>Disabled radio button</Radio>
```

## Label Position

The `labelPosition` prop controls where the label sits. It accepts `"end"` (default), which places the label after the radio button, and `"start"`, which places it before. The position follows the writing direction, so in a right-to-left layout `"start"` puts the label on the right.

```html
<Radio name="myRadio" value="after">Label after the radio button</Radio>
<Radio name="myRadio" value="before" labelPosition="start">Label before the radio button</Radio>
```

## Customization

The Radio Button component offers multiple ways to customize its appearance:

- `labelClasses` - Custom styles for the label. If used within a Fieldset or Form component, it inherits their `labelClasses`. The `size`, `animationSpeed` and `reset` props are inherited the same way.

- `wrapperClasses` - Additional classes for the wrapper , useful for layout adjustments.

- `class` - Apply custom styles to the radio button itself.

```html
<Radio name="myRadio"
  wrapperClasses="flex relative gap-4 text-nowrap"
  labelClasses="text-red-500 font-bold z-[1]"
  class="w-8 h-6"
>
  Option 1
</Radio>

<Radio name="myRadio"
  wrapperClasses="flex relative gap-4 text-nowrap"
  labelClasses="text-red-500 font-bold z-[1]"
  class="w-8 h-6"
>
  Option 2
</Radio>
```

## Accessibility

The `Radio` component uses the native `<input type="radio">`, so screen readers announce whether it is selected. When you bind `group`, the `aria-checked` attribute reflects the selection as well. It also supports `aria-disabled` through `props` for disabled states. Each radio button is associated with a `<Label>` for better accessibility. The radio buttons are grouped together, so only one can be selected at a time, and the label can be placed before the radio button with `labelPosition="start"` for right-aligned designs.

## Configuration

### Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | unknown | `undefined` | The option this radio button stands for. It is sent with the form when the radio button is selected. |
| `group` | unknown | `undefined` | The selected value of the radio button group. Bindable: use `bind:group` on every radio button of the group to read or set the selection. |
| `size` | [INPUT_SIZE](https://www.theui.dev/docs/types#input-size-type) | `Inherited from Fieldset or Form, or "md` | Controls the size of the radio button. Available options: "sm", "md", "lg", "xl". |
| `animationSpeed` | [ANIMATE_SPEED](https://www.theui.dev/docs/types#animation-speed-type) | `Inherited from Fieldset or Form, or "normal` | Determines the animation speed for state changes. Options: "none", "slower", "slow", "normal", "fast", "faster". |
| `reset` | boolean | `Inherited from Fieldset or Form, or false` | If set to true, removes all default styles, allowing full customization. |
| `labelClasses` | string | `Inherited from Fieldset or Form, or "` | Custom classes for styling the label. |
| `wrapperClasses` | string | `` | Additional classes for styling the main wrapper of the radio button. |
| `labelPosition` | 'start' \| 'end' | `end` | Places the label before ("start") or after ("end") the radio button, following the writing direction. |

### Dynamic props

| Name | Description |
| --- | --- |
| `disabled` | Disables the radio button and dims it together with its label. |
| `readonly` | Prevents changes and dims the radio button together with its label. |

### Snippets

| Name | Description |
| --- | --- |
| `children` | The label content of the radio button. |

---

Category: Components
Keywords: radio-button, form-input, component, svelte, svelte-5, theui-svelte, component-library, ui, tailwindcss
Full documentation set: https://www.theui.dev/llms-full.txt
