App-wide toasts: one ToastRegion for every layout, a documented API, and toasts across redirects

Open
#223 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active

Research direction

Start with apps/aurora/app/root.tsx, packages/contents/routes/layout.tsx, and the existing queue and region in packages/layout/config/toast.ts and packages/layout/components/Toast/Toast.tsx. Read how setAuthOnResponse works in @plone/react-router before designing the redirect flash flow. Done means toasts render across layouts, redirect toasts appear once and do not return on reload, and screen-reader announcements remain supported.

Written by the indexing model from the issue text.

Description

Summary

Aurora has a global toast queue that any package or add-on can push to. The ToastRegion that renders that queue is mounted in one layout only, @plone/contents, so a toast raised anywhere else is queued and never shown. A loader or action that ends in a server redirect also has no way to carry a toast to the page it redirects to.

This issue proposes:

  1. Mounting the region once for the whole app.
  2. A small documented API for raising a toast.
  3. A flash mechanism that carries a toast across a redirect.

Where things are now

All links are at 1.0.0-alpha.16.

The queue. It is a module-level UNSTABLE_ToastQueue, registered as two utilities, toast/queue and toast/show. show adds an item with a fixed 25 s timeout.
packages/layout/config/toast.ts

The region. AppToast wraps UNSTABLE_ToastRegion.
packages/layout/components/Toast/Toast.tsx

Where it is mounted. Only in the contents layout:
packages/contents/routes/layout.tsx#L130

Who uses it:

  • ErrorToast, from the contents route's ErrorBoundary.
  • The Plate media upload toast.
  • The contents provider.

What is not covered. The public layout (@plone/publicui/routes/index.tsx), the CMS layout (@plone/cmsui/routes/layout.tsx, which hosts the edit form, add form and control panels) and the root Layout mount no region. A toast/show call on any of those pages adds to a queue nothing renders.

There is also a second toast component, @plone/components' Toast: an icon, a title and content, with no queue or region. Two components with the same name, one of which is not connected to the queue, invite the wrong import.

Why it matters

Each of these needs feedback that survives a navigation, or that is raised on a page outside the contents view:

  • Saving a control panel. #220 makes control panels saveable. "Saved" and "could not save" belong in a toast, but the control panel routes live in cmsui's layout, which has no region.
  • Saving content. The edit and add forms are in cmsui's layout too.
  • Add-ons explaining a redirect. This came up while porting pas.plugins.identity to Aurora (collective/pas-plugins-identity#132). When a signed-in user's profile is missing required fields, the add-on holds them until it is complete:
    • Volto redirects to the profile's edit form and says why in a toast: "Please fill in Organisation, Country before you can continue."
    • Aurora has no toast that reaches the edit form, so the add-on had to add a whole page (/complete-profile) just to give that explanation (collective/pas-plugins-identity#139).
    • The same add-on shows "Access withdrawn" inline after withdrawing an OAuth application, where Volto uses a toast (collective/pas-plugins-identity#140).
  • Server redirects. An action that succeeds and redirects, such as save then go to the view, or log in then go to came_from, cannot say anything on the page it lands on. Today the only option is a query parameter that each destination page would have to know about.

Proposal

1. One region for the whole app

Mount AppToast once in the root Layout (apps/aurora/app/root.tsx#L175), inside <body>, and remove the one in the contents layout. The queue is already global, so every page then shows what any page raised. Because the queue lives in module scope, a toast raised just before a client-side navigation is shown on the next page.

A slot such as toasts, with AppToast as the default, would let a theme replace the region without forking the root.

2. A small documented API

The toast/show utility works, but its contract is undocumented, has one fixed timeout, and has no notion of kind. Something like this:

import { toast } from '@plone/layout/toast';

toast.success({ title: t('cmsui.controlpanel.saved') });
toast.error({ title: t('…'), description: error.message });
toast.info({ title: '…', timeout: 8000 }); // timeout optional; null = until dismissed

The proposed design:

  • Built on the existing queue, with className replaced by a kind that the region styles and gives an icon to.
  • Exposed through the registry, keeping toast/show working as an alias for compatibility, so an add-on can call it without importing @plone/layout directly.
  • Merged with the other Toast. @plone/components' Toast (icon, title, content) could become the content renderer of this region, or be renamed.
3. Toasts across a server redirect (flash)

For a loader or action that redirects, a helper in @plone/react-router next to setAuthOnResponse:

return setToastOnResponse(redirect(next), {
  kind: 'success',
  title: 'Saved',
});

How it would work:

  • Writing. The toast is stored in a short-lived, signed, HttpOnly cookie, the way the auth cookie is.
  • Reading. The root loader reads it, returns it, and clears the cookie on the same response.
  • Showing. The root component adds it to the queue on hydration and on each navigation where it appears.

This is the same pattern as Volto's addMessage and Plone Classic's status messages, without a store.

Acceptance
  • A toast raised from any route is visible, whichever layout the page uses: public, cmsui, contents, or one an add-on registers.
  • A toast set on a redirect response is shown once on the page the redirect lands on, and not again on reload.
  • Errors are announced to screen readers. RAC's region already does this, and it should stay that way.

Related

These are other gaps found while porting pas.plugins.identity to Aurora, for context:

  • Add-on endpoints on @plone/client. #221 adds PloneClient.extend() and a clientEndpoints utility. Until it lands, the add-on calls its REST endpoints with its own fetch helper.
  • Saveable control panels. #220, which needs the feedback this issue is about.
  • Filed since:
Dominant language
JavaScript
Stars
10
Forks
9
Avg merge
4d 7h
Merged PRs (30d)
54

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from plone/aurora

All issues in plone/aurora

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.