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

Open in new tab
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>
);
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."
  />
);
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} />;
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>
);
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>
);
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>
  );
};
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>
);
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>
);

Properties #

NameDescriptionTypeDefault
label

Label for the checkbox. Always provide one, even when it is hidden with hideLabel.

ReactNode—
hideLabel

Hides the label visually while keeping it available to assistive technology.

booleanfalse
description

Assistive text shown below the control, such as what checking it leads to.

ReactNode—
error

Optional error message for the checkbox, shown instead of the built-in validation message. Also marks the control invalid for assistive technology.

ReactNode—
name

The name the value is submitted under.

string—
value

The value submitted under name while the box is checked. Defaults to on, as it does on a bare checkbox.

string—
checked

Whether the box is checked. Pair with onChange to control the field.

boolean—
defaultChecked

Whether an uncontrolled box starts checked.

boolean—
indeterminate

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.

booleanfalse
required

Requires the box to be checked before the form submits. Draws the marker on the label and is announced to assistive technology.

booleanfalse
hideRequired

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.

booleanfalse
disabled

Disables the control. Blocks interaction and drops the value from the submitted data.

boolean—
form

The id of the form the control submits with. Only needed when the control sits outside that form.

string—

Events #

NameDescriptionType
onChange

Handler called when the box is toggled.

(event: ChangeEvent<HTMLInputElement>) => void
onFocus

Handler called when the control receives focus.

(event: FocusEvent<HTMLInputElement>) => void
onBlur

Handler called when the control loses focus.

(event: FocusEvent<HTMLInputElement>) => void

Styles #

CSS custom properties provide finer-grained control than properties. See Styling API for more details. Prefer the Properties API when it suits your need.

NameDescriptionDefault
--k-checkbox-bg

Overrides the background color of the unchecked box.

var(--k-color-bg-base)
--k-checkbox-border

Overrides the border color of the unchecked box in every state.

var(--k-color-border-control)
--k-checkbox-fill

Overrides the fill of the checked box.

var(--k-color-theme-primary)
--k-checkbox-fill-hover

Overrides the fill of the checked box on hover and focus.

var(--k-color-theme-primary-hover)
--k-checkbox-mark

Overrides the color of the check mark.

var(--k-color-text-on-primary)
--k-checkbox-radius

Overrides the border radius of the box.

var(--k-radius-md)
--k-checkbox-text

Overrides the label text color.

var(--k-color-text-base)
--k-checkbox-focus

Overrides the focus ring color.

var(--k-color-border-focus)

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. 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.

See the accessibility guidelines and the A11Y checklist for more details.


Usage #

There are two necessary parts to using the Checkbox component. The @kesko/components package and the CSS Framework are required. Everything else is optional:

npm install @kesko/components @kesko/css

Once installed, load the CSS Framework in your app’s entry point or root layout:

import "@kesko/css";

Then, you can import the Checkbox:

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

Projects without React can get the same styles from @kesko/css by importing the components stylesheet as well:

@import "@kesko/css";
@import "@kesko/css/components";

Resources #