Stepper with Hold to Repeat
Plus and minus buttons that repeat while held, accelerate the longer you hold, and respect minimum and maximum limits.
AI prompt
Clean UI mockup of a quantity stepper control, a rounded pill with a minus button on the left, a large centred number 24 in an editable field, and a plus button on the right, the plus button shown pressed with a subtle darker fill, a small caption below reading Hold to change quickly, a second state where the minus button is disabled and greyed at the minimum value 0, light theme, flat modern style. Negative: heavy shadows, gradients, text errors, clutter. Reduced motion: static design.About this pattern
Definition
A stepper changes a number with plus and minus buttons. For wide ranges, holding a button should keep stepping, slowly at first and then faster, so people can move quickly without typing. Limits disable the buttons, and the number stays directly editable for exact values.
Purpose & UX principles
- A single press changes by one; holding starts repeating after about 400 ms.
- Accelerate in stages, for example every 120 ms, then every 60 ms, then every 30 ms with a larger step.
- Stop on release, on leaving the button and at the limits.
- Keep a number input in the middle with an accessible name and role spinbutton behaviour.
Interaction states
- A quantity stepper from 0 to 200 with hold to repeat and editable number.
Motion specification
- The number updates in real time while holding.
- Reduced motion: repeat is still available because it is user-driven, but the value flash is off.
Responsive behaviour
- Support keyboard: arrow keys step, Page Up and Page Down step by ten.
- Announce the value change politely; avoid announcing every repeat step.
Code
<div class="bx"><div class="sp" id="sp"><button type="button" class="b" id="mn" aria-label="Decrease">−</button><input id="nv" type="text" inputmode="numeric" role="spinbutton" aria-label="Quantity" aria-valuemin="0" aria-valuemax="200" aria-valuenow="24" value="24"><button type="button" class="b" id="pl" aria-label="Increase">+</button></div><p class="out" id="out"></p></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.sp{display:flex;align-items:center;justify-content:center;gap:6px;padding:22px 0;border:1px solid var(--line);border-radius:12px;background:var(--surface)}.b{width:46px;height:46px;border-radius:50%;border:1.5px solid #c9c9c4;background:#fff;font-size:24px;line-height:1;user-select:none;touch-action:manipulation}.b:active{background:#171717;color:#fff;border-color:#171717}.b:disabled{opacity:.35;cursor:default}#nv{width:84px;height:46px;border:1px solid var(--line);border-radius:12px;text-align:center;font:700 22px system-ui,sans-serif;background:#fff;color:var(--text)}.flash{animation:fl .2s}@keyframes fl{from{background:#e8edff}}.out{font-size:12px;color:var(--muted)}@media (prefers-reduced-motion:reduce){.flash{animation:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const nv=document.getElementById('nv'),mn=document.getElementById('mn'),pl=document.getElementById('pl'),out=document.getElementById('out'),MIN=0,MAX=200;let v=24,tm=null,t0=0;
function set(n){v=Math.max(MIN,Math.min(MAX,n));nv.value=v;nv.setAttribute('aria-valuenow',v);mn.disabled=v<=MIN;pl.disabled=v>=MAX;nv.classList.remove('flash');void nv.offsetWidth;nv.classList.add('flash');out.textContent=v<=MIN?'Minimum reached.':v>=MAX?'Maximum reached.':'Hold a button to change quickly.';}
function hold(dir){set(v+dir);t0=performance.now();(function step(){const el=performance.now()-t0,gap=el<1200?120:el<2500?60:30,inc=el<2500?1:5;tm=setTimeout(()=>{set(v+dir*inc);if((dir>0&&v<MAX)||(dir<0&&v>MIN))step();},el===0?400:gap);})();}
function stop(){clearTimeout(tm);}
[[mn,-1],[pl,1]].forEach(([b,d])=>{b.addEventListener('pointerdown',e=>{e.preventDefault();b.setPointerCapture(e.pointerId);hold(d);});['pointerup','pointercancel','pointerleave'].forEach(t=>b.addEventListener(t,stop));b.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===' '){e.preventDefault();if(!e.repeat)set(v+d);else set(v+d);}});});
nv.addEventListener('change',()=>{const n=parseInt(nv.value,10);set(isNaN(n)?v:n);});nv.addEventListener('keydown',e=>{if(e.key==='ArrowUp'){e.preventDefault();set(v+1);}else if(e.key==='ArrowDown'){e.preventDefault();set(v-1);}else if(e.key==='PageUp'){e.preventDefault();set(v+10);}else if(e.key==='PageDown'){e.preventDefault();set(v-10);}});set(v);One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Stepper with Hold to Repeat — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.sp{display:flex;align-items:center;justify-content:center;gap:6px;padding:22px 0;border:1px solid var(--line);border-radius:12px;background:var(--surface)}.b{width:46px;height:46px;border-radius:50%;border:1.5px solid #c9c9c4;background:#fff;font-size:24px;line-height:1;user-select:none;touch-action:manipulation}.b:active{background:#171717;color:#fff;border-color:#171717}.b:disabled{opacity:.35;cursor:default}#nv{width:84px;height:46px;border:1px solid var(--line);border-radius:12px;text-align:center;font:700 22px system-ui,sans-serif;background:#fff;color:var(--text)}.flash{animation:fl .2s}@keyframes fl{from{background:#e8edff}}.out{font-size:12px;color:var(--muted)}@media (prefers-reduced-motion:reduce){.flash{animation:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="sp" id="sp"><button type="button" class="b" id="mn" aria-label="Decrease">−</button><input id="nv" type="text" inputmode="numeric" role="spinbutton" aria-label="Quantity" aria-valuemin="0" aria-valuemax="200" aria-valuenow="24" value="24"><button type="button" class="b" id="pl" aria-label="Increase">+</button></div><p class="out" id="out"></p></div>
</div>
<script>
const nv=document.getElementById('nv'),mn=document.getElementById('mn'),pl=document.getElementById('pl'),out=document.getElementById('out'),MIN=0,MAX=200;let v=24,tm=null,t0=0;
function set(n){v=Math.max(MIN,Math.min(MAX,n));nv.value=v;nv.setAttribute('aria-valuenow',v);mn.disabled=v<=MIN;pl.disabled=v>=MAX;nv.classList.remove('flash');void nv.offsetWidth;nv.classList.add('flash');out.textContent=v<=MIN?'Minimum reached.':v>=MAX?'Maximum reached.':'Hold a button to change quickly.';}
function hold(dir){set(v+dir);t0=performance.now();(function step(){const el=performance.now()-t0,gap=el<1200?120:el<2500?60:30,inc=el<2500?1:5;tm=setTimeout(()=>{set(v+dir*inc);if((dir>0&&v<MAX)||(dir<0&&v>MIN))step();},el===0?400:gap);})();}
function stop(){clearTimeout(tm);}
[[mn,-1],[pl,1]].forEach(([b,d])=>{b.addEventListener('pointerdown',e=>{e.preventDefault();b.setPointerCapture(e.pointerId);hold(d);});['pointerup','pointercancel','pointerleave'].forEach(t=>b.addEventListener(t,stop));b.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===' '){e.preventDefault();if(!e.repeat)set(v+d);else set(v+d);}});});
nv.addEventListener('change',()=>{const n=parseInt(nv.value,10);set(isNaN(n)?v:n);});nv.addEventListener('keydown',e=>{if(e.key==='ArrowUp'){e.preventDefault();set(v+1);}else if(e.key==='ArrowDown'){e.preventDefault();set(v-1);}else if(e.key==='PageUp'){e.preventDefault();set(v+10);}else if(e.key==='PageDown'){e.preventDefault();set(v-10);}});set(v);
</script>
</body></html>