Skip to content

Switch

Turn a setting on or off and apply it immediately.

import { Switch } from '@youj/react'

Installation & usage

npm i @youj/react

For an app already set up with npx @youj/cli init, which installs the peers (@youj/core, @youj/motion, motion and Base UI) and wires the provider. New app? Start at Installation.

Use Switch for settings that apply immediately, such as muting a channel or turning on previews. For a value saved with a form, use a Checkbox. For named alternatives such as light and dark, use a Toggle Group or Radio Group.

import { Field, FieldLabel, Switch } from '@youj/react'

export function AirplaneMode() {
  return (
    <Field orientation="horizontal">
      <Switch />
      <FieldLabel>Airplane mode</FieldLabel>
    </Field>
  )
}

In a settings row, put the label and its description in a FieldContent first and the switch last, so the label leads and the control lines up at the end of every row.

Reference

Propssize, variant, sound (3)

Props

Switch props
PropType
size'sm' | 'default' | 'lg'

default 'default'

'default'
variant'default' | 'liquid'

liquid draws a glass droplet for a thumb that stretches as it travels, overshoots and settles, pouring the accent into the track behind it.

default 'default'

'default'
soundboolean

With sound on (YoujProvider’s sound), toggling plays toggleOn or toggleOff. false keeps this one silent.

default true

true
Other props pass through: extends SwitchPrimitive.Root.Props.
Data attributeValues
data-sizesm, default, lg
data-variantdefault, liquid
data-checked, data-unchecked, data-disabledBase UI's state

It also takes Base UI's checked, defaultChecked, onCheckedChange, disabled, readOnly, required, name and value. The required default comes from the surrounding Field.

MotionThumb travel, track color, press, liquid · reduced motion

Motion

Motion tokens
MomentTokens
Thumb travelone thumb width, mirrored under right-to-left
spring.snappy

0.2s, bounce 0

0.2s, bounce 0
Track coloroff to on
duration.fastease.standard

140ms · cubic-bezier(0.2, 0, 0, 1)

140mscubic-bezier(0.2, 0, 0, 1)
Pressthe thumb squeezes to 85%
duration.instantease.standard

80ms · cubic-bezier(0.2, 0, 0, 1)

80mscubic-bezier(0.2, 0, 0, 1)

Reduced motion

The thumb jumps to its end and doesn't squeeze on press. The track still changes color.

The spring drives a single value from 0 to 1, and CSS turns it into the thumb's translate. A switch flipped back mid-flight retargets the spring from where the thumb is, so rapid toggling never jumps.

Liquid

Motion tokens
MomentTokens
Droplet travelovershoots its end and settles
spring.settle

0.45s, bounce 0.3

0.45s, bounce 0.3
Stretchup to 30% wider and 18% flatter with the spring’s speed, squashed against the end while it overshoots
spring.settle

0.45s, bounce 0.3

0.45s, bounce 0.3
Fillthe accent, clipped to the traveled part of the track, follows the same value
spring.settle

0.45s, bounce 0.3

0.45s, bounce 0.3
Pressthe droplet swells to 110%
duration.instantease.standard

80ms · cubic-bezier(0.2, 0, 0, 1)

80mscubic-bezier(0.2, 0, 0, 1)

Reduced motion

The plain switch's: the droplet jumps to its end, doesn't stretch or swell, and the fill shows at once.

The liquid switch springs on settle, the kit's one spring that overshoots, so the droplet lands with a small wobble. Its stretch is read from the spring's speed, so a rapid toggle that retargets mid-flight stretches it the right amount.

Reduced motion

The thumb moves to its end at once and the press doesn't squeeze it. The thumb's position and the track color still show the state. The liquid droplet does the same: no stretch, no swell, and the fill shows at once.

AccessibilityKeyboard, ARIA

Accessibility

  • It's a switch role with aria-checked. Space toggles it.
  • A FieldLabel in the same Field names it. Outside a Field, give it aria-label, or pair Label htmlFor with the switch's id.
  • In both states the thumb clears 3:1 against the track, and the track clears 3:1 against the page. The liquid droplet is a near-white glass on the same off track and the accent fill. The thumb's side shows the state too, so it doesn't rest on color alone.
  • The hit area extends past the track, and grows on touch screens.