wrolpi/app/src/themes/mantine.ts
Roland 32b5ab391b
ui: token-driven component library on Mantine
Adds src/components/ui: buttons, messages, labels, progress, status, tables,
form controls, panels, cards, statistics, modals, tooltips, tabs, and icons.
Everything resolves its colors through theme tokens, so no component branches
on the current theme, and Semantic UI keeps rendering the app meanwhile.

Mantine is bridged rather than restyled: its own CSS variables are pointed at
our tokens. Four details were not obvious, and each caused a visible defect
in night mode before being fixed:

  - Structural colors must go in the resolver's light/dark blocks. Mantine
    emits its values under [data-mantine-color-scheme], which outranks the
    :root block that `variables` becomes, so its grays were winning.
  - The dark and gray palettes are mapped shade by shade to tokens. Mantine
    reads them directly for table borders, zebra stripes and input chrome;
    they can be neither flattened nor left alone.
  - Mantine writes --button-* as inline styles, which no stylesheet can
    outrank, so the dashed danger treatment sets real properties instead.
  - Switch colors are written onto inner elements, so they need scoped CSS
    rather than a variable on the root. Adds a --knob token for control
    handles, because Mantine's default thumb follows theme.white, which is
    near-black in the dark-based themes.

An audit of every painted pixel in the /theme-sample gallery now finds no
non-red channel in night mode.

Icons come from @tabler/icons-react with the 102 Semantic names the app uses
mapped to components; they stroke with currentColor, so they follow the theme
without any rule of their own. Toasts keep the react-semantic-toasts-2
signature so those call sites migrate by changing an import.

TypeScript 5 is required: Mantine's declarations use `const` type parameters,
which 4.9 cannot parse (Mantine 7 is the same, so downgrading is no escape).
That needs two npm overrides to keep clean installs working, both verified
against a Docker build: react-scripts declares peerOptional typescript ^3||^4,
and fork-ts-checker 6.5.2 assigns typescript.performance.mark, which TS 5
made getter-only - crashing the dev server's type checker on every rebuild.

semantic-baseline.test.js ratchets the removal: it fails if a file not already
listed imports Semantic UI. An ESLint rule would have printed 75 warnings per
rebuild, and CI unsets CI= for its build step, so it would guard nothing there.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-08 18:04:00 -06:00

127 lines
5.5 KiB
TypeScript

import {createTheme, CSSVariablesResolver, MantineColorsTuple} from '@mantine/core';
/*
* Bridge between Mantine and the WROLPi theme tokens.
*
* Mantine components read Mantine's own CSS variables, so rather than styling
* components one by one we point those variables at our tokens. A component
* therefore never branches on the current theme, and a new theme is still just
* a table of token values. See tokens.css and ui-design.md.
*/
/**
* A Mantine palette whose every shade is the same token.
*
* Mantine picks a shade per variant (filled uses 6, light uses 1, and so on).
* We have one value per color, and the flat design has no shade ramp to honor,
* so every shade resolves to the token and `color="teal"` follows the theme.
*/
const tokenColor = (token: string): MantineColorsTuple =>
Array(10).fill(`var(--${token})`) as unknown as MantineColorsTuple;
/** The Semantic UI color names WROLPi uses. Each maps to a token of the same name. */
export const semanticColorNames = [
'red', 'orange', 'yellow', 'olive', 'green', 'teal', 'blue', 'violet', 'purple', 'pink',
'brown', 'grey',
] as const;
export type SemanticColorName = typeof semanticColorNames[number];
const semanticColors = Object.fromEntries(
semanticColorNames.map(name => [name, tokenColor(name)])
);
export const mantineTheme = createTheme({
// Hard corners everywhere. The radius variables are zeroed below as well, so a
// component that hardcodes `radius="md"` cannot reintroduce a rounded corner.
defaultRadius: 0,
fontFamily: 'var(--font-body)',
fontFamilyMonospace: 'var(--font-mono)',
headings: {fontFamily: 'var(--font-body)', fontWeight: '600'},
// Text drawn on filled buttons. Mantine reads `white` for that, and our tokens
// already define the correct value per theme (white in light, near-black in dark).
white: 'var(--btn-text)',
black: 'var(--text)',
primaryColor: 'blue',
// Every shade is identical, so the shade only decides which variable name is read.
primaryShade: 6,
// Mantine cannot compute contrast from a `var()`, so it must not try.
autoContrast: false,
colors: {
...semanticColors,
/*
* Mantine reads `dark` and `gray` shades directly for surfaces, borders, and
* secondary text — table borders, zebra stripes, input chrome, disabled states.
* They cannot be flattened (the steps carry meaning) and they cannot be left
* alone either, or those pixels stay neutral gray, which night mode forbids.
* So each shade is mapped to the token that plays its role.
*/
dark: [
'var(--text)', 'var(--text)', // 0-1: primary text
'var(--muted)', 'var(--muted)', // 2-3: secondary text
'var(--border)', 'var(--border)', // 4-5: borders and subtle surfaces
'var(--panel)', // 6: default surface
'var(--bg)', // 7: body
'var(--head)', 'var(--head)', // 8-9: recessed surfaces
] as unknown as MantineColorsTuple,
gray: [
'var(--panel)', // 0: lightest surface
'var(--bg)', // 1: page
'var(--head)', 'var(--head)', // 2-3: recessed surfaces
'var(--border)', 'var(--border)', // 4-5: borders
'var(--muted)', 'var(--muted)', // 6-7: secondary text
'var(--text)', 'var(--text)', // 8-9: primary text
] as unknown as MantineColorsTuple,
},
});
/**
* Colors Mantine defines per color scheme. These MUST go in both `light` and
* `dark` rather than `variables`: Mantine emits its own values under
* `[data-mantine-color-scheme=...]`, which outranks the `:root` block that
* `variables` becomes. Put them in `variables` and Mantine's grays win — which
* in night mode means neutral gray pixels, defeating the whole point of it.
*/
const schemeColors = {
// Surfaces and text.
'--mantine-color-body': 'var(--bg)',
'--mantine-color-text': 'var(--text)',
'--mantine-color-bright': 'var(--text)',
'--mantine-color-dimmed': 'var(--muted)',
'--mantine-color-placeholder': 'var(--muted)',
'--mantine-color-anchor': 'var(--blue)',
'--mantine-color-error': 'var(--red)',
// The "default" variant: inputs, outline buttons, plain surfaces.
'--mantine-color-default': 'var(--panel)',
'--mantine-color-default-hover': 'var(--head)',
'--mantine-color-default-color': 'var(--text)',
'--mantine-color-default-border': 'var(--border)',
'--mantine-color-disabled': 'var(--head)',
'--mantine-color-disabled-color': 'var(--muted)',
'--mantine-color-disabled-border': 'var(--border)',
};
/** Point Mantine's own variables at our tokens. */
export const cssVariablesResolver: CSSVariablesResolver = () => ({
// Not scheme-specific, so `:root` is enough.
variables: {
// Hard corners.
'--mantine-radius-default': '0',
'--mantine-radius-xs': '0',
'--mantine-radius-sm': '0',
'--mantine-radius-md': '0',
'--mantine-radius-lg': '0',
'--mantine-radius-xl': '0',
// Flat surfaces: elevation is expressed with borders and background steps.
'--mantine-shadow-xs': 'none',
'--mantine-shadow-sm': 'none',
'--mantine-shadow-md': 'none',
'--mantine-shadow-lg': 'none',
'--mantine-shadow-xl': 'none',
},
light: schemeColors,
dark: schemeColors,
});