Dropdown Menu
An action menu with full keyboard support, typeahead and outside-click closing.
AI prompt
Build a menu button "Options" with a dropdown menu of five items: Rename, Duplicate, Move to folder (disabled), Share and Delete (danger). The trigger has aria-haspopup="menu", aria-expanded and aria-controls; the menu is role="menu" with role="menuitem" children. Open with click, Enter, Space or ArrowDown (focus the first enabled item); ArrowUp and ArrowDown move focus skipping disabled items; Home and End jump; typing a letter focuses the next item starting with it; Escape closes and returns focus to the trigger; Tab closes; clicking outside closes. Selecting an item shows "Chose: <name>" below and closes the menu. The menu opens below with an 8px gap, flips above if it would overflow the demo container, and animates scale 0.96 to 1 plus fade over 140ms. Use a roving tabindex on the items. Reduced motion: opacity only.About this pattern
Definition
A button that opens a list of actions below it. Choosing an item runs the action and closes the menu. It follows the WAI-ARIA menu button pattern.
Purpose & UX principles
- Groups secondary actions behind one control to keep the interface calm.
- Use a native
selectfor choosing a value in a form; this pattern is for actions ("Rename", "Duplicate", "Delete"). - Put destructive actions last and visually distinct.
Interaction states
- Closed, open, item hover and active (keyboard-highlighted), disabled item.
- Trigger:
aria-haspopup="menu",aria-expanded,aria-controls. - Keys: Enter, Space or Down opens; Up and Down move; Home and End jump; typing a letter jumps to a matching item; Escape closes and returns focus to the trigger; Tab closes.
Motion specification
- Menu: scale from 0.96 (origin top-left) and fade, 140ms ease-out.
- Highlight moves instantly; no transition, so keyboard navigation feels immediate.
- Reduced motion: opacity only.
Responsive behaviour
- The menu opens below the trigger and flips upward if there is not enough room.
- On phones consider an action sheet that slides up from the bottom, with the same items.
Code
<div class="area" id="area">
<div class="wrap">
<button class="trig" id="trig" aria-haspopup="menu" aria-expanded="false" aria-controls="menu">Options ▾</button>
<ul class="menu" id="menu" role="menu" aria-label="Options" hidden>
<li role="menuitem" tabindex="-1">Rename</li><li role="menuitem" tabindex="-1">Duplicate</li>
<li role="menuitem" tabindex="-1" aria-disabled="true">Move to folder</li><li role="menuitem" tabindex="-1">Share</li>
<li role="separator"></li><li role="menuitem" tabindex="-1" class="danger">Delete</li>
</ul>
</div>
<p id="out" role="status">Choose an option</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}
.area{position:relative;width:min(380px,100%);height:300px;border:1px solid var(--line);border-radius:18px;background:var(--surface);padding:24px;display:flex;flex-direction:column;align-items:center;gap:60px}
.wrap{position:relative}
.trig{height:44px;padding:0 18px;border-radius:12px;border:1px solid var(--line);background:var(--surface-2);color:var(--text);font:inherit;font-weight:600;cursor:pointer}
.trig:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.menu{position:absolute;left:0;top:calc(100% + 8px);min-width:200px;list-style:none;padding:6px;border-radius:14px;border:1px solid var(--line);background:var(--surface-2);box-shadow:0 14px 32px rgba(0,0,0,.14);z-index:3;transform-origin:top left;animation:in .14s ease-out}
.menu.up{top:auto;bottom:calc(100% + 8px);transform-origin:bottom left}
[role=menuitem]{padding:10px 12px;border-radius:8px;font-size:14px;cursor:pointer;outline:none}
[role=menuitem]:hover,[role=menuitem]:focus{background:rgba(0,0,0,.06)}
[role=menuitem][aria-disabled=true]{opacity:.4;cursor:not-allowed}
[role=menuitem][aria-disabled=true]:hover{background:none}
[role=separator]{height:1px;margin:6px 4px;background:var(--line)}
.danger{color:var(--danger)}
#out{color:var(--muted);font-size:14px}
@keyframes in{from{opacity:0;transform:scale(.96)}}
@media (prefers-reduced-motion:reduce){.menu{animation:none}}const trig=document.getElementById('trig'),menu=document.getElementById('menu'),out=document.getElementById('out'),area=document.getElementById('area');
const items=()=>[...menu.querySelectorAll('[role=menuitem]:not([aria-disabled=true])')];
function open(focusFirst){
menu.hidden=false;menu.classList.remove('up');
const a=area.getBoundingClientRect(),m=menu.getBoundingClientRect();if(m.bottom>a.bottom-4)menu.classList.add('up');
trig.setAttribute('aria-expanded','true');const it=items();if(focusFirst!==false)it[0].focus();
}
function close(ret){menu.hidden=true;trig.setAttribute('aria-expanded','false');if(ret)trig.focus();}
trig.addEventListener('click',()=>menu.hidden?open():close());
trig.addEventListener('keydown',e=>{if(['Enter',' ','ArrowDown'].includes(e.key)){e.preventDefault();open();}});
menu.addEventListener('keydown',e=>{
const it=items(),i=it.indexOf(document.activeElement);
if(e.key==='ArrowDown'){e.preventDefault();it[(i+1)%it.length].focus();}
else if(e.key==='ArrowUp'){e.preventDefault();it[(i-1+it.length)%it.length].focus();}
else if(e.key==='Home'){e.preventDefault();it[0].focus();}
else if(e.key==='End'){e.preventDefault();it[it.length-1].focus();}
else if(e.key==='Escape'){e.preventDefault();close(true);}
else if(e.key==='Tab'){close(false);}
else if(e.key==='Enter'||e.key===' '){e.preventDefault();document.activeElement.click();}
else if(e.key.length===1){const k=e.key.toLowerCase(),n=it.slice(i+1).concat(it.slice(0,i+1)).find(x=>x.textContent.toLowerCase().startsWith(k));if(n)n.focus();}
});
menu.addEventListener('click',e=>{const li=e.target.closest('[role=menuitem]');if(!li||li.getAttribute('aria-disabled')==='true')return;out.textContent='Chose: '+li.textContent;close(true);});
document.addEventListener('pointerdown',e=>{if(!menu.hidden&&!menu.contains(e.target)&&e.target!==trig)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>Dropdown 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}}
.area{position:relative;width:min(380px,100%);height:300px;border:1px solid var(--line);border-radius:18px;background:var(--surface);padding:24px;display:flex;flex-direction:column;align-items:center;gap:60px}
.wrap{position:relative}
.trig{height:44px;padding:0 18px;border-radius:12px;border:1px solid var(--line);background:var(--surface-2);color:var(--text);font:inherit;font-weight:600;cursor:pointer}
.trig:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.menu{position:absolute;left:0;top:calc(100% + 8px);min-width:200px;list-style:none;padding:6px;border-radius:14px;border:1px solid var(--line);background:var(--surface-2);box-shadow:0 14px 32px rgba(0,0,0,.14);z-index:3;transform-origin:top left;animation:in .14s ease-out}
.menu.up{top:auto;bottom:calc(100% + 8px);transform-origin:bottom left}
[role=menuitem]{padding:10px 12px;border-radius:8px;font-size:14px;cursor:pointer;outline:none}
[role=menuitem]:hover,[role=menuitem]:focus{background:rgba(0,0,0,.06)}
[role=menuitem][aria-disabled=true]{opacity:.4;cursor:not-allowed}
[role=menuitem][aria-disabled=true]:hover{background:none}
[role=separator]{height:1px;margin:6px 4px;background:var(--line)}
.danger{color:var(--danger)}
#out{color:var(--muted);font-size:14px}
@keyframes in{from{opacity:0;transform:scale(.96)}}
@media (prefers-reduced-motion:reduce){.menu{animation:none}}
</style></head>
<body><div class="stage">
<div class="area" id="area">
<div class="wrap">
<button class="trig" id="trig" aria-haspopup="menu" aria-expanded="false" aria-controls="menu">Options ▾</button>
<ul class="menu" id="menu" role="menu" aria-label="Options" hidden>
<li role="menuitem" tabindex="-1">Rename</li><li role="menuitem" tabindex="-1">Duplicate</li>
<li role="menuitem" tabindex="-1" aria-disabled="true">Move to folder</li><li role="menuitem" tabindex="-1">Share</li>
<li role="separator"></li><li role="menuitem" tabindex="-1" class="danger">Delete</li>
</ul>
</div>
<p id="out" role="status">Choose an option</p>
</div>
</div>
<script>
const trig=document.getElementById('trig'),menu=document.getElementById('menu'),out=document.getElementById('out'),area=document.getElementById('area');
const items=()=>[...menu.querySelectorAll('[role=menuitem]:not([aria-disabled=true])')];
function open(focusFirst){
menu.hidden=false;menu.classList.remove('up');
const a=area.getBoundingClientRect(),m=menu.getBoundingClientRect();if(m.bottom>a.bottom-4)menu.classList.add('up');
trig.setAttribute('aria-expanded','true');const it=items();if(focusFirst!==false)it[0].focus();
}
function close(ret){menu.hidden=true;trig.setAttribute('aria-expanded','false');if(ret)trig.focus();}
trig.addEventListener('click',()=>menu.hidden?open():close());
trig.addEventListener('keydown',e=>{if(['Enter',' ','ArrowDown'].includes(e.key)){e.preventDefault();open();}});
menu.addEventListener('keydown',e=>{
const it=items(),i=it.indexOf(document.activeElement);
if(e.key==='ArrowDown'){e.preventDefault();it[(i+1)%it.length].focus();}
else if(e.key==='ArrowUp'){e.preventDefault();it[(i-1+it.length)%it.length].focus();}
else if(e.key==='Home'){e.preventDefault();it[0].focus();}
else if(e.key==='End'){e.preventDefault();it[it.length-1].focus();}
else if(e.key==='Escape'){e.preventDefault();close(true);}
else if(e.key==='Tab'){close(false);}
else if(e.key==='Enter'||e.key===' '){e.preventDefault();document.activeElement.click();}
else if(e.key.length===1){const k=e.key.toLowerCase(),n=it.slice(i+1).concat(it.slice(0,i+1)).find(x=>x.textContent.toLowerCase().startsWith(k));if(n)n.focus();}
});
menu.addEventListener('click',e=>{const li=e.target.closest('[role=menuitem]');if(!li||li.getAttribute('aria-disabled')==='true')return;out.textContent='Chose: '+li.textContent;close(true);});
document.addEventListener('pointerdown',e=>{if(!menu.hidden&&!menu.contains(e.target)&&e.target!==trig)close(false);});
</script>
</body></html>