表单
消息编辑器
用于撰写消息或提示词,输入区随内容增高,下方排列工具。
本页内容
消息编辑器用于撰写消息或提示词。凸起面板内有一个无独立边框、随文本增高的输入区,输入区下方是一排低调的工具按钮,发送按钮位于末尾。输入区聚焦时,面板会像输入框一样显示一道墨色轮廓和柔和的光晕。
消息编辑器
<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 发送
在 textarea 中,按 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 | 在 form 上使用凸起面板包裹字段及其工具。面板呈现字段的焦点状态,并让按钮圆角与面板圆角同心 |
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 | 面板的内边距,也是计算面板与按钮圆角差值的依据。 |