Switch component lets users turn a setting on or off, with the effect applied right away. It renders the label, the switch and the assistive text as one field.
import { Stack, Switch } from "@kesko/components";
export default () => (
<Stack>
<Switch label="Order notifications" name="order-notifications" defaultChecked />
<Switch label="Shopping list reminders" name="list-reminders" />
</Stack>
);<k-stack>
<k-switch>
<label class="k-label" for="order-notifications">Order notifications</label>
<input type="checkbox" role="switch" id="order-notifications" name="order-notifications" checked />
</k-switch>
<k-switch>
<label class="k-label" for="list-reminders">Shopping list reminders</label>
<input type="checkbox" role="switch" id="list-reminders" name="list-reminders" />
</k-switch>
</k-stack>import { Switch } from "@kesko/components";
export default () => (
<Switch
label="Substitute out-of-stock products"
name="substitute"
defaultChecked
description="We replace them with the closest match available."
/>
);<k-switch>
<label class="k-label" for="substitute">Substitute out-of-stock products</label>
<input type="checkbox" role="switch" id="substitute" name="substitute" checked aria-describedby="substitute-description" />
<p class="k-field-description" id="substitute-description">We replace them with the closest match available.</p>
</k-switch>import { Switch } from "@kesko/components";
const secondary = {
"--k-switch-fill": "var(--k-color-theme-secondary)",
"--k-switch-fill-hover": "var(--k-color-theme-secondary-hover)",
"--k-switch-thumb": "var(--k-color-text-on-secondary)",
};
export default () => <Switch label="Remember me on this device" name="remember" defaultChecked style={secondary} />;<k-switch
style="
--k-switch-fill: var(--k-color-theme-secondary);
--k-switch-fill-hover: var(--k-color-theme-secondary-hover);
--k-switch-thumb: var(--k-color-text-on-secondary);"
>
<label class="k-label" for="remember">Remember me on this device</label>
<input type="checkbox" role="switch" id="remember" name="remember" checked />
</k-switch>import { Stack, Switch } from "@kesko/components";
export default () => (
<Stack>
<Switch label="Home delivery" name="home-delivery" disabled />
<Switch label="Pickup from store" name="pickup" defaultChecked disabled />
</Stack>
);<k-stack>
<k-switch>
<label class="k-label" for="home-delivery">Home delivery</label>
<input type="checkbox" role="switch" id="home-delivery" name="home-delivery" disabled />
</k-switch>
<k-switch>
<label class="k-label" for="pickup">Pickup from store</label>
<input type="checkbox" role="switch" id="pickup" name="pickup" checked disabled />
</k-switch>
</k-stack>import { Fieldset, Switch } from "@kesko/components";
export default () => (
<Fieldset
label="Notifications"
description="Choose how we keep you posted about your orders."
style={{ maxInlineSize: "24rem" }}
>
<Switch label="Email" name="channels" value="email" expand defaultChecked />
<Switch label="Text message" name="channels" value="sms" expand />
<Switch label="Push notification" name="channels" value="push" expand defaultChecked />
</Fieldset>
);<fieldset class="k-fieldset" aria-describedby="channels-description" style="max-inline-size: 24rem;">
<legend class="k-fieldset-legend">Notifications</legend>
<p class="k-field-description" id="channels-description">Choose how we keep you posted about your orders.</p>
<k-switch expand>
<label class="k-label" for="email">Email</label>
<input type="checkbox" role="switch" id="email" name="channels" value="email" checked />
</k-switch>
<k-switch expand>
<label class="k-label" for="sms">Text message</label>
<input type="checkbox" role="switch" id="sms" name="channels" value="sms" />
</k-switch>
<k-switch expand>
<label class="k-label" for="push">Push notification</label>
<input type="checkbox" role="switch" id="push" name="channels" value="push" checked />
</k-switch>
</fieldset>import { Fieldset, Stack, Switch } from "@kesko/components";
export default () => (
<Stack gap="xl">
<Fieldset label="Horizontal" style={{ maxInlineSize: "20rem" }}>
<Switch label="Order notifications" name="order-horizontal" expand defaultChecked />
<Switch label="Shopping list reminders" name="list-horizontal" expand />
</Fieldset>
<Fieldset label="Vertical">
<Stack direction="row" gap="lg">
<Switch label="Order notifications" name="order-vertical" orientation="vertical" defaultChecked />
<Switch label="Shopping list reminders" name="list-vertical" orientation="vertical" />
</Stack>
</Fieldset>
<Fieldset label="Reverse">
<Switch label="Order notifications" name="order-reverse" orientation="reverse" defaultChecked />
<Switch label="Shopping list reminders" name="list-reverse" orientation="reverse" />
</Fieldset>
</Stack>
);<k-stack gap="xl">
<fieldset class="k-fieldset" style="max-inline-size: 20rem;">
<legend class="k-fieldset-legend">Horizontal</legend>
<k-switch expand>
<label class="k-label" for="order-horizontal">Order notifications</label>
<input type="checkbox" role="switch" id="order-horizontal" name="order-horizontal" checked />
</k-switch>
<k-switch expand>
<label class="k-label" for="list-horizontal">Shopping list reminders</label>
<input type="checkbox" role="switch" id="list-horizontal" name="list-horizontal" />
</k-switch>
</fieldset>
<fieldset class="k-fieldset">
<legend class="k-fieldset-legend">Vertical</legend>
<k-stack direction="row" gap="lg">
<k-switch orientation="vertical">
<label class="k-label" for="order-vertical">Order notifications</label>
<input type="checkbox" role="switch" id="order-vertical" name="order-vertical" checked />
</k-switch>
<k-switch orientation="vertical">
<label class="k-label" for="list-vertical">Shopping list reminders</label>
<input type="checkbox" role="switch" id="list-vertical" name="list-vertical" />
</k-switch>
</k-stack>
</fieldset>
<fieldset class="k-fieldset">
<legend class="k-fieldset-legend">Reverse</legend>
<k-switch orientation="reverse">
<label class="k-label" for="order-reverse">Order notifications</label>
<input type="checkbox" role="switch" id="order-reverse" name="order-reverse" checked />
</k-switch>
<k-switch orientation="reverse">
<label class="k-label" for="list-reverse">Shopping list reminders</label>
<input type="checkbox" role="switch" id="list-reverse" name="list-reverse" />
</k-switch>
</fieldset>
</k-stack>import { Stack, Switch } from "@kesko/components";
export default () => (
<Stack style={{ maxInlineSize: "20rem" }}>
<Switch label="I accept the terms of service" name="terms" expand required />
<Switch label="I have read the privacy notice" name="privacy" expand required hideRequired />
</Stack>
);<k-stack style="max-inline-size: 20rem;">
<k-switch expand>
<label class="k-label" for="terms">I accept the terms of service<span class="k-label-required" aria-hidden="true">*</span></label>
<input type="checkbox" role="switch" id="terms" name="terms" required />
</k-switch>
<k-switch expand>
<label class="k-label" for="privacy">I have read the privacy notice</label>
<input type="checkbox" role="switch" id="privacy" name="privacy" required />
</k-switch>
</k-stack>import { Fieldset, Switch } from "@kesko/components";
export default () => (
<Fieldset label="Notifications" style={{ maxInlineSize: "20rem" }}>
<Switch label="Email" name="channels" value="email" expand defaultChecked />
<Switch label="Text message" name="channels" value="sms" expand />
<Switch label="Push notification" name="channels" value="push" expand defaultChecked />
</Fieldset>
);<fieldset class="k-fieldset" style="max-inline-size: 20rem;">
<legend class="k-fieldset-legend">Notifications</legend>
<k-switch expand>
<label class="k-label" for="email">Email</label>
<input type="checkbox" role="switch" id="email" name="channels" value="email" checked />
</k-switch>
<k-switch expand>
<label class="k-label" for="sms">Text message</label>
<input type="checkbox" role="switch" id="sms" name="channels" value="sms" />
</k-switch>
<k-switch expand>
<label class="k-label" for="push">Push notification</label>
<input type="checkbox" role="switch" id="push" name="channels" value="push" checked />
</k-switch>
</fieldset>import { Button, Stack, Switch } from "@kesko/components";
export default () => (
<form onSubmit={event => event.preventDefault()}>
<Stack style={{ maxInlineSize: "24rem" }}>
<Switch label="I accept the terms of service" name="terms" expand required />
<Switch
label="Charge my K-Plussa account"
name="charge"
expand
defaultChecked
error="Your K-Plussa account has no payment method yet."
/>
<Button type="submit">Save settings</Button>
</Stack>
</form>
);<form>
<k-stack style="max-inline-size: 24rem;">
<k-switch expand>
<label class="k-label" for="terms">I accept the terms of service<span class="k-label-required" aria-hidden="true">*</span></label>
<input type="checkbox" role="switch" id="terms" name="terms" required />
</k-switch>
<k-switch expand>
<label class="k-label" for="charge">Charge my K-Plussa account</label>
<input type="checkbox" role="switch" id="charge" name="charge" checked aria-describedby="charge-message" aria-invalid="true" />
<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-switch>
<button class="k-button" type="submit">Save settings</button>
</k-stack>
</form>Properties #
| Name | Description | Type | Default |
|---|---|---|---|
label | Label for the switch. Always provide one, even when it is hidden with | ReactNode | — |
hideLabel | Hides the label visually while keeping it available to assistive technology. | boolean | false |
orientation | Where the label sits: before the switch, after it, or above it. | "horizontal" | "reverse" | "vertical" | "horizontal" |
expand | Makes the field fill the width of its container, which lines the toggles of a list of switches up on one edge. | boolean | false |
description | Assistive text shown below the switch, such as what turning it on leads to. | ReactNode | — |
error | Optional error message for the switch, 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 switch is on. Pair with | boolean | — |
defaultChecked | Whether an uncontrolled switch starts on. | boolean | — |
required | Requires the switch to be on 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 switch 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-switch-bg | Overrides the track color while the switch is off. | var(--k-color-bg-base) |
--k-switch-border | Overrides the track border color while the switch is off, in every state. | var(--k-color-border-control) |
--k-switch-fill | Overrides the track color while the switch is on. | var(--k-color-theme-primary) |
--k-switch-fill-hover | Overrides the on track color on hover and focus. | var(--k-color-theme-primary-hover) |
--k-switch-thumb | Overrides the thumb color while the switch is on. | var(--k-color-text-on-primary) |
--k-switch-radius | Overrides the border radius of the track. | var(--k-radius-pill) |
--k-switch-focus | Overrides the focus ring color. | var(--k-color-border-focus) |
--k-switch-inline-size | Overrides the width of the field. The | fit-content |
Accessibility #
Switch is built on a native <input type="checkbox"> with role="switch" and a native <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 switch. If there is no room for it, passhideLabelrather than leaving it out. - Name the setting, not the action. “Order notifications” reads well as on or off; “Turn on notifications” does not. Keep it short and put the consequences in
description. - A switch promises an immediate effect. Use it for a setting that applies as soon as it is flipped, or that is saved by a nearby Save button that the page makes obvious. For a choice that is only submitted with the rest of a form, use a Checkbox.
- One setting per switch. Related settings share a
nameand sit in a Fieldset so the legend names the group, but a switch is never one option of a multiple-choice question. requiredis for consent-like toggles only. A setting that must be on is not a setting; say so in the copy instead of blocking the form.- Avoid
disabledfor validation. A disabled switch 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-switch-borderand--k-switch-fillneed 3:1 against it, and--k-switch-thumbneeds 3:1 against--k-switch-fill. 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 Switch 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 Switch:
import { Switch } 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.