表單
訊息編輯器
用來撰寫訊息或提示詞,上方是會自動增高的欄位,下方是一排工具。
本頁內容
訊息編輯器用來撰寫訊息或提示詞。略微浮起的面板上有一個無框欄位,會隨文字增高。下方是一排低調的工具,最末端是傳送按鈕。欄位取得焦點時,面板會像輸入欄位一樣,以一道深色輪廓和柔和的光暈顯示焦點。
訊息編輯器
<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>,從一行開始,最多增高到十行,之後便在欄位內捲動。用 --composer-rows 和 --composer-max-rows 分別調整起始與最大行數。如果瀏覽器無法依內容調整欄位大小,欄位會固定顯示三行,超出的內容可捲動查看。
按鈕距離面板邊緣 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 換行,只需加上一個事件監聽器,並在輸入法組字期間略過這項處理。
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 在欄位取得焦點時放大頁面。
- 當回覆正在產生時(例如 AI 助理的回覆),請讓欄位保持可用,讓使用者能先擬好下一則訊息,並在原本傳送按鈕的位置放一個以文字命名的「停止」按鈕:停用整個編輯器會連同「停止」和草稿一起拿走。回覆被停止時,請說明它只是部分內容,而非完整回覆。
#類別
| 類別 | 說明 |
|---|---|
composer | 表單中帶有浮起效果的面板,包住欄位及其工具。面板會顯示欄位的焦點狀態,按鈕的圓角也與面板的圓角同心。 |
composer-field | textarea 本身沒有底色。高度隨內容從 --composer-rows 行增至 --composer-max-rows 行,之後可捲動 |
composer-attachments | 欄位上方可換行的一列,以膠囊標籤或縮圖顯示隨訊息附上的項目。 |
composer-toolbar | 欄位下方的工具列 |
composer-end | 放在工具列末端的群組,供放置傳送按鈕 |
composer-hint | 工具旁的一行低調補充文字,例如模型名稱、字元數或快速鍵。 |
#自訂屬性
| 屬性 | 預設值 | 說明 |
|---|---|---|
--composer-rows | 1 | 欄位一開始顯示的行數 |
--composer-max-rows | 10 | 欄位開始捲動前可顯示的行數 |
--composer-radius | var(--radius-surface) | 面板的圓角 |
--composer-inset | .5rem | 面板的內距也決定按鈕圓角的內縮量 |