Elastic toggles
From a plain checkbox to a squishy, animated switch, one layer at a time.
What you are looking at
A toggle is one of the smallest things on a screen and one of the most touched. That makes it a good place to show what care for detail adds to an interface that already works.
The finished component. Press and hold one before you let go.
All six are one machine with different numbers. This article builds it a layer at a time, starting from a toggle with no motion at all, so you can decide where to stop for your own project.
The governing idea: fidelity lives in the details. Each layer is small on its own, a few milliseconds or a few pixels, and none of them changes what the toggle does. Together they make it answer the instant you touch it, and feel like someone cared.
The baseline
This is the starting point: a value that changes when you click, and nothing more.
This already does the job. Every layer from here changes how it feels, never what it does.
They work, and they feel like nothing. Your eye has to find the knob again after every click.
The names used from here on. A switch has one target and a click means the other state; a two-sided toggle has two targets and a click means this one.
It slides
The first layer is motion: the knob travels across instead of jumping, so your eye can follow it.
Now your eye can follow the knob across. Drag the duration up and it turns sluggish; around 200 ms it reads as instant but connected.
If all you need is a toggle that feels connected, you can stop here.
It stretches
A rigid knob shows that it moved. A knob that stretches as it leaves shows which way it went, and how fast.
the front runs ahead of the back
before it arrived
One flight of the balanced switch, to scale. The knob is as long as it is fast, so it is round again by the time it lands.
There is an obvious way to make a shape longer, and it is wrong. Try both in slow motion and watch the round ends.
Watch the ends while each one is long. The scaled knob turns into an egg and the scaled plate's corners smear; the built ones keep the same round ends at any length.
One level deeper. The obvious way is scaleX, but scaling stretches the corners too. So the shape is built from three pieces: two copies of itself and a plain bar between them. One copy stays, the other slides out, and the bar scales to fill the gap. The plate of the two-sided toggle is built the same way.
The motion is one move, not a stretch followed by a settle. The stretch follows the speed: longest when the knob is fastest, gone by the time it has slowed down. So the front never reaches the wall first and waits for the back to pile in behind it, which is what reads as a rubbery landing.
The knob taken apart. Only the bar is ever scaled, and it is a rectangle, so nothing round is distorted.
</> components/live/elastic-toggles-1/engine.ts (abridged): the stretch follows the speed, and…Show code
const speed = sample(curve.speed, progress); // 0 to 1
const extra = stretch * knobWidth * speed;
knob.style.width = `${knobWidth + extra}px`;
knob.style.transform = `translateX(${x - extra / 2}px)`;It springs back
How the knob comes to rest is most of what people mean when they say a toggle feels good.
At 0 the curve meets the rest line and stops. Raise it and the curve shoots past and swings back. The firm row sits at 0, the balanced row at 0.3, and the elastic row at the far end.
One level deeper. The curve is a damped spring: a weight on a spring, released. bounce sets the damping. At 0 it reaches rest as fast as it can without passing it; above 0 it passes, swings back, and the swings die away. The elastic row adds squash: the knob is 22% flatter while it is long, and springs past its full height on the way home.
</> The spring the knob rides. A flight samples it into forty keyframes.Show code
const zeta = Math.max(0.04, 1 - bounce); const omega = 9 + 30 * bounce; const wd = omega * Math.sqrt(1 - zeta * zeta); const p = t => 1 - Math.exp(-zeta * omega * t) * (Math.cos(wd * t) + (zeta * omega / wd) * Math.sin(wd * t));
It answers the press
A click only counts when the button comes back up. This layer lets the toggle react the moment it goes down, so it never ignores you.
Hold the mouse button down without releasing. The knob leans toward where it will go and waits. Let go and it leaves from that pose; drag off before letting go and it springs back.
One level deeper. On pointer down the knob takes 150 ms to lean toward where it would go, and holds. Letting go commits from that pose, so press and release read as one gesture; dragging off springs it back. A switch always leans toward its other state. A two-sided toggle leans toward the side under the pointer, and if that side is already chosen it only squeezes a little.
</> The press. It is on by default: pressFeedback turns it off and pressAmount scales it.Show code
el.addEventListener('pointerdown', () => lean.to(pressFeedback ? 1 : 0));
el.addEventListener('pointerup', () => inside ? commitFromPose() : lean.to(0));It cannot be stalled
Whatever a toggle switches on tends to make the page busy just as the knob is moving. This layer keeps the motion smooth through that.
The page freezes right after the flip, as it would while something heavy loads. The chained knob hangs stretched because the second half of its flight is waiting for script. The other one never notices.
One level deeper. Script and layout share one thread. A second one, the compositor, can move and fade finished layers while the first is busy, but only if it was told the whole plan up front. So the fix is one animation per moving part, with the whole flight written out as keyframes in the click.
</> One animate() call per element covers the whole flight.Show code
const frames = Array.from({ length: 41 }, (_, i) => pose(i / 40));
knob.animate(frames, { duration, fill: 'forwards' });Do not undo this from the outside. If the click starts heavy work straight away, frames still drop. The toggle reports twice: onChange in the click, and onSettle when the knob has come to rest. Heavy work belongs in the second.
Details nobody sees
Three corrections that nobody notices when they are right, and everybody feels when they are wrong.
On and off are the same size. The edge is a ring drawn on top of everything, the same in both states. If the fill covered a border instead, the track would look larger when on and the knob smaller. The knob is placed by arithmetic, 4px from every side, so it stays centred at any scale.
Colour is a fade, not a repaint. The on colours already exist as layers over the resting ones, and turning on only fades them in. Opacity, like transform, belongs to the compositor. The on fill is deliberately quiet, so the bright knob carries the state.
Nothing leaves the track. Before each flight the bounce is lowered until its widest swing fits in the gap around the knob. The swing is sized to the room it has, not cut off by it. The elastic row keeps its wobble in its height, where there is space.
</> The room around the knob sets the limit for the swing.Show code
const room = gap - 1; const swing = (progress - 1) / curve.maxOvershoot * room;
Where to stop
Put the layers back together and the six toggles are four numbers per row. Balanced is stretch 0.2, squash 0.1, bounce 0.3 over 720 ms. Firm is 0, 0, 0 over 220 ms. Elastic is 1, 0.22, 0.8 over 1100 ms. With reduced motion switched on, all three skip the flight and place the knob.
That is the whole machine. Every number here is a control on the component's page, next to the presets, and the prompt there builds the switch by default, with the two-sided toggle and the full set as options.
Not sure a switch is the right control in the first place? Checkbox or switch on Bootcamp explains when to use which.