@uniweb/theming 1.3.5 → 1.3.6

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.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "Theming utilities for the Uniweb platform — shade generation, CSS generation, theme processing",
5
5
  "type": "module",
6
6
  "exports": {
@@ -220,7 +220,7 @@ function generateContextCSS(context, tokens = {}, colorVars = {}) {
220
220
  * @param {Object} colorVars - Context-aware foundation color vars: { light: {...}, dark: {...} }
221
221
  * @returns {string} CSS for dark scheme support
222
222
  */
223
- function generateDarkSchemeCSS(config = {}, darkOverrides = {}, colorVars = {}, darkContexts = {}) {
223
+ function generateDarkSchemeCSS(config = {}, darkOverrides = {}, colorVars = {}, darkContexts = {}, darkBackground = null) {
224
224
  const respectSystemPreference = config.default === 'system'
225
225
 
226
226
  // Merge default dark tokens with user element overrides and dark color vars
@@ -242,6 +242,9 @@ function generateDarkSchemeCSS(config = {}, darkOverrides = {}, colorVars = {},
242
242
  for (const [name, tokens] of surfaces) {
243
243
  css += `.scheme-dark .context-${name} {\n${generateVarDeclarations(surfaceTokens(tokens))}\n}\n`
244
244
  }
245
+ // The page's own background under a dark scheme (`background: { light, dark }`, processor.js
246
+ // `pageBackground`), over the one rule 7 writes for both: (0,1,1) over (0,0,1).
247
+ if (darkBackground) css += `.scheme-dark body {\n background: ${darkBackground};\n}\n`
245
248
 
246
249
  if (respectSystemPreference) {
247
250
  css += `\n@media (prefers-color-scheme: dark) {\n`
@@ -257,6 +260,7 @@ function generateDarkSchemeCSS(config = {}, darkOverrides = {}, colorVars = {},
257
260
  }
258
261
  css += ` }\n`
259
262
  }
263
+ if (darkBackground) css += ` :root:not(.scheme-light) body {\n background: ${darkBackground};\n }\n`
260
264
  css += `}\n`
261
265
  }
262
266
 
@@ -685,7 +689,7 @@ export function generateThemeCSS(config = {}, options = {}) {
685
689
  // has no @uniweb/core dependency (it also runs server-side in the platform),
686
690
  // so the predicate is duplicated here by necessity; keep the two in lockstep.
687
691
  if (appearance.allowToggle || appearance.default === 'dark' || appearance.default === 'system' || appearance.schemes?.includes('dark')) {
688
- sections.push(generateDarkSchemeCSS(appearance, contexts.dark, colorVars, darkContexts))
692
+ sections.push(generateDarkSchemeCSS(appearance, contexts.dark, colorVars, darkContexts, config.darkBackground))
689
693
  }
690
694
 
691
695
  // 7. Site background — the page behind the sections.
@@ -699,7 +703,9 @@ export function generateThemeCSS(config = {}, options = {}) {
699
703
  // Defaulting to var(--section) means the page matches the sections sitting on
700
704
  // it and flips with the visitor's choice, because that token already does. A
701
705
  // site wanting a different canvas — a wash behind cards, an image — sets
702
- // `background:` in theme.yml and replaces this.
706
+ // `background:` in theme.yml and replaces this: in the theme's words, where a
707
+ // token follows the scheme too, or `{ light, dark }`, whose dark one the dark
708
+ // scheme's CSS draws (rule 6, `generateDarkSchemeCSS`).
703
709
  sections.push(
704
710
  `/* Site Background */\nbody {\n background: ${config.background || 'var(--section)'};\n}`
705
711
  )
package/src/processor.js CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  import { isValidColor, generatePalettes } from './shade-generator.js'
12
12
  import { getDefaultColors, getDefaultContextTokens, getDefaultMediumDarkSurface, BUILT_IN_CONTEXTS, isSurfaceName } from './css-generator.js'
13
- import { normalizeTokenValue, SEMANTIC_TOKENS } from './normalize.js'
13
+ import { normalizeTokenValue, normalizeColorValue, 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,12 +177,12 @@ function validateContexts(contexts) {
177
177
  return { valid: true, errors }
178
178
  }
179
179
 
180
- // ⭐ A name other than the built-in three is a surface of the site's own (`theme: brand`), its
180
+ // ⭐ A name other than the built-in three is a context of the site's own (`theme: brand`), its
181
181
  // class `context-<name>`, so it is a name a class can hold. ⛔ Until 2026-11-28 any other name
182
182
  // was an "unknown context".
183
183
  for (const [context, tokens] of Object.entries(contexts)) {
184
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
+ errors.push(`Context "${context}" is not a context's name: lowercase letters, digits and dashes, starting with a letter`)
186
186
  continue
187
187
  }
188
188
 
@@ -649,7 +649,7 @@ export function processTheme(rawConfig = {}, options = {}) {
649
649
  for (const [name, raw] of Object.entries(rawContexts)) {
650
650
  if (BUILT_IN_CONTEXTS.includes(name)) continue
651
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`)
652
+ warnings.push(`theme: contexts.${name}: not a context of the site's own (a name of lowercase letters, digits and dashes, holding a map of tokens) — left out`)
653
653
  continue
654
654
  }
655
655
  contexts[name] = { ...contexts.light, ...tokensOf(raw, `contexts.${name}`) }
@@ -770,12 +770,9 @@ export function processTheme(rawConfig = {}, options = {}) {
770
770
  // for a pair (`keepWritableCode`, code-theme.js).
771
771
  const code = keepWritableCode(rawConfig.code, warnings)
772
772
 
773
- // Site background (pass through as CSS value)
774
- let background = rawConfig.background || null
775
- if (background !== null && !isCssValue(background)) {
776
- warnings.push(`theme: background: ${JSON.stringify(background)} is not a CSS value — left out`)
777
- background = null
778
- }
773
+ // The page behind the sections, in the theme's words as a section's background color is, or one
774
+ // for each scheme (`pageBackground`).
775
+ const { background, darkBackground } = pageBackground(rawConfig.background, warnings)
779
776
 
780
777
  // Inline text styles (semantic names → CSS declarations). A name is written as
781
778
  // `span[<name>]`, each property as a declaration.
@@ -827,7 +824,8 @@ export function processTheme(rawConfig = {}, options = {}) {
827
824
  foundationVars: nonFontVars, // NON-font vars only (font vars live in fontRoles)
828
825
  colorVars, // Context-aware vars: { light: {...}, dark: {...} }
829
826
  code, // Code block theme for runtime injection
830
- background, // Site-level background CSS value
827
+ background, // the page's background as CSS: under a light scheme, or in both when it follows the scheme
828
+ ...(darkBackground && { darkBackground }), // the page's own under a dark scheme, when the site gives one
831
829
  inline, // Inline text style definitions
832
830
  ...(Object.keys(backgrounds).length > 0 && { backgrounds }), // named backgrounds, as written
833
831
  }
@@ -835,6 +833,45 @@ export function processTheme(rawConfig = {}, options = {}) {
835
833
  return { config, errors, warnings }
836
834
  }
837
835
 
836
+ /**
837
+ * THE PAGE BACKGROUND (`theme.yml` `background:`), the page behind the sections, as CSS.
838
+ *
839
+ * It takes what a section's background color takes, so a site writes it in the theme's words: a
840
+ * semantic token (`section`, `muted`), which follows the scheme as every token does; a palette
841
+ * shade (`primary-50`, `neutral-900/40`), the same in both schemes; or any other CSS value as
842
+ * written (`#faf7f0`, a gradient, an image's `url()`). A page of its own in each
843
+ * scheme is `{ light, dark }`, the form `vars:` takes; the dark one is drawn only where the site has
844
+ * a dark scheme (`generateDarkSchemeCSS`). ⛔ Until 2026-11-28 it took CSS alone, and the docs offered
845
+ * `light-dark()` for a page that follows the scheme, which drew its light value in both: nothing of
846
+ * ours sets `color-scheme`, which is what `light-dark()` reads.
847
+ *
848
+ * @param {*} raw - `theme.yml`'s `background`
849
+ * @param {string[]} warnings
850
+ * @returns {{ background: string|null, darkBackground: string|null }}
851
+ */
852
+ function pageBackground(raw, warnings) {
853
+ // Unset as before: any value that isn't one (`false`, `''`, `null`) leaves the page `section`.
854
+ const given = (value) => Boolean(value)
855
+ const css = (value, where) => {
856
+ const written = typeof value === 'string' ? normalizeColorValue(value.trim()) : value
857
+ if (written !== '' && isCssValue(written)) return String(written)
858
+ warnings.push(`theme: ${where}: ${JSON.stringify(value)} is not a color in the theme's words or a CSS value — left out`)
859
+ return null
860
+ }
861
+ if (!given(raw)) return { background: null, darkBackground: null }
862
+ if (typeof raw === 'object' && !Array.isArray(raw)) {
863
+ const other = Object.keys(raw).filter((key) => key !== 'light' && key !== 'dark')
864
+ if (other.length > 0) {
865
+ warnings.push(`theme: background: ${other.map((key) => `\`${key}\``).join(', ')} — the page's background is a value, or one for each scheme ({ light, dark }) — not read`)
866
+ }
867
+ return {
868
+ background: given(raw.light) ? css(raw.light, 'background.light') : null,
869
+ darkBackground: given(raw.dark) ? css(raw.dark, 'background.dark') : null,
870
+ }
871
+ }
872
+ return { background: css(raw, 'background'), darkBackground: null }
873
+ }
874
+
838
875
  /**
839
876
  * Load foundation variables from vars.js export
840
877
  *