本文へ移動
日本語
「メンション入力」は Pro に含まれます。解除するとプレビューが表示されます。

仕組み

There is no contenteditable. The textarea draws only the caret and selection; a mirror with the same box model sits behind it and draws the text, with each mention as an accent token. A hidden second mirror measures the caret so the list opens under the @, clamped inside the field. Text stays a plain string, easy to store and diff.

向いている場面

  • Comments, replies and notes where people notify teammates by name.
  • Any message field that must keep native mobile keyboards, autocorrect and IME.

向かない場面

  • Rich formatting (bold, links, images): use a rich-text editor.
  • Picking recipients only, without free text: use a tags input or multi-select.

使い方

import { useState } from 'react'
import { MentionInput } from './MentionInput.jsx'

const people = [
  { id: 'm', name: 'Mira Holt', role: 'Design lead' },
  { id: 'j', name: 'Jonas Reed', role: 'Illustrator' },
]

export function Reply() {
  const [text, setText] = useState('')
  return (
    <MentionInput
      aria-label="Reply"
      value={text}
      onChange={(next, mentioned) => setText(next)}
      people={people}
      placeholder="Type @ to mention someone"
    />
  )
}

プロパティ

プロパティ型既定値説明
people{ id?, name, role?, handle?, color? }[]—Who can be mentioned. A word of name or the handle is matched from its start.
valuestring—Controlled text. Mentions are stored as plain "@Name".
defaultValuestring''Initial text when uncontrolled.
onChange(text, mentioned: Person[]) => void—Called on every edit with the text and the people currently mentioned in it.
triggerstring'@'Character that starts a mention; it must follow a space or the start.
rowsnumber3Visible rows of the textarea.
maxLengthnumber—Character limit; shows a counter that turns amber past 90%.
renderPerson(person) => ReactNode—Replaces the default initial avatar in the list.
labelsobject—Replaces built-in text: people, noMatch, mentioned.
...restTextareaHTMLAttributes—Go to the textarea, including onKeyDown (called after list navigation) and aria-label. List layer: --ds-mention-z (30).

アクセシビリティ

  • The textarea is a combobox (aria-autocomplete="list", aria-multiline) controlling a listbox; the highlighted person is announced through aria-activedescendant.
  • Keyboard: Down / Up move through people, Enter or Tab insert, Esc closes the list and keeps the text; Backspace after a token removes the whole name.
  • Each inserted mention is announced in a polite live region. The visible text is the real text, so screen readers read "@Name" naturally.
  • People rows are 44px. Reduced motion removes the list and token glow animations.

モーション

The list scales in from 0.96 and 4px above in 200ms ease-out. A new token starts with a stronger tint and a 4px glow that settle over 360ms. The field turns white with a halo in 220ms.

プロジェクトに追加

Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。

MentionInput.jsxmention-input.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。