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>
This commit is contained in:
Roland 2026-08-01 17:31:44 -06:00 • committed by lrnselfreliance
parent aaf187dfc8
commit 08c5c233c8
9 changed files with 365 additions and 65 deletions

View file

@ -1,12 +1,12 @@
import React from 'react';
import {
ActionInput, Button, Header, Icon, IconStack, Loading, MultiSelect, Panel, PathInput,
Placeholder, Statistic, StatisticGroup, TabBar, Table, tabClassName, TextInput,
Placeholder, Statistic, StatisticGroup, Status, TabBar, Table, tabClassName, TextInput,
} from './index';
import {contrastingColor} from '../Common';
import {Notifications} from '@mantine/notifications';
import {toast} from './toast';
import {themeNames} from '../../themes/names';
import {monochromeThemes, themeNames} from '../../themes/names';
/* `--panel` is authored as a hex; computed backgrounds come back as rgb(). */
const hexToRgb = (hex) => {
@ -175,6 +175,147 @@ const sampleTable = <Table>
</Table.Body>
</Table>;
describe('semantic roles read as severity in every theme', () => {
/*
* The point of the roles, and the only thing that makes them worth having.
*
* Light and dark spend a hue per role, so this is nearly free there. Night is red and
* nothing else and amber is amber and nothing else, so a role there is a step on a
* brightness ramp -- and it has to be, because `--orange` in night is byte-identical to
* `--text` and `--yellow` to `--amber`. Picking between hue names in night is picking
* between synonyms, which is why all four toast kinds were the same pixel.
*
* None of this is checkable in jsdom: every value is a token behind two levels of
* var(), and the claim is about resolved luminance.
*/
const ROLES = ['neutral', 'info', 'success', 'warning', 'danger'];
const roleColours = () => {
const root = getComputedStyle(document.documentElement);
const panel = hexToRgb(root.getPropertyValue('--panel').trim());
return {
panel,
text: hexToRgb(root.getPropertyValue('--text').trim()),
roles: ROLES.map(role => ({
role, colour: hexToRgb(root.getPropertyValue(`--${role}`).trim()),
})),
};
};
themeNames.forEach((theme) => {
it(`gives every role a colour of its own in ${theme}`, () => {
// True everywhere. Two roles resolving to the same value is the original bug.
cy.mountUI(<Panel>roles</Panel>, {theme});
cy.get('.wrolpi-panel').should(() => {
const {roles} = roleColours();
const colours = roles.map(r => r.colour);
colours.forEach((colour, index) => {
expect(colour, `${roles[index].role} resolved`).to.match(/^rgb\(/);
});
expect(new Set(colours).size, `five distinct roles, got ${colours.join(' ')}`)
.to.equal(5);
});
});
it(`keeps every role legible in ${theme}`, () => {
cy.mountUI(<Panel>roles</Panel>, {theme});
cy.get('.wrolpi-panel').should(() => {
const {panel, roles} = roleColours();
roles.forEach(({role, colour}) => {
/*
* 3:1 is the floor for anything that is not body text. `neutral` is
* allowed to sit at the bottom of the ramp -- for `pending` and disabled,
* being dim IS the signal -- but it still has to be visible at all.
*/
expect(contrast(colour, panel), `${role} against the panel`)
.to.be.at.least(role === 'neutral' ? 2.5 : 3);
});
});
});
it(`gives each Status kind a colour of its own in ${theme}`, () => {
/*
* The end of the chain, through a real component: four states, four distinct
* painted colours. In night these used to need three hand-written
* `html[data-theme="night"]` overrides; the roles replaced all of them.
*/
cy.mountUI(
<Panel>
<Status kind='complete'>complete</Status>
<Status kind='active'>downloading</Status>
<Status kind='pending'>pending</Status>
<Status kind='failed'>failed</Status>
</Panel>,
{theme},
);
cy.get('.wrolpi-status').should(($all) => {
expect($all.length).to.equal(4);
const colours = [...$all].map(el => getComputedStyle(el).color);
expect(new Set(colours).size, `four distinct colours, got ${colours.join(' ')}`)
.to.equal(4);
});
});
});
monochromeThemes.forEach((theme) => {
it(`orders the roles by brightness in ${theme}, which is all it has`, () => {
/*
* Only here. Light and dark spend a hue per role, so their contrast order is
* an accident of which hues were chosen -- in light, blue is DARKER than green
* on white, so asserting a ramp there fails on a design that is perfectly fine.
* With hue gone, though, ordering is the only thing severity can be encoded in.
*/
cy.mountUI(<Panel>roles</Panel>, {theme});
cy.get('.wrolpi-panel').should(() => {
const {panel, roles} = roleColours();
const ratios = roles.map(({role, colour}) => ({role, ratio: contrast(colour, panel)}));
ratios.slice(1).forEach((entry, index) => {
expect(entry.ratio, `${entry.role} is louder than ${ratios[index].role}`)
.to.be.greaterThan(ratios[index].ratio);
});
});
});
it(`makes danger at least as loud as ordinary text in ${theme}`, () => {
/*
* Also only here. A light theme's text is near-black at ~14:1 and no danger
* colour will ever match it; on a single hue the roles and the text are drawn
* from the same ramp, so the loudest role must not be quieter than the prose.
*/
cy.mountUI(<Panel>roles</Panel>, {theme});
cy.get('.wrolpi-panel').should(() => {
const {panel, text, roles} = roleColours();
const danger = roles.find(r => r.role === 'danger').colour;
expect(contrast(danger, panel), 'danger vs --text against the panel')
.to.be.at.least(contrast(text, panel) * 0.95);
});
});
});
it('does not leave failure to colour alone', () => {
// Brightness is the first thing lost on a dim screen, and it is all night has.
cy.mountUI(
<Panel>
<Status kind='failed'>failed</Status>
<Status kind='complete'>complete</Status>
</Panel>,
{theme: 'night'},
);
cy.get('.wrolpi-status-failed').should(($failed) => {
const other = Cypress.$('.wrolpi-status-complete')[0];
expect(parseInt(getComputedStyle($failed[0]).fontWeight, 10), 'failed is heavier')
.to.be.greaterThan(parseInt(getComputedStyle(other).fontWeight, 10));
});
});
});
describe('a table header is a surface, not just bold text', () => {
themeNames.forEach((theme) => {
it(`separates the header from every body row in ${theme}`, () => {