wrolpi/app/src/components/ui/toast.ts
Roland 08c5c233c8 ui: semantic roles, so night and amber can tell severity apart at all
A component now asks for what a colour MEANS -- `danger` -- and the theme
decides how that looks.  Five roles: neutral, info, success, warning, danger.

The names are the ones already in the tree.  `--primary/--success/--warning/
--danger` were declared in tokens.css with ZERO consumers, and `MessageKind`
was already `info|success|warning|error`; this is one vocabulary instead of
three.  `error` stays a valid kind because 26 call sites write it, and `danger`
is the same thing under the token's name for new code.

The token set is the easy half.  Light and dark spend a hue per role.  Night is
red and nothing else and amber is amber and nothing else, and there a role has
to be a step on a BRIGHTNESS ramp -- which is why this was worth doing:

  night  --orange is byte-identical to --text
         --yellow is byte-identical to --amber
  amber  the same collisions, one hue over

A call site picking between hue names in those themes is picking between
synonyms.  That is why all four toast kinds were the same pixel in night, and
why an error and an informational message were indistinguishable.

Both ramps are built rather than copied: every role clears 3:1 on the panel
(neutral is allowed lower -- for pending and disabled, dim IS the signal),
severity is strictly increasing in contrast, and danger reaches that theme's
own --text brightness so the loudest role is never quieter than prose.  Amber
needed its own scale, not night's: its --text is 8.6:1 where night's is 5.0:1,
so night's ramp would have left every role dimmer than ordinary text.

Fixes in passing: night's info was 2.29:1, under the 3:1 floor.  And Status
loses its three `html[data-theme="night"]` overrides entirely -- it was the one
component already encoding severity by brightness by hand, and the roles are
that generalised.

Roles are registered as Mantine colors too, so `color='danger'` works anywhere
`color='red'` does.

`isMonochromeTheme` is a new predicate in themes/names.  It is the same set as
`explicitOnlyThemes` today and deliberately separate: one is about how a theme
is chosen, the other about what a theme has to work with.

Scope: tokens plus the library.  The ~24 app-level `color='...'` call sites are
a follow-up -- they need a judgement per site, and two of them already disagree
(Downloads.js colours the same "Download error" label `orange` on one line and
`red` on another).  The 22 `violet` uses are not semantic at all and stay hues.

Tests: the ramp claims are asserted per theme in a browser, and the ordering
and danger-vs-text claims run ONLY for the monochrome themes -- in light, blue
is darker than green on white, so a brightness ramp there would fail a design
that is perfectly fine.  Every guard checked red against a planted regression,
including the original bug (warning and danger collapsed to one value in night)
and night's info back at its old 2.29:1.

The roles were folded into each theme's own block rather than kept in a
standalone one, so the existing "same tokens in every theme" and "only red hues
in night" guards cover them for free.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-01 17:54:22 -06:00

89 lines
3.3 KiB
TypeScript

import React from 'react';
import {notifications} from '@mantine/notifications';
import {RoleName} from '../../themes/mantine';
/*
* Toasts.
*
* Keeps the react-semantic-toasts-2 call signature so the ~19 call sites migrate
* by changing their import, not their code. The container is mounted by
* ThemeProvider.
*/
/* `error` is the original spelling and stays; `danger` is the token's name for it. */
export type ToastType = 'info' | 'success' | 'warning' | 'error' | 'danger';
export interface ToastOptions {
type?: ToastType;
title?: string;
description?: string;
/** Milliseconds before auto-dismiss. 0 keeps it until dismissed. */
time?: number;
/**
* Makes the whole toast activate this — Semantic's behaviour, which `Events.js` still
* relies on for the three events that carry a URL: a page shared from the browser
* extension, a finished archive upload, and a generated screenshot.
*/
onClick?: () => void;
}
/*
* Roles, not hues. With hues, all four kinds were the same pixel in night and amber:
* `--yellow` there is byte-identical to `--amber`, and `--orange` to `--text`. An
* error toast was indistinguishable from an informational one, which is the failure
* mode a toast exists to avoid.
*/
const toastRoles: Record<ToastType, RoleName> = {
info: 'info',
success: 'success',
warning: 'warning',
error: 'danger',
danger: 'danger',
};
/**
* The props that make a toast followable.
*
* `tabIndex` and the key handler are not decoration: WROLPi runs on devices with a keyboard
* and no mouse, and a toast only a pointer can follow is a feature half the deployments
* cannot use. There is deliberately no `role` — a `button` or `link` role may not contain
* interactive content, and the dismiss button lives inside.
*/
const followable = (onClick: () => void) => ({
style: {cursor: 'pointer'},
tabIndex: 0,
onClick: (event: React.MouseEvent) => {
// The dismiss button is inside the toast, so its click reaches here too. Closing a
// notification is not following it, and for these three that would be a navigation
// the user did not ask for.
if ((event.target as HTMLElement).closest('button')) {
return;
}
onClick();
},
onKeyDown: (event: React.KeyboardEvent) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
onClick();
}
},
});
export function toast({type = 'info', title, description, time = 5000, onClick}: ToastOptions = {}) {
notifications.show({
title,
message: description,
color: toastRoles[type],
// Semantic treated 0 as "stay up"; Mantine wants false for that. Its `getAutoClose`
// returns any number as given and the container then calls `setTimeout(hide, 0)`, so
// passing the 0 through dismisses the toast on the next tick.
autoClose: time === 0 ? false : time,
withBorder: true,
// Mantine leaves its close button unnamed, which a screen reader reads as "button".
closeButtonProps: {'aria-label': 'Dismiss notification'},
...(onClick ? followable(onClick) : {}),
});
}
/** Dismiss every visible toast. Useful when navigating away from a failed action. */
export const clearToasts = () => notifications.clean();