# RadioButton

Radio button with an optional label and description. It renders a native `<input type="radio">` inside a `<label>`, so a click on the text selects it: `name`, `required`, `disabled` and events (`onchange`, `onclick`, ...) work as in HTML, and radios with the same `name` form a group that the arrow keys move through. Bind the selected value of the group with `bind:group`, as with Svelte's `bind:group`: every radio of the group gets the same variable and its own `value`. `card` turns it into a bordered box that highlights when selected. To render a whole group from a list use `RadioGroup`. Its state is exposed as `data-checked` (from `group`, or `checked` without it), `data-disabled` and `data-card` for app CSS.

```ts
import { RadioButton } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/forms/RadioButton.svelte`.

## Examples

### Basic

Radios with the same `name` form a group: the arrow keys move the selection. `bind:group` with the same variable on every radio keeps the selected `value` in sync, as Svelte's `bind:group` does.

```svelte
<script lang="ts">
	import { RadioButton } from '@likable-hair/svelte';

	let gender = $state('female');
</script>

<div class="demo">
	<div class="row">
		<RadioButton name="gender" value="male" label="Male" bind:group={gender} />
		<RadioButton name="gender" value="female" label="Female" bind:group={gender} />
		<RadioButton name="gender" value="other" label="Other" bind:group={gender} />
	</div>
	<p>Selected: <strong>{gender}</strong></p>
	<div class="row">
		<RadioButton name="disabled" value="a" label="Disabled" disabled />
		<RadioButton name="disabled" value="b" label="Disabled and checked" checked disabled />
	</div>
</div>

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

	.row {
		display: flex;
		flex-wrap: wrap;
		gap: 10px 20px;
	}

	p {
		margin: 0;
		font-size: 13px;
	}
</style>
```

### Cards

`card` shows the radio as a bordered box that highlights when selected, and `description` adds a secondary line, read as the description of the radio. The layout of the cards is up to the app.

```svelte
<script lang="ts">
	import { RadioButton } from '@likable-hair/svelte';

	let plan = $state('monthly');
</script>

<div class="plans">
	<RadioButton
		name="plan"
		value="monthly"
		label="Monthly"
		description="€29 / month"
		card
		bind:group={plan}
	/>
	<RadioButton
		name="plan"
		value="yearly"
		label="Yearly"
		description="€290 / year"
		card
		bind:group={plan}
	/>
</div>

<style>
	.plans {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
		gap: 10px;
		width: 100%;
		max-width: 400px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` (required) | `string \| number` |  | Value of this radio. It becomes `group` when the radio is selected; in a form it is sent as text. |
| `group` (bindable) | `string \| number` |  | Value of the selected radio of the group. Use `bind:group` with the same variable on every radio of the group. |
| `checked` | `boolean` |  | Native `checked`, one way. Used only while `group` is `undefined`. |
| `label` | `string` |  | Text next to the circle. A click on it selects the radio. |
| `description` | `string` |  | Secondary text below the label. It is read as the description of the radio, not as part of its name. |
| `card` | `boolean` | `false` | Shows the radio as a bordered box that highlights when selected. |
| `disabled` | `boolean` | `false` | Native `disabled`. |
| `input` (bindable) | `HTMLInputElement` |  | The native `<input>` element. |
| `class` | `{ container?: string; input?: string; label?: string; description?: string; }` | `{}` | Extra classes for each part: the root `<label>`, the input, the label text and the description. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `labelSnippet` | `{ label: string \| undefined }` | Replaces the text next to the circle. A click on it still selects the radio. |
| `descriptionSnippet` | `{ description: string \| undefined }` | Replaces the secondary text below the label. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--radio-button-size` | `--radio-button-default-size`: `18px` |
| `--radio-button-duration` | `--radio-button-default-duration`: `var(--global-duration)` |
| `--radio-button-gap` | `--radio-button-default-gap`: `10px` |
| `--radio-button-label-color` | `--radio-button-default-label-color`: `var(--global-color-text)` |
| `--radio-button-label-font-size` | `--radio-button-default-label-font-size`: `14px` |
| `--radio-button-label-font-weight` | `--radio-button-default-label-font-weight`: `400` |
| `--radio-button-label-line-height` | `--radio-button-default-label-line-height`: `1.5` |
| `--radio-button-border-width` | `--radio-button-default-border-width`: `1.5px` |
| `--radio-button-border-color` | `--radio-button-default-border-color`: `var(--global-color-border-strong)` |
| `--radio-button-background` | `--radio-button-default-background`: `var(--global-input-background)` |
| `--radio-button-box-shadow` | `--radio-button-default-box-shadow`: `0 0 #0000` |
| `--radio-button-dot-size` | `--radio-button-default-dot-size`: `8px` |
| `--radio-button-dot-color` | `--radio-button-default-dot-color`: `var(--global-color-on-primary)` |
| `--radio-button-easing` | `--radio-button-default-easing`: `var(--global-ease-spring)` |
| `--radio-button-checked-border-color` | `--radio-button-default-checked-border-color`: `transparent` |
| `--radio-button-checked-background` | `--radio-button-default-checked-background`: `var(--global-fill-primary)` |
| `--radio-button-checked-box-shadow` | `--radio-button-default-checked-box-shadow`: `var(--global-shadow-fill)` |
| `--radio-button-hover-border-color` | `--radio-button-default-hover-border-color`: `var(--global-color-primary)` |
| `--radio-button-focus-ring-width` | `--radio-button-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--radio-button-focus-ring-color` | `--radio-button-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--radio-button-focus-ring-offset` | `--radio-button-default-focus-ring-offset`: `var(--global-focus-ring-offset)` |
| `--radio-button-description-color` | `--radio-button-default-description-color`: `var(--global-color-text-3)` |
| `--radio-button-description-font-size` | `--radio-button-default-description-font-size`: `12px` |
| `--radio-button-card-padding` | `--radio-button-default-card-padding`: `12px 14px` |
| `--radio-button-card-border-width` | `--radio-button-default-card-border-width`: `var(--global-border-width)` |
| `--radio-button-card-border-color` | `--radio-button-default-card-border-color`: `var(--global-color-border)` |
| `--radio-button-card-border-radius` | `--radio-button-default-card-border-radius`: `var(--global-radius-lg)` |
| `--radio-button-card-background` | `--radio-button-default-card-background`: `var(--global-color-surface-solid)` |
| `--radio-button-card-box-shadow` | `--radio-button-default-card-box-shadow`: `0 0 #0000` |
| `--radio-button-card-hover-border-color` | `--radio-button-default-card-hover-border-color`: `var(--global-color-border-strong)` |
| `--radio-button-card-checked-border-color` | `--radio-button-default-card-checked-border-color`: `var(--global-color-primary)` |
| `--radio-button-card-checked-background` | `--radio-button-default-card-checked-background`: `var(--global-color-primary-soft)` |
| `--radio-button-disabled-opacity` | `--radio-button-default-disabled-opacity`: `0.4` |
