Skip to content

feat(theme): no-code custom theme builder with a WCAG contrast gate - #7527

Closed
eli32-vlc wants to merge 2 commits into
fmhy:mainfrom
eli32-vlc:feat/custom-theme-contrast
Closed

eli32-vlc wants to merge 2 commits into
fmhy:mainfrom
eli32-vlc:feat/custom-theme-contrast

Conversation

@eli32-vlc

Copy link
Copy Markdown
Contributor

What this adds

A way for visitors to build their own theme without writing CSS, plus an optional sanitized CSS box, and a contrast gate so Apply cannot be clicked on a theme that fails WCAG AA.

The existing theme system (themes/, presets + generated color-* themes) already applies colours through inline --vp-* variables. This adds the missing pieces: user-authored themes, a no-code builder, and measurement before apply.

How it works

themes/ gains five modules:

File Job
colorMath.ts Dependency-free WCAG maths: parse hex/rgb()/hsl()/color(srgb …), contrast ratio, hue-preserving lightness fix
contrastAudit.ts Measures a theme in both modes and grades each check pass / warn / fail / unknown
themeBuilder.ts Recipe (accent + surface tone + font) → complete Theme for light and dark
cssSanitizer.ts Parses user CSS and drops what is unsafe
customTheme.ts Storage, the single <style> element, and the one path that may write a theme

UI lives in components/CustomThemePanel.vue, mounted under the presets in AppearancePanel.vue.

Why contrast is a gate, not advice

commitCustomTheme is the only path that writes a custom theme. It builds the Theme from the recipe, runs auditTheme on that exact object in both modes, sanitizes the CSS, and refuses to persist when anything fails. The Apply button is disabled from the same reports, so the gate cannot drift from what the panel shows.

Measured per mode:

  • Blocks (fail): primary + secondary text on page/alt/elevated backgrounds · link and accent text (--vp-c-brand-1, -2) on all three surfaces · button labels in base, hover and pressed · callout text and text-deep titles · selected text on the selection fill. All at 4.5:1.
  • Advisory (warn): faint --vp-c-text-3 metadata below AA but ≥3:1 · callout borders and button fill against the page below 3:1. These are not copy a person reads, so they never block.

Two site-specific details drive the rules:

  1. --vp-c-brand-1 is held to the text threshold, because this site renders links, active nav items and page headings with it (.vp-doc a { color: var(--vp-c-brand-1) }), not as decoration.
  2. Translucent elevated backgrounds (#1a1a1acc in several presets) are flattened onto the page before measuring, and a theme that omits text is measured against VitePress's own defaults (#dfdfd6/#98989f/#6a6a71 dark) rather than assumed legible.

The builder never trusts a picked colour: every text role goes through meetContrast, which binary-searches lightness with hue and saturation untouched, for the smallest change that clears the bar. Ratio comparison is done unrounded internally, so a colour that lands at 4.494 is not reported as passing 4.5.

Verified across 192 combinations (12 accents × 4 tones × 2 fonts × 2 modes): 0 audit failures, 0 unreadable button labels.

CSS safety

sanitizeCustomCss parses the sheet instead of pattern-matching it, so nested @media, @supports and @keyframes survive. It drops:

  • Resource and script vectors: url(), image-set(), paint(), expression(), progid:, protocol handlers, encoded escapes, @import / @charset / @namespace.
  • Selectors that restyle the document or the chrome: html, body, :root, *, #app, .VPNav, and the appearance/feedback components. Without this a saved theme could hide the very controls that turn it off, leaving no way to undo it.
  • Properties that can hide or move UI: position, z-index, display, visibility, opacity, pointer-events, overflow, clip-path, and friends. Inside @keyframes those three are allowed again, since there they are an effect rather than a way to hide the page.

Structural problems (unbalanced braces, a declaration with no selector, an oversized sheet, a banned at-rule) come back as errors and block the write entirely, because half a stylesheet is worse than none. Individual drops come back as warnings so the panel can list them.

Honest limitation, stated in the UI: colours typed into the CSS box are not contrast-measured. The audit reads theme variables, and a hand-written color: is invisible to it. The panel says so rather than implying the whole theme was checked.

Reload behaviour

The recipe and stylesheet live in vitepress-theme-spec and vitepress-theme-css. The head bootstrap in config.mts replays both before paint, so a reload shows the custom theme instead of flashing the default one. Storage is read on mount, not during setup, because the panel is server-rendered inside the compact picker and the server/first-client markup must agree.

Tests

tests/theme-color-math.test.mjs (18 tests, runs under the existing node --test) covers the maths the guarantee rests on: the published #767676-on-white boundary, symmetry, hue preservation through meetContrast, alpha parsing for every syntax the configs use (rgba(), hsl(), modern space/slash forms, color(srgb …), 8-digit hex), the unrounded-threshold case, and the honest-failure path when no lightness can reach the target.

While writing these, two real bugs surfaced and were fixed: rgbToHsl was missing a subtraction in two hue branches (a sky blue round-tripped to magenta), and modern rgb(0 0 0 / 0.5) alpha was being dropped, which would have made every translucent surface audit as opaque.

Not done

  • No new dependencies. The maths is hand-rolled specifically so nothing has to be installed.
  • No upstream push, no merge.

Adds a theme builder to the existing theme system so a visitor can make
their own theme from an accent colour, a surface tone and a text style,
without writing CSS, and optionally layer sanitized CSS on top.

The point of the change is that Apply cannot be clicked on an unreadable
theme. Every text role the builder produces is pushed through a
hue-preserving contrast fix, and the finished theme is measured in both
modes before anything is written: links, body and muted text on all three
surfaces, button labels in base/hover/pressed, callout text and titles,
and selected text. `--vp-c-brand-1` is held to the text threshold because
this site renders links, active nav and headings with it rather than as
decoration. Translucent elevated backgrounds are flattened onto the page
first, and a theme that omits `text` is measured against VitePress's own
defaults instead of being assumed legible.

User CSS is parsed rather than pattern-matched, so nested `@media`,
`@supports` and `@keyframes` survive. It drops resource functions and
protocol handlers, `@import`, and anything that could hide the chrome:
document-level selectors, the appearance and feedback components, and
properties like `position`/`z-index`/`display`. Without that last part a
saved theme could hide the very controls that turn it off, leaving no way
to undo it. Structural problems block the write entirely; individual drops
are reported so the panel can list them.

Colours typed into the CSS box are not contrast-measured, since the audit
reads theme variables, and the panel says so rather than implying the
whole theme was checked.

The recipe and stylesheet are persisted in localStorage and replayed by
the head bootstrap before paint, so a reload does not flash the default
theme. Storage is read on mount, not during setup, because the panel is
server-rendered inside the compact picker.

tests/theme-color-math.test.mjs covers the WCAG maths directly, including
the published #767676-on-white boundary, hue preservation, alpha parsing
for every syntax the configs use, and the honest-failure path when no
lightness can reach the threshold.
@github-actions github-actions Bot added core Core changes to our VitePress setup docs Content changes labels Nov 30, 2026
@eli32-vlc eli32-vlc closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core changes to our VitePress setup docs Content changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant