Mention picker
The list that opens under a trigger typed in a box: the focus stays in the box.
import { MentionPicker, mentionOptionId, conversationCaretPosition, filterMentionOptions } from "@forgeron/ui";- Léa MartinTeam lead
- Karim AzizController
- Jo BernardAccounting
- Sam RocheDirector
Picked: nobody yet
import {
filterMentionOptions,
MentionPicker,
mentionOptionId,
type MentionOption,
} from "@forgeron/ui";
import { useState } from "react";
const people: MentionOption[] = [
{ id: "lea", label: "Léa Martin", hint: "Team lead" },
{ id: "karim", label: "Karim Aziz", hint: "Controller" },
{ id: "jo", label: "Jo Bernard", hint: "Accounting" },
{ id: "sam", label: "Sam Roche", hint: "Director" },
];
export default function Default() {
const [query, setQuery] = useState("");
const [active, setActive] = useState(0);
const [picked, setPicked] = useState("nobody yet");
const options = filterMentionOptions(people, query);
return (
<div style={{ width: 300 }}>
<input
aria-label="Search people"
role="combobox"
aria-expanded
aria-controls="mp-demo"
aria-activedescendant={
options.length
? mentionOptionId("mp-demo", Math.min(active, options.length - 1))
: undefined
}
value={query}
placeholder="Type a name"
style={{
width: "100%",
height: 32,
padding: "0 8px",
border: "1px solid var(--line-strong)",
background: "var(--bg-sunk)",
color: "var(--ink)",
borderRadius: 0,
boxSizing: "border-box",
}}
onChange={(e) => {
setQuery(e.target.value);
setActive(0);
}}
onKeyDown={(e) => {
if (e.key === "ArrowDown") setActive((a) => Math.min(a + 1, options.length - 1));
else if (e.key === "ArrowUp") setActive((a) => Math.max(a - 1, 0));
else if (e.key === "Enter" && options[active]) setPicked(options[active]!.label);
else return;
e.preventDefault();
}}
/>
<div style={{ margin: "8px 0" }}>
<MentionPicker
id="mp-demo"
label="People"
mark="@"
options={options}
activeIndex={active}
onActiveIndexChange={setActive}
onSelect={(o) => setPicked(o.label)}
/>
</div>
<p style={{ font: "12px var(--font-mono)", color: "var(--ink-muted)" }}>Picked: {picked}</p>
</div>
);
}API → Props
| Prop | Type | Default | Description |
|---|---|---|---|
options | MentionOption[] | — | { id, label, hint?, avatar?, insert?, keywords? }. |
activeIndex / onActiveIndexChange | number; (i) => void | — | The highlighted row; the caller moves it with the arrows. |
onSelect | (option) => void | — | Enter, Tab or a click. |
id | string | — | Put aria-controls={id} and aria-activedescendant={mentionOptionId(id, i)} on the box. |
label / empty / loading / mark | string; string; boolean; string | — | Names the list, and its words. |
position / placement | { top, left }; above | below | — | px in a positioned parent; conversationCaretPosition(textarea, index) measures the caret. |
className | string | — | Extra classes. |