A messaging block is one conversation: who is in it, the turns in the order they came, and a composer to write the next one. Each turn is a message; files, a picture, a voice message and reactions stand in the same rows. In an inbox it is what the main region holds, beside the list of conversations, in a fixed app shell.
Messaging
<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() }}Blocks are markup to copy. They take the page's own text color and size; the previews set fg-text-body text-sm on the body. Each block is a size container, so it reflows by its own width: in a wide page, a sidebar's worth of room or a dialog.
#What is in it
- One sheet: a header with the people in the room, the thread in a scroll area of its own, and the composer at the foot, so the composer stays where it is however long the thread grows.
- One reading column. The sheet fills the room it is given, but the people, the thread and the composer keep one column,
max-w(720) mx-auto, on one axis. In a wide page a sent turn would otherwise sit a screen away from the turn it answers; in a sidebar or a dialog the column is simply the whole width. A bubble then takes at most 34rem of it, which is the message's own limit. - The thread is an
<ol>. The order is the content. A divider with a label names the day. - Turns from one person are grouped. Their bubbles stand 4px apart and only the last one shows the avatar and the name with the time; the others keep the avatar's place with an
invisibleavatar, so the bubbles line up. Every turn still starts with the author and the time in a visually hidden prefix, so the group reads the same without the drawing. - An attachment is a turn of its own. A file or a figure stands where a bubble would, at the start of a received turn and at the end of a sent one.
- A voice message is a waveform in a bubble, with a play button and a seek over the bars. In the preview a timer stands in for the audio.
- Delivery is words. A sent turn says "Read" or "Sent" with its time in
message-meta; nothing depends on a tick mark's color. - Reactions follow the turn they answer, at its start or its end, as checkboxes with a name and a count.
- The composer sends on Enter and keeps Shift+Enter for a new line, and leaves Enter to an input method while it composes. Files wait above the field as chips that can be taken back. Send is disabled until there is something to send.
#What needs a script
main.js adds what is written, and the files attached, to the end of the thread as the reader's own turns, sets the text as text and never as markup, keeps the newest turn in view and gives focus back to the field. Sending, ordering, read receipts and keeping the thread in step with the others are the application's: a turn added here stays "Sent". Without the script the composer is a form that posts to the server.
#Accessibility
- The sheet is a
<section>named by the room's name; the thread is a region with its own name, in the tab order so it can be scrolled with the keys. - The thread's list is
aria-live="polite"for additions, so a new turn is announced without moving focus. - The author and the time of every turn are in its accessible text, whether or not the avatar and the meta are drawn; the visible meta is then
aria-hiddenso it is not read twice. - The picture's
altsays what it shows; a voice message's buttons are named "Play" and "Seek", and the seek says the time in words. - The attach button is named; its file input is out of the tab order, since the button opens it.
#Related
- SurfaceTrays, sheets and wells: the material ladder every component is built on.
- HeadingA title in the ink of the page, with the line over it and the paragraph under it.
- AvatarA person or a team as a photo or initials, alone or stacked.
- Scroll areaA scrolling region whose edges fade where content continues.
- DividerOne hairline between sections, with an optional label.
- MessageOne turn of a conversation, as a bubble that points at its author.
- FileOne file as a thing with a name: an attachment, an upload or a download.
- FigureAn image or a video with its caption, finished at the edge.
- WaveformA recording as a row of bars, in ink as far as it has played.
- ReactionEmoji answers to a message, each with its count, that a press adds to.
- ComposerWhere a message or a prompt is written: a growing field over a row of tools.
- ChipA small pill that is a filter or a removable value.
- ButtonA raised control that lifts on hover and sinks when pressed.