メッセージングブロックは 1 つの会話です。参加者、届いた順に並ぶ発言、次の発言を書くためのメッセージ入力欄で構成します。各発言はメッセージで、ファイル、画像、音声メッセージ、リアクションも同じ行に並びます。受信トレイでは、固定シェルの中で、会話の一覧の横にあるメイン領域に置きます。
メッセージング
<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 を設定しています。各ブロックはサイズコンテナーで、広いページでも、サイドバーほどの幅でも、ダイアログ内でも、自身の幅に応じて配置が変わります。
#構成
- 1 枚のシート。 参加者を示すヘッダー、専用のスクロール領域に入れたスレッド、下端のメッセージ入力欄で構成します。スレッドがどれだけ長くなっても、入力欄の位置は変わりません。
- 1 本の読む列。 シートは与えられた幅いっぱいに広がりますが、参加者、スレッド、入力欄は
max-w(720) mx-autoの 1 本の列に、同じ軸でそろえます。そうしないと、広いページでは送信したメッセージが返信先のメッセージから画面の端と端に離れてしまいます。サイドバーやダイアログでは、この列がそのまま全幅になります。吹き出しは列の中で最大 34rem で、これはメッセージ自身の上限です。 - スレッドは
<ol>です。 順序そのものがコンテンツです。ラベル付きの区切り線で日付を示します。 - 同じ人の発言はグループにまとめます。 吹き出しの間隔は 4px で、アバターと名前と時刻は最後の吹き出しにだけ表示します。ほかの吹き出しは
invisibleのアバターでアバターの位置を確保し、吹き出しの位置をそろえます。それでも各発言は、視覚的に隠した前置きで発言者と時刻から始まるため、見た目がなくてもグループは同じように読めます。 - 添付ファイルはそれ自体が 1 つの発言です。 ファイルや図版を吹き出しの位置に置きます。受信した発言では先頭側、送信した発言では末尾側です。
- 音声メッセージは、吹き出しの中の波形です。 再生ボタンを備え、バーの上でシークできます。プレビューでは、音声の代わりにタイマーを使っています。
- 配信状況は文言で示します。 送信した発言は、
message-metaに時刻とともに「Read」(既読)や「Sent」(送信済み)と表示します。チェックマークの色に頼る情報はありません。 - リアクションは、応答先の発言の後に置きます。 発言の先頭側または末尾側に、名前と件数を持つチェックボックスとして並べます。
- メッセージ入力欄は Enter キーで送信します。 Shift+Enter は改行に使い、入力メソッドで変換中の Enter キーは入力メソッドに任せます。送信前の添付ファイルは、取り外せるチップとして入力欄の上に表示します。送信するものがあるまで、送信ボタンは無効です。
#スクリプトが必要な処理
main.js は、書かれた内容と添付されたファイルを、読み手自身の発言としてスレッドの末尾に追加します。テキストはマークアップとしてではなく、必ずテキストとして設定します。最新の発言が見える状態を保ち、入力欄にフォーカスを戻します。送信、並び順、既読の通知、ほかの参加者とのスレッドの同期はアプリケーションが担当します。ここで追加した発言は「Sent」のままです。スクリプトがない場合、メッセージ入力欄はサーバーに送信するフォームになります。
#アクセシビリティ
- シートは会話の名前を名前とする
<section>です。スレッドは独自の名前を持つ領域で、キーボードでスクロールできるようタブ順序に含めます。 - スレッドのリストは追加に対して
aria-live="polite"なので、新しい発言はフォーカスを移さずに読み上げられます。 - 各発言の発言者と時刻は、アバターやメタ情報が表示されているかどうかにかかわらず、アクセシブルなテキストに含まれます。そのため、表示されるメタ情報には
aria-hiddenを付け、二重に読み上げられないようにします。 - 画像の
altでは、何が写っているかを伝えます。音声メッセージのボタンには「Play」と「Seek」という名前を付け、シークは時刻を文言で伝えます。 - 添付ボタンには名前を付けます。ファイル入力はボタンから開くため、タブ順序から外します。
#関連項目
- サーフェストレイ、シート、ウェル。すべてのコンポーネントの土台となる、奥行きの異なる素材です。
- 見出しページの前景色で表示する見出しと、その上のラベル、下の導入文。
- アバター人物やチームを写真やイニシャルで表し、単独でも重ねても使えます。
- スクロール領域コンテンツが続く側の端がフェードするスクロール領域。
- 区切り線セクションを分ける細い線です。ラベルを付けることもできます。
- メッセージ会話の一つひとつの発言を、発言者を指す吹き出しで表示します。
- ファイル添付、アップロード、ダウンロードの各ファイルを名前付きの項目として表示します。
- 図版縁を整えた、キャプション付きの画像や動画。
- 波形録音をバーの列で表示し、再生済みの範囲を濃く示します。
- リアクションメッセージへのリアクションを絵文字ごとに件数付きで表示し、押すと自分のリアクションを追加できるコンポーネント。
- メッセージ入力欄メッセージやプロンプトを作成するためのコンポーネント。入力に合わせて伸びる入力欄の下にツールが並びます。
- チップフィルターや削除可能な値を表す、小さなカプセル形の要素。
- ボタン面からわずかに浮いた操作要素です。ポインターを重ねるとさらに浮き上がり、押すと沈み込みます。