ui: the navbar measures its foreground instead of assuming one

The bar's background is the one surface a user picks -- twelve colour names,
which each theme resolves to twelve of its own colours.  Forty-eight
backgrounds, and the links and status icons were drawn on all of them in a
single token, `--btn-text`, which is near-black in three of the four themes.

Night's `--brown` is #451212.  A near-black glyph on it is 1.33:1.  Nine of
night's twelve colours and seven of amber's sat below 4.5:1, and twelve of the
forty-eight below 3:1 -- icons the same colour as the bar behind them.

So the foreground is measured against the background the theme actually
resolved, choosing between that theme's own `--btn-text`, `--black` and
`--white`.  Its own, not literal ones: a white pixel in night mode undoes the
dark adaptation the theme exists to protect, so night's "white" is #ff8a8a.
Measuring at runtime rather than shipping an `--on-red` token per colour also
means the custom YAML themes still ahead get this for free.

Nothing is below 3:1 any more and the worst case is 3.27:1, up from 1.33:1.
No combination is worse than it was.

Ten of the forty-eight are still under 4.5:1 and no foreground will lift them:
a monochrome theme resolves all twelve names onto one ramp, and a mid-tone of a
hue cannot carry text of that same hue.  That shortfall is in the palette, so
/theme-sample now draws all twelve bars with the measured ratio printed beside
each one, calling out the ten -- it is a decision to make while looking at them.

The WCAG primitives move to themes/contrast so theme code can measure a colour
without importing Common.js, which pulls in half the component library.
Common.js forwards `contrastRatio` with a delegating function rather than a bare
re-export: six specs mock it with `{...jest.requireActual('./Common')}`, the
spread invokes every getter, and those spreads run re-entrantly while the module
is still evaluating -- a re-export getter fires before its import binding exists
and takes the whole suite down.

Tested: navColors.test.js measures all forty-eight against the palette parsed
out of tokens.css, and holds Nav.js to the call by source since neither bar is
reachable from a test that could measure it.  ui-layout.cy.js measures the
painted result in a real browser in all four themes and holds it against the
number the component reports.  Reverting to the fixed foreground reddens four
jest cases and three cypress ones.

1115 jest / 62 suites, 177 in ui-layout.cy.js, clean npm run build.
This commit is contained in:
Roland 2026-08-02 14:07:19 -06:00
parent bbe3cac9a8
commit bbb267aa83
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
8 changed files with 690 additions and 66 deletions

View file

@ -43,6 +43,8 @@ import {
} from './ui';
import {semanticColorNames} from '../themes/mantine';
import {contrastingColor} from './Common';
import {navBarStyle, navColorNames, useNavColors} from '../themes/navColors';
import {IconMenu2, IconSearch} from '@tabler/icons-react';
/*
* A gallery of every component in the library, in the current theme.
@ -76,6 +78,54 @@ const Row = ({children}) => <div style={{display: 'flex', gap: 10, flexWrap: 'wr
{children}
</div>;
/*
* One navigation bar, in one of the twelve colours a user can pick for it.
*
* The bar is the only surface in the app whose background the USER chooses, and every theme
* resolves those twelve names to twelve of its own colours -- forty-eight backgrounds that
* the same links and status icons have to stay legible on. They did not: the bar drew every
* one of them in `--btn-text`, which is near-black in three of the four themes, and night's
* `--brown` is #451212. That glyph was 1.33:1 against the bar behind it.
*
* These are the real `.wrolpi-navbar` chrome and the real `navBarStyle`, not a mock-up, so a
* sample cannot show a bar the app does not draw. The measured ratio is printed beside each
* one because ten of the forty-eight still sit under 4.5:1 and no foreground will lift them
* -- a monochrome theme resolves all twelve names onto one ramp, and a mid-tone of a hue
* cannot carry text of that same hue. Fixing those means changing the palette, which is a
* decision to make while looking at them.
*/
const AA_CONTRAST = 4.5;
export const NavBarSample = ({color}) => {
const colors = useNavColors(color);
const short = colors.ratio !== null && colors.ratio < AA_CONTRAST;
return <div data-nav-sample={color} style={{marginBottom: 8}}>
<nav className='wrolpi-navbar' style={{...navBarStyle(colors), minHeight: '2.8em'}}>
<span className='wrolpi-navbar-link'><i>WROLPi</i>&nbsp;<Icon name='lock'/></span>
<span className='wrolpi-navbar-link'>Videos</span>
<span className='wrolpi-navbar-link'>Map</span>
<div className='wrolpi-navbar-right' style={{gap: '0.5em', paddingRight: '0.5em'}}>
<Icon name='warning sign' size='large' label='Drive health warning'/>
<Icon name='hdd' size='large' label='Drive temperature warning'/>
<Icon name='plug' size='large' label='API is not responding'/>
<IconButton icon={IconSearch} label='Search' variant='subtle'/>
<IconButton icon={IconMenu2} label='Menu' variant='subtle'/>
</div>
</nav>
<div style={{
fontSize: 11, color: short ? 'var(--danger)' : 'var(--muted)',
padding: '3px 2px 0', display: 'flex', gap: 8,
}}>
<strong style={{fontWeight: 600}}>{color}</strong>
<span data-nav-sample-ratio={color}>
{colors.ratio === null ? '—' : `${colors.ratio.toFixed(2)}:1`}
</span>
{short && <span>below 4.5:1 — the palette has no better option</span>}
</div>
</div>
};
// Numbers the stacked toasts so it is obvious which press produced which, and that a second
// press adds a toast rather than replacing the first. Module scope, so it survives re-renders.
let toastCounter = 0;
@ -109,6 +159,20 @@ export function ThemeSamplePage() {
</Panel>
</Section>
<Section label='Navigation bars'>
<Panel>
<p style={{fontSize: 12, color: 'var(--muted)', marginTop: 0}}>
Every colour the navigation bar can be set to (Settings &rarr; navbar
colour), in the <strong>{theme}</strong> theme. The text and icons are not
a fixed colour: each bar is measured, and drawn in whichever of this
theme's own colours reads best on it. The ratio under each bar is that
measurement — anything under 4.5:1 is called out, and means the palette
itself has no colour that reads on that background.
</p>
{navColorNames.map(color => <NavBarSample key={color} color={color}/>)}
</Panel>
</Section>
<Section label='Media filtering'>
<Panel>
<div style={{display: 'flex', gap: 18, flexWrap: 'wrap', alignItems: 'flex-start'}}>