@uniweb/theming 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/theming",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Theming utilities for the Uniweb platform — shade generation, CSS generation, theme processing",
5
5
  "type": "module",
6
6
  "exports": {
@@ -0,0 +1,157 @@
1
+ /**
2
+ * `theme.yml`'s `code:` — what it may say, and what of it travels.
3
+ *
4
+ * A site says how its code blocks look in one of three forms, which kit's
5
+ * <Code> reads (`parseCodeConfig` in kit's Code renderer):
6
+ *
7
+ * code: dracula a theme, by name
8
+ * code: { light: github-light, dark: … } a theme per scheme — each side
9
+ * a name, or a map of the next form
10
+ * code: { theme?: github-dark, <colour>: … } a theme, with colours overridden
11
+ *
12
+ * This layer carries the declaration as written — the default belongs to
13
+ * whoever renders code — and says what is wrong with it: a key kit does not
14
+ * read, a theme named with anything but a name, a colour that is not one. Kit
15
+ * would ignore each of them, or refuse it, without a word. ⛔ Until 2026-10-19
16
+ * the check read every value as a palette colour, so a pair of names, `theme:`,
17
+ * `transparent` and `var(…)` were reported as invalid colours (and the build
18
+ * printed none of the report), and the safety filter left out a pair whose
19
+ * sides were maps — so kit drew the default theme.
20
+ *
21
+ * @module @uniweb/theming/code-theme
22
+ */
23
+
24
+ import { keepWritable } from './values.js'
25
+
26
+ /**
27
+ * The colours a `code:` block may set: the block's ground and text, and the
28
+ * token scopes kit's <Code> maps. A cross-package test holds this list and
29
+ * kit's together, so a key kit learns is a key this check accepts.
30
+ */
31
+ export const CODE_COLOR_KEYS = Object.freeze([
32
+ 'background',
33
+ 'foreground',
34
+ 'comment',
35
+ 'string',
36
+ 'keyword',
37
+ 'operator',
38
+ 'function',
39
+ 'variable',
40
+ 'number',
41
+ 'constant',
42
+ 'type',
43
+ 'property',
44
+ 'tag',
45
+ 'attribute',
46
+ 'punctuation',
47
+ ])
48
+
49
+ // A theme's name, as Shiki names every theme it ships: lowercase words joined by
50
+ // single hyphens. Kit refuses any other, since the name becomes part of a URL.
51
+ const THEME_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
52
+
53
+ // A colour, in the forms CSS has — which is what a declared colour becomes, in
54
+ // the highlighter's inline styles. Not only the forms a palette can be shaded
55
+ // from: `transparent`, a named colour and `var(--x)` are colours here.
56
+ const HEX = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i
57
+ const COLOR_FUNCTION = /^(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color|color-mix|light-dark|var)\(.+\)$/i
58
+ const COLOR_WORD = /^[a-z]+$/i
59
+
60
+ /**
61
+ * Whether a string can name a code theme.
62
+ *
63
+ * @param {*} name
64
+ * @returns {boolean}
65
+ */
66
+ export function isCodeThemeName(name) {
67
+ return typeof name === 'string' && THEME_NAME.test(name)
68
+ }
69
+
70
+ /**
71
+ * Whether a value is a colour a code block can be painted with.
72
+ *
73
+ * @param {*} value
74
+ * @returns {boolean}
75
+ */
76
+ export function isCodeColor(value) {
77
+ return typeof value === 'string' && (HEX.test(value) || COLOR_FUNCTION.test(value) || COLOR_WORD.test(value))
78
+ }
79
+
80
+ const THEME_HINT = "name one of Shiki's themes, such as github-dark"
81
+ const describe = (value) => (Array.isArray(value) ? 'a list' : value === null ? 'null' : typeof value)
82
+
83
+ /** One side — the whole block, or `light` / `dark` of a pair: a theme name, or a map of `theme` and colours. */
84
+ function checkSide(side, where, errors) {
85
+ if (typeof side === 'string') {
86
+ if (!isCodeThemeName(side)) errors.push(`${where}: ${JSON.stringify(side)} is not a theme name — ${THEME_HINT}`)
87
+ return
88
+ }
89
+ if (!side || typeof side !== 'object' || Array.isArray(side)) {
90
+ errors.push(`${where} must be a theme name or a map of colours, got ${describe(side)}`)
91
+ return
92
+ }
93
+ for (const [key, value] of Object.entries(side)) {
94
+ if (key === 'theme') {
95
+ if (!isCodeThemeName(value)) errors.push(`${where}.theme: ${JSON.stringify(value)} is not a theme name — ${THEME_HINT}`)
96
+ } else if (key === 'light' || key === 'dark') {
97
+ errors.push(`${where}.${key}: a side of a pair cannot hold another pair`)
98
+ } else if (!CODE_COLOR_KEYS.includes(key)) {
99
+ errors.push(`${where}.${key} is not a code colour — the colours are ${CODE_COLOR_KEYS.join(', ')}`)
100
+ } else if (typeof value !== 'string') {
101
+ errors.push(`${where}.${key} must be a colour, got ${describe(value)}`)
102
+ } else if (!isCodeColor(value)) {
103
+ errors.push(`${where}.${key} has invalid color value: ${value}`)
104
+ }
105
+ }
106
+ }
107
+
108
+ /**
109
+ * What is wrong with a `code:` declaration, as messages for the build to print.
110
+ *
111
+ * @param {*} code - `theme.yml`'s `code:`
112
+ * @returns {{ valid: boolean, errors: string[] }}
113
+ */
114
+ export function validateCodeTheme(code) {
115
+ const errors = []
116
+
117
+ if (code === undefined || code === null) return { valid: true, errors } // the renderer's default
118
+ if (typeof code === 'string') {
119
+ checkSide(code, 'code', errors)
120
+ } else if (typeof code !== 'object' || Array.isArray(code)) {
121
+ errors.push(`code must be a theme name, a light/dark pair, or a map of colours, got ${describe(code)}`)
122
+ } else if ('light' in code || 'dark' in code) {
123
+ for (const [key, value] of Object.entries(code)) {
124
+ if (key === 'light' || key === 'dark') checkSide(value, `code.${key}`, errors)
125
+ else errors.push(`code.${key} beside light and dark does nothing — set it on a side, as code.light.${key}`)
126
+ }
127
+ } else {
128
+ checkSide(code, 'code', errors)
129
+ }
130
+
131
+ return { valid: errors.length === 0, errors }
132
+ }
133
+
134
+ /**
135
+ * The declaration as it travels: as written, less any value a stylesheet could
136
+ * not hold (`keepWritable`, which says what it left out). A side of a pair that
137
+ * is a map is filtered the same way, value by value — not left out whole for
138
+ * not being one value.
139
+ *
140
+ * @param {*} code - `theme.yml`'s `code:`
141
+ * @param {string[]} warnings - where a warning is added
142
+ * @returns {*} what travels to the renderer, or null for nothing declared
143
+ */
144
+ export function keepWritableCode(code, warnings) {
145
+ if (code === undefined || code === null) return null
146
+ if (typeof code !== 'object' || Array.isArray(code)) return code
147
+
148
+ const kept = {}
149
+ for (const [key, value] of Object.entries(code)) {
150
+ if ((key === 'light' || key === 'dark') && value && typeof value === 'object' && !Array.isArray(value)) {
151
+ kept[key] = keepWritable(value, `code.${key}`, warnings)
152
+ } else {
153
+ Object.assign(kept, keepWritable({ [key]: value }, 'code', warnings))
154
+ }
155
+ }
156
+ return kept
157
+ }
@@ -473,7 +473,7 @@ function generateFontCSS(fontRoles = {}, fonts = {}, base = '/') {
473
473
  // --- Apply font families to elements ---
474
474
  // Defining --font-* on :root is not enough — something must set `font-family`
475
475
  // on rendered elements, the same way colors get an application rule
476
- // ([id^="section-"] { background-color: var(--section) }). The processor has
476
+ // ([id^="section-"]:not([data-uw-background="self"]) { background-color: var(--section) }). The processor has
477
477
  // already resolved which roles apply (`role.apply`): a built-in role applies
478
478
  // only when the site set it (don't stomp a foundation's own typography with
479
479
  // system-ui); a foundation-owned/redefined role applies always via its
@@ -590,7 +590,12 @@ export function generateThemeCSS(config = {}, options = {}) {
590
590
  const lightColorVars = colorVars.light || {}
591
591
  const defaultTokens = { ...DEFAULT_CONTEXT_TOKENS.light, ...(contexts.light || {}), ...lightColorVars }
592
592
  const defaultVars = generateVarDeclarations(defaultTokens)
593
- sections.push(`/* Default Semantic Tokens */\n:root {\n${defaultVars}\n}\n\n/* Section backgrounds — applied once, resolves via context tokens */\n[id^="section-"] {\n background-color: var(--section);\n}`)
593
+ // ⭐ A section type that declares `background: 'self'` paints everything behind its content, its
594
+ // context's surface included: the runtime marks its wrapper `data-uw-background="self"`, and this
595
+ // rule leaves it unpainted, so a panel narrower than its section sits on the page's own surface.
596
+ // ⛔ Until 2026-11-06 the rule painted every wrapper, so `'self'` covered the author's background
597
+ // layer and not the surface, and a panel foundation cleared the band with a rule of its own.
598
+ sections.push(`/* Default Semantic Tokens */\n:root {\n${defaultVars}\n}\n\n/* Section backgrounds — applied once, resolves via context tokens */\n[id^="section-"]:not([data-uw-background="self"]) {\n background-color: var(--section);\n}`)
594
599
 
595
600
  // 4. Context classes
596
601
  const contextCSS = [
package/src/processor.js CHANGED
@@ -12,6 +12,7 @@ import { isValidColor, generatePalettes } from './shade-generator.js'
12
12
  import { getDefaultColors, getDefaultContextTokens } from './css-generator.js'
13
13
  import { normalizeTokenValue } from './normalize.js'
14
14
  import { isCssName, isCssProperty, isCssValue, isLoadableUrl, keepWritable } from './values.js'
15
+ import { validateCodeTheme, keepWritableCode } from './code-theme.js'
15
16
 
16
17
  /**
17
18
  * Named neutral presets mapping to Tailwind gray families
@@ -251,35 +252,6 @@ function validateAppearance(appearance) {
251
252
  return { valid: errors.length === 0, errors }
252
253
  }
253
254
 
254
- /**
255
- * Validate code block theme configuration
256
- *
257
- * @param {Object} code - Code theme configuration
258
- * @returns {{ valid: boolean, errors: string[] }}
259
- */
260
- function validateCodeTheme(code) {
261
- const errors = []
262
-
263
- if (!code || typeof code !== 'object') {
264
- return { valid: true, errors } // No code config is valid (use defaults)
265
- }
266
-
267
- // Validate color values
268
- for (const [name, value] of Object.entries(code)) {
269
- if (typeof value !== 'string') {
270
- errors.push(`code.${name} must be a string, got ${typeof value}`)
271
- continue
272
- }
273
-
274
- // Basic color format check (hex, rgb, hsl, or color name)
275
- if (!isValidColor(value)) {
276
- errors.push(`code.${name} has invalid color value: ${value}`)
277
- }
278
- }
279
-
280
- return { valid: errors.length === 0, errors }
281
- }
282
-
283
255
  /**
284
256
  * Context-aware var types (stored per light/dark context)
285
257
  */
@@ -747,10 +719,9 @@ export function processTheme(rawConfig = {}, options = {}) {
747
719
  // site naming a bundled theme and overriding one colour would arrive carrying
748
720
  // fifteen, and the theme it named would be overridden in full by values it
749
721
  // never asked for. Whoever renders code owns the default; this only carries
750
- // what the site said.
751
- const code = rawConfig.code && typeof rawConfig.code === 'object'
752
- ? keepWritable(rawConfig.code, 'code', warnings)
753
- : rawConfig.code ?? null
722
+ // what the site said — less what a stylesheet could not hold, side by side
723
+ // for a pair (`keepWritableCode`, code-theme.js).
724
+ const code = keepWritableCode(rawConfig.code, warnings)
754
725
 
755
726
  // Site background (pass through as CSS value)
756
727
  let background = rawConfig.background || null