
# Switch



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

## Examples

### Default

```tsx
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>
);

```

```html
<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>
```

### Assistive text

```tsx
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."
  />
);

```

```html
<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>
```

### Custom style

```tsx
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} />;

```

```html
<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>
```

### Disabled

```tsx
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>
);

```

```html
<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>
```

### Group

```tsx
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>
);

```

```html
<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>
```

### Orientation

```tsx
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>
);

```

```html
<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>
```

### Required

```tsx
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>
);

```

```html
<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>
```

### Settings list

```tsx
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>
);

```

```html
<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>
```

### Validation

```tsx
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>
);

```

```html
<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>
```


## Props

- `label` — `ReactNode` Label for the switch. 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.
- `orientation` — `"horizontal" | "reverse" | "vertical"` (default: `"horizontal"`) Where the label sits: before the switch, after it, or above it.
- `expand` — `boolean` (default: `false`) Makes the field fill the width of its container, which lines the toggles of a list of switches up on one edge.
- `description` — `ReactNode` Assistive text shown below the switch, such as what turning it on leads to.
- `error` — `ReactNode` Optional error message for the switch, 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 switch is on. Defaults to `on`, as it does on a bare checkbox.
- `checked` — `boolean` Whether the switch is on. Pair with `onChange` to control the field.
- `defaultChecked` — `boolean` Whether an uncontrolled switch starts on.
- `required` — `boolean` (default: `false`) Requires the switch to be on 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 switch 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-switch-bg` (default: `var(--k-color-bg-base)`) Overrides the track color while the switch is off.
- `--k-switch-border` (default: `var(--k-color-border-control)`) Overrides the track border color while the switch is off, in every state.
- `--k-switch-fill` (default: `var(--k-color-theme-primary)`) Overrides the track color while the switch is on.
- `--k-switch-fill-hover` (default: `var(--k-color-theme-primary-hover)`) Overrides the on track color on hover and focus.
- `--k-switch-thumb` (default: `var(--k-color-text-on-primary)`) Overrides the thumb color while the switch is on.
- `--k-switch-radius` (default: `var(--k-radius-pill)`) Overrides the border radius of the track.
- `--k-switch-focus` (default: `var(--k-color-border-focus)`) Overrides the focus ring color.
- `--k-switch-inline-size` (default: `fit-content`) Overrides the width of the field. The `expand` prop sets it to `100%`.

## 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](/components/checkbox/).
- **One setting per switch.** Related settings share a `name` and sit in a [Fieldset](/components/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](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 { Switch } 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.

