Question presented to candidate: "In our comment box, typing @ followed by a few letters should pop up a list of teammates right under where you are typing. Arrow keys pick one, Enter or Tab inserts it, and the cursor ends up just after the name. Build it on a plain textarea, no editor library."
Requirements:
- Detect a mention token at the caret: an
@at the start of the text or after whitespace or(, followed only by handle characters up to the caret. An email address such asada@example.comis not a mention. - Filter users by handle prefix first, then by the start of any word in their name; show at most 5.
- Keyboard: ArrowUp / ArrowDown (wrapping), Enter or Tab inserts, Escape closes the menu for that token; mouse selection must not blur the textarea.
- Insert
@handlein place of the typed token (reusing a following space if there is one) and put the caret right after it; editing in the middle of the text must keep the text after the caret. - Show the menu at the caret position, and list who is mentioned in the text.
- Accessibility: listbox and options,
aria-activedescendanton the textarea, and a polite live region announcing the number of suggestions.
What a strong answer should cover:
- Deriving everything (open, query, matches) from two values: the text and the caret position.
- The token-detection rule and its edge cases (emails, start of text, parentheses, Escape for one token only).
- Restoring the caret after a controlled update (
setSelectionRangeafter the framework writes the value). - Getting pixel coordinates of a caret inside a textarea with a mirror element, and why it needs real layout.
- Storage trade-offs: plain
@handletext versus structured tokens such as@[Name](id).
Clarifying questions expected:
- "Where do users come from: a fixed list or a server search as you type?"
- "Must a mention be atomic, so Backspace deletes the whole name?"
- "Do we store plain text or a structured format with user ids?"
Code / implementation expected: Yes. Pure helpers for detection, filtering, insertion and extraction, plus a textarea component with keyboard handling, caret restore and a positioned menu.