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.

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

Properties #

NameDescriptionTypeDefault
label

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

ReactNode—
hideLabel

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

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

booleanfalse
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 name while the switch is on. Defaults to on, as it does on a bare checkbox.

string—
checked

Whether the switch is on. Pair with onChange to control the field.

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.

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

NameDescriptionDefault
--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 expand prop sets it to 100%.

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, pass hideLabel rather 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 name and sit in a Fieldset so the legend names the group, but a switch is never one option of a multiple-choice question.
  • required is 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 disabled for 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-border and --k-switch-fill need 3:1 against it, and --k-switch-thumb needs 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/css

Once 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 #