訊息對話區塊呈現一段對話:參與的成員、依先後順序排列的每一則發言,以及用來撰寫下一則訊息的訊息編輯器。每則發言都是一個訊息;檔案、圖片、語音訊息和表情回應也都排在同樣的列中。在收件匣中,它就是主區域的內容,位於對話清單旁邊,放在固定式應用程式框架中。
訊息對話
<section class="sheet container-inline flex flex-col overflow-hidden" data-example="messaging" aria-labelledby="messaging-title"> <header class="p-md"> <div class="flex items-center gap-sm max-w(720) mx-auto"> <div class="avatar-group" aria-hidden="true"> <span class="avatar avatar-sm"> <img class="avatar-image" src="/demo/people/yiching-lin.webp" alt width="256" height="256"> </span> <span class="avatar avatar-sm"> <img class="avatar-image" src="/demo/people/misaki-sato.webp" alt width="256" height="256"> </span> <span class="avatar avatar-sm">KN</span> </div> <div class="flex flex-col gap-3xs min-w(0)"> <h2 class="heading heading-sm" id="messaging-title">Launch room</h2> <p class="m(0) fg-text-muted">Yi-ching Lin, Misaki Sato, Kenta Nakamura and you</p> </div> </div> </header> <div class="scroll-area h(400) px-md bt(1) bt-solid b-line-hair" tabindex="0" role="region" aria-label="Messages in Launch room"> <ol class="flex flex-col max-w(720) mx-auto my(0) px(0) py-md" aria-live="polite" aria-relevant="additions" data-messages> <li class="divider divider-label mb-md" role="separator">Friday, 13 November</li> <li class="message"> <span class="avatar avatar-sm message-avatar invisible" aria-hidden="true"></span> <div class="message-bubble"> <span class="sr-only">Yi-ching Lin, 09:12:</span>Morning! The launch checklist is final, the pricing change is the only thing left.</div> </li> <li class="message"> <span class="avatar avatar-sm message-avatar"> <img class="avatar-image" src="/demo/people/yiching-lin.webp" alt width="256" height="256"> </span> <a class="file file-interactive max-w(320)" href="#messaging-title"> <span class="file-page" aria-hidden="true">pdf</span> <span class="file-body"> <span class="file-name"> <span class="sr-only">Yi-ching Lin attached</span>Launch checklist.pdf</span> <span class="file-meta">218 KB · PDF document</span> </span> </a> <span class="message-meta" aria-hidden="true">Yi-ching Lin · <time datetime="2026-11-13T09:12">09:12</time> </span> </li> <li class="message" id="messaging-voice"> <span class="avatar avatar-sm message-avatar"> <img class="avatar-image" src="/demo/people/misaki-sato.webp" alt width="256" height="256"> </span> <div class="message-bubble flex items-center gap(10) pl(8)"> <span class="sr-only">Misaki Sato, 09:20, a voice message:</span> <button type="button" class="btn btn-primary btn-pill btn-square btn-sm" aria-label="Play"> <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="M6.1664 3.8652L11.8319 7.1338A1 1 0 0 1 11.8319 8.8662L6.1664 12.1348A1 1 0 0 1 4.6667 11.2686L4.6667 4.7314A1 1 0 0 1 6.1664 3.8652Z" /> </svg> </button> <div class="waveform"> <span class="waveform-bar" style="--level: 0.18"></span> <span class="waveform-bar" style="--level: 0.21"></span> <span class="waveform-bar" style="--level: 0.16"></span> <span class="waveform-bar" style="--level: 0.23"></span> <span class="waveform-bar" style="--level: 0.31"></span> <span class="waveform-bar" style="--level: 0.12"></span> <span class="waveform-bar" style="--level: 0.12"></span> <span class="waveform-bar" style="--level: 0.31"></span> <span class="waveform-bar" style="--level: 0.20"></span> <span class="waveform-bar" style="--level: 0.12"></span> <span class="waveform-bar" style="--level: 0.40"></span> <span class="waveform-bar" style="--level: 0.40"></span> <span class="waveform-bar" style="--level: 0.59"></span> <span class="waveform-bar" style="--level: 0.59"></span> <span class="waveform-bar" style="--level: 0.68"></span> <span class="waveform-bar" style="--level: 0.51"></span> <span class="waveform-bar" style="--level: 0.60"></span> <span class="waveform-bar" style="--level: 0.81"></span> <span class="waveform-bar" style="--level: 0.84"></span> <span class="waveform-bar" style="--level: 0.98"></span> <span class="waveform-bar" style="--level: 1.00"></span> <span class="waveform-bar" style="--level: 0.78"></span> <span class="waveform-bar" style="--level: 0.94"></span> <span class="waveform-bar" style="--level: 1.00"></span> <span class="waveform-bar" style="--level: 0.91"></span> <span class="waveform-bar" style="--level: 0.68"></span> <span class="waveform-bar" style="--level: 0.88"></span> <span class="waveform-bar" style="--level: 0.88"></span> <span class="waveform-bar" style="--level: 1.00"></span> <span class="waveform-bar" style="--level: 1.00"></span> <input class="waveform-seek" type="range" min="0" max="14" step="0.1" value="0" aria-label="Seek" aria-valuetext="0:00 of 0:14"> </div> <span class="waveform-time">0:14</span> </div> <span class="message-meta" aria-hidden="true">Misaki Sato · <time datetime="2026-11-13T09:20">09:20</time> </span> </li> <li class="message message-sent"> <div class="message-bubble"> <span class="sr-only">You, 09:24:</span>Listened. Moving the pricing change to Monday works for me.</div> </li> <li class="message message-sent"> <figure class="figure max-w(260)"> <img class="figure-media" src="/demo/scenes/wallpaper-dawn-silk-4x3.webp" alt="The new hero of the pricing page: soft folds of ivory silk in morning light" width="2000" height="1500" style="--figure-ratio: 4 / 3"> <figcaption class="figure-caption sr-only">You, 09:25</figcaption> </figure> <div class="reaction-group" role="group" aria-label="Reactions to your picture"> <label class="reaction" style="--count: 2"> <input type="checkbox" class="reaction-input" name="messaging-hero" value="👍" aria-label="Thumbs up, 2 others"> <span class="reaction-emoji" aria-hidden="true">👍</span> <span class="reaction-count" aria-hidden="true"></span> </label> <label class="reaction" style="--count: 1"> <input type="checkbox" class="reaction-input" name="messaging-hero" value="🎉" aria-label="Party popper, 1 other"> <span class="reaction-emoji" aria-hidden="true">🎉</span> <span class="reaction-count" aria-hidden="true"></span> </label> </div> <span class="message-meta">Read · <time datetime="2026-11-13T09:25">09:25</time> </span> </li> <li class="message"> <span class="avatar avatar-sm message-avatar" aria-hidden="true">KN</span> <div class="message-bubble"> <span class="sr-only">Kenta Nakamura, 09:31:</span>That hero reads much better. Ship it Monday at ten, I’ll watch the edge logs.</div> <span class="message-meta" aria-hidden="true">Kenta Nakamura · <time datetime="2026-11-13T09:31">09:31</time> </span> </li> </ol> </div> <div class="px-md pb-md"> <form class="composer max-w(720) mx-auto" data-composer> <textarea class="composer-field" name="message" aria-label="Message to Launch room" placeholder="Write to Launch room"></textarea> <div class="composer-toolbar"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Attach a file" data-attach> <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="M11.3333 5.3333V10A4 4 0 0 1 3.3333 10V4.6667A2.6667 2.6667 0 0 1 8.6667 4.6667V10A1.3333 1.3333 0 0 1 6 10V5.3333" transform="rotate(45 8 8)" /> </svg> </button> <input type="file" class="sr-only" tabindex="-1" aria-hidden="true" multiple data-files> <div class="composer-end"> <span class="composer-hint">Enter to send, Shift+Enter for a new line</span> <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> </div></section>/** * A conversation on local data. The composer sends on Enter and keeps Shift+Enter for a new line, adds what is * written, and any files attached, to the end of the thread as the reader's own turns, and keeps the newest turn * in view. Sending, ordering and keeping the thread in step with others are the application's: here a sent turn * stays "Sent". The voice message plays on a timer that stands in for its audio. Returns the function that stops it. */const PLAY = 'M8 5.6v12.8a1 1 0 0 0 1.5.86l10.2-6.4a1 1 0 0 0 0-1.72L9.5 4.74A1 1 0 0 0 8 5.6'const PAUSE = 'M6.5 6.25C6.5 5.56 7.06 5 7.75 5H9c.69 0 1.25.56 1.25 1.25v11.5c0 .69-.56 1.25-1.25 1.25H7.75c-.69 0-1.25-.56-1.25-1.25zM13.75 6.25c0-.69.56-1.25 1.25-1.25h1.25c.69 0 1.25.56 1.25 1.25v11.5c0 .69-.56 1.25-1.25 1.25H15c-.69 0-1.25-.56-1.25-1.25z'const clock = (seconds) => `${Math.floor(seconds / 60)}:${String(Math.floor(seconds) % 60).padStart(2, '0')}`const size = (bytes) => bytes < 1024 ? `${bytes} B` : bytes < 1048576 ? `${Math.round(bytes / 1024)} KB` : `${(bytes / 1048576).toFixed(1)} MB`export function mount(root) { const controller = new AbortController() const { signal } = controller /** * The conversation's own clock: a sent message is a minute after the one before it, so the times read in order * with the demo's morning instead of the reader's. It belongs to this mount, so a thread shown again starts * after 09:31 again. */ let minutes = 9 * 60 + 31 const now = () => { minutes += 1 return `${String(Math.floor(minutes / 60) % 24).padStart(2, '0')}:${String(minutes % 60).padStart(2, '0')}` } const doc = root.ownerDocument const thread = root.querySelector('[data-messages]') const scroller = thread.closest('.scroll-area') const form = root.querySelector('[data-composer]') const field = form.querySelector('.composer-field') const send = form.querySelector('button[aria-label="Send"]') const picker = form.querySelector('[data-files]') /** @type {File[]} */ let files = [] const make = (tag, attributes = {}, ...children) => { const element = doc.createElement(tag) for (const [name, value] of Object.entries(attributes)) element.setAttribute(name, value) element.append(...children) return element } // The thread opens on its newest turn. scroller.scrollTop = scroller.scrollHeight const ready = () => { send.disabled = !field.value.trim() && !files.length } /** The chips of the files waiting to go with the message, each with a button that takes it back. */ function chips() { form.querySelector('.composer-attachments')?.remove() if (files.length) { form.prepend(make('div', { class: 'composer-attachments' }, ...files.map((file, index) => make('span', { class: 'chip' }, make('span', { class: 'chip-label' }, file.name), make('button', { type: 'button', class: 'chip-remove', 'aria-label': `Remove ${file.name}`, 'data-index': String(index) }))))) } ready() } /** A turn of the reader's, at the end of the thread. */ function turn(content, label) { const time = now() const item = make('li', { class: 'message message-sent' }, content, make('span', { class: 'message-meta' }, 'Sent · ', make('time', { datetime: `2026-11-13T${time}` }, time))) content.prepend(make('span', { class: 'sr-only' }, `You, ${time}${label}`)) thread.append(item) // The newest turn is shown at once, as the thread's end, wherever the reader had scrolled to. scroller.scrollTop = scroller.scrollHeight } function submit() { const text = field.value.trim() if (!text && !files.length) return for (const file of files) { turn(make('div', { class: 'file max-w(320)' }, make('span', { class: 'file-page', 'aria-hidden': 'true' }, (file.name.split('.').pop() || 'file').slice(0, 4)), make('span', { class: 'file-body' }, make('span', { class: 'file-name' }, file.name), make('span', { class: 'file-meta' }, size(file.size)))), ' attached: ') } // What is written is text: it is set as text, never as markup. // Lines stay lines: each is a text node, with a <br> between them. if (text) turn(make('div', { class: 'message-bubble' }, ...text.split('\n').flatMap((line, index) => index ? [make('br'), line] : [line])), ': ') field.value = '' files = [] picker.value = '' chips() field.focus() } form.addEventListener('submit', (event) => { event.preventDefault(); submit() }, { signal }) field.addEventListener('keydown', (event) => { if (event.key === 'Enter' && !event.shiftKey && !event.isComposing) { event.preventDefault() submit() } }, { signal }) field.addEventListener('input', ready, { signal }) form.querySelector('[data-attach]').addEventListener('click', () => picker.click(), { signal }) picker.addEventListener('change', () => { files = [...files, ...picker.files] // The field is emptied, so a file that was removed can be chosen again: the same choice sends no `change`. picker.value = '' chips() field.focus() }, { signal }) form.addEventListener('click', (event) => { const remove = event.target.closest('.chip-remove') if (!remove) return files.splice(Number(remove.dataset.index), 1) chips() field.focus() }, { signal }) // The voice message: a timer stands in for the audio, and the bars fill as it plays. const voice = root.querySelector('#messaging-voice') const waveform = voice.querySelector('.waveform') const seek = voice.querySelector('.waveform-seek') const time = voice.querySelector('.waveform-time') const play = voice.querySelector('.btn') const length = Number(seek.max) let start = 0 let frame = 0 const show = (at) => { seek.value = String(at) seek.setAttribute('aria-valuetext', `${clock(at)} of ${clock(length)}`) waveform.style.setProperty('--waveform-progress', String(at / length)) time.textContent = clock(at > 0 ? at : length) } const playing = (on) => { play.setAttribute('aria-label', on ? 'Pause' : 'Play') play.querySelector('path').setAttribute('d', on ? PAUSE : PLAY) } const stop = () => { cancelAnimationFrame(frame); frame = 0; playing(false) } const tick = (moment) => { const at = Math.min((moment - start) / 1000, length) show(at) if (at < length) frame = requestAnimationFrame(tick) else { stop(); show(0) } } play.addEventListener('click', () => { if (frame) return stop() start = performance.now() - Number(seek.value) * 1000 frame = requestAnimationFrame(tick) playing(true) }, { signal }) seek.addEventListener('input', () => { start = performance.now() - Number(seek.value) * 1000 show(Number(seek.value)) }, { signal }) ready() return () => { controller.abort() stop() }}撰寫訊息後按 Enter 送出、附加檔案、播放語音訊息,或加上表情回應。
區塊提供可直接複製的標記,沿用頁面的文字顏色和字級。預覽在 body 上設定 fg-text-body text-sm。每個區塊都是尺寸容器,無論放在寬大的頁面、側邊欄大小的空間,還是對話框裡,都會依自身寬度重新排列。
#組成
- 一個面板:標頭列出聊天室中的成員,對話串放在自己的捲動區域中,訊息編輯器則位於底部,因此無論對話串多長,訊息編輯器都會留在原處。
- 一條閱讀欄。 面板會填滿它得到的空間,但成員、對話串和訊息編輯器都留在同一條
max-w(720) mx-auto的欄、同一條軸線上。否則在寬頁面中,自己送出的訊息會離它回覆的訊息隔著整個畫面;在側邊欄或對話框中,這一欄就是全部的寬度。訊息泡泡在欄內最多占 34rem,這是訊息元件本身的上限。 - 對話串是一個
<ol>。 順序本身就是內容。帶有標籤的分隔線會標示日期。 - 同一個人的連續發言會分組。 它們的訊息泡泡相距 4px,只有最後一則會顯示頭像、姓名和時間;其他發言則以
invisible頭像保留頭像的位置,讓訊息泡泡對齊。每則發言仍會在視覺上隱藏的前綴中寫出作者和時間,因此即使沒有視覺呈現,分組讀起來也一樣。 - 附件是獨立的一則發言。 檔案或圖文會放在訊息泡泡的位置:收到的發言放在開頭側,送出的發言放在結尾側。
- 語音訊息是訊息泡泡中的波形,附有播放按鈕,並可透過波形長條調整播放位置。在預覽中,以計時器代替音訊。
- 送達狀態以文字表達。 送出的發言會在
message-meta中寫出「已讀」或「已送出」及其時間;沒有任何資訊只靠勾號的顏色表達。 - 表情回應跟在它所回應的發言之後,位於開頭側或結尾側,是帶有名稱和計數的核取方塊。
- 訊息編輯器按 Enter 送出,以 Shift+Enter 換行;輸入法組字期間,Enter 則交給輸入法處理。待送出的檔案會以可移除的膠囊標籤顯示在欄位上方。在有內容可送出之前,「傳送」按鈕都會停用。
#哪些功能需要腳本
main.js 會將撰寫的內容和附加的檔案,以讀者自己的發言加到對話串的最後;它以純文字設定內容,從不當成標記,讓最新的發言保持在可見範圍內,並將焦點交回欄位。傳送、排序、已讀回條,以及讓對話串與其他人保持同步,則由應用程式負責:在這裡加入的發言會一直顯示「已送出」。沒有腳本時,訊息編輯器是一個將內容提交到伺服器的表單。
#無障礙
- 面板使用
<section>,以聊天室的名稱命名;對話串是一個有自己名稱的區域,並位於 Tab 順序中,因此可以用按鍵捲動。 - 對話串的清單設定
aria-live="polite"以通知新增的內容,因此新的發言會被朗讀出來,而不會移動焦點。 - 每則發言的作者和時間都包含在其無障礙文字中,無論是否繪製頭像和中繼資訊;因此可見的中繼資訊會加上
aria-hidden,以免被讀出兩次。 - 圖片的
alt說明圖片內容;語音訊息的按鈕命名為「播放」和「定位」,定位控制項會以文字說明時間。 - 附加按鈕有名稱;它的檔案輸入欄位不在 Tab 順序中,因為由按鈕負責開啟。
#相關內容
- 表面托盤、面板與凹槽構成所有元件共用的材質層次。
- 標題以頁面的高對比文字色呈現標題,搭配上方的前導標籤與下方的引言。
- 頭像以相片或姓名縮寫代表個人或團隊,可單獨顯示或重疊排列。
- 捲動區域內容超出哪一側的邊緣,該側就會淡出。
- 分隔線以一條細線分隔區段,可加上標籤。
- 訊息將對話中的一則訊息呈現為指向發言者的氣泡。
- 檔案將單一檔案呈現為帶有名稱的獨立物件,可用於附件、上傳或下載。
- 圖文呈現搭配圖說的圖片或影片,並修飾媒體邊緣。
- 波形以一排直條呈現錄音,已播放的部分以文字色填滿。
- 表情回應用表情符號回應訊息。每個表情都顯示回應人數,按一下即可加入。
- 訊息編輯器用來撰寫訊息或提示詞,上方是會自動增高的欄位,下方是一排工具。
- 膠囊標籤用於切換篩選條件或顯示可移除值的小型膠囊標籤。
- 按鈕凸起的控制項,懸停時微微抬升,按下時向內凹陷。