mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-30 03:46:29 -05:00
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:
parent
bbe3cac9a8
commit
bbb267aa83
8 changed files with 690 additions and 66 deletions
|
|
@ -29,6 +29,7 @@ import {
|
|||
import {useBluetooth, useDesktop, useHotspot, useSearchDirectories, useSearchOrder, useThrottle, useVnc, useWROLMode} from "../hooks/customHooks";
|
||||
import {Media, SettingsContext, StatusContext, ThemeContext} from "../contexts/contexts";
|
||||
import {themeChoices} from "../themes/names";
|
||||
import {contrastRatio as measureContrast} from "../themes/contrast";
|
||||
import {FilePreviewContext} from "./FilePreview";
|
||||
import _ from "lodash";
|
||||
import {killDownloads, startDownloads, unlockCookies} from "../api";
|
||||
|
|
@ -1575,52 +1576,28 @@ export const toLocaleString = (num, locale = 'en-US') => {
|
|||
return num.toLocaleString(locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* WCAG relative luminance: sRGB channels linearised first, then Rec. 709 weighted.
|
||||
/*
|
||||
* The WCAG primitives now live in themes/contrast, so the theme code can measure a colour
|
||||
* without importing this module -- Common.js pulls in half the component library, and
|
||||
* `themes/` importing it is how an import cycle starts. Forwarded because a good many call
|
||||
* sites and tests already take `contrastRatio` from here.
|
||||
*
|
||||
* The linearisation is the part that matters. Weighting the gamma-encoded bytes directly --
|
||||
* which is what this did before -- is a rough approximation that misjudges mid-tone blues and
|
||||
* purples badly enough to pick the wrong text colour for them.
|
||||
*/
|
||||
function relativeLuminance(color) {
|
||||
const rgb = (typeof color === 'string') ? hexToRGBArray(color) : color;
|
||||
if (!rgb) {
|
||||
return 0;
|
||||
}
|
||||
const linear = rgb.map(value => {
|
||||
const channel = value / 255;
|
||||
return channel <= 0.03928 ? channel / 12.92 : Math.pow((channel + 0.055) / 1.055, 2.4);
|
||||
});
|
||||
return (0.2126 * linear[0]) + (0.7152 * linear[1]) + (0.0722 * linear[2]);
|
||||
}
|
||||
|
||||
/** WCAG contrast ratio between two colours, from 1:1 (identical) to 21:1 (black on white). */
|
||||
export function contrastRatio(a, b) {
|
||||
const luminances = [relativeLuminance(a), relativeLuminance(b)].sort((x, y) => y - x);
|
||||
return (luminances[0] + 0.05) / (luminances[1] + 0.05);
|
||||
}
|
||||
|
||||
/**
|
||||
* A hex colour as [r, g, b], accepting both `#rrggbb` and the shorthand `#rgb`.
|
||||
* It is forwarded by a function that delegates rather than by `export {contrastRatio}`, and
|
||||
* that is load-bearing. A bare re-export compiles to a getter that dereferences the imported
|
||||
* module, and six specs mock this module with `{...jest.requireActual('./Common')}` -- a
|
||||
* spread invokes every getter, and those spreads run re-entrantly while Common.js is still
|
||||
* evaluating (Common -> customHooks -> Common, both mocked that way). The getter fires
|
||||
* before the import binding is assigned, throws `Cannot read properties of undefined`, and
|
||||
* takes the whole suite with it. A function is read by the spread but not called, so it
|
||||
* dereferences nothing until evaluation has finished.
|
||||
*
|
||||
* The shorthand matters because tag colours are not only chosen in the colour picker: they
|
||||
* live in the database and in a config file, and a config is something a user edits by hand
|
||||
* The hex parser accepting the `#rgb` shorthand matters and is not incidental: tag colours
|
||||
* are not only chosen in the colour picker, they live in a config file a user edits by hand
|
||||
* -- configs are the source of truth in WROLPi. Rejecting `#fff` left the luminance at zero,
|
||||
* so a near-white tag was treated as black and handed light text.
|
||||
*/
|
||||
function hexToRGBArray(color) {
|
||||
let hex = (typeof color === 'string' ? color : '').replace(/^#/, '');
|
||||
if (hex.length === 3) {
|
||||
// #abc means #aabbcc, not #0a0b0c.
|
||||
hex = hex.split('').map(digit => digit + digit).join('');
|
||||
}
|
||||
if (!/^[0-9a-fA-F]{6}$/.test(hex)) {
|
||||
console.error('Invalid hex color: ' + color);
|
||||
return;
|
||||
}
|
||||
let rgb = [];
|
||||
for (let i = 0; i <= 2; i++) rgb[i] = parseInt(hex.substr(i * 2, 2), 16);
|
||||
return rgb;
|
||||
export function contrastRatio(a, b) {
|
||||
return measureContrast(a, b);
|
||||
}
|
||||
|
||||
export const TAG_TEXT_DARK = '#000000';
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import {useReorganizationStatus} from "../contexts/FileWorkerStatusContext";
|
|||
import {SearchIconButton} from "./Search";
|
||||
import {HELP_VIEWER_URI, NAME, semanticUIColorMap} from "./Vars";
|
||||
import {useOverflowNav} from "../hooks/useOverflowNav";
|
||||
import {defaultNavColor, navBarStyle, useNavColors} from "../themes/navColors";
|
||||
import _ from "lodash";
|
||||
|
||||
function updateFavicon(colorName) {
|
||||
|
|
@ -141,7 +142,7 @@ function NavIconWrapper({children}) {
|
|||
function useNavColorSetting() {
|
||||
// Use localstorage to avoid flickering navbar color on startup.
|
||||
const {settings} = React.useContext(SettingsContext);
|
||||
const [navColor, setNavColor] = useLocalStorage('nav_color', 'violet');
|
||||
const [navColor, setNavColor] = useLocalStorage('nav_color', defaultNavColor);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!_.isEmpty(settings)) {
|
||||
|
|
@ -163,7 +164,12 @@ export function NavBar() {
|
|||
const wrolModeEnabled = useWROLMode();
|
||||
const {status} = React.useContext(StatusContext);
|
||||
const navColor = useNavColorSetting();
|
||||
const wrolpiIcon = <img src='/icon.svg' height='32px' width='32px' alt='WROLPi Home Icon'/>;
|
||||
/*
|
||||
* The bar's foreground is measured against the background the theme resolved for the
|
||||
* user's colour, not taken from a single token. See themes/navColors.
|
||||
*/
|
||||
const navColors = useNavColors(navColor);
|
||||
const wrolpiIcon =<img src='/icon.svg' height='32px' width='32px' alt='WROLPi Home Icon'/>;
|
||||
const name = <i>{NAME || wrolpiIcon}</i>;
|
||||
const topNavText = wrolModeEnabled ? <>{name} <Icon name='lock'/></> : name;
|
||||
|
||||
|
|
@ -362,15 +368,7 @@ export function NavBar() {
|
|||
|
||||
return <>
|
||||
<Media between={['mobile', 'tablet']}>
|
||||
<nav className='wrolpi-navbar' id='global_navbar'
|
||||
style={{
|
||||
background: `var(--${navColor})`,
|
||||
color: 'var(--btn-text)',
|
||||
// The hotspot glyph stacks two icons, and the corner one paints a disc
|
||||
// of its surface behind itself. The bar's colour is the user's choice,
|
||||
// so it has to be handed down rather than looked up from a token.
|
||||
'--icon-stack-bg': `var(--${navColor})`,
|
||||
}}>
|
||||
<nav className='wrolpi-navbar' id='global_navbar' style={navBarStyle(navColors)}>
|
||||
{homeLink}
|
||||
<div className='wrolpi-navbar-right'>
|
||||
{icons}
|
||||
|
|
@ -380,12 +378,12 @@ export function NavBar() {
|
|||
</nav>
|
||||
</Media>
|
||||
<Media greaterThanOrEqual='tablet'>
|
||||
<DesktopNav navColor={navColor} homeLink={homeLink} icons={icons}/>
|
||||
<DesktopNav navColors={navColors} homeLink={homeLink} icons={icons}/>
|
||||
</Media>
|
||||
</>
|
||||
}
|
||||
|
||||
function DesktopNav({navColor, homeLink, icons}) {
|
||||
function DesktopNav({navColors, homeLink, icons}) {
|
||||
const containerRef = React.useRef(null);
|
||||
const homeRef = React.useRef(null);
|
||||
const rightMenuRef = React.useRef(null);
|
||||
|
|
@ -400,15 +398,7 @@ function DesktopNav({navColor, homeLink, icons}) {
|
|||
|
||||
return (
|
||||
<div ref={containerRef}>
|
||||
<nav className='wrolpi-navbar' id='global_navbar'
|
||||
style={{
|
||||
background: `var(--${navColor})`,
|
||||
color: 'var(--btn-text)',
|
||||
// The hotspot glyph stacks two icons, and the corner one paints a disc
|
||||
// of its surface behind itself. The bar's colour is the user's choice,
|
||||
// so it has to be handed down rather than looked up from a token.
|
||||
'--icon-stack-bg': `var(--${navColor})`,
|
||||
}}>
|
||||
<nav className='wrolpi-navbar' id='global_navbar' style={navBarStyle(navColors)}>
|
||||
<span ref={homeRef} style={{display: 'contents'}}>{homeLink}</span>
|
||||
{(isReady ? visibleLinks : allLinks).map((link, idx) => (
|
||||
<span
|
||||
|
|
|
|||
|
|
@ -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> <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 → 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'}}>
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ import {CardPoster, contrastingColor, HelpHeader, LoadStatistic} from '../Common
|
|||
import {Notifications} from '@mantine/notifications';
|
||||
import {clearToasts, toast} from './toast';
|
||||
import {monochromeThemes, themeNames} from '../../themes/names';
|
||||
import {navColorNames} from '../../themes/navColors';
|
||||
import {NavBarSample} from '../ThemeSamplePage';
|
||||
|
||||
/* `--panel` is authored as a hex; computed backgrounds come back as rgb(). */
|
||||
const hexToRgb = (hex) => {
|
||||
|
|
@ -2071,3 +2073,128 @@ describe('pagination sits in the middle of its container', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('the navigation bar is legible on every colour a user can pick', () => {
|
||||
/*
|
||||
* The bar's background is the one surface the USER chooses, by hue name, and each theme
|
||||
* resolves those twelve names to twelve of its own colours. Forty-eight backgrounds; the
|
||||
* links and status icons have to read on all of them. They did not -- every one was
|
||||
* drawn in `--btn-text`, near-black in three of the four themes, against backgrounds like
|
||||
* night's `--brown` (#451212). That glyph was 1.33:1 against the bar behind it.
|
||||
*
|
||||
* navColors.test.js measures the mapping against the palette parsed out of tokens.css,
|
||||
* which is the cheap and exhaustive half. What it cannot see is whether the value the
|
||||
* mapping returns reaches the pixels: whether the tokens resolve through the cascade,
|
||||
* whether the style lands on the bar, and what an icon inside it actually inherits. So
|
||||
* this measures the painted result and holds it against the number the component claims.
|
||||
*
|
||||
* `NavBarSample` is the gallery's bar rather than <NavBar/> itself, which needs the
|
||||
* settings, status and worker contexts and a dozen polling hooks. Both build their style
|
||||
* with the same `navBarStyle(useNavColors(colour))` call, and navColors.test.js has a
|
||||
* source guard holding NavBar to that.
|
||||
*/
|
||||
const barsFor = (theme) => {
|
||||
cy.mountUI(<div>
|
||||
{navColorNames.map(color => <NavBarSample key={color} color={color}/>)}
|
||||
</div>, {theme});
|
||||
};
|
||||
|
||||
themeNames.forEach((theme) => {
|
||||
it(`paints a resolved background for all twelve colours in ${theme}`, () => {
|
||||
barsFor(theme);
|
||||
|
||||
navColorNames.forEach((color) => {
|
||||
cy.get(`[data-nav-sample="${color}"] .wrolpi-navbar`).should(($nav) => {
|
||||
const background = getComputedStyle($nav[0]).backgroundColor;
|
||||
// An unresolved token paints nothing at all, and a transparent bar over
|
||||
// the page would read as "the colour just looks wrong" rather than as a
|
||||
// broken style.
|
||||
expect(background, `${theme}/${color} bar background`)
|
||||
.to.not.equal('rgba(0, 0, 0, 0)');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it(`clears 3:1 on every colour in ${theme}`, () => {
|
||||
/*
|
||||
* 3:1 is the WCAG floor for graphical objects, which is what these icons are.
|
||||
* Twelve of the forty-eight combinations were below it before measurement.
|
||||
* Ten are still under 4.5:1 and cannot be lifted by any foreground -- a
|
||||
* monochrome theme resolves all twelve names onto one ramp, and a mid-tone of a
|
||||
* hue carries no text of that same hue. That residue is the palette's, and it
|
||||
* is printed beside each sample in the gallery so it can be decided on.
|
||||
*/
|
||||
barsFor(theme);
|
||||
|
||||
navColorNames.forEach((color) => {
|
||||
cy.contrastRatio(`[data-nav-sample="${color}"] .wrolpi-navbar-link`)
|
||||
.then((ratio) => {
|
||||
expect(ratio, `${theme}/${color} link contrast`).to.be.greaterThan(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it(`gives its icons the same foreground as its links in ${theme}`, () => {
|
||||
// The actual complaint was about the icons, not the words. They take `color`
|
||||
// by inheritance, which is a thing a stylesheet can quietly break.
|
||||
barsFor(theme);
|
||||
|
||||
navColorNames.forEach((color) => {
|
||||
cy.get(`[data-nav-sample="${color}"]`).should(($sample) => {
|
||||
const link = $sample[0].querySelector('.wrolpi-navbar-link');
|
||||
const icon = $sample[0].querySelector('.wrolpi-navbar-right svg');
|
||||
expect(icon, `${theme}/${color} has an icon to measure`).to.not.be.null;
|
||||
expect(getComputedStyle(icon).color, `${theme}/${color} icon colour`)
|
||||
.to.equal(getComputedStyle(link).color);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it(`paints the ratio it reports in ${theme}`, () => {
|
||||
/*
|
||||
* Ties the number to the pixels. Without this the gallery could print a
|
||||
* comfortable figure beside a bar drawn in something else entirely, and both
|
||||
* halves would look right on their own.
|
||||
*/
|
||||
barsFor(theme);
|
||||
|
||||
navColorNames.forEach((color) => {
|
||||
cy.get(`[data-nav-sample-ratio="${color}"]`).invoke('text').then((text) => {
|
||||
const reported = parseFloat(text);
|
||||
expect(reported, `${theme}/${color} reported a ratio`).to.be.greaterThan(1);
|
||||
cy.contrastRatio(`[data-nav-sample="${color}"] .wrolpi-navbar-link`)
|
||||
.then((painted) => {
|
||||
expect(painted, `${theme}/${color} painted vs reported`)
|
||||
.to.be.closeTo(reported, 0.05);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('does not simply use --btn-text everywhere, which is what it replaced', () => {
|
||||
/*
|
||||
* The inverse. Every case above would pass on the old fixed foreground in the two
|
||||
* themes where `--btn-text` happens to be the right end of the palette, so without
|
||||
* this the suite could go green on a revert. Night is where the difference is
|
||||
* largest: `--brown` takes `--white` (#ff8a8a) and `--red` keeps `--btn-text`, so
|
||||
* the bar's foreground is demonstrably not one value.
|
||||
*/
|
||||
cy.mountUI(<div>
|
||||
<NavBarSample color='brown'/>
|
||||
<NavBarSample color='red'/>
|
||||
</div>, {theme: 'night'});
|
||||
|
||||
cy.get('[data-nav-sample="brown"] .wrolpi-navbar-link').then(($brown) => {
|
||||
cy.get('[data-nav-sample="red"] .wrolpi-navbar-link').then(($red) => {
|
||||
const brown = getComputedStyle($brown[0]).color;
|
||||
const red = getComputedStyle($red[0]).color;
|
||||
expect(brown, 'brown and red bars take different foregrounds')
|
||||
.to.not.equal(red);
|
||||
// And the one that changed is the one that was unreadable.
|
||||
expect(toRgb(brown), 'brown no longer takes --btn-text')
|
||||
.to.not.equal(toRgb('#0a0000'));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue