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

# Checkbox

The Checkbox component wraps the native checkbox input in the library's styling. It takes sizes, animation speeds and label classes, and inherits them from a Form or Fieldset.

## About

It is still a native `<input type="checkbox">` underneath, so it submits with the form and behaves the way a browser checkbox does. What the component adds is the styling, the linked label, and the settings it picks up from its parent.

## Example

Here is a basic usage example of the Checkbox component:

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

<Checkbox name="terms">I agree to the terms and conditions</Checkbox>
```

## Name and Value

The `name` and `value` props are what the form submission uses. The `name` prop groups checkboxes, allowing multiple selections under the same category. The `value` prop is the value sent with the form when the checkbox is checked. To control whether it is checked, use the `checked` prop.

```html
<Checkbox name="myCheckbox" value="dark-mode">Enable Dark Mode</Checkbox>
<Checkbox name="myCheckbox" value="notifications">Enable Notifications</Checkbox>
```

## Checked State

The `checked` prop sets whether the checkbox is checked. It is `false` by default. The prop is bindable, so use `bind:checked` to read the state when the user toggles the checkbox, or to change it from your code.

```html
<script>
  let subscribed = $state(true);
</script>

<Checkbox name="newsletter" bind:checked={subscribed}>Subscribe to the newsletter</Checkbox>
<p>Subscribed: {subscribed}</p>
```

## Checkbox Sizes

The `size` prop controls the overall size of the checkbox, making it adaptable to different layouts and UI designs. The available sizes are `"sm"`, `"md"` (default), `"lg"` and `"xl"`.

```html
<Checkbox name="myCheckbox" size="sm">Small (sm) checkbox</Checkbox>
<Checkbox name="myCheckbox" size="md">Medium (md) checkbox</Checkbox>
<Checkbox name="myCheckbox" size="lg">Large (lg) checkbox</Checkbox>
<Checkbox name="myCheckbox" size="xl">Extra Large (xl) checkbox</Checkbox>
```

## Animation

The `animationSpeed` prop determines the speed of the checkbox's interaction effects, such as transitions when toggling states. It helps improve user experience by making interactions feel smoother. The available animation speeds are `"none"`, `"slower"`, `"slow"` `"normal"` (default), `"fast"` and `"faster"`.

Setting it to `"none"`disables animations, while other values can be used to adjust the transition speed based on preference.

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

## Rounded Checkbox

The `rounded` prop defines the border-radius of the checkbox, allowing customization of its shape. The available values are `"none"`, `"sm"` (default), `"md"`, `"lg"`, `"xl"`, `"2xl"` and `"full"`.

Inside a `Form` or `Fieldset`, the checkbox uses their `rounded` value instead, unless you set the prop on the checkbox.

```html
<Checkbox name="myCheckbox" size="xl" rounded="none">Rounded none checkbox</Checkbox>
<Checkbox name="myCheckbox" size="xl" rounded="sm">Rounded small checkbox</Checkbox>
<Checkbox name="myCheckbox" size="xl" rounded="md">Rounded medium checkbox</Checkbox>
<Checkbox name="myCheckbox" size="xl" rounded="lg">Rounded large checkbox</Checkbox>
<Checkbox name="myCheckbox" size="xl" rounded="xl">Rounded extra large checkbox</Checkbox>
<Checkbox name="myCheckbox" size="xl" rounded="full">Rounded full checkbox</Checkbox>
```

## Resetting Style

The `reset` prop removes all default styles from the Checkbox component, allowing only custom styles to be applied. This is useful when you want full control over the design. Just set `reset` to `true` to apply reset.

```html
<Checkbox name="myCheckbox" reset={true} class="border border-green-500 bg-green-200 checked:bg-green-500 focus:ring-0">Enable Dark Mode</Checkbox>
```

## Label Position

The `labelPosition` prop controls where the label sits. It accepts `"end"` (default), which places the label after the checkbox, 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
<Checkbox name="myCheckbox">Label after the checkbox</Checkbox>
<Checkbox name="myCheckbox" labelPosition="start">Label before the checkbox</Checkbox>
```

## Disabled Checkbox

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

```html
<Checkbox name="myCheckbox" disabled>Disabled checkbox</Checkbox>
```

## Customization

The Checkbox component provides 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`, `rounded` and `reset` props are inherited the same way.

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

- `class` - Apply custom styles to the checkbox itself.

```html
<Checkbox
  name="myCheckbox"
  wrapperClasses="flex flex-col relative gap-0"
  labelClasses="font-bold z-[1]"
  class="checked:bg-green-500 focus:ring-green-200 focus:border-green-200 w-full h-8"
>
  CUSTOM CHECKBOX
</Checkbox>
```

## Accessibility

The `Checkbox` component uses the native `<input type="checkbox">`, so screen readers announce whether it is checked without extra attributes. It also supports `aria-required` and `aria-disabled` attributes through `props`, making it more accessible for users with assistive technologies. Each checkbox has an associated `<Label>` component to provide a clear description, and the label can be placed before the checkbox for right-aligned designs.

## Configuration

### Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | unknown | `undefined` | Defines the value sent with the form when the checkbox is checked. |
| `checked` | boolean | `false` | Whether the checkbox is checked. Bindable: use `bind:checked` to read or control the state. |
| `size` | [INPUT_SIZE](https://www.theui.dev/docs/types#input-size-type) | `Inherited from Fieldset or Form, or "md` | Controls the size of the checkbox. 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". |
| `rounded` | [ROUNDED](https://www.theui.dev/docs/types#rounded-type) | `Inherited from Fieldset or Form, or "sm` | Defines the border-radius of the checkbox. Options: "none", "sm", "md", "lg", "xl", "2xl", "full". |
| `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 checkbox. |
| `labelPosition` | 'start' \| 'end' | `end` | Places the label before ("start") or after ("end") the checkbox, following the writing direction. |

### Dynamic props

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

### Snippets

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

---

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