mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 13:19:58 -05:00
Acting on the review of #621. No bugs were found; these are the four suggestions and two nits. The real one: measurement accepted only hex. `relativeLuminance` returned 0 for anything else, which is black's luminance and indistinguishable from it -- so a token written `rgb(92 79 168)` measured as pure black, the "best" foreground was chosen against a colour the theme does not contain, and the wrong hex was then frozen into an inline style where CSS could no longer correct it. Silent, and worse than not measuring. `parseColor` now reads `rgb()`/`rgba()` in both the comma and space forms, percentages, and `#rgba`/`#rrggbbaa`; `isMeasurable` lets a caller tell "unreadable" from "black"; and navColorsFrom falls back to the tokens when the background or every candidate is unreadable. Unreadable candidates are filtered rather than the whole measurement abandoned, so one oddly written token does not cost the other two. This matters because the header claimed custom YAML themes get correct foregrounds for free, and a hand-written palette is exactly where a non-hex value will first appear. The Cypress inverse hardcoded night's `--btn-text` as `#0a0000`. The unit tests parse tokens.css precisely so a palette edit cannot leave a stale claim behind, and that literal was the one place the rule was broken. It reads the token from the theme now, asserts it resolved, and additionally pins that red still takes `--btn-text` while brown no longer does. Documented rather than changed: the warning indicators still pick their colour from the `conflictingColors` name list. It is the same defect as the one fixed here and measurably worse -- `--danger` and `--warning` are 1.0-1.8:1 against the bar in every theme, light and dark included, because a red icon on a red bar is a red icon on a red bar. The fix is not a better colour: severity has to stay distinguishable from the bar's text as well as from itself, and a monochrome theme has no third brightness, so it has to stop being a hue. That is a redesign, and the note now sits beside the residue in navColors.ts rather than only in a PR description. The gallery draws the indicators as the bar draws them when nothing is wrong; it does not stage a fake overheating Pi to exhibit a fault nobody has agreed how to fix. Also documented: re-measurement is triggered by `data-theme`, so a future custom-theme editor patching custom properties in place would leave the bar on the values it last measured. There is no event for "a custom property was assigned" to observe instead, so the note says what such an editor must do. Nits: "Eleven" in a comment where the assertion locks ten (and a stale 2.91:1 beside it, now 3.27:1), and a missing space in `const wrolpiIcon =<img`. Six new unit cases, planted: dropping the fallback reddens two. 1121 jest / 62 suites, 196 in ui-layout.cy.js, clean npm run build.
191 lines
9.5 KiB
TypeScript
191 lines
9.5 KiB
TypeScript
import React from 'react';
|
|
import {bestForeground, contrastRatio, isMeasurable} from './contrast';
|
|
|
|
|
|
/*
|
|
* The navigation bar's colours.
|
|
*
|
|
* The bar's background is the one colour in the interface the USER picks (Settings ->
|
|
* navbar colour), and it is picked by hue name -- `violet`, `olive`, `brown` -- which each
|
|
* theme then resolves to its own hex. So the bar has a background the theme cannot know in
|
|
* advance, and its links and status icons have to stay legible on all twelve of them in all
|
|
* four themes: forty-eight combinations, not one.
|
|
*
|
|
* It used to draw them all with the single `--btn-text` token, which is white in light and
|
|
* near-black in the other three. Near-black on night's `--brown` (#451212) is 1.33:1 and on
|
|
* amber's (#452708) 1.48:1 -- nine of night's twelve colours and seven of amber's sat below
|
|
* 4.5:1, which is why the bar's icons were unreadable. `--btn-text` is the right token for
|
|
* a button, whose fill the theme chooses; it was never the right one here.
|
|
*
|
|
* So the foreground is MEASURED instead, against the background the theme actually resolved.
|
|
* The two candidates are the theme's own `--black` and `--white` rather than literal black and
|
|
* white, because a literal white pixel in night mode defeats the entire point of the mode --
|
|
* night's "white" is #ff8a8a, a bright red, and amber's is #ffc875.
|
|
*
|
|
* Measuring at runtime rather than shipping a paired `--on-red`/`--on-olive` token for every
|
|
* colour also means a theme only has to supply a palette: the correct foreground for each
|
|
* entry falls out of it, with nothing extra to declare and nothing to keep in step.
|
|
*
|
|
* What is NOT measured, and should be: the warning indicators. NavBar still picks their
|
|
* colour from `conflictingColors`, a hardcoded list of navbar names that swaps `yellow`/`red`
|
|
* for `null`/`black` on the five bars those hues would blend into. It is the same defect
|
|
* this replaced -- a name list standing in for a measurement -- and it is worse than the one
|
|
* fixed here rather than better: `--danger` and `--warning` measure 1.0-1.8:1 against the bar
|
|
* in EVERY theme, light and dark included, because a red icon on a red bar is a red icon on a
|
|
* red bar. It is left alone deliberately, because the fix is not a better colour. Those
|
|
* icons have to stay distinguishable from each other AND from the bar's own text, and in a
|
|
* monochrome theme there is no third brightness to spend, so severity has to stop being a
|
|
* hue -- a filled badge, or a shape. That is a redesign of how severity reads, not a
|
|
* substitution, and it is not this change.
|
|
*
|
|
* /theme-sample draws the indicators as they are, taking the measured bar colour, which is
|
|
* what the bar does when nothing is wrong. It does not stage a fake overheating Pi to
|
|
* exhibit a fault nobody has agreed how to fix.
|
|
*
|
|
* Two limits on the measuring that IS done here, both of which the custom YAML themes will
|
|
* meet before anything else does. A token has to be a colour these measurements can read -- hex or `rgb()`; anything
|
|
* else falls back to CSS rather than being measured (see navColorsFrom). And re-measurement
|
|
* is triggered by `data-theme` changing, which is how a theme is applied today; a future
|
|
* editor that patches custom properties in place without touching the attribute would leave
|
|
* the bar on the values it last measured (see useNavColors).
|
|
*/
|
|
|
|
/**
|
|
* The colours offered for the bar, in picker order.
|
|
*
|
|
* The same keys as `semanticUIColorMap` in components/Vars.js, which maps them to the fixed
|
|
* hexes used for the favicon and the mobile status bar (those are files and OS chrome, so
|
|
* they cannot take a token). navColors.test.js holds the two lists together.
|
|
*/
|
|
export const navColorNames: string[] = [
|
|
'red', 'orange', 'yellow', 'olive', 'green', 'teal',
|
|
'blue', 'violet', 'purple', 'pink', 'brown', 'grey',
|
|
];
|
|
|
|
export const defaultNavColor = 'violet';
|
|
|
|
export interface NavColors {
|
|
/** CSS colour for the bar itself. */
|
|
background: string;
|
|
/** CSS colour its text and icons take. */
|
|
color: string;
|
|
/**
|
|
* Measured contrast of `color` on `background`, or null when the tokens could not be
|
|
* read and the pair below is the un-measured fallback. Surfaced so /theme-sample can
|
|
* show the number next to the sample.
|
|
*/
|
|
ratio: number | null;
|
|
}
|
|
|
|
/**
|
|
* The bar's colours, given a way to read theme tokens.
|
|
*
|
|
* Takes a reader rather than an element so it can be measured against the real palette in a
|
|
* unit test without a browser -- the shipped tokens are parsed out of tokens.css and handed
|
|
* in. Forty-eight combinations is far too many to eyeball.
|
|
*/
|
|
export function navColorsFrom(read: (name: string) => string, navColor: string): NavColors {
|
|
const name = navColorNames.includes(navColor) ? navColor : defaultNavColor;
|
|
const background = read(`--${name}`);
|
|
/*
|
|
* `--btn-text` leads because it is the token this used to use unconditionally, so where
|
|
* it was already the best choice nothing changes -- and in the monochrome themes it is
|
|
* genuinely the darkest thing the theme has (night's is #0a0000 against a `--black` of
|
|
* #2a0808), which makes it the right foreground for every bright bar. It was never
|
|
* wrong; it was just applied without looking at what it was being drawn on.
|
|
*/
|
|
const candidates = [read('--btn-text'), read('--black'), read('--white')].filter(Boolean);
|
|
|
|
const measurable = candidates.filter(isMeasurable);
|
|
|
|
if (!isMeasurable(background) || measurable.length === 0) {
|
|
/*
|
|
* Nothing readable to measure, so measure nothing and let CSS do its job.
|
|
*
|
|
* Two cases reach here. The ordinary one is that there is no stylesheet yet -- jsdom,
|
|
* or a very early render -- and the reader returns empty strings. The other is a
|
|
* token this cannot parse: a hand-written theme writing `rgb(90 79 168)` or
|
|
* `color-mix(...)` rather than a hex. That one used to be silent and worse than
|
|
* useless: an unparseable colour measured as luminance zero, indistinguishable from
|
|
* black, so the bar picked its foreground against a colour the theme does not contain
|
|
* and then froze the wrong hex into an inline style, where CSS could not correct it.
|
|
*
|
|
* Falling back to the tokens themselves is the pre-measurement behaviour: the bar is
|
|
* painted by CSS, follows the theme, and is only ever wrong about the foreground --
|
|
* which is a great deal better than being confidently wrong about both.
|
|
*/
|
|
return {background: `var(--${name})`, color: 'var(--btn-text)', ratio: null};
|
|
}
|
|
|
|
const color = bestForeground(background, measurable) as string;
|
|
return {background, color, ratio: contrastRatio(color, background)};
|
|
}
|
|
|
|
/** The bar's colours read from a live document. */
|
|
export function resolveNavColors(
|
|
navColor: string,
|
|
element: Element = document.documentElement,
|
|
): NavColors {
|
|
const styles = window.getComputedStyle(element);
|
|
return navColorsFrom(name => styles.getPropertyValue(name).trim(), navColor);
|
|
}
|
|
|
|
/**
|
|
* The inline style for a bar painted in these colours.
|
|
*
|
|
* Shared by the real navigation bar and the /theme-sample gallery, so a sample cannot end
|
|
* up demonstrating a bar the app does not actually draw.
|
|
*/
|
|
export function navBarStyle(colors: NavColors): React.CSSProperties {
|
|
return {
|
|
background: colors.background,
|
|
color: colors.color,
|
|
/*
|
|
* 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': colors.background,
|
|
} as React.CSSProperties;
|
|
}
|
|
|
|
/**
|
|
* The bar's colours, kept current as the theme changes.
|
|
*
|
|
* Watches the `data-theme` attribute rather than subscribing to ThemeContext, because the
|
|
* attribute is what the tokens actually key off and it is stamped by ThemeProvider in an
|
|
* effect of its own. React runs child effects before parent effects, so a consumer that
|
|
* re-read the tokens when the context value changed would read them one theme late -- and
|
|
* the bar would sit on the previous theme's foreground until something else re-rendered it.
|
|
*
|
|
* The attribute is also stamped by the inline script in index.html before React loads, and
|
|
* that is a change no context can report at all.
|
|
*
|
|
* This assumes token VALUES only change when `data-theme` does, which holds for every way a
|
|
* theme is applied today. It will not hold for a custom-theme editor that rewrites custom
|
|
* properties in place: the measured pair is a snapshot of hexes, so the bar would keep the
|
|
* old one until the theme name or the chosen colour changed. When that editor exists it
|
|
* should either re-stamp the attribute or announce the change for this to observe -- there
|
|
* is no event for "a custom property was assigned" to watch instead.
|
|
*/
|
|
export function useNavColors(navColor: string): NavColors {
|
|
const [colors, setColors] = React.useState<NavColors>(
|
|
() => navColorsFrom(() => '', navColor));
|
|
|
|
React.useLayoutEffect(() => {
|
|
const update = () => setColors(resolveNavColors(navColor));
|
|
update();
|
|
|
|
if (typeof MutationObserver !== 'function') {
|
|
return;
|
|
}
|
|
const observer = new MutationObserver(update);
|
|
observer.observe(document.documentElement, {
|
|
attributes: true,
|
|
attributeFilter: ['data-theme'],
|
|
});
|
|
return () => observer.disconnect();
|
|
}, [navColor]);
|
|
|
|
return colors;
|
|
}
|