Rack & Pinion
const css = `
.toggle-rack { position: relative; display: inline-flex; align-items: center; gap: 12px; color: light-dark(#42454c, #e5e6e9); font-size: 14px; cursor: pointer; user-select: none; }
.toggle-rack__input { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0; }
.toggle-rack__track { position: relative; width: 86px; height: 40px; border-radius: 6px; }
.toggle-rack__input:focus-visible + .toggle-rack__track { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 3px; }
/* A toothed rack; the gear's teeth mesh with it as it rolls. */
.toggle-rack__rack {
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: 9px;
border-radius: 0 0 3px 3px;
background:
linear-gradient(light-dark(#cbced3, #393c41), light-dark(#cbced3, #393c41)) 0 100% / 100% 5px no-repeat,
conic-gradient(from 135deg at 50% 0, light-dark(#cbced3, #393c41) 0 90deg, transparent 90deg) 0 0 / 6px 4px repeat-x;
}
.toggle-rack__gear {
position: absolute;
left: 2px;
bottom: 7px;
width: 30px;
height: 30px;
transition: transform 0.75s cubic-bezier(0.65, 0, 0.35, 1);
}
.toggle-rack__gear svg { display: block; width: 30px; height: 30px; }
.toggle-rack__teeth { fill: light-dark(#5f626a, #a7abb2); transition: fill 0.3s; }
.toggle-rack__hub { fill: light-dark(#f3f4f7, #1a1c1f); }
.toggle-rack__spoke { fill: none; stroke: light-dark(#cbced3, #393c41); stroke-width: 2; }
.toggle-rack__end { position: absolute; right: 2px; bottom: 10px; width: 4px; height: 14px; border-radius: 2px; background: light-dark(#dbdde1, #2e3035); transition: background-color 0.3s; }
/* 52px of travel on a 15px radius is 199 degrees of roll. */
.toggle-rack__input:checked + .toggle-rack__track .toggle-rack__gear { transform: translateX(52px) rotate(199deg); }
.toggle-rack__input:checked + .toggle-rack__track .toggle-rack__teeth { fill: light-dark(#373a40, #f1f2f3); }
.toggle-rack__input:checked + .toggle-rack__track .toggle-rack__end { background: light-dark(#373a40, #f1f2f3); transition-delay: 0.6s; }
@media (prefers-reduced-motion: reduce) {
.toggle-rack__track * { transition-duration: 0.01s !important; transition-delay: 0s !important; }
}
`;
// Ten teeth around a 15px gear, as one closed path.
const GEAR = (() => {
const pts: string[] = [];
for (let i = 0; i < 10; i++) {
const a = (i * 36 * Math.PI) / 180;
[[-13, 11.6], [-6, 15], [6, 15], [13, 11.6]].forEach(([da, r]) => {
const t = a + (da * Math.PI) / 180;
pts.push(`${(15 + Math.cos(t) * r).toFixed(2)} ${(15 + Math.sin(t) * r).toFixed(2)}`);
});
}
return `M${pts.join('L')}Z`;
})();
export function RackToggle() {
return (
<>
<style>
{css}
</style>
<label className="toggle-rack">
<input className="toggle-rack__input" type="checkbox" role="switch" />
<span className="toggle-rack__track" aria-hidden="true">
<span className="toggle-rack__rack" />
<span className="toggle-rack__end" />
<span className="toggle-rack__gear">
<svg viewBox="0 0 30 30">
<path className="toggle-rack__teeth" d={GEAR} />
<circle className="toggle-rack__hub" cx="15" cy="15" r="7" />
<path className="toggle-rack__spoke" d="M15 9.5v11M9.5 15h11" />
</svg>
</span>
</span>
Drive gear
</label>
</>
);
}Markup where the component goes, CSS in your stylesheet. No JavaScript.
<label class="toggle-rack"><input class="toggle-rack__input" type="checkbox" role="switch"/><span class="toggle-rack__track" aria-hidden="true"><span class="toggle-rack__rack"></span><span class="toggle-rack__end"></span><span class="toggle-rack__gear"><svg viewBox="0 0 30 30"><path class="toggle-rack__teeth" d="M26.30 12.39L29.92 13.43L29.92 16.57L26.30 17.61L25.68 19.53L27.99 22.50L26.15 25.04L22.61 23.75L20.97 24.94L21.10 28.70L18.12 29.67L16.01 26.56L13.99 26.56L11.88 29.67L8.90 28.70L9.03 24.94L7.39 23.75L3.85 25.04L2.01 22.50L4.32 19.53L3.70 17.61L0.08 16.57L0.08 13.43L3.70 12.39L4.32 10.47L2.01 7.50L3.85 4.96L7.39 6.25L9.03 5.06L8.90 1.30L11.88 0.33L13.99 3.44L16.01 3.44L18.12 0.33L21.10 1.30L20.97 5.06L22.61 6.25L26.15 4.96L27.99 7.50L25.68 10.47Z"></path><circle class="toggle-rack__hub" cx="15" cy="15" r="7"></circle><path class="toggle-rack__spoke" d="M15 9.5v11M9.5 15h11"></path></svg></span></span>Drive gear</label>.toggle-rack { position: relative; display: inline-flex; align-items: center; gap: 12px; color: light-dark(#42454c, #e5e6e9); font-size: 14px; cursor: pointer; user-select: none; }
.toggle-rack__input { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0; }
.toggle-rack__track { position: relative; width: 86px; height: 40px; border-radius: 6px; }
.toggle-rack__input:focus-visible + .toggle-rack__track { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 3px; }
/* A toothed rack; the gear's teeth mesh with it as it rolls. */
.toggle-rack__rack {
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: 9px;
border-radius: 0 0 3px 3px;
background:
linear-gradient(light-dark(#cbced3, #393c41), light-dark(#cbced3, #393c41)) 0 100% / 100% 5px no-repeat,
conic-gradient(from 135deg at 50% 0, light-dark(#cbced3, #393c41) 0 90deg, transparent 90deg) 0 0 / 6px 4px repeat-x;
}
.toggle-rack__gear {
position: absolute;
left: 2px;
bottom: 7px;
width: 30px;
height: 30px;
transition: transform 0.75s cubic-bezier(0.65, 0, 0.35, 1);
}
.toggle-rack__gear svg { display: block; width: 30px; height: 30px; }
.toggle-rack__teeth { fill: light-dark(#5f626a, #a7abb2); transition: fill 0.3s; }
.toggle-rack__hub { fill: light-dark(#f3f4f7, #1a1c1f); }
.toggle-rack__spoke { fill: none; stroke: light-dark(#cbced3, #393c41); stroke-width: 2; }
.toggle-rack__end { position: absolute; right: 2px; bottom: 10px; width: 4px; height: 14px; border-radius: 2px; background: light-dark(#dbdde1, #2e3035); transition: background-color 0.3s; }
/* 52px of travel on a 15px radius is 199 degrees of roll. */
.toggle-rack__input:checked + .toggle-rack__track .toggle-rack__gear { transform: translateX(52px) rotate(199deg); }
.toggle-rack__input:checked + .toggle-rack__track .toggle-rack__teeth { fill: light-dark(#373a40, #f1f2f3); }
.toggle-rack__input:checked + .toggle-rack__track .toggle-rack__end { background: light-dark(#373a40, #f1f2f3); transition-delay: 0.6s; }
@media (prefers-reduced-motion: reduce) {
.toggle-rack__track * { transition-duration: 0.01s !important; transition-delay: 0s !important; }
}Goal: Create a "Rack & Pinion" toggle.
Visual behavior: A rack-and-pinion switch: the thumb is a gear that rolls along a toothed rack, turning exactly as far as it travels, and lights up when it reaches the end stop.
Technique:
- A real native checkbox with role="switch" (visually hidden: absolute, 1x1, opacity 0) inside a <label>; every visual is aria-hidden and styled from the input with :checked + sibling selectors.
- The gear is a ten-tooth SVG path generated once; the rack's teeth are a conic-gradient pattern tiled along the bottom.
- On, the gear transitions translateX(52px) rotate(199deg) together: 52px on a 15px radius is 199 degrees of rolling, so the teeth never slip.
Accessibility: A native checkbox with role="switch" inside a <label>, so Space toggles it and screen readers announce a switch; the visible label names it and :focus-visible on the input outlines the visual. Under prefers-reduced-motion the change is instant.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: neutral only. Black, white and grays, no hues. Use the color values above as written, or my project's closest neutral tokens.
Return a single self-contained component. No dependencies.Goal: Create a "Rack & Pinion" toggle.
Visual behavior: A rack-and-pinion switch: the thumb is a gear that rolls along a toothed rack, turning exactly as far as it travels, and lights up when it reaches the end stop.
Technique:
- A real native checkbox with role="switch" (visually hidden: absolute, 1x1, opacity 0) inside a <label>; every visual is aria-hidden and styled from the input with :checked + sibling selectors.
- The gear is a ten-tooth SVG path generated once; the rack's teeth are a conic-gradient pattern tiled along the bottom.
- On, the gear transitions translateX(52px) rotate(199deg) together: 52px on a 15px radius is 199 degrees of rolling, so the teeth never slip.
Accessibility: A native checkbox with role="switch" inside a <label>, so Space toggles it and screen readers announce a switch; the visible label names it and :focus-visible on the input outlines the visual. Under prefers-reduced-motion the change is instant.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: use my project's existing color tokens. The color values above are grayscale placeholders. Map each one by its role (surface, text, border, accent, glow) to my theme, and keep their relative lightness so contrast and the effect still read.
Return a single self-contained component. No dependencies.Paste into Claude, ChatGPT or Cursor after filling in {{USER_STACK}}, {{USER_STYLING}} and {{USER_CONSTRAINTS}}. Neutral keeps the greys you see here; Match my project asks the model to map them onto your own colour tokens.
Free to use in any project, commercial included. MIT-0, no attribution required. Licence
About this component
A rack-and-pinion switch: the thumb is a gear that rolls along a toothed rack, turning exactly as far as it travels, and lights up when it reaches the end stop.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- click, state change
- Techniques
- svg, css transition, conic gradient
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
A native checkbox with role="switch" inside a <label>, so Space toggles it and screen readers announce a switch; the visible label names it and :focus-visible on the input outlines the visual. Under prefers-reduced-motion the change is instant.