Checkbox component lets users choose one or more options from a limited number of options. For a single choice, use the Radio component instead.
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>
);<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>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."
/>
);<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>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} />;<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>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>
);<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>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>
);<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>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>
);
};<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>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>
);<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>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>
);<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>Properties #
| Name | Description | Type | Default |
|---|---|---|---|
label | Label for the checkbox. Always provide one, even when it is hidden with | ReactNode | — |
hideLabel | Hides the label visually while keeping it available to assistive technology. | boolean | false |
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 | string | — |
checked | Whether the box is checked. Pair with | 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. | boolean | false |
required | Requires the box to be checked before the form submits. Draws the marker on the label and is announced to assistive technology. | boolean | false |
hideRequired | Hides the required marker on the label while keeping | boolean | false |
disabled | Disables the control. Blocks interaction and drops the value from the submitted data. | boolean | — |
form | The | string | — |
Events #
| Name | Description | Type |
|---|---|---|
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.
| Name | Description | Default |
|---|---|---|
--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, passhideLabelrather 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
nameand 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. indeterminateis 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
requiredmakes the form wait for it. - Avoid
disabledfor 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-borderand--k-checkbox-fillneed 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/csspnpm add @kesko/components @kesko/cssyarn add @kesko/components @kesko/cssbun add @kesko/components @kesko/cssOnce 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 #
Usage guidelines
Learn how to use the component library with React, Next.js, or basic HTML.
Localization
How to translate the components for multiple languages.
CSS Framework
Utilities and tokens for everything the components don’t cover.
Themes
How to customize the component library theme.
Accessibility checklist
Interactive, browser-based Kesko accessibility checklist.