Long Press Menu
Press and hold a tile to open a context menu, with visible progress while holding.
AI prompt
Create a 120x120px tile labelled "Photo 01". Use pointer events: on pointerdown start a 500ms timer and animate a circular progress ring around the pointer-down area (an SVG circle with stroke-dashoffset animating over 500ms linear). If the pointer is released earlier or moves more than 10px, cancel and reset the ring. When the timer completes, open a menu with the actions Share, Rename and Delete positioned next to the tile; the menu fades and scales from 0.96 in 140ms. Prevent the native context menu with the contextmenu event, and also open the menu when the ContextMenu key or Shift+F10 is pressed while the tile is focused. Menu items are buttons; arrow keys move between them, Escape closes and returns focus to the tile. Selecting an item shows "Share chosen" etc. in an aria-live line. Clicking outside closes the menu. Under prefers-reduced-motion remove the ring and animation.About this pattern
Definition
A hold gesture: pressing an element for about half a second opens a menu of actions for it. A growing ring shows the hold progress so the gesture feels deliberate.
Purpose & UX principles
- Reveals secondary actions without cluttering the interface.
- Visible progress teaches the gesture and lets people cancel by releasing early.
- Keyboard equivalent: the Menu key or Shift+F10 opens the same menu.
Interaction states
- Idle, holding (ring grows), open (menu shown, tile highlighted), cancelled, focus-visible.
Motion specification
- The ring fills over 500ms linear, matching the hold time.
- The menu fades and scales from 0.96 in 140ms.
- Reduced motion: no ring animation; the menu appears instantly.
Responsive behaviour
- Works with touch and mouse; the hold time is 500ms and a movement over 10px cancels it.
- The menu opens within the viewport, flipping above when near the bottom.
Code
<div class="wrap">
<button class="tile" id="tile" aria-haspopup="menu">Photo 01<svg class="ring" viewBox="0 0 40 40" aria-hidden="true"><circle cx="20" cy="20" r="17"/></svg></button>
<div class="menu" id="menu" role="menu" hidden>
<button role="menuitem">Share</button><button role="menuitem">Rename</button><button role="menuitem">Delete</button>
</div>
<p class="live" id="live" aria-live="polite">Press and hold the tile.</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}
.wrap{position:relative;display:grid;justify-items:center;gap:12px}
.tile{position:relative;width:120px;height:120px;border-radius:16px;border:1px solid var(--line);background:linear-gradient(135deg,#e4e4e0,#c9c9c4);font:inherit;font-size:13px;font-weight:600;cursor:pointer;user-select:none;-webkit-user-select:none;touch-action:none;-webkit-touch-callout:none;transition:transform .15s}
.tile:active{transform:scale(.97)}
.tile:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
.ring{position:absolute;width:48px;height:48px;left:calc(50% - 24px);top:calc(50% - 24px);transform:rotate(-90deg);opacity:0;pointer-events:none}
.ring circle{fill:none;stroke:var(--accent);stroke-width:3;stroke-linecap:round;stroke-dasharray:107;stroke-dashoffset:107}
.holding .ring{opacity:1}
.holding .ring circle{stroke-dashoffset:0;transition:stroke-dashoffset .5s linear}
.menu{position:absolute;left:calc(50% + 70px);top:0;min-width:130px;padding:5px;background:var(--surface);border:1px solid var(--line);border-radius:12px;box-shadow:0 12px 28px rgba(0,0,0,.14);animation:in .14s ease-out;transform-origin:left top;z-index:3}
.menu[hidden]{display:none}
.menu button{display:block;width:100%;text-align:left;padding:8px 12px;border:0;border-radius:8px;background:none;font:inherit;font-size:13px;cursor:pointer}
.menu button:hover,.menu button:focus{background:var(--bg);outline:none}
@keyframes in{from{opacity:0;transform:scale(.96)}}
.live{font-size:12px;color:var(--muted)}
@media (prefers-reduced-motion:reduce){.ring{display:none}.menu{animation:none}.tile{transition:none}}const tile=document.getElementById('tile'),menu=document.getElementById('menu'),live=document.getElementById('live');
let timer=0,sx=0,sy=0;
const items=()=>[...menu.querySelectorAll('button')];
function open(){menu.hidden=false;tile.setAttribute('aria-expanded','true');items()[0].focus();live.textContent='Menu open';}
function close(f){menu.hidden=true;tile.setAttribute('aria-expanded','false');if(f)tile.focus();}
function cancel(){clearTimeout(timer);tile.classList.remove('holding');}
tile.addEventListener('pointerdown',e=>{if(!menu.hidden)return;sx=e.clientX;sy=e.clientY;tile.classList.add('holding');timer=setTimeout(()=>{tile.classList.remove('holding');open();},500);});
tile.addEventListener('pointermove',e=>{if(Math.hypot(e.clientX-sx,e.clientY-sy)>10)cancel();});
['pointerup','pointercancel','pointerleave'].forEach(t=>tile.addEventListener(t,cancel));
tile.addEventListener('contextmenu',e=>{e.preventDefault();});
tile.addEventListener('keydown',e=>{if(e.key==='ContextMenu'||(e.shiftKey&&e.key==='F10')){e.preventDefault();open();}});
menu.addEventListener('keydown',e=>{const i=items(),k=i.indexOf(document.activeElement);if(e.key==='Escape')close(true);else if(e.key==='ArrowDown'){e.preventDefault();i[(k+1)%i.length].focus();}else if(e.key==='ArrowUp'){e.preventDefault();i[(k-1+i.length)%i.length].focus();}});
menu.addEventListener('click',e=>{const b=e.target.closest('button');if(!b)return;live.textContent=b.textContent+' chosen';close(true);});
document.addEventListener('pointerdown',e=>{if(!menu.hidden&&!menu.contains(e.target)&&e.target!==tile)close(false);});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>Long Press Menu — 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}}
.wrap{position:relative;display:grid;justify-items:center;gap:12px}
.tile{position:relative;width:120px;height:120px;border-radius:16px;border:1px solid var(--line);background:linear-gradient(135deg,#e4e4e0,#c9c9c4);font:inherit;font-size:13px;font-weight:600;cursor:pointer;user-select:none;-webkit-user-select:none;touch-action:none;-webkit-touch-callout:none;transition:transform .15s}
.tile:active{transform:scale(.97)}
.tile:focus-visible{outline:2px solid var(--accent);outline-offset:3px}
.ring{position:absolute;width:48px;height:48px;left:calc(50% - 24px);top:calc(50% - 24px);transform:rotate(-90deg);opacity:0;pointer-events:none}
.ring circle{fill:none;stroke:var(--accent);stroke-width:3;stroke-linecap:round;stroke-dasharray:107;stroke-dashoffset:107}
.holding .ring{opacity:1}
.holding .ring circle{stroke-dashoffset:0;transition:stroke-dashoffset .5s linear}
.menu{position:absolute;left:calc(50% + 70px);top:0;min-width:130px;padding:5px;background:var(--surface);border:1px solid var(--line);border-radius:12px;box-shadow:0 12px 28px rgba(0,0,0,.14);animation:in .14s ease-out;transform-origin:left top;z-index:3}
.menu[hidden]{display:none}
.menu button{display:block;width:100%;text-align:left;padding:8px 12px;border:0;border-radius:8px;background:none;font:inherit;font-size:13px;cursor:pointer}
.menu button:hover,.menu button:focus{background:var(--bg);outline:none}
@keyframes in{from{opacity:0;transform:scale(.96)}}
.live{font-size:12px;color:var(--muted)}
@media (prefers-reduced-motion:reduce){.ring{display:none}.menu{animation:none}.tile{transition:none}}
</style></head>
<body><div class="stage">
<div class="wrap">
<button class="tile" id="tile" aria-haspopup="menu">Photo 01<svg class="ring" viewBox="0 0 40 40" aria-hidden="true"><circle cx="20" cy="20" r="17"/></svg></button>
<div class="menu" id="menu" role="menu" hidden>
<button role="menuitem">Share</button><button role="menuitem">Rename</button><button role="menuitem">Delete</button>
</div>
<p class="live" id="live" aria-live="polite">Press and hold the tile.</p>
</div>
</div>
<script>
const tile=document.getElementById('tile'),menu=document.getElementById('menu'),live=document.getElementById('live');
let timer=0,sx=0,sy=0;
const items=()=>[...menu.querySelectorAll('button')];
function open(){menu.hidden=false;tile.setAttribute('aria-expanded','true');items()[0].focus();live.textContent='Menu open';}
function close(f){menu.hidden=true;tile.setAttribute('aria-expanded','false');if(f)tile.focus();}
function cancel(){clearTimeout(timer);tile.classList.remove('holding');}
tile.addEventListener('pointerdown',e=>{if(!menu.hidden)return;sx=e.clientX;sy=e.clientY;tile.classList.add('holding');timer=setTimeout(()=>{tile.classList.remove('holding');open();},500);});
tile.addEventListener('pointermove',e=>{if(Math.hypot(e.clientX-sx,e.clientY-sy)>10)cancel();});
['pointerup','pointercancel','pointerleave'].forEach(t=>tile.addEventListener(t,cancel));
tile.addEventListener('contextmenu',e=>{e.preventDefault();});
tile.addEventListener('keydown',e=>{if(e.key==='ContextMenu'||(e.shiftKey&&e.key==='F10')){e.preventDefault();open();}});
menu.addEventListener('keydown',e=>{const i=items(),k=i.indexOf(document.activeElement);if(e.key==='Escape')close(true);else if(e.key==='ArrowDown'){e.preventDefault();i[(k+1)%i.length].focus();}else if(e.key==='ArrowUp'){e.preventDefault();i[(k-1+i.length)%i.length].focus();}});
menu.addEventListener('click',e=>{const b=e.target.closest('button');if(!b)return;live.textContent=b.textContent+' chosen';close(true);});
document.addEventListener('pointerdown',e=>{if(!menu.hidden&&!menu.contains(e.target)&&e.target!==tile)close(false);});
</script>
</body></html>