Toggle switch

A switch whose thumb slides across with a small overshoot while the track colour changes.

ButtonPress Pure CSS time-based Targets: Web · App All modern browsers
Target
Implement the "Toggle switch" effect in vanilla HTML/CSS/JS.

Context: A switch whose thumb slides across with a small overshoot while the track colour changes.
Trigger: the element is pressed (pointerdown → pointerup).
Timeline: time-based (duration + easing).

Motion tokens (normal):
- translateX (thumb): 0 → track width − thumb width
- background (track): neutral → accent
- duration: 220ms
- easing: cubic-bezier(0.34, 1.56, 0.64, 1)
- note: track colour uses 150ms ease; thumb uses the overshoot curve

Motion tokens (prefers-reduced-motion):
- translateX (thumb): 0 → track width − thumb width
- background (track): neutral → accent
- duration: 120ms
- easing: ease-out
- note: same motion, no overshoot

Technique: prefer pure CSS transitions/animations; use JS only to toggle classes.

Acceptance:
- animate only transform and opacity unless the spec lists other properties
- no layout shift; content is visible without the animation
- when prefers-reduced-motion is set, use the reduced tokens only
- use a real button with role=switch and aria-checked; Space toggles it
- thumb position comes from transform, not left/right

Generated from the spec. Prompts differ by target name only; the tokens are the source of truth.