# Checkbox

Select one or multiple options from a set.

```tsx
import { Checkbox, Field } from '@fluxui-dev/react';

<Field variant="inline">
    <Checkbox />

    <Field.Label>I agree to the terms and conditions</Field.Label>

    <Field.Error />
</Field>
```

## Checkbox group

Organize a list of related checkboxes vertically.

```tsx
<Checkbox.Group label="Notifications" defaultValue={['push', 'email']}>
    <Checkbox label="Push notifications" value="push" />
    <Checkbox label="Email" value="email" />
    <Checkbox label="In-app alerts" value="app" />
    <Checkbox label="SMS" value="sms" />
</Checkbox.Group>
```

## With descriptions

Align descriptions for each checkbox directly below its label.

```tsx
<Checkbox.Group label="Subscription preferences" defaultValue={['newsletter']}>
    <Checkbox
        value="newsletter"
        label="Newsletter"
        description="Receive our monthly newsletter with the latest updates and offers."
    />
    <Checkbox
        value="updates"
        label="Product updates"
        description="Stay informed about new features and product updates."
    />
    <Checkbox
        value="invitations"
        label="Event invitations"
        description="Get invitations to our exclusive events and webinars."
    />
</Checkbox.Group>
```

## Horizontal group

Organize a group of related checkboxes horizontally.

```tsx
<Checkbox.Group
    label="Languages"
    description="Choose the languages you want to support."
    defaultValue={['english', 'spanish']}
    className="flex items-start gap-4 *:gap-x-2"
>
    <Checkbox value="english" label="English" />
    <Checkbox value="spanish" label="Spanish" />
    <Checkbox value="french" label="French" />
    <Checkbox value="german" label="German" />
</Checkbox.Group>
```

## Check-all

Control a group of checkboxes with a single checkbox.

```tsx
<Checkbox.Group defaultValue={['caleb']}>
    <Checkbox.All label="Select all" />

    <Checkbox value="caleb" label="Caleb Porzio" />
    <Checkbox value="hugo" label="Hugo Sainte-Marie" />
    <Checkbox value="keith" label="Keith Damiani" />
</Checkbox.Group>
```

## Checked

Mark a checkbox as checked by default.

```tsx
<Checkbox defaultChecked />
```

## Disabled

Prevent users from interacting with and modifying a checkbox.

```tsx
<Checkbox disabled />
```

## Checkbox cards

A bordered alternative to standard checkboxes.

```tsx
<Checkbox.Group label="Subscription preferences" variant="cards" defaultValue={['newsletter']} className="max-sm:flex-col">
    <Checkbox
        value="newsletter"
        label="Newsletter"
        description="Get the latest updates and offers."
    />
    <Checkbox
        value="updates"
        label="Product updates"
        description="Learn about new features and products."
    />
    <Checkbox
        value="invitations"
        label="Event invitations"
        description="Invitations to exclusive events."
    />
</Checkbox.Group>
```

You can swap between vertical and horizontal card layouts by conditionally applying `flex-col` based on the viewport.

```tsx
<Checkbox.Group ... className="max-sm:flex-col">
    {/* ... */}
</Checkbox.Group>
```

## Vertical cards

You can arrange a set of checkbox cards vertically by simply adding the `flex-col` class to the group container.

```tsx
<Checkbox.Group label="Subscription preferences" variant="cards" defaultValue={['newsletter']} className="flex-col">
    <Checkbox
        value="newsletter"
        label="Newsletter"
        description="Get the latest updates and offers."
    />
    <Checkbox
        value="updates"
        label="Product updates"
        description="Learn about new features and products."
    />
    <Checkbox
        value="invitations"
        label="Event invitations"
        description="Invitations to exclusive events."
    />
</Checkbox.Group>
```

## Cards with icons

Pass an icon component to each card's `icon` prop to show it before the label.

```tsx
import { CalendarIcon, CubeIcon, NewspaperIcon } from '@fluxui-dev/react/icons';

<Checkbox.Group label="Subscription preferences" variant="cards" defaultValue={['newsletter']} className="flex-col">
    <Checkbox
        value="newsletter"
        icon={NewspaperIcon}
        label="Newsletter"
        description="Get the latest updates and offers."
    />
    <Checkbox
        value="updates"
        icon={CubeIcon}
        label="Product updates"
        description="Learn about new features and products."
    />
    <Checkbox
        value="invitations"
        icon={CalendarIcon}
        label="Event invitations"
        description="Invitations to exclusive events."
    />
</Checkbox.Group>
```

## Custom card content

You can compose your own custom cards through the `Checkbox` component's children.

```tsx
import { Checkbox, Heading, Text } from '@fluxui-dev/react';

<Checkbox.Group label="Subscription preferences" variant="cards" defaultValue={['newsletter']} className="flex-col">
    <Checkbox value="newsletter">
        <Checkbox.Indicator />

        <div className="flex-1">
            <Heading className="leading-4">Newsletter</Heading>
            <Text size="sm" className="mt-2">Get the latest updates and offers.</Text>
        </div>
    </Checkbox>

    <Checkbox value="updates">
        <Checkbox.Indicator />

        <div className="flex-1">
            <Heading className="leading-4">Product updates</Heading>
            <Text size="sm" className="mt-2">Learn about new features and products.</Text>
        </div>
    </Checkbox>

    <Checkbox value="invitations">
        <Checkbox.Indicator />

        <div className="flex-1">
            <Heading className="leading-4">Event invitations</Heading>
            <Text size="sm" className="mt-2">Invitations to exclusive events.</Text>
        </div>
    </Checkbox>
</Checkbox.Group>
```

## Pills

Compact, rounded checkboxes that look like tags or badges. Perfect for filters, categories, and any time you want lightweight selectable options.

```tsx
<Checkbox.Group label="Categories" variant="pills">
    <Checkbox value="fantasy" label="Fantasy" />
    <Checkbox value="science-fiction" label="Science fiction" />
    <Checkbox value="horror" label="Horror" />
    <Checkbox value="mystery" label="Mystery" />
    <Checkbox value="romance" label="Romance" />
    <Checkbox value="autobiography" label="Autobiography" />
    <Checkbox value="thriller" label="Thriller" />
    <Checkbox value="poetry" label="Poetry" />
    <Checkbox value="children" label="Children" />
</Checkbox.Group>
```

## Buttons

Button-style checkbox options that look like a toolbar. Perfect for toggle controls and feature selections where you want a more prominent visual style.

```tsx
import { BellIcon, ChartBarIcon, CloudArrowUpIcon } from '@fluxui-dev/react/icons';

<Checkbox.Group label="Features" variant="buttons">
    <Checkbox value="notifications" icon={BellIcon} label="Notifications" />
    <Checkbox value="analytics" icon={ChartBarIcon} label="Analytics" />
    <Checkbox value="backups" icon={CloudArrowUpIcon} label="Backups" />
</Checkbox.Group>
```

## Reference

### Checkbox

**Prop:**

- `label` - Label text displayed next to the checkbox. When provided, wraps the checkbox in a structure with an adjacent label.
- `description` - Help text displayed below the checkbox. When provided alongside `label`, appears between the label and checkbox.
- `error` - Error message displayed below the checkbox, marking it invalid and defaulting to the validation error for the checkbox's `name`.
- `value` - Value associated with the checkbox when used in a group. When the checkbox is checked, this value is included in the group's value.
- `name` - Form field name. The checkbox submits its `value` (or `on`) while checked.
- `checked` - Controlled checked state of a checkbox outside a group, paired with `onCheckedChange`.
- `defaultChecked` - Initial checked state of a checkbox outside a group when uncontrolled.
- `onCheckedChange` - Callback fired when the checkbox state changes, receiving the new checked state.
- `indeterminate` - Sets the checkbox to an indeterminate state, represented by a dash instead of a checkmark. Useful for "select all" checkboxes when only some items are selected; your code controls it, and a click does not clear it.
- `disabled` - Prevents user interaction with the checkbox.
- `invalid` - Sets `aria-invalid` and `data-invalid` on the checkbox.
- `variant` - Visual style. Options: `default`, `cards`, `pills`, `buttons`. Defaults to the group's variant. In development, a prop the variant ignores logs a console warning.
- `size` - Height of a `buttons` checkbox. Options: `base` (default), `sm`, `xs`. Defaults to the group's size.
- `icon` - Icon displayed before the label of a card or button: a component or an element.
- `indicator` - Controls card indicator placement: `true` or `end` after the text (default), `start` before it, or `false` to hide it.
- `accent` - Applies the accent color to a checked card, pill, or button when `true` (default), or zinc when `false`.
- `className` - CSS classes applied to the checkbox, or to the card, pill, or button. With `label` set, they stay on the checkbox, not the field around it; to space the whole field, [write the `Field` yourself](https://fluxui.dev/react/components/field#spacing-a-field) and pass it `className`.

**Slot:**

- `children` - Custom content for a card, pill, or button, used in place of `label`.

**Attribute:**

- `data-flux-checkbox` - Applied to the root element for styling and identification.
- `data-checked` - Applied when the checkbox is checked.
- `data-indeterminate` - Applied when the checkbox is in an indeterminate state.

### Checkbox.Group

**Prop:**

- `label` - Label text displayed above the checkbox group. When provided, wraps the group in a `Field` component with an adjacent `Field.Label` component.
- `description` - Help text displayed below the group label. When provided alongside `label`, appears between the label and the checkboxes.
- `error` - Error message displayed below the group, marking it invalid.
- `value` - Controlled array of checked values, paired with `onValueChange`.
- `defaultValue` - Initial array of checked values when uncontrolled.
- `onValueChange` - Callback fired when a checkbox in the group is checked or unchecked, receiving the new array of values.
- `name` - Form field name for the group, submitting each checked box as `name[]`.
- `variant` - Visual style of the group. Options: `default`, `cards`, `pills`, `buttons`.
- `size` - Size of the checkboxes in a `buttons` group. Options: `base` (default), `sm`, `xs`.
- `indicator` - Where the cards of a `cards` group show their indicator.
- `disabled` - Prevents user interaction with all checkboxes in the group.
- `invalid` - Sets `aria-invalid` and `data-invalid` on the group.
- `className` - CSS classes applied to the group element. With `label` set, they stay on the group, below the label; to space the whole field, [write the `Field` yourself](https://fluxui.dev/react/components/field#spacing-a-field) and pass it `className`.

**Slot:**

- `children` - The checkboxes to be grouped together. Can include `Checkbox`, `Checkbox.All`, and other elements.

### Checkbox.All

A special checkbox that controls all enabled checkboxes within its group. It becomes checked when all are checked, unchecked when none are checked, and indeterminate when some (but not all) are checked.

**Prop:**

- `label` - Text label displayed next to the checkbox.
- `description` - Help text displayed below the checkbox.
- `disabled` - Prevents user interaction with the checkbox.

### Checkbox.Indicator

The box with the check mark, for custom card content.
