Mentions example

Mention people with cancellation and recovery.

Search a small authored directory, insert stable identities and recover a failed lookup. A deliberately slow source can ignore cancellation so stale-response protection is visible.

Complete React client module · MIT · Next.js and Vite

Interactive example

Mention people with cancellation and recovery

Search a small authored directory, insert stable identities and recover a failed lookup. A deliberately slow source can ignore cancellation so stale-response protection is visible.

Keyboard

  • Type @ and a name. Arrow keys move through available people; Enter inserts a stable identity.
  • Escape closes the suggestions without deleting the draft. Tab leaves the editor.
  • After a lookup failure, Enter retries from the editor; the Retry search button is also available.

States covered

  • Async directory
  • AbortSignal
  • Late stale response
  • Retry without draft loss
  • Stable mention IDs
  • Disabled people

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-mentions-async-directory.json
Complete source
React / TypeScript
/*!
MIT License

Copyright (c) 2026 Jaison

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import {useCallback,useRef,useState} from 'react';
import {Button,Checkbox,Mentions,richTextToText,type RichTextMentionSource,UIProvider} from '@bld8/ui';
const people=[{id:'person-maya',label:'Maya Chen',description:'Interface engineering'},{id:'person-oliver',label:'Oliver Reed',description:'Documentation'},{id:'person-sam',label:'Sam Rivera',description:'Design systems'},{id:'person-leon',label:'Leon Park',description:'Not available for new reviews',disabled:true}];
export default function AsyncMentionsExample(){const [fail,setFail]=useState(false),[ignore,setIgnore]=useState(false),[lastQuery,setLastQuery]=useState('none'),[settled,setSettled]=useState('none'),[status,setStatus]=useState('No comment read.');const flags=useRef({fail,ignore});flags.current={fail,ignore};
  const source=useCallback<RichTextMentionSource>(({query,signal})=>new Promise((resolve,reject)=>{setLastQuery(query);const failThis=flags.current.fail,ignoreThis=flags.current.ignore&&query==='ol';if(failThis){flags.current.fail=false;setFail(false);}const timer=setTimeout(()=>{signal.removeEventListener('abort',cancel);setSettled(query);if(failThis)reject(Error('Directory unavailable. Try again.'));else resolve(people.filter(person=>person.label.toLowerCase().includes(query.toLowerCase())));},query==='ol'?350:35);function cancel(){if(ignoreThis)return;clearTimeout(timer);reject(new DOMException('Cancelled','AbortError'));}if(signal.aborted)cancel();else signal.addEventListener('abort',cancel,{once:true});}),[]);
  return <UIProvider><div style={{display:'grid',gap:20,maxWidth:760}}><div style={{display:'flex',gap:20,flexWrap:'wrap'}}><Checkbox label="Fail next directory lookup" checked={fail} onCheckedChange={setFail}/><Checkbox label="Ignore cancellation for slow ol lookup" checked={ignore} onCheckedChange={setIgnore}/></div>
    <form aria-label="Mention comment form" style={{display:'grid',gap:20}} onSubmit={event=>{event.preventDefault();const json=String(new FormData(event.currentTarget).get('comment'));setStatus('Comment: '+richTextToText(JSON.parse(json))+' | JSON: '+json);}}><Mentions label="Comment with people" name="comment" mentionSource={source} mentionDebounceMs={0} hint="Type @ to search. Mentioning someone sends no notification."/><Button type="submit">Read comment values</Button></form>
    <p className="bui-hint" data-mention-request>{'Latest query: '+lastQuery}</p><p className="bui-hint" data-mention-settled>{'Latest completed query: '+settled}</p><p role="status" style={{overflowWrap:'anywhere'}}>{status}</p>
  </div></UIProvider>;}