@uniweb/theming 1.3.4 → 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.4",
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.
@@ -795,7 +792,7 @@ export function processTheme(rawConfig = {}, options = {}) {
795
792
  : styles
796
793
  }
797
794
 
798
- // ⭐ NAMED BACKGROUNDS (`backgrounds:`): a background defined once, which a section picks by its
795
+ // ⭐ NAMED BACKGROUNDS (`backgrounds:`): a background defined once, with the `theme` it needs, which a section picks by its
799
796
  // name (`background: glow`), as written: core resolves the name and normalizes the value when a
800
797
  // section names it (`Block.namedBackground`). A name is a surface's kind of name, and never one a
801
798
  // background value already means — a token (`muted`) or a palette shade (`primary-100`) — so a
@@ -807,9 +804,12 @@ export function processTheme(rawConfig = {}, options = {}) {
807
804
  continue
808
805
  }
809
806
  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`)
807
+ warnings.push(`theme: backgrounds.${name}: a background is a value such as /images/hero.jpg, or a map (color, gradient, image, video, overlay, theme) — left out`)
811
808
  continue
812
809
  }
810
+ if (isMap(definition) && definition.surface !== undefined) {
811
+ warnings.push(`theme: backgrounds.${name}.surface is \`theme\`, the theme a section reads in, as a section writes it — \`surface\` is not read`)
812
+ }
813
813
  backgrounds[name] = definition
814
814
  }
815
815
 
@@ -824,7 +824,8 @@ export function processTheme(rawConfig = {}, options = {}) {
824
824
  foundationVars: nonFontVars, // NON-font vars only (font vars live in fontRoles)
825
825
  colorVars, // Context-aware vars: { light: {...}, dark: {...} }
826
826
  code, // Code block theme for runtime injection
827
- 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
828
829
  inline, // Inline text style definitions
829
830
  ...(Object.keys(backgrounds).length > 0 && { backgrounds }), // named backgrounds, as written
830
831
  }
@@ -832,6 +833,45 @@ export function processTheme(rawConfig = {}, options = {}) {
832
833
  return { config, errors, warnings }
833
834
  }
834
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
+
835
875
  /**
836
876
  * Load foundation variables from vars.js export
837
877
  *
@@ -212,6 +212,14 @@ function buildBlockRules(block, appearance) {
212
212
  light.push(...buildElementVars(theme.contexts?.light), ...buildFoundationVars(ctxVars.light))
213
213
  themeDark.push(...buildElementVars(theme.contexts?.dark), ...buildFoundationVars(ctxVars.dark))
214
214
  dark.push(...themeDark)
215
+ } else if (block.themeFollowsScheme) {
216
+ // ⭐ A pinned context that follows the scheme (`medium`, a site's own with a dark version,
217
+ // `Block.contextFollowsScheme`): its own tokens are the light scheme's and its `dark:` ones the
218
+ // dark scheme's, as an Auto section's are, so a light band's colors never sit on the dark
219
+ // version's light text. ⛔ For an hour of 2026-11-28 (1.37.0) they applied in both schemes.
220
+ light.push(...buildElementVars(theme.contexts?.[block.themeName]), ...buildFoundationVars(ctxVars.light))
221
+ themeDark.push(...buildElementVars(theme.darkContexts?.[block.themeName]), ...buildFoundationVars(ctxVars.dark))
222
+ dark.push(...themeDark)
215
223
  } else {
216
224
  always.push(...buildElementVars(theme.contexts?.[block.themeName]), ...buildFoundationVars(ctxVars[block.themeName]))
217
225
  }