Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 + generatedcolor-*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:colorMath.tsrgb()/hsl()/color(srgb …), contrast ratio, hue-preserving lightness fixcontrastAudit.tsthemeBuilder.tsThemefor light and darkcssSanitizer.tscustomTheme.ts<style>element, and the one path that may write a themeUI lives in
components/CustomThemePanel.vue, mounted under the presets inAppearancePanel.vue.Why contrast is a gate, not advice
commitCustomThemeis the only path that writes a custom theme. It builds theThemefrom the recipe, runsauditThemeon 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:
--vp-c-brand-1,-2) on all three surfaces · button labels in base, hover and pressed · callout text andtext-deeptitles · selected text on the selection fill. All at 4.5:1.--vp-c-text-3metadata 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:
--vp-c-brand-1is 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.#1a1a1accin several presets) are flattened onto the page before measuring, and a theme that omitstextis measured against VitePress's own defaults (#dfdfd6/#98989f/#6a6a71dark) 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
sanitizeCustomCssparses the sheet instead of pattern-matching it, so nested@media,@supportsand@keyframessurvive. It drops:url(),image-set(),paint(),expression(),progid:, protocol handlers, encoded escapes,@import/@charset/@namespace.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.position,z-index,display,visibility,opacity,pointer-events,overflow,clip-path, and friends. Inside@keyframesthose 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
errorsand block the write entirely, because half a stylesheet is worse than none. Individual drops come back aswarningsso 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-specandvitepress-theme-css. The head bootstrap inconfig.mtsreplays 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 existingnode --test) covers the maths the guarantee rests on: the published#767676-on-white boundary, symmetry, hue preservation throughmeetContrast, 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:
rgbToHslwas missing a subtraction in two hue branches (a sky blue round-tripped to magenta), and modernrgb(0 0 0 / 0.5)alpha was being dropped, which would have made every translucent surface audit as opaque.Not done