> For the complete documentation index, see [llms.txt](https://docs.rierino.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rierino.com/sdk-core-ui/components/checkbox.md). Agents can also search and read this documentation through its [MCP server](https://docs.rierino.com/~gitbook/mcp).

# Checkbox

Turns a single option on or off, or picks several options from a group.

{% code title="checkbox/demo.tsx" %}

```tsx
import { useState } from "react";
import { Checkbox } from "@rierino-open/core-ui";

export default function CheckboxDemo() {
  const [accepted, setAccepted] = useState(false);

  return (
    <div style={{ display: "flex", alignItems: "center", gap: 8, fontSize: 13 }}>
      <Checkbox
        aria-labelledby="terms-label"
        isSelected={accepted}
        onChange={setAccepted}
      />
      <span id="terms-label">Publish to the web store</span>
    </div>
  );
}
```

{% endcode %}

## Usage

{% tabs %}
{% tab title="React app" %}

```tsx
import { Checkbox } from '@rierino-open/core-ui';

<Checkbox aria-label="Publish" isSelected={publish} onChange={setPublish} />
```

{% endtab %}

{% tab title="Hosted editor" %}

```jsx
import CoreUI from '@rierino-open/core-ui';
const { Checkbox } = CoreUI;

<Checkbox aria-label="Publish" isSelected={publish} onChange={setPublish} />
```

{% endtab %}
{% endtabs %}

`onChange` receives the new state as a boolean. Use `isSelected` for a controlled checkbox, or `defaultSelected` to let it manage its own state.

{% hint style="info" %}
**Checkbox draws only the box**

Checkbox doesn't render a label: `children`, `label` and `description` are ignored. Put the label text next to it, and name the checkbox with `aria-label`, or with `aria-labelledby` pointing at that text, as in the examples.
{% endhint %}

## Examples

### States

`isIndeterminate` shows a dash, for a "select all" checkbox when only some items are selected. It only changes the look: the checkbox's value is still `isSelected`.

{% code title="checkbox/states.tsx" %}

```tsx
import { Checkbox } from "@rierino-open/core-ui";

const row = { display: "flex", alignItems: "center", gap: 8, fontSize: 13 } as const;

export default function CheckboxStates() {
  return (
    <div style={{ display: "grid", gap: 12 }}>
      <div style={row}>
        <Checkbox aria-label="Unchecked" />
        Unchecked
      </div>
      <div style={row}>
        <Checkbox aria-label="Checked" defaultSelected />
        Checked
      </div>
      <div style={row}>
        <Checkbox aria-label="Indeterminate" isIndeterminate />
        Indeterminate
      </div>
      <div style={row}>
        <Checkbox aria-label="Disabled" isDisabled defaultSelected />
        Disabled
      </div>
    </div>
  );
}
```

{% endcode %}

### Select all

The usual table pattern: the header checkbox is selected when every row is, and indeterminate when only some are.

{% code title="checkbox/select-all.tsx" %}

```tsx
import { useState } from "react";
import { Checkbox } from "@rierino-open/core-ui";

const products = ["Linen shirt", "Chino trousers", "Wool coat", "Leather belt"];

export default function CheckboxSelectAll() {
  const [selected, setSelected] = useState<string[]>(["Wool coat"]);
  const all = selected.length === products.length;
  const some = selected.length > 0 && !all;

  const toggle = (name: string, on: boolean) =>
    setSelected((current) => (on ? [...current, name] : current.filter((n) => n !== name)));

  return (
    <div style={{ display: "grid", gap: 10, fontSize: 13, minWidth: 240 }}>
      <div style={{ display: "flex", alignItems: "center", gap: 8, fontWeight: 600 }}>
        <Checkbox
          aria-label="Select all products"
          isSelected={all}
          isIndeterminate={some}
          onChange={(on) => setSelected(on ? products : [])}
        />
        {selected.length} of {products.length} selected
      </div>
      {products.map((name) => (
        <div key={name} style={{ display: "flex", alignItems: "center", gap: 8 }}>
          <Checkbox
            aria-label={name}
            isSelected={selected.includes(name)}
            onChange={(on) => toggle(name, on)}
          />
          {name}
        </div>
      ))}
    </div>
  );
}
```

{% endcode %}

### CheckboxGroup

`CheckboxGroup` holds several checkboxes that share one value: an array of the selected checkboxes' `value`s. It renders a `label` above them and a `description` below.

{% code title="checkbox/group.tsx" %}

```tsx
import { useState } from "react";
import { Checkbox, CheckboxGroup } from "@rierino-open/core-ui";

const channels = [
  { value: "web", label: "Web store" },
  { value: "mobile", label: "Mobile app" },
  { value: "retail", label: "Retail stores" },
];

export default function CheckboxGroupExample() {
  const [values, setValues] = useState<string[]>(["web"]);

  return (
    <CheckboxGroup
      label="Sales channels"
      description="Where the product can be bought."
      value={values}
      onChange={setValues}
    >
      {channels.map((channel) => (
        <div key={channel.value} style={{ display: "flex", alignItems: "center", gap: 8, fontSize: 13 }}>
          <Checkbox value={channel.value} aria-label={channel.label} />
          {channel.label}
        </div>
      ))}
    </CheckboxGroup>
  );
}
```

{% endcode %}

### Validation

Set `isInvalid` on the group and pass `errorMessage`, which is shown below the checkboxes while the group is invalid.

{% code title="checkbox/group-invalid.tsx" %}

```tsx
import { useState } from "react";
import { Checkbox, CheckboxGroup } from "@rierino-open/core-ui";

const regions = [
  { value: "eu", label: "Europe" },
  { value: "me", label: "Middle East" },
];

export default function CheckboxGroupInvalid() {
  const [values, setValues] = useState<string[]>([]);

  return (
    <CheckboxGroup
      label="Regions"
      value={values}
      onChange={setValues}
      isInvalid={values.length === 0}
      errorMessage="Pick at least one region."
    >
      {regions.map((region) => (
        <div key={region.value} style={{ display: "flex", alignItems: "center", gap: 8, fontSize: 13 }}>
          <Checkbox value={region.value} aria-label={region.label} />
          {region.label}
        </div>
      ))}
    </CheckboxGroup>
  );
}
```

{% endcode %}

## Accessibility

* Renders a native checkbox inside a `<label>`, so it works with forms and screen readers. `Space` toggles it.
* **Every checkbox needs a name.** Since it has no visible label of its own, pass `aria-label` or `aria-labelledby`.
* In a `CheckboxGroup`, the group's `label` names the group, and each checkbox still needs its own name.
* **Keyboard focus is only partly visible.** An unchecked box shows focus by its border turning `--primary`; a checked box already has that border, so it shows no change. The focus ring the styles intend doesn't render yet.

## Styling

| Attribute            | Present when                                     |
| -------------------- | ------------------------------------------------ |
| `data-selected`      | The checkbox is checked                          |
| `data-indeterminate` | `isIndeterminate` is set                         |
| `data-focus-visible` | The checkbox has keyboard focus                  |
| `data-disabled`      | `isDisabled` is set; the checkbox is also dimmed |
| `data-invalid`       | The checkbox, or its group, is invalid           |

The box uses `--border-color` when unchecked, `--border-color-focus` while focused and `--primary` when checked. See [theme variables](https://docs.rierino.com/sdk-core-ui/foundations/colors.md#theme-variables).

## API reference

### Checkbox

| Prop              | Type                              | Default | Description                                                                                                                                                                        |
| ----------------- | --------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isInvalid`       | `boolean`                         |         | Whether the input value is invalid.                                                                                                                                                |
| `isDisabled`      | `boolean`                         |         | Whether the input is disabled.                                                                                                                                                     |
| `isReadOnly`      | `boolean`                         |         | Whether the input can be selected but not changed by the user.                                                                                                                     |
| `aria-label`      | `string`                          |         | Defines a string value that labels the current element.                                                                                                                            |
| `aria-labelledby` | `string`                          |         | Identifies the element (or elements) that labels the current element.                                                                                                              |
| `name`            | `string`                          |         | The name of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefname).                      |
| `defaultSelected` | `boolean`                         |         | Whether the element should be selected (uncontrolled).                                                                                                                             |
| `isSelected`      | `boolean`                         |         | Whether the element should be selected (controlled).                                                                                                                               |
| `onChange`        | `((isSelected: boolean) => void)` |         | Handler that is called when the element's selection state changes.                                                                                                                 |
| `value`           | `string`                          |         | The value of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefvalue).                    |
| `isIndeterminate` | `boolean`                         |         | Indeterminism is presentational only. The indeterminate visual representation remains regardless of user interaction.                                                              |
| `className`       | `string \| ((state) => string)`   |         | The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state. |

### CheckboxGroup

| Prop           | Type                                                   | Default | Description                                                                                                                                                                        |
| -------------- | ------------------------------------------------------ | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isRequired`   | `boolean`                                              |         | Whether user input is required on the input before form submission.                                                                                                                |
| `isInvalid`    | `boolean`                                              |         | Whether the input value is invalid.                                                                                                                                                |
| `isDisabled`   | `boolean`                                              |         | Whether the input is disabled.                                                                                                                                                     |
| `isReadOnly`   | `boolean`                                              |         | Whether the input can be selected but not changed by the user.                                                                                                                     |
| `value`        | `string[]`                                             |         | The current value (controlled).                                                                                                                                                    |
| `defaultValue` | `string[]`                                             |         | The default value (uncontrolled).                                                                                                                                                  |
| `onChange`     | `((value: string[]) => void)`                          |         | Handler that is called when the value changes.                                                                                                                                     |
| `name`         | `string`                                               |         | The name of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefname).                      |
| `className`    | `string \| ((state) => string)`                        |         | The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state. |
| `label`        | `string`                                               |         | The group's visible label, shown above the checkboxes.                                                                                                                             |
| `description`  | `string`                                               |         | Help text shown below the checkboxes.                                                                                                                                              |
| `errorMessage` | `string \| ((validation: ValidationResult) => string)` |         | Shown below the checkboxes while the group is invalid (`isInvalid`).                                                                                                               |

Both also accept the other props of React Aria's [Checkbox](https://react-spectrum.adobe.com/react-aria/Checkbox.html#props) and [CheckboxGroup](https://react-spectrum.adobe.com/react-aria/CheckboxGroup.html#props).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rierino.com/sdk-core-ui/components/checkbox.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
