Skip to Content
CodeUI

Toast

@codeui/styles v0.3.0

Short notifications in plain HTML, with the open toasts written into the region by hand.

Load @codeui/styles/toast.css, or all.css. The Solid component is documented in Toast.

Demo

toast/basic.html
<div class="cui-toast-list" data-cui="toast-list" style="max-width: 420px; margin-inline: auto">  <div class="cui-toast" data-cui="toast" data-tone="success" role="status" aria-atomic="true" aria-labelledby="t1-title" aria-describedby="t1-desc" tabindex="0">    <span class="cui-toast-icon" data-cui="toast-icon" aria-hidden="true">      <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">        <path d="M20 6 9 17l-5-5" />      </svg>    </span>    <div class="cui-toast-title" data-cui="toast-title" id="t1-title">Snippet exported</div>    <div class="cui-toast-description" data-cui="toast-description" id="t1-desc">hello-world.png is in your downloads.</div>    <button type="button" class="cui-toast-action" data-cui="toast-action">Open folder</button>    <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">        <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />      </svg>    </button>    <div class="cui-toast-progress-track" data-cui="toast-progress-track" role="presentation" aria-hidden="true">      <div class="cui-toast-progress-fill" data-cui="toast-progress-fill" style="--cui-toast-progress-fill-width: 60%"></div>    </div>  </div></div>

Markup

PartElementClassdata-cui
Regiondivcui-toast-regiontoast-region
Listdiv or olcui-toast-listtoast-list
Rootdiv or licui-toasttoast
Iconspancui-toast-icontoast-icon
Titledivcui-toast-titletoast-title
Descriptiondivcui-toast-descriptiontoast-description
Actionbuttoncui-toast-actiontoast-action
Close buttonbuttoncui-toast-close-buttontoast-close-button
Progress trackdivcui-toast-progress-tracktoast-progress-track
Progress filldivcui-toast-progress-filltoast-progress-fill

The root is a grid: icon, text, action, close button, with the progress track as a full-width last row. Every part is optional except the description. Without a title, the description centers against the icon.

AttributeValuesDefault
data-tone on the rootneutral, info, success, warning, dangerneutral
data-position on the regiontop-left, top-center, top-right, bottom-left, bottom-center, bottom-rightbottom-center

The demos on this page put the list in normal flow, without the fixed region, so each toast shows in place.

Variants

Tones

toast/tones.html
<div class="cui-toast-list" data-cui="toast-list" style="max-width: 420px; margin-inline: auto">  <div class="cui-toast" data-cui="toast" data-tone="neutral" role="status" aria-atomic="true" aria-describedby="t1-desc" tabindex="0">    <div class="cui-toast-description" data-cui="toast-description" id="t1-desc">Snippet duplicated.</div>    <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">        <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />      </svg>    </button>  </div>  <div class="cui-toast" data-cui="toast" data-tone="info" role="status" aria-atomic="true" aria-describedby="t2-desc" tabindex="0">    <span class="cui-toast-icon" data-cui="toast-icon" aria-hidden="true">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor">        <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd" />      </svg>    </span>    <div class="cui-toast-description" data-cui="toast-description" id="t2-desc">A new theme pack is available in Settings.</div>    <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">        <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />      </svg>    </button>  </div>  <div class="cui-toast" data-cui="toast" data-tone="success" role="status" aria-atomic="true" aria-describedby="t3-desc" tabindex="0">    <span class="cui-toast-icon" data-cui="toast-icon" aria-hidden="true">      <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">        <path d="M20 6 9 17l-5-5" />      </svg>    </span>    <div class="cui-toast-description" data-cui="toast-description" id="t3-desc">Snippet exported as PNG.</div>    <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">        <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />      </svg>    </button>  </div>  <div class="cui-toast" data-cui="toast" data-tone="warning" role="status" aria-atomic="true" aria-describedby="t4-desc" tabindex="0">    <span class="cui-toast-icon" data-cui="toast-icon" aria-hidden="true">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor">        <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-5a1 1 0 01.993.883L11 6v4a1 1 0 01-1.993.117L9 10V6a1 1 0 011-1zm0 8a1 1 0 100 2 1 1 0 000-2z" clip-rule="evenodd" />      </svg>    </span>    <div class="cui-toast-description" data-cui="toast-description" id="t4-desc">JetBrains Mono failed to load. Using the fallback font.</div>    <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">        <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />      </svg>    </button>  </div>  <div class="cui-toast" data-cui="toast" data-tone="danger" role="alert" aria-atomic="true" aria-labelledby="t5-title" aria-describedby="t5-desc" tabindex="0">    <span class="cui-toast-icon" data-cui="toast-icon" aria-hidden="true">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor">        <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-5a1 1 0 01.993.883L11 6v4a1 1 0 01-1.993.117L9 10V6a1 1 0 011-1zm0 8a1 1 0 100 2 1 1 0 000-2z" clip-rule="evenodd" />      </svg>    </span>    <div class="cui-toast-title" data-cui="toast-title" id="t5-title">Export failed</div>    <div class="cui-toast-description" data-cui="toast-description" id="t5-desc">The image is larger than 16 MB.</div>    <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">        <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />      </svg>    </button>  </div></div>

The tone sets --cui-toast-accent, which colors the icon and the progress fill. The card itself keeps the toast surface in every tone. A neutral toast usually has no icon.

Content

toast/content.html
<div class="cui-toast-list" data-cui="toast-list" style="max-width: 420px; margin-inline: auto">  <div class="cui-toast" data-cui="toast" data-tone="neutral" role="status" aria-atomic="true" aria-labelledby="t1-title" aria-describedby="t1-desc" tabindex="0">    <div class="cui-toast-title" data-cui="toast-title" id="t1-title">Snippet saved</div>    <div class="cui-toast-description" data-cui="toast-description" id="t1-desc">main.rs is in your workspace.</div>    <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">        <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />      </svg>    </button>  </div>  <div class="cui-toast" data-cui="toast" data-tone="neutral" role="status" aria-atomic="true" aria-describedby="t2-desc" tabindex="0">    <div class="cui-toast-description" data-cui="toast-description" id="t2-desc">Link copied.</div>  </div>  <div class="cui-toast" data-cui="toast" data-tone="neutral" role="status" aria-atomic="true" aria-describedby="t3-desc" tabindex="0">    <div class="cui-toast-description" data-cui="toast-description" id="t3-desc">Snippet moved to trash.</div>    <button type="button" class="cui-toast-action" data-cui="toast-action">Undo</button>    <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">        <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />      </svg>    </button>  </div></div>

A title is optional. The action is one inline text button in the soft brand color. The close button is a 24px icon button with an accessible name. Keep one action per toast.

Region and position

toast/region.html
<div class="cui-toast-region" data-cui="toast-region" data-position="bottom-right" role="region" aria-label="Notifications (F8)" tabindex="-1">  <div class="cui-toast-list" data-cui="toast-list">    <div class="cui-toast" data-cui="toast" data-tone="success" role="status" aria-atomic="true" aria-describedby="t1-desc" tabindex="0">      <span class="cui-toast-icon" data-cui="toast-icon" aria-hidden="true">        <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">          <path d="M20 6 9 17l-5-5" />        </svg>      </span>      <div class="cui-toast-description" data-cui="toast-description" id="t1-desc">Snippet exported as PNG.</div>      <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">        <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">          <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />        </svg>      </button>    </div>  </div></div>

The region is fixed to the viewport, up to 512px wide with 16px padding. Bottom positions sit 64px above the bottom edge. Center positions translate the region by half its width. The list stacks toasts with 8px between them and aligns them to the region's side. In this demo the region is fixed to the frame.

States

Time bar

toast/progress.html
<div class="cui-toast-list" data-cui="toast-list" style="max-width: 420px; margin-inline: auto">  <div class="cui-toast" data-cui="toast" data-tone="info" role="status" aria-atomic="true" aria-labelledby="t1-title" aria-describedby="t1-desc" tabindex="0">    <span class="cui-toast-icon" data-cui="toast-icon" aria-hidden="true">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor">        <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd" />      </svg>    </span>    <div class="cui-toast-title" data-cui="toast-title" id="t1-title">Exporting snippet</div>    <div class="cui-toast-description" data-cui="toast-description" id="t1-desc">Rendering at 2× scale.</div>    <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">        <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />      </svg>    </button>    <div class="cui-toast-progress-track" data-cui="toast-progress-track" role="presentation" aria-hidden="true">      <div class="cui-toast-progress-fill" data-cui="toast-progress-fill" style="--cui-toast-progress-fill-width: 35%"></div>    </div>  </div>  <div class="cui-toast" data-cui="toast" data-tone="success" role="status" aria-atomic="true" aria-describedby="t2-desc" tabindex="0">    <span class="cui-toast-icon" data-cui="toast-icon" aria-hidden="true">      <svg class="cui-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">        <path d="M20 6 9 17l-5-5" />      </svg>    </span>    <div class="cui-toast-description" data-cui="toast-description" id="t2-desc">Snippet exported as PNG.</div>    <button type="button" class="cui-toast-close-button" data-cui="toast-close-button" aria-label="Dismiss">      <svg class="cui-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">        <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />      </svg>    </button>    <div class="cui-toast-progress-track" data-cui="toast-progress-track" role="presentation" aria-hidden="true">      <div class="cui-toast-progress-fill" data-cui="toast-progress-fill" style="--cui-toast-progress-fill-width: 80%"></div>    </div>  </div></div>

Set --cui-toast-progress-fill-width on the fill, such as 35%. Update it as the close timer runs.

Entrance and exit

data-opened on the root plays the entrance once: it rises its own height and fades in over 400ms, or drops at a top position. data-closed fades it out over 200ms and keeps it hidden. Remove the root after the exit ends. With reduced motion, both are fades. data-cui-replaced together with data-opened skips the entrance, for a toast whose content changed in place.

Swipe

data-swipe="move" translates the root by --cui-toast-swipe-move-x. cancel returns it to place over 200ms. end slides it out from --cui-toast-swipe-end-x with a fade.

Focus

The root, the action and the close button show the 2px focus ring on :focus-visible. The action and close button get the neutral fill on hover.

What you supply

  • Adding and removing toasts, the close timer, and pausing it while the pointer or focus is inside the region.
  • role="status" with polite announcements for most tones, and role="alert" for danger.
  • An accessible name on the region, and a hotkey such as F8 that moves focus into it.
  • The swipe gesture and its geometry variables.
  • The icon for each tone. The Solid Toaster also inverts the theme, a light card on a dark page. Set data-cui-theme on the region to do the same.

Helper

import "@codeui/styles/toast.css";
import { applyStyleProps } from "@codeui/styles";
import { toastStyleProps } from "@codeui/styles/toast";

const region = document.querySelector<HTMLElement>("[data-cui=toast-region]")!;
const toast = region.querySelector<HTMLElement>("[data-cui=toast]")!;

applyStyleProps(region, toastStyleProps.region({ position: "top-right" }));
applyStyleProps(toast, toastStyleProps({ tone: "success" }));
// toast: class="cui-toast" data-cui="toast" data-tone="success"

On this page