Skip to content
English

How it works

The thumb is two circles plus a rectangle between them: on press the second circle moves and the rectangle expands, forming a stretched capsule, with a drop-shadow on the parent for one outline. Only transform animates, never width. On release it springs over in 420ms. The off track is a 16% tint of the text colour, so it reads on white cards and grey panels alike; hover deepens it, and keyboard focus grows a soft accent halo. The stretch direction is remembered so the retract never jumps sides. On touch the thumb follows the finger 1:1 and release past halfway decides.

Use it for

  • Instant on/off options in settings: notifications, dark mode, auto-save.
  • Preference switches that benefit from a bit of tactile feel, such as profile settings.
  • Mobile settings lists: drag works and the hit area is at least 44px.

Look elsewhere when

  • Options that only apply after pressing Save: use a checkbox, since a switch implies instant effect.
  • Choosing one of several options: use radios or a segmented control.
  • Rows of switches in a table: many bounces at once look busy, so use size sm without icons or use checkboxes.

Usage

import { useState } from 'react'
import { ElasticSwitch } from './ElasticSwitch.jsx'

export function NotifySetting() {
  const [on, setOn] = useState(true)
  return (
    <ElasticSwitch
      label="Notifications"
      description="Instant alerts for orders and messages"
      labelPlacement="start"
      icon="check"
      checked={on}
      onCheckedChange={setOn}
    />
  )
}

Props

PropTypeDefaultWhat it does
checkedboolean—Controlled on/off state; use with onCheckedChange.
defaultCheckedbooleanfalseInitial state when uncontrolled.
onCheckedChange(checked: boolean) => void—Called on every change: click, keyboard, drag or label click.
labelReactNode—Label text; clicking it also toggles, and it is the switch's accessible name.
descriptionReactNode—Helper text under the label, linked through aria-describedby.
labelPlacement'start' | 'end''end'Label position; start fills the row and pushes the switch right, good for settings lists.
size'sm' | 'md' | 'lg''md'Size: 34×20 / 44×26 / 56×32 (thumb 16 / 20 / 26px).
icon'none' | 'check' | 'theme' | { on: ReactNode, off: ReactNode }'none'Icon inside the thumb: check (tick/cross), theme (sun/moon), or your own pair.
namestring—Form field name; submits name=value when on and nothing when off, like a native checkbox.
valuestring'on'Value submitted when on.
disabledbooleanfalseDisables the switch.
refRef<HTMLButtonElement>—Forwarded to the role=switch button, for example to focus it.
...restButtonHTMLAttributes—className and style go on the outer element; other native attributes (id, aria-*, onFocus…) go to the role=switch button.

Accessibility

  • A native <button role="switch"> whose aria-checked reflects the state; Tab focuses it and Space or Enter toggles.
  • Label and description are linked with aria-labelledby and aria-describedby; clicking the label also toggles.
  • A soft accent halo shows on keyboard focus (a transparent outline keeps it visible in forced-colors mode); decorative parts such as icons and the thumb are aria-hidden.
  • A transparent enlarged hit area makes the target at least 44px tall.
  • With reduced motion there is no stretch or bounce: the position switches directly and only the color fades.

Motion

Press stretch 160ms ease-out; release movement and retract 420ms with a spring curve; track fill 240ms; hover tint 160ms; focus halo 220ms ease-out; icon rotate and scale 360ms spring plus 160ms opacity.

Add it to your project

Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.

ElasticSwitch.jsxelastic-switch.csstokens.cssREADME.md

Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.

Form inputs
Availability grid comes with Pro. Its preview shows once you unlock it.
Boolean operations comes with Pro. Its preview shows once you unlock it.
Cascader comes with Pro. Its preview shows once you unlock it.