Dojo NG is being built in the open, one section at a time — follow along on Heptapod.

GUIDE

Snackbar or toast?

Dojo NG has two ways to show a short message at the edge of the screen. dj-snackbar is one element you place and open yourself. toast() from @dojo-ng/toast is a function you call from anywhere, with a queue, Undo buttons, and toasts that follow a promise. They look the same, because each toast is a snackbar.

Last action

Nothing yet. Press a button.
Real toasts at the bottom of the page. Delete offers Undo (press Alt+T to reach it from the keyboard). Send follows a promise that takes 2 seconds and fails half the time. Pressing "Fail on account A" again updates the same toast instead of stacking another.

Which one

UseWhen
dj-snackbarOne message with a fixed place in the page, opened and closed by your code, such as an "offline" bar that stays while the network is down.
toast()Messages raised from many places in the app, that can arrive together, close on their own, or offer Undo.

If a message needs more than a sentence and one or two buttons, it is a dialog, not a toast.

Toasts

Call toast(). It finds the page's <dj-toaster>, or adds one to document.body the first time, so there is nothing to set up.

import { toast } from "@dojo-ng/toast";

toast("Settings saved", { type: "success" });

const t = toast("Message deleted", {
  action: { label: "Undo", onClick: () => restore(message) },
});
// Later, from code:
t.dismiss();
  • type is info (the default), success, warning, error, or loading.
  • A toast closes after 5 seconds. error and loading toasts stay until closed. Set duration in milliseconds, or Infinity to keep any toast open.
  • A toast with an action stays at least 8 seconds, so there is time to reach the button. The toast closes after the action runs.
  • At most three toasts show at once. The rest wait and come in as shown ones close.
  • Timers pause while the pointer is over the toasts, while focus is in them, and while the page is hidden.

Following a promise

toast.promise() shows a loading toast and turns it into a success or error toast in place when the promise settles. The success toast's 5 seconds start when the promise settles, not when it started.

toast.promise(send(draft), {
  loading: "Sending",
  success: "Sent",
  error: (e) => `Send failed: ${e.message}`,
});

Updating a toast

toast() returns { id, dismiss(), update(options) }. Passing an id that is already showing updates that toast instead of adding one, which keeps a repeated message from stacking.

// One toast per account: a second failure updates it instead of stacking another.
toast(`${account}: could not move that message`, { type: "error", id: `move-failed:${account}` });

Keyboard and screen readers

  • Alt+T moves focus to the newest toast's first button, so a keyboard user can reach Undo. Escape closes that toast and puts focus back where it was.
  • The toaster announces every toast through one pair of live regions for the page: errors assertively, everything else politely, in the order they appear. A toast with an action also announces its shortcut.

Placement and limits

Write the toaster yourself to change where toasts appear, how many show at once, or the shortcut. placement is one of top-start, top-center, top-end, bottom-start, bottom-center (the default), or bottom-end. Start and end follow the page direction, so they swap in a right-to-left page. The newest toast sits nearest the edge.

<dj-toaster placement="top-end" max-visible="2" shortcut="Alt+N"></dj-toaster>

The snackbar on its own

dj-snackbar stays the right choice for a message you control directly. Toggle open; type tints it success, warning, or error. It announces itself through its own live regions.

<dj-snackbar id="offline" type="warning">You are offline. Changes will sync later.</dj-snackbar>
<script type="module">
  import "@dojo-ng/snackbar";
  const bar = document.getElementById("offline");
  addEventListener("offline", () => (bar.open = true));
  addEventListener("online", () => (bar.open = false));
</script>
Next: Writing plugins Extend the grid, the editor, and the chart without forking them.