Cómo funciona
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.
Úsalo para
- 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.
Busca otra opción cuando
- 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.
Uso
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}
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
checked | boolean | — | Controlled on/off state; use with onCheckedChange. |
defaultChecked | boolean | false | Initial state when uncontrolled. |
onCheckedChange | (checked: boolean) => void | — | Called on every change: click, keyboard, drag or label click. |
label | ReactNode | — | Label text; clicking it also toggles, and it is the switch's accessible name. |
description | ReactNode | — | 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. |
name | string | — | Form field name; submits name=value when on and nothing when off, like a native checkbox. |
value | string | 'on' | Value submitted when on. |
disabled | boolean | false | Disables the switch. |
ref | Ref<HTMLButtonElement> | — | Forwarded to the role=switch button, for example to focus it. |
...rest | ButtonHTMLAttributes | — | className and style go on the outer element; other native attributes (id, aria-*, onFocus…) go to the role=switch button. |
Accesibilidad
- 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.
Movimiento
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.
Añádelo a tu proyecto
Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.
ElasticSwitch.jsxelastic-switch.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.





