Interaction · Complete runnable example

Persistent toast queue

Keep up to three messages visible until the reader dismisses them.

Live example

This preview runs in an isolated document. Clipboard writes and saved preferences may be blocked, and printing requires the standalone file. A dialog affects only the example. Reset reloads the original example state.

When to use it

Use this queue for nonessential local feedback while preserving the underlying result in the page. Messages remain visible and there is no automatic timeout. The example adds demonstration text only; it does not perform the action described by that text.

Try the important details

  1. Add three notifications, then try a fourth to see the queue limit.
  2. Use the keyboard to dismiss a message and check the next useful focus target.
  3. Enter punctuation or HTML-looking text; it remains literal text.

How it works

  • A separate status region politely announces additions and limits without moving focus.
  • The list is capped at three messages. A full queue rejects a new addition instead of silently removing an existing message.
  • Dismissal moves focus only when the removed item held focus. The next dismiss button or the add button becomes the target.

The complete source

The preview, copied text, and downloaded file use this same document.

HTML

<!doctype html>
<!--
MIT License
Copyright (c) 2026 MRHTML

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta
      http-equiv="Content-Security-Policy"
      content="default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; img-src data: blob:; connect-src 'none'; form-action 'none'; object-src 'none'; base-uri 'none'"
    />
    <title>A little feedback. | MRHTML example</title>
    <style>
      :root {
        color-scheme: light;
        --paper: #f5f3ee;
        --surface: #fff;
        --ink: #172a3b;
        --muted: #536575;
        --line: #d2dce1;
        --blue: #234ed8;
        --soft: #eef3ff;
        --orange: #ad4b17;
        --error: #a62440;
        font:
          16px/1.6 system-ui,
          -apple-system,
          BlinkMacSystemFont,
          'Segoe UI',
          sans-serif;
        color: var(--ink);
        background: var(--paper);
      }
      * {
        box-sizing: border-box;
      }
      body {
        margin: 0;
        padding: clamp(24px, 6vw, 64px) 22px;
        min-height: 100svh;
      }
      main {
        width: min(760px, 100%);
        margin: auto;
        min-width: 0;
      }
      h1,
      h2,
      h3,
      p {
        margin: 0;
      }
      h1 {
        font-size: clamp(2rem, 6vw, 3rem);
        line-height: 1.08;
        letter-spacing: -0.055em;
        font-weight: 680;
        max-width: 680px;
      }
      h2 {
        font-size: 1.2rem;
        line-height: 1.3;
        letter-spacing: -0.02em;
      }
      p {
        color: var(--muted);
      }
      header {
        margin-bottom: 28px;
      }
      .brand {
        font:
          700 0.73rem/1.5 ui-monospace,
          monospace;
        letter-spacing: 0.1em;
        text-transform: uppercase;
        color: var(--blue);
        margin-bottom: 20px;
      }
      .intro {
        max-width: 62ch;
        margin-top: 16px;
      }
      .surface {
        background: var(--surface);
        border: 1px solid var(--line);
        border-radius: 18px;
        padding: clamp(20px, 4vw, 32px);
      }
      .note {
        font-size: 0.85rem;
        margin-top: 20px;
      }
      .eyebrow {
        font:
          600 0.72rem/1.5 ui-monospace,
          monospace;
        letter-spacing: 0.08em;
        text-transform: uppercase;
      }
      .actions {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
        margin-top: 20px;
      }
      button,
      input,
      textarea,
      select {
        font: inherit;
      }
      button {
        border: 1px solid var(--ink);
        background: var(--ink);
        color: #fff;
        border-radius: 8px;
        padding: 10px 17px;
        min-height: 44px;
        font-weight: 650;
        cursor: pointer;
      }
      button.secondary {
        background: transparent;
        color: var(--ink);
        border-color: var(--line);
      }
      button:hover:not(:disabled) {
        background: var(--blue);
        border-color: var(--blue);
        color: #fff;
      }
      button:disabled {
        cursor: not-allowed;
        opacity: 0.55;
      }
      label {
        font-weight: 600;
      }
      input:not([type='checkbox']):not([type='range']):not([type='file']),
      textarea,
      select {
        width: 100%;
        min-width: 0;
        border: 1px solid #8e9faa;
        border-radius: 8px;
        padding: 10px 12px;
        background: #fff;
        color: var(--ink);
      }
      textarea {
        resize: vertical;
        min-height: 180px;
      }
      input[type='range'] {
        width: 100%;
        accent-color: var(--blue);
      }
      input[type='checkbox'] {
        width: 20px;
        height: 20px;
        accent-color: var(--blue);
      }
      :focus-visible {
        outline: 3px solid var(--orange);
        outline-offset: 4px;
      }
      a {
        color: var(--blue);
      }
      .status {
        min-height: 1.6em;
        font-size: 0.9rem;
        margin-top: 18px;
        overflow-wrap: anywhere;
      }
      .status[data-error='true'] {
        color: var(--error);
      }
      [hidden] {
        display: none !important;
      }
      .sr-only {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border: 0;
      }
      .field {
        display: grid;
        gap: 7px;
        margin-top: 16px;
      }
      footer {
        font-size: 0.75rem;
        color: var(--muted);
        border-top: 1px solid var(--line);
        margin-top: 30px;
        padding-top: 16px;
        display: flex;
        justify-content: space-between;
        gap: 12px;
      }
      noscript {
        display: block;
        padding: 14px;
        background: #fff2d9;
        border: 1px solid #ad4b17;
        margin-top: 20px;
        border-radius: 8px;
        color: var(--ink);
      }
      @media (max-width: 400px) {
        body {
          padding: 25px 16px;
        }
        .surface {
          padding: 20px;
        }
        button {
          padding: 10px 14px;
        }
      }
      @media (prefers-reduced-motion: reduce) {
        *,
        *::before,
        *::after {
          scroll-behavior: auto !important;
          animation: none !important;
          transition: none !important;
        }
      }

      main {
        width: min(920px, 100%);
      }
      .toast-layout {
        display: grid;
        grid-template-columns: 1.1fr 1fr;
        gap: 24px;
      }
      .queue {
        min-width: 0;
        padding: 25px 0;
      }
      .queue-heading {
        display: flex;
        justify-content: space-between;
        gap: 16px;
        border-bottom: 1px solid var(--line);
        padding-bottom: 16px;
        margin-bottom: 20px;
      }
      #queue-count {
        font:
          500 0.8rem ui-monospace,
          monospace;
      }
      #empty-queue {
        font-size: 0.9rem;
        padding: 25px 0;
      }
      #toast-list {
        padding: 0;
        list-style: none;
        margin: 0;
        display: grid;
        gap: 12px;
      }
      #toast-list li {
        display: grid;
        grid-template-columns: 1fr auto;
        gap: 12px;
        align-items: start;
        background: #fff;
        border: 1px solid var(--line);
        border-left: 3px solid var(--blue);
        padding: 18px;
        border-radius: 8px;
      }
      #toast-list p {
        color: var(--ink);
        font-size: 0.9rem;
        overflow-wrap: anywhere;
      }
      #toast-list button {
        font-size: 0.76rem;
        padding: 6px 9px;
        min-height: 38px;
      }
      @media (max-width: 700px) {
        .toast-layout {
          grid-template-columns: 1fr;
        }
        .queue {
          padding-top: 0;
        }
      }
    </style>
  </head>
  <body>
    <main>
      <header>
        <p class="brand">MRHTML / Runnable example</p>
        <h1>A little feedback.</h1>
        <p class="intro">
          A small, persistent notification queue. Each message stays until you
          dismiss it.
        </p>
      </header>
      <div class="toast-layout">
        <section class="surface">
          <p class="eyebrow">Compose a local notification</p>
          <div class="field">
            <label for="toast-message">Message</label
            ><input
              id="toast-message"
              maxlength="140"
              value="Your preview is ready to review."
              aria-describedby="toast-help"
            />
          </div>
          <p id="toast-help" class="note">
            140 characters maximum. This only adds a message to this example.
          </p>
          <div class="actions">
            <button id="add-toast" type="button" disabled>
              Add notification</button
            ><button id="clear-toasts" class="secondary" type="button" disabled>
              Dismiss all
            </button>
          </div>
          <p id="toast-status" class="status" role="status" aria-atomic="true">
            No notifications yet.
          </p>
        </section>
        <section class="queue" aria-labelledby="queue-title">
          <div class="queue-heading">
            <h2 id="queue-title">Your queue</h2>
            <span id="queue-count">0 / 3</span>
          </div>
          <p id="empty-queue">New notifications appear here.</p>
          <ol id="toast-list"></ol>
        </section>
      </div>
      <p class="note">
        The queue holds three messages. When full, dismiss one before adding
        another. Essential feedback should also live beside the relevant
        content.
      </p>
      <noscript
        >The content is readable without JavaScript. Interactive controls are
        unavailable.</noscript
      >
      <footer>
        <span>MRHTML example</span><span>Original code · MIT</span>
      </footer>
    </main>
    <script>
      'use strict';
      (() => {
        const message = document.querySelector('#toast-message'),
          add = document.querySelector('#add-toast'),
          clear = document.querySelector('#clear-toasts'),
          list = document.querySelector('#toast-list'),
          status = document.querySelector('#toast-status');
        let serial = 0;
        function count() {
          const n = list.children.length;
          document.querySelector('#queue-count').textContent = `${n} / 3`;
          document.querySelector('#empty-queue').hidden = n > 0;
          clear.disabled = n === 0;
          return n;
        }
        add.disabled = false;
        add.addEventListener('click', () => {
          const text = message.value.trim();
          if (!text) {
            status.textContent =
              'Enter a message before adding a notification.';
            message.focus();
            return;
          }
          if (count() === 3) {
            status.textContent =
              'Queue full. Dismiss a notification before adding another.';
            return;
          }
          const item = document.createElement('li'),
            body = document.createElement('p'),
            dismiss = document.createElement('button');
          serial++;
          body.textContent = text.slice(0, 140);
          body.id = `toast-${serial}`;
          dismiss.type = 'button';
          dismiss.className = 'secondary';
          dismiss.textContent = 'Dismiss';
          dismiss.setAttribute('aria-describedby', body.id);
          dismiss.addEventListener('click', () => {
            const focused = item.contains(document.activeElement),
              next =
                item.nextElementSibling?.querySelector('button') ||
                item.previousElementSibling?.querySelector('button') ||
                add;
            item.remove();
            count();
            if (focused) next.focus();
            status.textContent = 'Notification dismissed.';
          });
          item.append(body, dismiss);
          list.append(item);
          count();
          status.textContent = `Notification ${serial}: ${text.slice(0, 140)}`;
        });
        clear.addEventListener('click', () => {
          list.replaceChildren();
          count();
          add.focus();
          status.textContent = 'All notifications dismissed.';
        });
      })();
    </script>
  </body>
</html>