Grind
import { useId, useState } from 'react';
const css = `
@keyframes radio-grind-crank-a { from { transform: rotate(0deg); } to { transform: rotate(720deg); } }
@keyframes radio-grind-crank-b { from { transform: rotate(0deg); } to { transform: rotate(720deg); } }
@keyframes radio-grind-pour { 0% { opacity: 0; transform: translateY(0); } 15% { opacity: 1; } 100% { opacity: 0; transform: translateY(26px); } }
@keyframes radio-grind-settle-a { 0% { transform: translateY(-3px); } 100% { transform: none; } }
@keyframes radio-grind-settle-b { 0% { transform: translateY(-3px); } 100% { transform: none; } }
.radio-grind { --radio-grind-s: 2px; display: flex; align-items: center; gap: 18px; }
.radio-grind:has(.radio-grind__input[value='medium']:checked) { --radio-grind-s: 3.6px; }
.radio-grind:has(.radio-grind__input[value='coarse']:checked) { --radio-grind-s: 5.4px; }
.radio-grind__mill { position: relative; width: 64px; height: 104px; }
.radio-grind__hopper { position: absolute; left: 7px; top: 8px; width: 50px; height: 16px; clip-path: polygon(0 0, 100% 0, 82% 100%, 18% 100%); background: light-dark(#cbced3, #393c41); }
.radio-grind__head { position: absolute; left: 12px; top: 24px; width: 40px; height: 24px; border-radius: 4px; background: linear-gradient(light-dark(#6d7179, #7d8087), light-dark(#cbced3, #393c41)); box-shadow: inset 0 0 0 1px light-dark(#5f626a, #a7abb2); }
.radio-grind__crank { position: absolute; left: 44px; top: 32px; width: 0; height: 0; }
.radio-grind__crank::before { content: ''; position: absolute; left: -1.5px; top: -14px; width: 3px; height: 15px; border-radius: 2px; background: light-dark(#50535b, #c6c8cd); }
.radio-grind__crank::after { content: ''; position: absolute; left: -3.5px; top: -18px; width: 7px; height: 7px; border-radius: 50%; background: light-dark(#42454c, #e5e6e9); }
.radio-grind[data-flip='a'] .radio-grind__crank { animation: radio-grind-crank-a 0.9s cubic-bezier(0.4, 0, 0.3, 1); }
.radio-grind[data-flip='b'] .radio-grind__crank { animation: radio-grind-crank-b 0.9s cubic-bezier(0.4, 0, 0.3, 1); }
.radio-grind__chute { position: absolute; left: 28px; top: 48px; width: 8px; height: 6px; background: light-dark(#dbdde1, #2e3035); }
.radio-grind__drop { position: absolute; left: 30px; top: 54px; width: 3px; height: 3px; border-radius: 50%; background: light-dark(#5f626a, #a7abb2); opacity: 0; }
.radio-grind[data-flip] .radio-grind__drop { animation: radio-grind-pour 0.35s linear var(--d) 2; }
.radio-grind__cup {
position: absolute;
left: 9px;
bottom: 0;
width: 46px;
height: 30px;
overflow: hidden;
border: 2px solid light-dark(#6d7179, #7d8087);
border-top: 0;
border-radius: 0 0 9px 9px;
}
.radio-grind__pile { position: absolute; inset: 0; }
.radio-grind[data-flip='a'] .radio-grind__pile { animation: radio-grind-settle-a 0.4s ease-out 0.5s backwards; }
.radio-grind[data-flip='b'] .radio-grind__pile { animation: radio-grind-settle-b 0.4s ease-out 0.5s backwards; }
.radio-grind__bit {
position: absolute;
width: var(--radio-grind-s);
height: var(--radio-grind-s);
margin: calc(var(--radio-grind-s) / -2) 0 0 calc(var(--radio-grind-s) / -2);
border-radius: 35%;
background: light-dark(#5f626a, #a7abb2);
transform: rotate(var(--r));
transition: width 0.4s ease calc(var(--i) * 12ms), height 0.4s ease calc(var(--i) * 12ms), margin 0.4s ease calc(var(--i) * 12ms);
}
.radio-grind__bit:nth-child(3n) { background: light-dark(#6d7179, #7d8087); }
.radio-grind__bit:nth-child(4n) { background: light-dark(#50535b, #c6c8cd); }
.radio-grind__opts { display: grid; gap: 2px; }
.radio-grind__opt {
position: relative;
padding: 4px 10px;
border-radius: 6px;
color: light-dark(#5f626a, #a7abb2);
font-size: 12px;
font-weight: 600;
cursor: pointer;
transition: color 0.2s, background-color 0.2s;
}
.radio-grind__opt:hover { color: light-dark(#42454c, #e5e6e9); }
.radio-grind__input { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0; }
.radio-grind__opt:has(.radio-grind__input:focus-visible) { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 1px; }
.radio-grind__opt:has(.radio-grind__input:checked) { background: light-dark(#ebedf0, #232529); color: light-dark(#373a40, #f1f2f3); }
@media (prefers-reduced-motion: reduce) {
.radio-grind * { animation: none !important; transition-duration: 0.01s !important; transition-delay: 0s !important; }
}
`;
// A mound of grounds in the cup: fixed positions, so the server and the browser agree.
const BITS = Array.from({ length: 34 }, (_, i) => {
const x = 4 + ((i * 13.7) % 38);
const h = 15 * (1 - ((x - 23) / 22) ** 2);
const y = 30 - ((i * 7.3) % 1) * h;
return { x: Number(x.toFixed(1)), y: Number(y.toFixed(1)), r: (i * 37) % 90 };
});
const GRINDS = [
{ value: 'fine', label: 'Fine' },
{ value: 'medium', label: 'Medium' },
{ value: 'coarse', label: 'Coarse' },
];
export function GrindRadio() {
const name = useId();
const [flip, setFlip] = useState(0);
return (
<>
<style>
{css}
</style>
<div className="radio-grind" data-flip={flip ? (flip % 2 ? 'a' : 'b') : undefined} onChange={() => setFlip((f) => f + 1)}>
<span className="radio-grind__mill" aria-hidden="true">
<span className="radio-grind__hopper" />
<span className="radio-grind__head" />
<span className="radio-grind__crank" />
<span className="radio-grind__chute" />
{[0, 0.12, 0.24].map((d) => (
<span key={d} className="radio-grind__drop" style={{ '--d': `${d}s` } as React.CSSProperties} />
))}
<span className="radio-grind__cup">
<span className="radio-grind__pile">
{BITS.map((b, i) => (
<span key={i} className="radio-grind__bit" style={{ left: b.x, top: b.y, '--r': `${b.r}deg`, '--i': i } as React.CSSProperties} />
))}
</span>
</span>
</span>
<div className="radio-grind__opts" role="radiogroup" aria-label="Grind">
{GRINDS.map((g, i) => (
<label key={g.value} className="radio-grind__opt">
<input className="radio-grind__input" type="radio" name={name} value={g.value} defaultChecked={i === 0} />
{g.label}
</label>
))}
</div>
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initRadioGrind(el) on the mounted element instead.
<div class="radio-grind">
<span class="radio-grind__mill" aria-hidden="true">
<span class="radio-grind__hopper"></span>
<span class="radio-grind__head"></span>
<span class="radio-grind__crank"></span>
<span class="radio-grind__chute"></span>
<span class="radio-grind__drop" style="--d:0s"></span>
<span class="radio-grind__drop" style="--d:0.12s"></span>
<span class="radio-grind__drop" style="--d:0.24s"></span>
<span class="radio-grind__cup">
<span class="radio-grind__pile">
<span class="radio-grind__bit" style="left:4px;top:30px;--r:0deg;--i:0"></span>
<span class="radio-grind__bit" style="left:17.7px;top:25.8px;--r:37deg;--i:1"></span>
<span class="radio-grind__bit" style="left:31.4px;top:22.3px;--r:74deg;--i:2"></span>
<span class="radio-grind__bit" style="left:7.1px;top:23.6px;--r:21deg;--i:3"></span>
<span class="radio-grind__bit" style="left:20.8px;top:27px;--r:58deg;--i:4"></span>
<span class="radio-grind__bit" style="left:34.5px;top:24.5px;--r:5deg;--i:5"></span>
<span class="radio-grind__bit" style="left:10.2px;top:22.1px;--r:42deg;--i:6"></span>
<span class="radio-grind__bit" style="left:23.9px;top:28.5px;--r:79deg;--i:7"></span>
<span class="radio-grind__bit" style="left:37.6px;top:26.6px;--r:26deg;--i:8"></span>
<span class="radio-grind__bit" style="left:13.3px;top:21.5px;--r:63deg;--i:9"></span>
<span class="radio-grind__bit" style="left:27px;top:30px;--r:10deg;--i:10"></span>
<span class="radio-grind__bit" style="left:40.7px;top:28.4px;--r:47deg;--i:11"></span>
<span class="radio-grind__bit" style="left:16.4px;top:21.8px;--r:84deg;--i:12"></span>
<span class="radio-grind__bit" style="left:30.1px;top:17.9px;--r:31deg;--i:13"></span>
<span class="radio-grind__bit" style="left:5.8px;top:28.8px;--r:68deg;--i:14"></span>
<span class="radio-grind__bit" style="left:19.5px;top:22.7px;--r:15deg;--i:15"></span>
<span class="radio-grind__bit" style="left:33.2px;top:20.6px;--r:52deg;--i:16"></span>
<span class="radio-grind__bit" style="left:8.9px;top:29.1px;--r:89deg;--i:17"></span>
<span class="radio-grind__bit" style="left:22.6px;top:24px;--r:36deg;--i:18"></span>
<span class="radio-grind__bit" style="left:36.3px;top:23.3px;--r:73deg;--i:19"></span>
<span class="radio-grind__bit" style="left:12px;top:30px;--r:20deg;--i:20"></span>
<span class="radio-grind__bit" style="left:25.7px;top:25.6px;--r:57deg;--i:21"></span>
<span class="radio-grind__bit" style="left:39.4px;top:26px;--r:4deg;--i:22"></span>
<span class="radio-grind__bit" style="left:15.1px;top:18.2px;--r:41deg;--i:23"></span>
<span class="radio-grind__bit" style="left:28.8px;top:27.2px;--r:78deg;--i:24"></span>
<span class="radio-grind__bit" style="left:4.5px;top:27.8px;--r:25deg;--i:25"></span>
<span class="radio-grind__bit" style="left:18.2px;top:18.6px;--r:62deg;--i:26"></span>
<span class="radio-grind__bit" style="left:31.9px;top:28.7px;--r:9deg;--i:27"></span>
<span class="radio-grind__bit" style="left:7.6px;top:26.9px;--r:46deg;--i:28"></span>
<span class="radio-grind__bit" style="left:21.3px;top:19.6px;--r:83deg;--i:29"></span>
<span class="radio-grind__bit" style="left:35px;top:30px;--r:30deg;--i:30"></span>
<span class="radio-grind__bit" style="left:10.7px;top:26.9px;--r:67deg;--i:31"></span>
<span class="radio-grind__bit" style="left:24.4px;top:21px;--r:14deg;--i:32"></span>
<span class="radio-grind__bit" style="left:38.1px;top:22.9px;--r:51deg;--i:33"></span>
</span>
</span>
</span>
<div class="radio-grind__opts" role="radiogroup" aria-label="Grind">
<label class="radio-grind__opt"><input class="radio-grind__input" type="radio" name="radio-grind" checked="" value="fine"/>Fine</label>
<label class="radio-grind__opt"><input class="radio-grind__input" type="radio" name="radio-grind" value="medium"/>Medium</label>
<label class="radio-grind__opt"><input class="radio-grind__input" type="radio" name="radio-grind" value="coarse"/>Coarse</label>
</div>
</div>@keyframes radio-grind-crank-a { from { transform: rotate(0deg); } to { transform: rotate(720deg); } }
@keyframes radio-grind-crank-b { from { transform: rotate(0deg); } to { transform: rotate(720deg); } }
@keyframes radio-grind-pour { 0% { opacity: 0; transform: translateY(0); } 15% { opacity: 1; } 100% { opacity: 0; transform: translateY(26px); } }
@keyframes radio-grind-settle-a { 0% { transform: translateY(-3px); } 100% { transform: none; } }
@keyframes radio-grind-settle-b { 0% { transform: translateY(-3px); } 100% { transform: none; } }
.radio-grind { --radio-grind-s: 2px; display: flex; align-items: center; gap: 18px; }
.radio-grind:has(.radio-grind__input[value='medium']:checked) { --radio-grind-s: 3.6px; }
.radio-grind:has(.radio-grind__input[value='coarse']:checked) { --radio-grind-s: 5.4px; }
.radio-grind__mill { position: relative; width: 64px; height: 104px; }
.radio-grind__hopper { position: absolute; left: 7px; top: 8px; width: 50px; height: 16px; clip-path: polygon(0 0, 100% 0, 82% 100%, 18% 100%); background: light-dark(#cbced3, #393c41); }
.radio-grind__head { position: absolute; left: 12px; top: 24px; width: 40px; height: 24px; border-radius: 4px; background: linear-gradient(light-dark(#6d7179, #7d8087), light-dark(#cbced3, #393c41)); box-shadow: inset 0 0 0 1px light-dark(#5f626a, #a7abb2); }
.radio-grind__crank { position: absolute; left: 44px; top: 32px; width: 0; height: 0; }
.radio-grind__crank::before { content: ''; position: absolute; left: -1.5px; top: -14px; width: 3px; height: 15px; border-radius: 2px; background: light-dark(#50535b, #c6c8cd); }
.radio-grind__crank::after { content: ''; position: absolute; left: -3.5px; top: -18px; width: 7px; height: 7px; border-radius: 50%; background: light-dark(#42454c, #e5e6e9); }
.radio-grind[data-flip='a'] .radio-grind__crank { animation: radio-grind-crank-a 0.9s cubic-bezier(0.4, 0, 0.3, 1); }
.radio-grind[data-flip='b'] .radio-grind__crank { animation: radio-grind-crank-b 0.9s cubic-bezier(0.4, 0, 0.3, 1); }
.radio-grind__chute { position: absolute; left: 28px; top: 48px; width: 8px; height: 6px; background: light-dark(#dbdde1, #2e3035); }
.radio-grind__drop { position: absolute; left: 30px; top: 54px; width: 3px; height: 3px; border-radius: 50%; background: light-dark(#5f626a, #a7abb2); opacity: 0; }
.radio-grind[data-flip] .radio-grind__drop { animation: radio-grind-pour 0.35s linear var(--d) 2; }
.radio-grind__cup {
position: absolute;
left: 9px;
bottom: 0;
width: 46px;
height: 30px;
overflow: hidden;
border: 2px solid light-dark(#6d7179, #7d8087);
border-top: 0;
border-radius: 0 0 9px 9px;
}
.radio-grind__pile { position: absolute; inset: 0; }
.radio-grind[data-flip='a'] .radio-grind__pile { animation: radio-grind-settle-a 0.4s ease-out 0.5s backwards; }
.radio-grind[data-flip='b'] .radio-grind__pile { animation: radio-grind-settle-b 0.4s ease-out 0.5s backwards; }
.radio-grind__bit {
position: absolute;
width: var(--radio-grind-s);
height: var(--radio-grind-s);
margin: calc(var(--radio-grind-s) / -2) 0 0 calc(var(--radio-grind-s) / -2);
border-radius: 35%;
background: light-dark(#5f626a, #a7abb2);
transform: rotate(var(--r));
transition: width 0.4s ease calc(var(--i) * 12ms), height 0.4s ease calc(var(--i) * 12ms), margin 0.4s ease calc(var(--i) * 12ms);
}
.radio-grind__bit:nth-child(3n) { background: light-dark(#6d7179, #7d8087); }
.radio-grind__bit:nth-child(4n) { background: light-dark(#50535b, #c6c8cd); }
.radio-grind__opts { display: grid; gap: 2px; }
.radio-grind__opt {
position: relative;
padding: 4px 10px;
border-radius: 6px;
color: light-dark(#5f626a, #a7abb2);
font-size: 12px;
font-weight: 600;
cursor: pointer;
transition: color 0.2s, background-color 0.2s;
}
.radio-grind__opt:hover { color: light-dark(#42454c, #e5e6e9); }
.radio-grind__input { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0; }
.radio-grind__opt:has(.radio-grind__input:focus-visible) { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 1px; }
.radio-grind__opt:has(.radio-grind__input:checked) { background: light-dark(#ebedf0, #232529); color: light-dark(#373a40, #f1f2f3); }
@media (prefers-reduced-motion: reduce) {
.radio-grind * { animation: none !important; transition-duration: 0.01s !important; transition-delay: 0s !important; }
}function initRadioGrind(root) {
var flip = 0;
// Each change flips data-flip between a and b, so the keyframes restart every time.
function onChange() {
flip += 1;
root.setAttribute('data-flip', flip % 2 ? 'a' : 'b');
}
root.addEventListener('change', onChange);
return function () { root.removeEventListener('change', onChange); };
}
document.querySelectorAll('.radio-grind').forEach(initRadioGrind);Goal: Create a "Grind" radio.
Visual behavior: A coffee grind picker: choose fine, medium or coarse and the crank turns, grounds pour from the chute, and the pile in the cup settles finer or coarser.
Technique:
- Real native radios (visually hidden: absolute, 1x1, opacity 0) inside <label>s; the scene is aria-hidden and every state is read from the checked input with :has().
- Thirty-four grounds sit at fixed positions in a mound; their size comes from one variable set with :has() and changes with a small per-ground delay.
- Effects that should replay on every change (rather than run once) alternate between two identically written keyframes, chosen by a data-flip attribute that flips between a and b on each change, so the browser restarts them.
Accessibility: A role="radiogroup" with an aria-label around native radio inputs sharing one name, so the arrow keys move the choice and Tab enters the group once; :focus-visible on the input outlines its option. Under prefers-reduced-motion the grounds change size without the crank or the pour.
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 "Grind" radio.
Visual behavior: A coffee grind picker: choose fine, medium or coarse and the crank turns, grounds pour from the chute, and the pile in the cup settles finer or coarser.
Technique:
- Real native radios (visually hidden: absolute, 1x1, opacity 0) inside <label>s; the scene is aria-hidden and every state is read from the checked input with :has().
- Thirty-four grounds sit at fixed positions in a mound; their size comes from one variable set with :has() and changes with a small per-ground delay.
- Effects that should replay on every change (rather than run once) alternate between two identically written keyframes, chosen by a data-flip attribute that flips between a and b on each change, so the browser restarts them.
Accessibility: A role="radiogroup" with an aria-label around native radio inputs sharing one name, so the arrow keys move the choice and Tab enters the group once; :focus-visible on the input outlines its option. Under prefers-reduced-motion the grounds change size without the crank or the pour.
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 coffee grind picker: choose fine, medium or coarse and the crank turns, grounds pour from the chute, and the pile in the cup settles finer or coarser.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- click, state change, delay stagger
- Techniques
- css variables, keyframes, transition delay stagger
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
A role="radiogroup" with an aria-label around native radio inputs sharing one name, so the arrow keys move the choice and Tab enters the group once; :focus-visible on the input outlines its option. Under prefers-reduced-motion the grounds change size without the crank or the pour.