
# Checkbox



> Checkbox component lets users choose one or more options from a limited number of options. For a single choice, use the Radio component instead.

## Examples

### Default

```tsx
import { Checkbox, Stack } from "@kesko/components";

export default () => (
  <Stack>
    <Checkbox label="Save my delivery address" name="save-address" defaultChecked />
    <Checkbox label="Use the same address for the invoice" name="same-address" />
  </Stack>
);

```

```html
<k-stack>
  <k-checkbox>
    <input type="checkbox" id="save-address" name="save-address" checked />
    <label for="save-address">Save my delivery address</label>
  </k-checkbox>
  <k-checkbox>
    <input type="checkbox" id="same-address" name="same-address" />
    <label for="same-address">Use the same address for the invoice</label>
  </k-checkbox>
</k-stack>
```

### Assistive text

```tsx
import { Checkbox } from "@kesko/components";

export default () => (
  <Checkbox
    label="Call me before the delivery"
    name="call"
    description="The driver calls about 15 minutes before arriving, so you have time to get to the door."
  />
);

```

```html
<k-checkbox>
  <input type="checkbox" id="call" name="call" aria-describedby="call-description" />
  <label for="call">Call me before the delivery</label>
  <p class="k-field-description" id="call-description">The driver calls about 15 minutes before arriving, so you have time to get to the door.</p>
</k-checkbox>
```

### Custom style

```tsx
import { Checkbox } from "@kesko/components";

const square = {
  "--k-checkbox-fill": "var(--k-color-theme-secondary)",
  "--k-checkbox-fill-hover": "var(--k-color-theme-secondary-hover)",
  "--k-checkbox-radius": "var(--k-radius-xs)",
};

export default () => <Checkbox label="Remember me on this device" name="remember" defaultChecked style={square} />;

```

```html
<k-checkbox
  style="
    --k-checkbox-fill: var(--k-color-theme-secondary);
    --k-checkbox-fill-hover: var(--k-color-theme-secondary-hover);
    --k-checkbox-radius: var(--k-radius-xs);"
>
  <input type="checkbox" id="remember" name="remember" checked />
  <label for="remember">Remember me on this device</label>
</k-checkbox>
```

### Disabled

```tsx
import { Checkbox, Stack } from "@kesko/components";

export default () => (
  <Stack>
    <Checkbox label="Home delivery" name="home-delivery" disabled />
    <Checkbox label="Pickup from store" name="pickup" defaultChecked disabled />
  </Stack>
);

```

```html
<k-stack>
  <k-checkbox>
    <input type="checkbox" id="home-delivery" name="home-delivery" disabled />
    <label for="home-delivery">Home delivery</label>
  </k-checkbox>
  <k-checkbox>
    <input type="checkbox" id="pickup" name="pickup" checked disabled />
    <label for="pickup">Pickup from store</label>
  </k-checkbox>
</k-stack>
```

### Group

```tsx
import { Checkbox, Fieldset } from "@kesko/components";

export default () => (
  <Fieldset label="Delivery options">
    <Checkbox label="Leave the order at the door" name="options" value="door" />
    <Checkbox label="Call before delivery" name="options" value="call" defaultChecked />
    <Checkbox label="Deliver with recyclable packaging" name="options" value="recyclable" />
  </Fieldset>
);

```

```html
<fieldset class="k-fieldset" aria-describedby="options-description">
  <legend class="k-fieldset-legend">Delivery options</legend>
  <k-checkbox>
    <input type="checkbox" id="door" name="options" value="door" />
    <label for="door">Leave the order at the door</label>
  </k-checkbox>
  <k-checkbox>
    <input type="checkbox" id="call" name="options" value="call" checked />
    <label for="call">Call before delivery</label>
  </k-checkbox>
  <k-checkbox>
    <input type="checkbox" id="recyclable" name="options" value="recyclable" />
    <label for="recyclable">Deliver with recyclable packaging</label>
  </k-checkbox>
</fieldset>
```

### Indeterminate

```tsx
import { Checkbox, Stack } from "@kesko/components";
import { useState } from "react";

const stores = ["K-Citymarket Sello", "K-Supermarket Kamppi", "K-Market Töölö"];

export default () => {
  const [selected, setSelected] = useState(["K-Supermarket Kamppi"]);
  const all = selected.length === stores.length;
  const some = selected.length > 0 && !all;

  return (
    <Stack>
      <Checkbox
        label="All stores"
        checked={all}
        indeterminate={some}
        onChange={event => setSelected(event.target.checked ? stores : [])}
      />
      <Stack style={{ paddingInlineStart: "var(--k-space-xl)" }}>
        {stores.map(store => (
          <Checkbox
            key={store}
            label={store}
            name="stores"
            value={store}
            checked={selected.includes(store)}
            onChange={event =>
              setSelected(event.target.checked ? [...selected, store] : selected.filter(item => item !== store))
            }
          />
        ))}
      </Stack>
    </Stack>
  );
};

```

```html
<k-stack>
  <k-checkbox>
    <input type="checkbox" id="all-stores" />
    <label for="all-stores">All stores</label>
  </k-checkbox>
  <k-stack style="padding-inline-start: var(--k-space-xl);">
    <k-checkbox>
      <input type="checkbox" id="sello" name="stores" value="K-Citymarket Sello" />
      <label for="sello">K-Citymarket Sello</label>
    </k-checkbox>
    <k-checkbox>
      <input type="checkbox" id="kamppi" name="stores" value="K-Supermarket Kamppi" checked />
      <label for="kamppi">K-Supermarket Kamppi</label>
    </k-checkbox>
    <k-checkbox>
      <input type="checkbox" id="toolo" name="stores" value="K-Market Töölö" />
      <label for="toolo">K-Market Töölö</label>
    </k-checkbox>
  </k-stack>
</k-stack>
<script>
  const parent = document.getElementById("all-stores");
  const children = [...document.querySelectorAll('input[name="stores"]')];
  const update = () => {
    const checked = children.filter(child => child.checked).length;
    parent.checked = checked === children.length;
    parent.indeterminate = checked > 0 && checked < children.length;
  };
  parent.addEventListener("change", () => {
    children.forEach(child => (child.checked = parent.checked));
  });
  children.forEach(child => child.addEventListener("change", update));
  update();
</script>
```

### Required

```tsx
import { Checkbox, Stack } from "@kesko/components";

export default () => (
  <Stack>
    <Checkbox label="I accept the terms of service" name="terms" required />
    <Checkbox label="I have read the privacy notice" name="privacy" required hideRequired />
  </Stack>
);

```

```html
<k-stack>
  <k-checkbox>
    <input type="checkbox" id="terms" name="terms" required />
    <label for="terms">I accept the terms of service<span class="k-label-required" aria-hidden="true">*</span></label>
  </k-checkbox>
  <k-checkbox>
    <input type="checkbox" id="privacy" name="privacy" required />
    <label for="privacy">I have read the privacy notice</label>
  </k-checkbox>
</k-stack>
```

### Validation

```tsx
import { Button, Checkbox, Stack } from "@kesko/components";

export default () => (
  <form onSubmit={event => event.preventDefault()}>
    <Stack>
      <Checkbox label="I accept the terms of service" name="terms" required />
      <Checkbox
        label="Charge my K-Plussa account"
        name="charge"
        defaultChecked
        error="Your K-Plussa account has no payment method yet."
      />
      <Button type="submit">Place order</Button>
    </Stack>
  </form>
);

```

```html
<form>
  <k-stack>
    <k-checkbox>
      <input type="checkbox" id="terms" name="terms" required />
      <label for="terms">I accept the terms of service<span class="k-label-required" aria-hidden="true">*</span></label>
    </k-checkbox>
    <k-checkbox>
      <input type="checkbox" id="charge" name="charge" checked aria-describedby="charge-message" aria-invalid="true" />
      <label for="charge">Charge my K-Plussa account</label>
      <k-field-message variant="error" role="alert"><svg viewBox="0 0 24 24" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"><path d="M22.113 18.023L13.745 3.08c-.75-1.343-2.738-1.338-3.49 0L1.888 18.023C1.153 19.332 2.13 21 3.633 21h16.735c1.503 0 2.476-1.666 1.745-2.978zM20.367 19.5H3.633a.504.504 0 0 1-.435-.745l8.364-14.94a.506.506 0 0 1 .875 0l8.368 14.942a.501.501 0 0 1-.438.743zm-8.885-5.66l-.31-4.785c0-.115.08-.195.194-.195h1.265c.116 0 .196.08.196.195l-.3 4.613c0 .127-.058.217-.208.242l-.61.102c-.128.023-.217-.035-.23-.172zM13 16.14c0 .552-.403.978-1 .978s-1-.425-1-.978.403-.965 1-.965 1 .425 1 .965z" fill="currentColor" /></svg><span id="charge-message">Your K-Plussa account has no payment method yet.</span></k-field-message>
    </k-checkbox>
    <button class="k-button" type="submit">Place order</button>
  </k-stack>
</form>
```


## Props

- `label` — `ReactNode` Label for the checkbox. Always provide one, even when it is hidden with `hideLabel`.
- `hideLabel` — `boolean` (default: `false`) Hides the label visually while keeping it available to assistive technology.
- `description` — `ReactNode` Assistive text shown below the control, such as what checking it leads to.
- `error` — `ReactNode` Optional error message for the checkbox, shown instead of the built-in validation message. Also marks the control invalid for assistive technology.
- `name` — `string` The name the value is submitted under.
- `value` — `string` The value submitted under `name` while the box is checked. Defaults to `on`, as it does on a bare checkbox.
- `checked` — `boolean` Whether the box is checked. Pair with `onChange` to control the field.
- `defaultChecked` — `boolean` Whether an uncontrolled box starts checked.
- `indeterminate` — `boolean` (default: `false`) Shows the box as partly checked, for a parent that stands for a group of which only some are checked. A visual and assistive state only: it shows for as long as this is true, also after a click, and is never submitted with the form.
- `required` — `boolean` (default: `false`) Requires the box to be checked before the form submits. Draws the marker on the label and is announced to assistive technology.
- `hideRequired` — `boolean` (default: `false`) Hides the required marker on the label while keeping `required` on the control. Use it when the form says once that every field is required.
- `disabled` — `boolean` Disables the control. Blocks interaction and drops the value from the submitted data.
- `form` — `string` The `id` of the form the control submits with. Only needed when the control sits outside that form.

## Events

- `onChange` — `(event: ChangeEvent<HTMLInputElement>) => void` Handler called when the box is toggled.
- `onFocus` — `(event: FocusEvent<HTMLInputElement>) => void` Handler called when the control receives focus.
- `onBlur` — `(event: FocusEvent<HTMLInputElement>) => void` Handler called when the control loses focus.

## CSS custom properties

- `--k-checkbox-bg` (default: `var(--k-color-bg-base)`) Overrides the background color of the unchecked box.
- `--k-checkbox-border` (default: `var(--k-color-border-control)`) Overrides the border color of the unchecked box in every state.
- `--k-checkbox-fill` (default: `var(--k-color-theme-primary)`) Overrides the fill of the checked box.
- `--k-checkbox-fill-hover` (default: `var(--k-color-theme-primary-hover)`) Overrides the fill of the checked box on hover and focus.
- `--k-checkbox-mark` (default: `var(--k-color-text-on-primary)`) Overrides the color of the check mark.
- `--k-checkbox-radius` (default: `var(--k-radius-md)`) Overrides the border radius of the box.
- `--k-checkbox-text` (default: `var(--k-color-text-base)`) Overrides the label text color.
- `--k-checkbox-focus` (default: `var(--k-color-border-focus)`) Overrides the focus ring color.

## Accessibility

Checkbox is built on a native `<input type="checkbox">` and `<label>`, so it behaves as expected with assistive technologies.

- **Always pass a `label`.** A screen reader reads it as the name, and clicking it toggles the box. If there is no room for it, pass `hideLabel` rather than leaving it out.
- **Write the label as the option, not a question.** “Send me offers by email” reads well as checked or unchecked; “Do you want offers?” does not. Keep it short and put the consequences in `description`.
- **One box on its own, a group in a [Fieldset](/components/fieldset/).** Related options share a `name` and sit in a Fieldset so the legend names the group. An error about the group, such as nothing chosen, goes on the Fieldset; an error about one box, such as unaccepted terms, stays on that box.
- **`indeterminate` is for a parent that stands for a group.** It shows that only some of the children are checked and is announced as mixed. It is never submitted: the form sends the children, not the parent.
- **Never pre-check consent.** A box that asks people to agree to something starts unchecked, and `required` makes the form wait for it.
- **Avoid `disabled` for validation.** A disabled box leaves the tab order and cannot explain itself. Keep it enabled and let the message say what is wrong.
- **Check contrast if you override colors.** The label needs 4.5:1 against the surface, and `--k-checkbox-border` and `--k-checkbox-fill` need 3:1 against it. Verify with the [WebAIM contrast checker](https://webaim.org/resources/contrastchecker/).

See the [accessibility guidelines](/accessibility/) and the [A11Y checklist](/a11y-checklist/) for more details.

## Integration

Install `@kesko/components` together with `@kesko/css`, which provides the design tokens and base styles it depends on, and `@kesko/themes` for your brand:

```sh
npm install @kesko/components @kesko/css @kesko/themes
```

Then load the base stylesheet and one brand theme once, in your app's entry point or root layout. Leave out `@kesko/css/components`: it mirrors the component styles for plain HTML, and `@kesko/components` already ships them.

```tsx
import "@kesko/css";
import "@kesko/themes/dist/k-rauta.css";
```

Finally, import the component into your project:

```tsx
import { Checkbox } from "@kesko/components";
```

Projects without React get the same markup and styling from `@kesko/css` by importing the base stylesheet, the `components` add-on and a brand theme, in that order:

```css
@import "@kesko/css";
@import "@kesko/css/components";
@import "@kesko/themes/dist/k-rauta.css";
```

The HTML examples above show the exact markup to copy.

## Resources

- [Usage guidelines](/components/usage/) — Learn how to use the component library with React, Next.js, or basic HTML.
- [Localization](/localization/) — How to translate the components for multiple languages.
- [CSS Framework](/css/) — Utilities and tokens for everything the components don’t cover.
- [Themes](/themes/) — How to customize the component library theme.
- [Accessibility checklist](/a11y-checklist/) — Interactive, browser-based Kesko accessibility checklist.

