メッセージ入力欄
メッセージやプロンプトを作成するためのコンポーネント。入力に合わせて伸びる入力欄の下にツールが並びます。
このページの内容
メッセージ入力欄は、メッセージやプロンプトを書くためのコンポーネントです。浮き上がったパネルに、テキスト量に合わせて伸びる枠のない入力欄を置きます。その下には控えめなツールを一列に並べ、列の最後に送信ボタンを配置します。入力欄にフォーカスが当たると、入力欄コンポーネントと同じように、パネルに濃色の輪郭線が 1 本表示され、その外側がほのかに光ります。
メッセージ入力欄
<form class="composer max-w(480) mx-auto"> <textarea class="composer-field" name="message" aria-label="Message" placeholder="Ask anything"></textarea> <div class="composer-toolbar"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Attach a file"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L13 8" /><path d="M8 3L8 13" /> </svg> </button> <button type="button" class="btn btn-quiet btn-sm"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="8" cy="8" r="6" /><path d="M8.7707 2.0497A6.0005 6.0005 0 0 0 7.2293 2.0497Q6.1162 3.1938 5.4813 4.6077Q4.8542 6.0044 4.7648 7.5H2.0209A5.9913 5.9913 0 0 0 2.0209 8.5H4.7648Q4.8542 9.9956 5.4813 11.3923Q6.1162 12.8062 7.2293 13.9503A6.0005 6.0005 0 0 0 8.7707 13.9503Q9.8838 12.8062 10.5187 11.3923Q11.1458 9.9956 11.2352 8.5H13.9791A5.9913 5.9913 0 0 0 13.9791 7.5H11.2352Q11.1458 6.0044 10.5187 4.6077Q9.8838 3.1938 8.7707 2.0497ZM9.6064 5.0173Q10.1455 6.2181 10.2331 7.5H5.7669Q5.8545 6.2181 6.3936 5.0173Q6.971 3.7314 8 2.6921Q9.029 3.7314 9.6064 5.0173ZM6.3936 10.9827Q5.8545 9.7819 5.7669 8.5H10.2331Q10.1455 9.7819 9.6064 10.9827Q9.029 12.2686 8 13.3079Q6.971 12.2686 6.3936 10.9827Z" fill="currentColor" stroke="none" fill-rule="evenodd" /> </svg>Search </button> <div class="composer-end"> <span class="composer-hint">Enter to send</span> <button class="btn btn-primary btn-square btn-sm" aria-label="Send"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(-90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(-90 8 8)" /> </svg> </button> </div> </div></form>import PlusIcon from '@master/icons-react/native/16/soft/plus'import GlobeIcon from '@master/icons-react/native/16/soft/globe'import ArrowUpIcon from '@master/icons-react/native/16/soft/arrow-up'<form className="composer max-w(480) mx-auto"> <textarea className="composer-field" name="message" aria-label="Message" placeholder="Ask anything">{``}</textarea> <div className="composer-toolbar"> <button type="button" className="btn btn-quiet btn-square btn-sm" aria-label="Attach a file"> <PlusIcon className="icon" /> </button> <button type="button" className="btn btn-quiet btn-sm"> <GlobeIcon className="icon" />Search </button> <div className="composer-end"> <span className="composer-hint">Enter to send</span> <button className="btn btn-primary btn-square btn-sm" aria-label="Send"> <ArrowUpIcon className="icon" /> </button> </div> </div></form><script> import PlusIcon from '@master/icons-svelte/native/16/soft/plus' import GlobeIcon from '@master/icons-svelte/native/16/soft/globe' import ArrowUpIcon from '@master/icons-svelte/native/16/soft/arrow-up'</script><form class="composer max-w(480) mx-auto"> <textarea class="composer-field" name="message" aria-label="Message" placeholder="Ask anything"></textarea> <div class="composer-toolbar"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Attach a file"> <PlusIcon class="icon" /> </button> <button type="button" class="btn btn-quiet btn-sm"><GlobeIcon class="icon" />Search</button> <div class="composer-end"> <span class="composer-hint">Enter to send</span> <button class="btn btn-primary btn-square btn-sm" aria-label="Send"> <ArrowUpIcon class="icon" /> </button> </div> </div></form><script setup>import PlusIcon from '@master/icons-vue/native/16/soft/plus'import GlobeIcon from '@master/icons-vue/native/16/soft/globe'import ArrowUpIcon from '@master/icons-vue/native/16/soft/arrow-up'</script><template> <form class="composer max-w(480) mx-auto"> <textarea class="composer-field" name="message" aria-label="Message" placeholder="Ask anything"></textarea> <div class="composer-toolbar"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Attach a file"> <PlusIcon class="icon" /> </button> <button type="button" class="btn btn-quiet btn-sm"><GlobeIcon class="icon" />Search</button> <div class="composer-end"> <span class="composer-hint">Enter to send</span> <button class="btn btn-primary btn-square btn-sm" aria-label="Send"> <ArrowUpIcon class="icon" /> </button> </div> </div> </form></template>入力欄には field-sizing: content を指定した <textarea> を使います。最初は 1 行で、10 行まで伸び、それを超えるとスクロールします。最小と最大の行数は、--composer-rows と --composer-max-rows で変更できます。入力内容に合わせたサイズ調整に対応していないブラウザーでは、高さは 3 行分に固定され、それを超えるとスクロールします。
ボタンはパネルの端から 8px 内側に配置します。角を同心円状に丸めることで、送信ボタンの曲線がパネルの角に沿います。
#添付ファイル
composer-attachments は入力欄の上に配置する折り返し可能な行です。メッセージに添えるファイルの チップ やサムネイルを並べます。
添付ファイル
<form class="composer max-w(480) mx-auto"> <div class="composer-attachments"> <span class="chip"> brief.pdf<button type="button" class="chip-remove" aria-label="Remove brief.pdf"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> <span class="chip"> notes.md<button type="button" class="chip-remove" aria-label="Remove notes.md"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> </div> <textarea class="composer-field" name="message" aria-label="Message">Summarise the brief against these notes, and list what the notes leave out. Keep it to one page so the team can read it before the review on Thursday.</textarea> <div class="composer-toolbar"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Attach a file"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3 8L13 8" /><path d="M8 3L8 13" /> </svg> </button> <div class="composer-end"> <span class="composer-hint">138 / 2000</span> <button class="btn btn-primary btn-sm">Send</button> </div> </div></form>import XIcon from '@master/icons-react/native/16/soft/x'import PlusIcon from '@master/icons-react/native/16/soft/plus'<form className="composer max-w(480) mx-auto"> <div className="composer-attachments"> <span className="chip"> brief.pdf<button type="button" className="chip-remove" aria-label="Remove brief.pdf"> <XIcon className="icon" /> </button> </span> <span className="chip"> notes.md<button type="button" className="chip-remove" aria-label="Remove notes.md"> <XIcon className="icon" /> </button> </span> </div> <textarea className="composer-field" name="message" aria-label="Message" defaultValue="Summarise the brief against these notes, and list what the notes leave out. Keep it to one page so the team can read it before the review on Thursday." /> <div className="composer-toolbar"> <button type="button" className="btn btn-quiet btn-square btn-sm" aria-label="Attach a file"> <PlusIcon className="icon" /> </button> <div className="composer-end"> <span className="composer-hint">138 / 2000</span> <button className="btn btn-primary btn-sm">Send</button> </div> </div></form><script> import XIcon from '@master/icons-svelte/native/16/soft/x' import PlusIcon from '@master/icons-svelte/native/16/soft/plus'</script><form class="composer max-w(480) mx-auto"> <div class="composer-attachments"> <span class="chip"> brief.pdf<button type="button" class="chip-remove" aria-label="Remove brief.pdf"> <XIcon class="icon" /> </button> </span> <span class="chip"> notes.md<button type="button" class="chip-remove" aria-label="Remove notes.md"> <XIcon class="icon" /> </button> </span> </div> <textarea class="composer-field" name="message" aria-label="Message">Summarise the brief against these notes, and list what the notes leave out. Keep it to one page so the team can read it before the review on Thursday.</textarea> <div class="composer-toolbar"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Attach a file"> <PlusIcon class="icon" /> </button> <div class="composer-end"> <span class="composer-hint">138 / 2000</span> <button class="btn btn-primary btn-sm">Send</button> </div> </div></form><script setup>import XIcon from '@master/icons-vue/native/16/soft/x'import PlusIcon from '@master/icons-vue/native/16/soft/plus'</script><template> <form class="composer max-w(480) mx-auto"> <div class="composer-attachments"> <span class="chip"> brief.pdf<button type="button" class="chip-remove" aria-label="Remove brief.pdf"> <XIcon class="icon" /> </button> </span> <span class="chip"> notes.md<button type="button" class="chip-remove" aria-label="Remove notes.md"> <XIcon class="icon" /> </button> </span> </div> <textarea class="composer-field" name="message" aria-label="Message">Summarise the brief against these notes, and list what the notes leave out. Keep it to one page so the team can read it before the review on Thursday.</textarea> <div class="composer-toolbar"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Attach a file"> <PlusIcon class="icon" /> </button> <div class="composer-end"> <span class="composer-hint">138 / 2000</span> <button class="btn btn-primary btn-sm">Send</button> </div> </div> </form></template>#無効状態
入力欄を無効にすると、パネルの浮き上がりがなくなり、くぼんだ表示になります。送信ボタンも無効にし、その理由をプレースホルダーかメッセージ入力欄の横に示してください。
無効状態
<form class="composer max-w(480) mx-auto"> <textarea class="composer-field" name="message" aria-label="Message" placeholder="Replies are closed for this thread" disabled></textarea> <div class="composer-toolbar"> <div class="composer-end"> <button class="btn btn-primary btn-square btn-sm" aria-label="Send" disabled> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(-90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(-90 8 8)" /> </svg> </button> </div> </div></form>import ArrowUpIcon from '@master/icons-react/native/16/soft/arrow-up'<form className="composer max-w(480) mx-auto"> <textarea className="composer-field" name="message" aria-label="Message" placeholder="Replies are closed for this thread" disabled>{``}</textarea> <div className="composer-toolbar"> <div className="composer-end"> <button className="btn btn-primary btn-square btn-sm" aria-label="Send" disabled> <ArrowUpIcon className="icon" /> </button> </div> </div></form><script> import ArrowUpIcon from '@master/icons-svelte/native/16/soft/arrow-up'</script><form class="composer max-w(480) mx-auto"> <textarea class="composer-field" name="message" aria-label="Message" placeholder="Replies are closed for this thread" disabled></textarea> <div class="composer-toolbar"> <div class="composer-end"> <button class="btn btn-primary btn-square btn-sm" aria-label="Send" disabled> <ArrowUpIcon class="icon" /> </button> </div> </div></form><script setup>import ArrowUpIcon from '@master/icons-vue/native/16/soft/arrow-up'</script><template> <form class="composer max-w(480) mx-auto"> <textarea class="composer-field" name="message" aria-label="Message" placeholder="Replies are closed for this thread" disabled></textarea> <div class="composer-toolbar"> <div class="composer-end"> <button class="btn btn-primary btn-square btn-sm" aria-label="Send" disabled> <ArrowUpIcon class="icon" /> </button> </div> </div> </form></template>#Enter で送信
テキストエリアでは、Enter を押すと改行します。Enter で送信し、Shift+Enter で改行するように変更するには、リスナーが一つあれば十分です。入力メソッドで文字の入力が確定するまでは、Enter による送信処理を行いません。
field.addEventListener('keydown', event => { if (event.key === 'Enter' && !event.shiftKey && !event.isComposing && event.keyCode !== 229) { event.preventDefault() field.form.requestSubmit() }})キー操作は製品によって異なるため、ツールの横に composer-hint で操作方法を示してください。
#アクセシビリティ
- 入力欄には、
aria-labelまたはメッセージ入力欄の上に表示する<label for>で名前を付けます。プレースホルダーとは別に名前が必要です。 - メッセージ入力欄は
<form>なので、送信ボタンから送信でき、パスワードマネージャーや拡張機能からも認識できます。 - アイコンボタンには
aria-labelを指定します。Tab キーで移動すると、入力欄の後に読み順どおりに各ツールへフォーカスが移ります。 - パネルの余白をクリックしても、既定では入力欄にフォーカスは移りません。パネル全体をクリック対象にするには、入力欄を
<label>で囲みます。 - タッチ画面でも入力欄の文字サイズは変わりません。フォーカス時の iOS の拡大は、ページのビューポートで防ぎます。
- アシスタントの返信のように返信を生成している間も、読者が次のメッセージを書けるよう入力欄は有効なままにし、送信ボタンの位置に、文言で名前を付けた「停止」ボタンを置きます。メッセージ入力欄全体を無効にすると、「停止」と下書きも一緒に使えなくなります。停止した返信は、完全なものではなく途中までであることを伝えます。
#クラス
| クラス | 説明 |
|---|---|
composer | フォームでフィールドとツールを囲む、浮き上がったシート。フィールドのフォーカス状態を描画し、ボタンの角はシートの角と同心になります。 |
composer-field | 独自のサーフェスを持たない textarea です。内容に合わせて --composer-rows 行から --composer-max-rows 行まで伸び、上限を超えるとスクロールします。 |
composer-attachments | メッセージに添えるチップやサムネイルを、フィールドの上で折り返して並べる行 |
composer-toolbar | フィールドの下に置くツールの行。 |
composer-end | ツールバーの終端に寄せるグループ。送信ボタンはここに配置。 |
composer-hint | ツールの横に置く控えめな 1 行の注記。モデル名、文字数、ショートカットなど |
#カスタムプロパティ
| プロパティ | 既定値 | 説明 |
|---|---|---|
--composer-rows | 1 | フィールドの初期行数 |
--composer-max-rows | 10 | フィールドがスクロールせずに表示できる最大行数 |
--composer-radius | var(--radius-surface) | シートの角の丸み |
--composer-inset | .5rem | シートのパディング。ボタンの角もこの内側のオフセットに合わせる |