Mention Autocomplete Menu
Type @ in a comment box to open a filtered list of people; choose with arrows and Enter, or click.
AI prompt
Clean UI mockup of a comment text box containing the text Thanks @ma and below it a small popup list of three people with round initials avatars and names Maya Chen, Marcus Lee and Mara Okafor, the first row highlighted, each with a small role label, light theme, soft border, flat modern style, a Post button at the bottom right. Negative: heavy shadows, clutter, text errors, device frames. Reduced motion: static design.About this pattern
Definition
A mention menu appears when someone types @ and narrows as they keep typing. It is an accessible combobox: focus stays in the text field while arrow keys move a highlighted option, Enter inserts it and Escape closes. It saves typing exact names and avoids tagging the wrong person.
Purpose & UX principles
- Open the list on @ at the start of a word; filter by the text after it.
- Keep focus in the input and use aria-activedescendant to point at the active option.
- Arrow keys move, Enter or Tab inserts, Escape closes; clicking also works.
- Show an avatar or role to tell similar names apart, and a No matches message.
Interaction states
- Type @ then a few letters in the comment box.
Motion specification
- The menu appears in 120ms.
- Reduced motion: no fade.
Responsive behaviour
- Give the list role listbox, each option role option, and announce results count.
- Close the menu when the cursor leaves the mention text.
Code
<div class="bx"><label class="fl" for="tx">Comment</label><div class="wr"><input id="tx" type="text" role="combobox" aria-expanded="false" aria-controls="lb" aria-autocomplete="list" autocomplete="off" placeholder="Type @ to mention someone"><ul id="lb" class="lb" role="listbox" aria-label="People" hidden></ul></div><p class="out" id="out" role="status"></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}
.fl{font-size:12px;font-weight:600}.wr{position:relative;margin-top:4px}#tx{width:100%;box-sizing:border-box;height:38px;border:1px solid var(--line);border-radius:10px;padding:0 12px;font:inherit;font-size:14px;background:var(--surface);color:var(--text)}.lb{position:absolute;left:0;right:0;top:calc(100% + 4px);list-style:none;background:#fff;border:1px solid var(--line);border-radius:10px;box-shadow:0 8px 20px rgba(0,0,0,.12);padding:4px;z-index:5;animation:fi .12s}.lb li{display:flex;align-items:center;gap:8px;padding:6px 8px;border-radius:7px;font-size:13px;cursor:pointer}.lb li[aria-selected=true]{background:#171717;color:#fff}.lb li i{width:22px;height:22px;border-radius:50%;display:grid;place-items:center;font:700 10px system-ui;font-style:normal;color:#fff}.lb li small{margin-left:auto;opacity:.65;font-size:11px}.lb li.no{cursor:default;color:var(--muted)}.out{font-size:12px;color:var(--muted);min-height:16px}@keyframes fi{from{opacity:0}}@media (prefers-reduced-motion:reduce){.lb{animation:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const P=[['Maya Chen','Design','#c2410c'],['Marcus Lee','Engineering','#2563eb'],['Mara Okafor','Product','#7c3aed'],['Noah Silva','Support','#0f766e'],['Priya Nair','Data','#be185d']],tx=document.getElementById('tx'),lb=document.getElementById('lb'),out=document.getElementById('out');let act=0,list=[],start=-1;
function ctx(){const pos=tx.selectionStart,m=/(^|\s)@([\w]*)$/.exec(tx.value.slice(0,pos));return m?{q:m[2],s:pos-m[2].length-1}:null;}
function render(){const c=ctx();if(!c){close();return;}start=c.s;list=P.filter(p=>p[0].toLowerCase().includes(c.q.toLowerCase())||p[0].toLowerCase().split(' ').some(w=>w.startsWith(c.q.toLowerCase())));act=Math.min(act,Math.max(0,list.length-1));lb.hidden=false;tx.setAttribute('aria-expanded','true');
lb.innerHTML=list.length?list.map((p,i)=>'<li role="option" id="o'+i+'" aria-selected="'+(i===act)+'" data-i="'+i+'"><i style="background:'+p[2]+'">'+p[0].split(' ').map(w=>w[0]).join('')+'</i>'+p[0]+'<small>'+p[1]+'</small></li>').join(''):'<li class="no" role="option" aria-selected="false">No matches</li>';
if(list.length)tx.setAttribute('aria-activedescendant','o'+act);else tx.removeAttribute('aria-activedescendant');out.textContent=list.length+' people found.';}
function close(){lb.hidden=true;tx.setAttribute('aria-expanded','false');tx.removeAttribute('aria-activedescendant');}
function pick(i){const p=list[i];if(!p)return;const pos=tx.selectionStart;tx.value=tx.value.slice(0,start)+'@'+p[0]+' '+tx.value.slice(pos);const np=start+p[0].length+2;tx.setSelectionRange(np,np);close();out.textContent='Mentioned '+p[0]+'.';tx.focus();}
tx.addEventListener('input',()=>{act=0;render();});tx.addEventListener('click',render);
tx.addEventListener('keydown',e=>{if(lb.hidden)return;if(e.key==='ArrowDown'){e.preventDefault();act=(act+1)%Math.max(1,list.length);render();}else if(e.key==='ArrowUp'){e.preventDefault();act=(act-1+list.length)%Math.max(1,list.length);render();}else if(e.key==='Enter'||e.key==='Tab'){if(list.length){e.preventDefault();pick(act);}}else if(e.key==='Escape'){close();}});
lb.addEventListener('mousedown',e=>{const li=e.target.closest('li[data-i]');if(li){e.preventDefault();pick(+li.dataset.i);}});tx.addEventListener('blur',()=>setTimeout(close,100));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>Mention Autocomplete 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}}
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}
.fl{font-size:12px;font-weight:600}.wr{position:relative;margin-top:4px}#tx{width:100%;box-sizing:border-box;height:38px;border:1px solid var(--line);border-radius:10px;padding:0 12px;font:inherit;font-size:14px;background:var(--surface);color:var(--text)}.lb{position:absolute;left:0;right:0;top:calc(100% + 4px);list-style:none;background:#fff;border:1px solid var(--line);border-radius:10px;box-shadow:0 8px 20px rgba(0,0,0,.12);padding:4px;z-index:5;animation:fi .12s}.lb li{display:flex;align-items:center;gap:8px;padding:6px 8px;border-radius:7px;font-size:13px;cursor:pointer}.lb li[aria-selected=true]{background:#171717;color:#fff}.lb li i{width:22px;height:22px;border-radius:50%;display:grid;place-items:center;font:700 10px system-ui;font-style:normal;color:#fff}.lb li small{margin-left:auto;opacity:.65;font-size:11px}.lb li.no{cursor:default;color:var(--muted)}.out{font-size:12px;color:var(--muted);min-height:16px}@keyframes fi{from{opacity:0}}@media (prefers-reduced-motion:reduce){.lb{animation:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><label class="fl" for="tx">Comment</label><div class="wr"><input id="tx" type="text" role="combobox" aria-expanded="false" aria-controls="lb" aria-autocomplete="list" autocomplete="off" placeholder="Type @ to mention someone"><ul id="lb" class="lb" role="listbox" aria-label="People" hidden></ul></div><p class="out" id="out" role="status"></p></div>
</div>
<script>
const P=[['Maya Chen','Design','#c2410c'],['Marcus Lee','Engineering','#2563eb'],['Mara Okafor','Product','#7c3aed'],['Noah Silva','Support','#0f766e'],['Priya Nair','Data','#be185d']],tx=document.getElementById('tx'),lb=document.getElementById('lb'),out=document.getElementById('out');let act=0,list=[],start=-1;
function ctx(){const pos=tx.selectionStart,m=/(^|\s)@([\w]*)$/.exec(tx.value.slice(0,pos));return m?{q:m[2],s:pos-m[2].length-1}:null;}
function render(){const c=ctx();if(!c){close();return;}start=c.s;list=P.filter(p=>p[0].toLowerCase().includes(c.q.toLowerCase())||p[0].toLowerCase().split(' ').some(w=>w.startsWith(c.q.toLowerCase())));act=Math.min(act,Math.max(0,list.length-1));lb.hidden=false;tx.setAttribute('aria-expanded','true');
lb.innerHTML=list.length?list.map((p,i)=>'<li role="option" id="o'+i+'" aria-selected="'+(i===act)+'" data-i="'+i+'"><i style="background:'+p[2]+'">'+p[0].split(' ').map(w=>w[0]).join('')+'</i>'+p[0]+'<small>'+p[1]+'</small></li>').join(''):'<li class="no" role="option" aria-selected="false">No matches</li>';
if(list.length)tx.setAttribute('aria-activedescendant','o'+act);else tx.removeAttribute('aria-activedescendant');out.textContent=list.length+' people found.';}
function close(){lb.hidden=true;tx.setAttribute('aria-expanded','false');tx.removeAttribute('aria-activedescendant');}
function pick(i){const p=list[i];if(!p)return;const pos=tx.selectionStart;tx.value=tx.value.slice(0,start)+'@'+p[0]+' '+tx.value.slice(pos);const np=start+p[0].length+2;tx.setSelectionRange(np,np);close();out.textContent='Mentioned '+p[0]+'.';tx.focus();}
tx.addEventListener('input',()=>{act=0;render();});tx.addEventListener('click',render);
tx.addEventListener('keydown',e=>{if(lb.hidden)return;if(e.key==='ArrowDown'){e.preventDefault();act=(act+1)%Math.max(1,list.length);render();}else if(e.key==='ArrowUp'){e.preventDefault();act=(act-1+list.length)%Math.max(1,list.length);render();}else if(e.key==='Enter'||e.key==='Tab'){if(list.length){e.preventDefault();pick(act);}}else if(e.key==='Escape'){close();}});
lb.addEventListener('mousedown',e=>{const li=e.target.closest('li[data-i]');if(li){e.preventDefault();pick(+li.dataset.i);}});tx.addEventListener('blur',()=>setTimeout(close,100));
</script>
</body></html>