Web and native

Switch in kanso-ui 0.59.0, for React.

import { Switch } from '@kanso-labs/kanso-ui'

A switch that turns a setting on or off, with its label and optionally a description or an error under it.

It follows the Material Design switch: a 52 by 32dp track with a full corner and a handle that is 16dp off, 24dp on and 28dp while pressed. The handle follows a pointer drag across the track and settles on whichever side of the middle it is released.

What you provide

  • children: the label beside the switch. A switch labelled some other way passes aria-label or aria-labelledby instead; one of the three is required.
  • A link or button in the label acts on its own; a press anywhere else in the label flips the switch.

Props

PropTypeDefaultDescription
childrenReactNode—The label, beside the switch. A switch that a row labels some other way leaves it out and passes `aria-label` or `aria-labelledby` instead — one of the three is required, since a switch with none is announced as an unnamed switch. A link or a button in it acts on its own, as in a native label, and leaves the switch as it was. A press anywhere else in the label toggles it.
descriptionstring—A hint under the label. Replaced by `error` when there is one, so the two never stack.
errorstring—The problem with the current value, in words. Its presence is what puts the switch in its error state — the `critical` edge on the track, and on the thumb while it is off, the message and `aria-invalid` follow from it. Outside a `Form` the message line arrives with the message, so the field grows when this does and moves what is under it. A `Form` holds that space from the start; so does a permanent `description`.
iconReactNodefalseThe icon in the thumb while the switch is on: `true` for the check, Material Design's "icon on selected switch" form, or a node to draw in its place. The thumb draws it at 16px in `ink`, so give a node `1em` square in `currentColor`.
unselectedIconReactNodefalseThe icon in the thumb while the switch is off: `true` for the cross, or a node to draw in its place. Given with `icon`, it is Material Design's form with an icon in both states, and the off thumb grows to the on thumb's 22px to hold it.

Also takes the props of AccessibleName, Omit<SwitchFieldProps, 'children' | 'isInvalid' | 'validationBehavior'>.

Example

import { Switch } from '@kanso-labs/kanso-ui'

<Switch defaultSelected icon description="Supporting line">
  Label
</Switch>

The design’s classes

k-switch on the <label>, role="switch" on its checkbox, and k-switch__reason.

Switch in kanso-ui and react-native-kanso-ui, restyled to this language.