# Checkbox

> Turns one option on or off.

A checkbox turns one option on or off, or picks several options from a list.

## When to use

- Use it for agreements and for choosing several items.
- Use a radio group or a [Select](https://design.sosialoka.tech/components/select/) when only one choice is allowed.
- Don't use it for settings that apply at once; use a switch.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/checkbox/_

## Anatomy

1. Checked box on primary
2. Unchecked box
3. Label, also clickable

A 16px box on `radius-sm` (6px) with a check icon, and a label beside it. Checked boxes fill with `primary`.

## Variants

There is one style. A checkbox can be checked, unchecked or indeterminate.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `checked` / `defaultChecked` | `v-model` / `default-value` | `value` | boolean | `false` | The state. |
| `onCheckedChange` | `@update:model-value` | `onChanged` | function | none | Runs when the state changes. |
| `disabled` | `disabled` | `enabled: false` | boolean | `false` | Blocks changes. |
| `id` | `id` | `label` | string | none | Links the label. |

## Code

**React**
```tsx
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"

export default function CheckboxDemo() {
  return (
    <div className="flex items-center gap-2">
      <Checkbox id="checkbox-demo-terms" />
      <Label htmlFor="checkbox-demo-terms">Saya setuju dengan syarat penggunaan</Label>
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"
</script>

<template>
  <div class="flex items-center gap-2">
    <Checkbox id="checkbox-demo-terms-vue" />
    <Label for="checkbox-demo-terms-vue">Saya setuju dengan syarat penggunaan</Label>
  </div>
</template>
```

**Flutter**
```dart
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class CheckboxDemo extends StatefulWidget {
  const CheckboxDemo({super.key});

  @override
  State<CheckboxDemo> createState() => _CheckboxDemoState();
}

class _CheckboxDemoState extends State<CheckboxDemo> {
  bool agreed = false;

  @override
  Widget build(BuildContext context) {
    return ShadCheckbox(
      value: agreed,
      onChanged: (value) => setState(() => agreed = value),
      label: const Text('Saya setuju dengan syarat penggunaan'),
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/checkbox.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/checkbox.json
```

## Accessibility

- Renders `role="checkbox"` with `aria-checked`; Space toggles it.
- Every checkbox needs a label, linked by `id`.
