@uniweb/theming 1.3.3 → 1.3.4
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 +1 -1
- package/src/css-generator.js +56 -2
- package/src/index.js +1 -1
- package/src/normalize.js +32 -0
- package/src/processor.js +71 -13
package/package.json
CHANGED
package/src/css-generator.js
CHANGED
|
@@ -161,6 +161,36 @@ function generatePaletteVars(palettes) {
|
|
|
161
161
|
return lines.join('\n')
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
+
/** The three built-in surfaces. `light` and `dark` are the scheme's own; `medium` follows it. */
|
|
165
|
+
export const BUILT_IN_CONTEXTS = Object.freeze(['light', 'medium', 'dark'])
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* `medium` under a dark scheme: one step off the dark page, as it is one step off the light page
|
|
169
|
+
* under a light scheme. Its text and links are the dark context's (`processor.js`).
|
|
170
|
+
*/
|
|
171
|
+
const DEFAULT_MEDIUM_DARK_SURFACE = {
|
|
172
|
+
section: 'var(--neutral-900)',
|
|
173
|
+
card: 'var(--neutral-800)',
|
|
174
|
+
muted: 'var(--neutral-700)',
|
|
175
|
+
border: 'var(--neutral-700)',
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** A copy of `medium`'s dark-scheme surface tokens. */
|
|
179
|
+
export function getDefaultMediumDarkSurface() {
|
|
180
|
+
return { ...DEFAULT_MEDIUM_DARK_SURFACE }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Whether a name may name a surface of a site's own: its class is `context-<name>`, and an author
|
|
185
|
+
* writes it as `theme: <name>`, so lowercase letters, digits and dashes, starting with a letter.
|
|
186
|
+
*
|
|
187
|
+
* @param {string} name
|
|
188
|
+
* @returns {boolean}
|
|
189
|
+
*/
|
|
190
|
+
export function isSurfaceName(name) {
|
|
191
|
+
return typeof name === 'string' && /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/.test(name)
|
|
192
|
+
}
|
|
193
|
+
|
|
164
194
|
/**
|
|
165
195
|
* Generate context class CSS
|
|
166
196
|
*
|
|
@@ -190,7 +220,7 @@ function generateContextCSS(context, tokens = {}, colorVars = {}) {
|
|
|
190
220
|
* @param {Object} colorVars - Context-aware foundation color vars: { light: {...}, dark: {...} }
|
|
191
221
|
* @returns {string} CSS for dark scheme support
|
|
192
222
|
*/
|
|
193
|
-
function generateDarkSchemeCSS(config = {}, darkOverrides = {}, colorVars = {}) {
|
|
223
|
+
function generateDarkSchemeCSS(config = {}, darkOverrides = {}, colorVars = {}, darkContexts = {}) {
|
|
194
224
|
const respectSystemPreference = config.default === 'system'
|
|
195
225
|
|
|
196
226
|
// Merge default dark tokens with user element overrides and dark color vars
|
|
@@ -199,8 +229,19 @@ function generateDarkSchemeCSS(config = {}, darkOverrides = {}, colorVars = {})
|
|
|
199
229
|
|
|
200
230
|
const vars = generateVarDeclarations(darkTokens)
|
|
201
231
|
|
|
232
|
+
// ⭐ A surface that follows the scheme (`medium`, and a site's own with `dark:`) under a dark
|
|
233
|
+
// scheme: its dark version, on the section that pins it (`processor.js`, `darkContexts`). The
|
|
234
|
+
// scheme's class before the context's outweighs the context's own rule, (0,2,0) over (0,1,0).
|
|
235
|
+
const surfaces = Object.entries(darkContexts).filter(([name]) => name !== 'light' && name !== 'dark')
|
|
236
|
+
// The site's dark context, then what the surface changes from it (`processor.js`), then the
|
|
237
|
+
// foundation's dark color vars.
|
|
238
|
+
const surfaceTokens = (tokens) => ({ ...DEFAULT_CONTEXT_TOKENS.dark, ...darkOverrides, ...tokens, ...darkColorVars })
|
|
239
|
+
|
|
202
240
|
let css = `/* Dark scheme (user preference) */\n`
|
|
203
241
|
css += `.scheme-dark {\n${vars}\n}\n`
|
|
242
|
+
for (const [name, tokens] of surfaces) {
|
|
243
|
+
css += `.scheme-dark .context-${name} {\n${generateVarDeclarations(surfaceTokens(tokens))}\n}\n`
|
|
244
|
+
}
|
|
204
245
|
|
|
205
246
|
if (respectSystemPreference) {
|
|
206
247
|
css += `\n@media (prefers-color-scheme: dark) {\n`
|
|
@@ -209,6 +250,13 @@ function generateDarkSchemeCSS(config = {}, darkOverrides = {}, colorVars = {})
|
|
|
209
250
|
css += ` --${name}: ${value};\n`
|
|
210
251
|
}
|
|
211
252
|
css += ` }\n`
|
|
253
|
+
for (const [surface, tokens] of surfaces) {
|
|
254
|
+
css += ` :root:not(.scheme-light) .context-${surface} {\n`
|
|
255
|
+
for (const [name, value] of Object.entries(surfaceTokens(tokens))) {
|
|
256
|
+
css += ` --${name}: ${value};\n`
|
|
257
|
+
}
|
|
258
|
+
css += ` }\n`
|
|
259
|
+
}
|
|
212
260
|
css += `}\n`
|
|
213
261
|
}
|
|
214
262
|
|
|
@@ -562,6 +610,7 @@ export function generateThemeCSS(config = {}, options = {}) {
|
|
|
562
610
|
const {
|
|
563
611
|
colors = DEFAULT_COLORS,
|
|
564
612
|
contexts = {},
|
|
613
|
+
darkContexts = {},
|
|
565
614
|
fonts = {},
|
|
566
615
|
fontRoles = {},
|
|
567
616
|
appearance = {},
|
|
@@ -612,6 +661,11 @@ export function generateThemeCSS(config = {}, options = {}) {
|
|
|
612
661
|
generateContextCSS('light', contexts.light, colorVars),
|
|
613
662
|
generateContextCSS('medium', contexts.medium, colorVars),
|
|
614
663
|
generateContextCSS('dark', contexts.dark, colorVars),
|
|
664
|
+
// A site's own surfaces, their light version with the foundation's light color vars, so a
|
|
665
|
+
// surface that keeps its light version in both schemes doesn't take the dark ones from `:root`.
|
|
666
|
+
...Object.keys(contexts)
|
|
667
|
+
.filter((name) => !BUILT_IN_CONTEXTS.includes(name) && isSurfaceName(name))
|
|
668
|
+
.map((name) => generateContextCSS(name, contexts[name], { [name]: colorVars.light || {} })),
|
|
615
669
|
]
|
|
616
670
|
sections.push('/* Color Contexts */\n' + contextCSS.join('\n\n'))
|
|
617
671
|
|
|
@@ -631,7 +685,7 @@ export function generateThemeCSS(config = {}, options = {}) {
|
|
|
631
685
|
// has no @uniweb/core dependency (it also runs server-side in the platform),
|
|
632
686
|
// so the predicate is duplicated here by necessity; keep the two in lockstep.
|
|
633
687
|
if (appearance.allowToggle || appearance.default === 'dark' || appearance.default === 'system' || appearance.schemes?.includes('dark')) {
|
|
634
|
-
sections.push(generateDarkSchemeCSS(appearance, contexts.dark, colorVars))
|
|
688
|
+
sections.push(generateDarkSchemeCSS(appearance, contexts.dark, colorVars, darkContexts))
|
|
635
689
|
}
|
|
636
690
|
|
|
637
691
|
// 7. Site background — the page behind the sections.
|
package/src/index.js
CHANGED
|
@@ -39,7 +39,7 @@ export {
|
|
|
39
39
|
} from './processor.js'
|
|
40
40
|
|
|
41
41
|
// Value normalization
|
|
42
|
-
export { normalizeTokenValue } from './normalize.js'
|
|
42
|
+
export { normalizeTokenValue, normalizeColorValue, SEMANTIC_TOKENS } from './normalize.js'
|
|
43
43
|
|
|
44
44
|
// What a theme value and name may be — one rule for the stylesheet and anything
|
|
45
45
|
// else that writes a theme value into a page (values.js)
|
package/src/normalize.js
CHANGED
|
@@ -96,3 +96,35 @@ export function normalizeTokenValue(value) {
|
|
|
96
96
|
// 7. Anything else — pass through (named colors like 'white', 'transparent', etc.)
|
|
97
97
|
return value
|
|
98
98
|
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The semantic tokens a context defines (`DEFAULT_CONTEXT_TOKENS`, css-generator.js), which a
|
|
102
|
+
* color in the theme's words may name. `tests/normalize.test.js` holds the two lists equal.
|
|
103
|
+
*/
|
|
104
|
+
export const SEMANTIC_TOKENS = Object.freeze([
|
|
105
|
+
'section', 'card', 'muted', 'body', 'heading', 'subtle', 'border', 'ring', 'link', 'link-hover',
|
|
106
|
+
'accent', 'primary', 'primary-foreground', 'primary-hover', 'primary-border',
|
|
107
|
+
'secondary', 'secondary-foreground', 'secondary-hover', 'secondary-border',
|
|
108
|
+
'success', 'success-subtle', 'warning', 'warning-subtle', 'error', 'error-subtle', 'info', 'info-subtle',
|
|
109
|
+
])
|
|
110
|
+
|
|
111
|
+
const TOKEN_SET = new Set(SEMANTIC_TOKENS)
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A color in the theme's words, as CSS: a semantic token (`section`, `card`, `muted`, `primary`…)
|
|
115
|
+
* is `var(--<token>)`, so it is the section's own in its surface and scheme; a palette shade
|
|
116
|
+
* (`primary-100`, `neutral-900/40`) and any other value are as `normalizeTokenValue` writes them.
|
|
117
|
+
* For a section's background, its gradient's ends and its overlay's [2026-11-28].
|
|
118
|
+
*
|
|
119
|
+
* @param {string} value
|
|
120
|
+
* @returns {string} Valid CSS value
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* normalizeColorValue('muted') // → 'var(--muted)'
|
|
124
|
+
* normalizeColorValue('primary-100') // → 'var(--primary-100)'
|
|
125
|
+
* normalizeColorValue('#fafafa') // → '#fafafa'
|
|
126
|
+
*/
|
|
127
|
+
export function normalizeColorValue(value) {
|
|
128
|
+
if (typeof value === 'string' && TOKEN_SET.has(value.trim())) return `var(--${value.trim()})`
|
|
129
|
+
return normalizeTokenValue(value)
|
|
130
|
+
}
|
package/src/processor.js
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import { isValidColor, generatePalettes } from './shade-generator.js'
|
|
12
|
-
import { getDefaultColors, getDefaultContextTokens } from './css-generator.js'
|
|
13
|
-
import { normalizeTokenValue } from './normalize.js'
|
|
12
|
+
import { getDefaultColors, getDefaultContextTokens, getDefaultMediumDarkSurface, BUILT_IN_CONTEXTS, isSurfaceName } from './css-generator.js'
|
|
13
|
+
import { normalizeTokenValue, SEMANTIC_TOKENS } from './normalize.js'
|
|
14
14
|
import { isCssName, isCssProperty, isCssValue, isLoadableUrl, keepWritable } from './values.js'
|
|
15
15
|
import { validateCodeTheme, keepWritableCode } from './code-theme.js'
|
|
16
16
|
|
|
@@ -177,16 +177,22 @@ function validateContexts(contexts) {
|
|
|
177
177
|
return { valid: true, errors }
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
|
|
181
|
-
|
|
180
|
+
// ⭐ A name other than the built-in three is a surface of the site's own (`theme: brand`), its
|
|
181
|
+
// class `context-<name>`, so it is a name a class can hold. ⛔ Until 2026-11-28 any other name
|
|
182
|
+
// was an "unknown context".
|
|
182
183
|
for (const [context, tokens] of Object.entries(contexts)) {
|
|
183
|
-
if (!
|
|
184
|
-
errors.push(`
|
|
184
|
+
if (!BUILT_IN_CONTEXTS.includes(context) && !isSurfaceName(context)) {
|
|
185
|
+
errors.push(`Context "${context}" is not a surface name: lowercase letters, digits and dashes, starting with a letter`)
|
|
185
186
|
continue
|
|
186
187
|
}
|
|
187
188
|
|
|
188
189
|
if (typeof tokens !== 'object' || tokens === null) {
|
|
189
190
|
errors.push(`Context "${context}" must be an object`)
|
|
191
|
+
continue
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (tokens.dark !== undefined && (typeof tokens.dark !== 'object' || tokens.dark === null)) {
|
|
195
|
+
errors.push(`Context "${context}": \`dark\` is its version under a dark scheme, a map of tokens`)
|
|
190
196
|
}
|
|
191
197
|
}
|
|
192
198
|
|
|
@@ -603,19 +609,51 @@ export function processTheme(rawConfig = {}, options = {}) {
|
|
|
603
609
|
}
|
|
604
610
|
|
|
605
611
|
// Process contexts (resolve bare palette refs like "primary-500" to var())
|
|
612
|
+
//
|
|
613
|
+
// ⭐ SURFACES. A context is a surface: the color a section sits on and the tokens that read on
|
|
614
|
+
// it. `light` and `dark` are the scheme's own two, and a section pinning one keeps it in either
|
|
615
|
+
// scheme. Every other surface follows the scheme: `contexts[name]` is its version under a light
|
|
616
|
+
// scheme, and `darkContexts[name]` what its version under a dark one changes from the site's
|
|
617
|
+
// dark context, which the dark scheme's CSS writes over it (`generateDarkSchemeCSS`): only what
|
|
618
|
+
// differs, since the page's payload carries the processed theme. `medium` always has one, one step off the dark page; a site's
|
|
619
|
+
// own surface has one when it gives `dark:`, and keeps its light version in both schemes when it
|
|
620
|
+
// gives none [Diego, 2026-11-28]. ⛔ Until then a pinned `medium` kept its light tokens on a dark
|
|
621
|
+
// page, which is why agents painted bands with `background: var(--muted)`.
|
|
606
622
|
const defaultContexts = getDefaultContextTokens()
|
|
607
623
|
const rawContexts = rawConfig.contexts || {}
|
|
608
624
|
const contexts = {}
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
const overrides = rawContexts[name] || {}
|
|
625
|
+
const darkContexts = {}
|
|
626
|
+
const tokensOf = (map, where) => {
|
|
612
627
|
const normalized = {}
|
|
613
|
-
|
|
614
|
-
|
|
628
|
+
for (const [token, value] of Object.entries(map || {})) {
|
|
629
|
+
if (token === 'dark') continue
|
|
615
630
|
normalized[token] = normalizeTokenValue(value)
|
|
616
631
|
}
|
|
632
|
+
return keepWritable(normalized, where, warnings)
|
|
633
|
+
}
|
|
634
|
+
const isMap = (value) => value && typeof value === 'object' && !Array.isArray(value)
|
|
617
635
|
|
|
618
|
-
|
|
636
|
+
for (const name of BUILT_IN_CONTEXTS) {
|
|
637
|
+
const overrides = isMap(rawContexts[name]) ? rawContexts[name] : {}
|
|
638
|
+
contexts[name] = { ...defaultContexts[name], ...tokensOf(overrides, `contexts.${name}`) }
|
|
639
|
+
if (name !== 'medium' && overrides.dark !== undefined) {
|
|
640
|
+
warnings.push(`theme: contexts.${name}.dark: \`${name}\` is the same in either scheme, so it has no dark version — left out`)
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
// `medium` under a dark scheme: the site's dark context, its surfaces one step off the dark page.
|
|
644
|
+
darkContexts.medium = {
|
|
645
|
+
...getDefaultMediumDarkSurface(),
|
|
646
|
+
...tokensOf(isMap(rawContexts.medium?.dark) ? rawContexts.medium.dark : {}, 'contexts.medium.dark'),
|
|
647
|
+
}
|
|
648
|
+
// A site's own surfaces, each built on the site's light context, and on its dark one for `dark:`.
|
|
649
|
+
for (const [name, raw] of Object.entries(rawContexts)) {
|
|
650
|
+
if (BUILT_IN_CONTEXTS.includes(name)) continue
|
|
651
|
+
if (!isSurfaceName(name) || !isMap(raw)) {
|
|
652
|
+
warnings.push(`theme: contexts.${name}: not a surface (a name of lowercase letters, digits and dashes, holding a map of tokens) — left out`)
|
|
653
|
+
continue
|
|
654
|
+
}
|
|
655
|
+
contexts[name] = { ...contexts.light, ...tokensOf(raw, `contexts.${name}`) }
|
|
656
|
+
if (isMap(raw.dark)) darkContexts[name] = tokensOf(raw.dark, `contexts.${name}.dark`)
|
|
619
657
|
}
|
|
620
658
|
|
|
621
659
|
// Fonts resolve into the font-role namespace after foundation vars are merged
|
|
@@ -757,10 +795,29 @@ export function processTheme(rawConfig = {}, options = {}) {
|
|
|
757
795
|
: styles
|
|
758
796
|
}
|
|
759
797
|
|
|
798
|
+
// ⭐ NAMED BACKGROUNDS (`backgrounds:`): a background defined once, which a section picks by its
|
|
799
|
+
// name (`background: glow`), as written: core resolves the name and normalizes the value when a
|
|
800
|
+
// section names it (`Block.namedBackground`). A name is a surface's kind of name, and never one a
|
|
801
|
+
// background value already means — a token (`muted`) or a palette shade (`primary-100`) — so a
|
|
802
|
+
// name cannot shadow a color [Diego, 2026-11-28].
|
|
803
|
+
const backgrounds = {}
|
|
804
|
+
for (const [name, definition] of Object.entries(isMap(rawConfig.backgrounds) ? rawConfig.backgrounds : {})) {
|
|
805
|
+
if (!isSurfaceName(name) || SEMANTIC_TOKENS.includes(name) || /-\d+$/.test(name)) {
|
|
806
|
+
warnings.push(`theme: backgrounds.${name}: not a background's name (lowercase letters, digits and dashes, and no token or palette shade) — left out`)
|
|
807
|
+
continue
|
|
808
|
+
}
|
|
809
|
+
if (typeof definition === 'string' ? !definition.trim() : !isMap(definition)) {
|
|
810
|
+
warnings.push(`theme: backgrounds.${name}: a background is a value such as /images/hero.jpg, or a map (color, gradient, image, video, overlay, surface) — left out`)
|
|
811
|
+
continue
|
|
812
|
+
}
|
|
813
|
+
backgrounds[name] = definition
|
|
814
|
+
}
|
|
815
|
+
|
|
760
816
|
const config = {
|
|
761
817
|
colors, // Raw colors for CSS generator
|
|
762
818
|
palettes, // Generated palettes for Theme class
|
|
763
|
-
contexts,
|
|
819
|
+
contexts, // each surface's tokens under a light scheme (`light` and `dark` in either)
|
|
820
|
+
darkContexts, // a surface that follows the scheme: what its dark version changes from `contexts.dark`
|
|
764
821
|
fonts, // name→value (+ import/faces) for the Theme API and loading
|
|
765
822
|
fontRoles, // resolved font-role map (value/applyTo/apply/load) for CSS gen
|
|
766
823
|
appearance,
|
|
@@ -769,6 +826,7 @@ export function processTheme(rawConfig = {}, options = {}) {
|
|
|
769
826
|
code, // Code block theme for runtime injection
|
|
770
827
|
background, // Site-level background CSS value
|
|
771
828
|
inline, // Inline text style definitions
|
|
829
|
+
...(Object.keys(backgrounds).length > 0 && { backgrounds }), // named backgrounds, as written
|
|
772
830
|
}
|
|
773
831
|
|
|
774
832
|
return { config, errors, warnings }
|