theme-vir 28.18.2 → 28.18.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.
@@ -48,6 +48,12 @@ export declare const defaultContrastLevels: Readonly<ArrayOrSelectParam<Contrast
48
48
  * @category Internal
49
49
  */
50
50
  export type BuildLowLevelColorThemeOptions = PartialWithUndefined<{
51
+ /**
52
+ * Theme var prefix.
53
+ *
54
+ * @default 'vir'
55
+ */
56
+ prefix: string;
51
57
  /**
52
58
  * The default theme colors for {@link defineColorTheme}. Defaults to
53
59
  * {@link defaultLightThemePair}.
@@ -74,31 +80,31 @@ export type BuildLowLevelColorThemeOptions = PartialWithUndefined<{
74
80
  *
75
81
  * @category Color Theme
76
82
  */
77
- export declare function buildColorTheme(colorPalette: Readonly<ColorPaletteVars>, { omittedColorValues, crossContrastLevels, }?: Readonly<BuildLowLevelColorThemeOptions>): {
83
+ export declare function buildColorTheme(colorPalette: Readonly<ColorPaletteVars>, { omittedColorValues, crossContrastLevels, prefix, }?: Readonly<BuildLowLevelColorThemeOptions>): {
78
84
  defaultLight: import("./color-theme.js").ColorTheme<Record<`${Lowercase<string>}-${Lowercase<string>}`, ((Required<Pick<{
79
- foreground: import("./color-theme.js").ColorInitValue;
80
- background: import("./color-theme.js").ColorInitValue;
85
+ foreground: import("./color-theme-init.js").ColorInitValue;
86
+ background: import("./color-theme-init.js").ColorInitValue;
81
87
  }, "foreground">> & Partial<Pick<{
82
- foreground: import("./color-theme.js").ColorInitValue;
83
- background: import("./color-theme.js").ColorInitValue;
88
+ foreground: import("./color-theme-init.js").ColorInitValue;
89
+ background: import("./color-theme-init.js").ColorInitValue;
84
90
  }, "background">>) | (Required<Pick<{
85
- foreground: import("./color-theme.js").ColorInitValue;
86
- background: import("./color-theme.js").ColorInitValue;
91
+ foreground: import("./color-theme-init.js").ColorInitValue;
92
+ background: import("./color-theme-init.js").ColorInitValue;
87
93
  }, "background">> & Partial<Pick<{
88
- foreground: import("./color-theme.js").ColorInitValue;
89
- background: import("./color-theme.js").ColorInitValue;
94
+ foreground: import("./color-theme-init.js").ColorInitValue;
95
+ background: import("./color-theme-init.js").ColorInitValue;
90
96
  }, "foreground">>)) & {}>>;
91
- darkOverride: import("./color-theme-override.js").ColorThemeOverride<Record<`${Lowercase<string>}-${Lowercase<string>}`, ((Required<Pick<{
92
- foreground: import("./color-theme.js").ColorInitValue;
93
- background: import("./color-theme.js").ColorInitValue;
97
+ darkOverride: import("./color-theme-init.js").ColorThemeOverride<Record<`${Lowercase<string>}-${Lowercase<string>}`, ((Required<Pick<{
98
+ foreground: import("./color-theme-init.js").ColorInitValue;
99
+ background: import("./color-theme-init.js").ColorInitValue;
94
100
  }, "foreground">> & Partial<Pick<{
95
- foreground: import("./color-theme.js").ColorInitValue;
96
- background: import("./color-theme.js").ColorInitValue;
101
+ foreground: import("./color-theme-init.js").ColorInitValue;
102
+ background: import("./color-theme-init.js").ColorInitValue;
97
103
  }, "background">>) | (Required<Pick<{
98
- foreground: import("./color-theme.js").ColorInitValue;
99
- background: import("./color-theme.js").ColorInitValue;
104
+ foreground: import("./color-theme-init.js").ColorInitValue;
105
+ background: import("./color-theme-init.js").ColorInitValue;
100
106
  }, "background">> & Partial<Pick<{
101
- foreground: import("./color-theme.js").ColorInitValue;
102
- background: import("./color-theme.js").ColorInitValue;
107
+ foreground: import("./color-theme-init.js").ColorInitValue;
108
+ background: import("./color-theme-init.js").ColorInitValue;
103
109
  }, "foreground">>)) & {}>>;
104
110
  };
@@ -101,7 +101,7 @@ export const defaultContrastLevels = {
101
101
  *
102
102
  * @category Color Theme
103
103
  */
104
- export function buildColorTheme(colorPalette, { omittedColorValues = defaultOmittedColorGroupColorValues, crossContrastLevels = defaultContrastLevels, } = {}) {
104
+ export function buildColorTheme(colorPalette, { omittedColorValues = defaultOmittedColorGroupColorValues, crossContrastLevels = defaultContrastLevels, prefix = 'vir', } = {}) {
105
105
  const contrastLevels = extractParam(crossContrastLevels, {
106
106
  mapFrom: contrastLevelLabel,
107
107
  });
@@ -109,6 +109,7 @@ export function buildColorTheme(colorPalette, { omittedColorValues = defaultOmit
109
109
  const defaultTheme = {
110
110
  background: 'white',
111
111
  foreground: 'black',
112
+ prefix,
112
113
  };
113
114
  const lightThemeColors = {};
114
115
  const darkThemeOverrides = {};
@@ -184,7 +185,7 @@ export function buildColorTheme(colorPalette, { omittedColorValues = defaultOmit
184
185
  });
185
186
  if (cross.crossWith === 'color-in-foreground-light-mode') {
186
187
  const name = [
187
- firstColor.prefix,
188
+ prefix,
188
189
  firstColor.colorName,
189
190
  'foreground',
190
191
  cross.contrast,
@@ -193,7 +194,7 @@ export function buildColorTheme(colorPalette, { omittedColorValues = defaultOmit
193
194
  }
194
195
  else if (cross.crossWith === 'color-in-background-light-mode') {
195
196
  const name = [
196
- firstColor.prefix,
197
+ prefix,
197
198
  firstColor.colorName,
198
199
  'background',
199
200
  cross.contrast,
@@ -202,7 +203,7 @@ export function buildColorTheme(colorPalette, { omittedColorValues = defaultOmit
202
203
  }
203
204
  else if (cross.crossWith === 'color-on-self-light-mode') {
204
205
  const name = [
205
- firstColor.prefix,
206
+ prefix,
206
207
  firstColor.colorName,
207
208
  'on',
208
209
  'self',
@@ -212,7 +213,7 @@ export function buildColorTheme(colorPalette, { omittedColorValues = defaultOmit
212
213
  }
213
214
  else if (cross.crossWith === 'color-in-foreground-dark-mode') {
214
215
  const name = [
215
- firstColor.prefix,
216
+ prefix,
216
217
  firstColor.colorName,
217
218
  'foreground',
218
219
  cross.contrast,
@@ -221,7 +222,7 @@ export function buildColorTheme(colorPalette, { omittedColorValues = defaultOmit
221
222
  }
222
223
  else if (cross.crossWith === 'color-in-background-dark-mode') {
223
224
  const name = [
224
- firstColor.prefix,
225
+ prefix,
225
226
  firstColor.colorName,
226
227
  'background',
227
228
  cross.contrast,
@@ -231,7 +232,7 @@ export function buildColorTheme(colorPalette, { omittedColorValues = defaultOmit
231
232
  }
232
233
  else if (cross.crossWith === 'color-on-self-dark-mode') {
233
234
  const name = [
234
- firstColor.prefix,
235
+ prefix,
235
236
  firstColor.colorName,
236
237
  'on',
237
238
  'self',
@@ -1,6 +1,6 @@
1
1
  import { type PartialWithUndefined } from '@augment-vir/common';
2
2
  import { BookPageControlType, type BookPage } from 'element-book';
3
- import { type ColorThemeOverride } from './color-theme-override.js';
3
+ import { type ColorThemeOverride } from './color-theme-init.js';
4
4
  import { type ColorTheme } from './color-theme.js';
5
5
  /**
6
6
  * Create multiple element-book pages to showcase a theme its overrides (if any).
@@ -1,9 +1,10 @@
1
- import { check } from '@augment-vir/assert';
1
+ import { assertWrap, check } from '@augment-vir/assert';
2
2
  import { groupArrayBy } from '@augment-vir/common';
3
3
  import { VirColorPair } from '@electrovir/color';
4
4
  import { BookPageControlType, defineBookPage, definePageControl, } from 'element-book';
5
- import { css, html, listen, nothing } from 'element-vir';
5
+ import { css, html, listen, nothing, onDomRendered } from 'element-vir';
6
6
  import { generateThemeCode } from './color-theme-code.js';
7
+ import { applyColorTheme } from './color-theme.js';
7
8
  const noneOverridesSelectionValue = 'None';
8
9
  /**
9
10
  * Create multiple element-book pages to showcase a theme its overrides (if any).
@@ -73,7 +74,14 @@ export function createColorThemeBookPages({ parent, title, theme, hideInverseCol
73
74
  `
74
75
  : nothing;
75
76
  return html `
76
- <div class="with-inverse">${normalTemplate}${inverseTemplate}</div>
77
+ <div
78
+ class="with-inverse"
79
+ ${onDomRendered((element) => {
80
+ applyColorTheme(assertWrap.instanceOf(element, HTMLElement), theme);
81
+ })}
82
+ >
83
+ ${normalTemplate}${inverseTemplate}
84
+ </div>
77
85
  `;
78
86
  }
79
87
  function createThemePageExamples(defineExample, defaultTheme, overrides) {
@@ -1,5 +1,5 @@
1
1
  import { type PartialWithUndefined } from '@augment-vir/common';
2
- import { type ColorThemeOverride } from './color-theme-override.js';
2
+ import { type ColorThemeOverride } from './color-theme-init.js';
3
3
  import { type ColorTheme } from './color-theme.js';
4
4
  /**
5
5
  * Convert a color theme into code to define that color theme.
@@ -1,4 +1,6 @@
1
+ import { check } from '@augment-vir/assert';
1
2
  import { getObjectTypedEntries, } from '@augment-vir/common';
3
+ import { CSSResult } from 'element-vir';
2
4
  /**
3
5
  * Convert a color theme into code to define that color theme.
4
6
  *
@@ -64,14 +66,9 @@ function colorInitValuesEqual(a, b) {
64
66
  if (typeof a !== typeof b) {
65
67
  return false;
66
68
  }
67
- if (typeof a === 'string' || typeof a === 'number') {
68
- return a === b;
69
- }
70
- if ('_$cssResult$' in a && '_$cssResult$' in b) {
71
- return a.cssText === b.cssText;
72
- }
73
- // For references and SingleCssVarDefinition, compare as JSON
74
- return JSON.stringify(a) === JSON.stringify(b);
69
+ const aString = check.isString(a) || a instanceof CSSResult ? String(a) : JSON.stringify(a);
70
+ const bString = check.isString(b) || b instanceof CSSResult ? String(b) : JSON.stringify(b);
71
+ return aString === bString;
75
72
  }
76
73
  function extractCssVarName(cssValue) {
77
74
  const match = cssValue.match(/^var\(--([^,)]+)/);
@@ -84,7 +81,7 @@ function colorInitValueToCode(value, indentLevel, paletteVarName) {
84
81
  else if (typeof value === 'number') {
85
82
  return String(value);
86
83
  }
87
- else if ('_$cssResult$' in value) {
84
+ else if (value instanceof CSSResult) {
88
85
  const cssText = String(value);
89
86
  if (paletteVarName) {
90
87
  const varName = extractCssVarName(cssText);
@@ -0,0 +1,47 @@
1
+ import { type CSSResult } from 'element-vir';
2
+ import { type CssVarName, type SingleCssVarDefinition } from 'lit-css-vars';
3
+ import { type RequireAtLeastOne } from 'type-fest';
4
+ import { type ColorTheme } from './color-theme.js';
5
+ /**
6
+ * Reference another color from this same definition inside {@link ColorInitValue}
7
+ *
8
+ * @category Internal
9
+ */
10
+ export type ColorInitReference = RequireAtLeastOne<{
11
+ refForeground: CssVarName;
12
+ refBackground: CssVarName;
13
+ refDefaultBackground: true;
14
+ refDefaultForeground: true;
15
+ }>;
16
+ /**
17
+ * All possible types for {@link ColorInit}.
18
+ *
19
+ * @category Internal
20
+ */
21
+ export type ColorInitValue = string | number | CSSResult | ColorInitReference | SingleCssVarDefinition;
22
+ /**
23
+ * An individual theme color init.
24
+ *
25
+ * @category Internal
26
+ */
27
+ export type ColorInit = RequireAtLeastOne<{
28
+ foreground: ColorInitValue;
29
+ background: ColorInitValue;
30
+ }>;
31
+ /**
32
+ * Base input type for the type parameter in `defineColorTheme`.
33
+ *
34
+ * @category Internal
35
+ */
36
+ export type ColorThemeInit = Record<CssVarName, ColorInit>;
37
+ /**
38
+ * Output of `defineColorThemeOverride`.
39
+ *
40
+ * @category Internal
41
+ */
42
+ export type ColorThemeOverride<Init extends ColorThemeInit = ColorThemeInit> = {
43
+ name: string;
44
+ overrides: Record<CssVarName, string>;
45
+ originalTheme: ColorTheme<Init>;
46
+ asTheme: ColorTheme<Init>;
47
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,6 @@
1
- import { type CssVarName } from 'lit-css-vars';
2
1
  import { type RequireAtLeastOne } from 'type-fest';
3
- import { themeDefaultKey, type ColorInit, type ColorTheme, type ColorThemeInit, type NoRefColorInit } from './color-theme.js';
2
+ import { type ColorInit, type ColorThemeInit, type ColorThemeOverride } from './color-theme-init.js';
3
+ import { themeDefaultKey, type ColorTheme, type NoRefColorInit } from './color-theme.js';
4
4
  /**
5
5
  * Input for {@link defineColorThemeOverride} color overrides.
6
6
  *
@@ -9,17 +9,6 @@ import { themeDefaultKey, type ColorInit, type ColorTheme, type ColorThemeInit,
9
9
  export type ColorThemeOverrideInit<Theme extends ColorTheme = ColorTheme> = Omit<Partial<{
10
10
  [ColorName in keyof Theme['colors']]: ColorInit;
11
11
  }>, typeof themeDefaultKey>;
12
- /**
13
- * Output of {@link defineColorThemeOverride}.
14
- *
15
- * @category Internal
16
- */
17
- export type ColorThemeOverride<Init extends ColorThemeInit = ColorThemeInit> = {
18
- name: string;
19
- overrides: Record<CssVarName, string>;
20
- originalTheme: ColorTheme<Init>;
21
- asTheme: ColorTheme<Init>;
22
- };
23
12
  /**
24
13
  * Define a color theme override. Use this to define multiple theme variations, like light mode vs
25
14
  * dark mode.
@@ -31,12 +20,3 @@ export declare function defineColorThemeOverride<const Init extends ColorThemeIn
31
20
  defaultOverride: Readonly<NoRefColorInit>;
32
21
  colorOverrides: Readonly<ColorThemeOverrideInit<ColorTheme<Init>>>;
33
22
  }>>): ColorThemeOverride<Init>;
34
- /**
35
- * Set all color theme CSS vars on the given element. If no override is given, the theme color
36
- * default values are assigned.
37
- *
38
- * @category Color Theme
39
- */
40
- export declare function applyColorTheme<const Theme extends ColorTheme>(
41
- /** This should usually be the top-level `html` element. */
42
- element: HTMLElement, fullTheme: Theme, themeOverride?: ColorThemeOverride | undefined): void;
@@ -1,5 +1,4 @@
1
- import { getObjectTypedEntries, getObjectTypedKeys, getObjectTypedValues, mapObjectValues, } from '@augment-vir/common';
2
- import { setCssVarValue } from 'lit-css-vars';
1
+ import { getObjectTypedEntries, getObjectTypedKeys, mapObjectValues } from '@augment-vir/common';
3
2
  import { createColorCssVarDefault, defineColorTheme, themeDefaultKey, } from './color-theme.js';
4
3
  function applyCssVarOverride({ originalTheme, layerKey, themeColor, override, overrideValues, }) {
5
4
  const layerOverride = override?.[layerKey];
@@ -72,36 +71,3 @@ export function defineColorThemeOverride(originalTheme, overrideName, { defaultO
72
71
  asTheme,
73
72
  };
74
73
  }
75
- /**
76
- * Set all color theme CSS vars on the given element. If no override is given, the theme color
77
- * default values are assigned.
78
- *
79
- * @category Color Theme
80
- */
81
- export function applyColorTheme(
82
- /** This should usually be the top-level `html` element. */
83
- element, fullTheme, themeOverride) {
84
- getObjectTypedValues(fullTheme.colors).forEach((themeColor) => {
85
- applyIndividualThemeColorValue({
86
- element,
87
- layerKey: 'background',
88
- themeColor,
89
- themeOverride,
90
- });
91
- applyIndividualThemeColorValue({
92
- element,
93
- layerKey: 'foreground',
94
- themeColor,
95
- themeOverride,
96
- });
97
- });
98
- }
99
- function applyIndividualThemeColorValue({ element, layerKey, themeOverride, themeColor, }) {
100
- const override = themeOverride?.overrides[String(themeColor[layerKey].name)];
101
- const value = override || themeColor[layerKey].default;
102
- setCssVarValue({
103
- forCssVar: themeColor[layerKey],
104
- onElement: element,
105
- toValue: value,
106
- });
107
- }
@@ -1,33 +1,8 @@
1
1
  import { type RequiredAndNotNull, type Values } from '@augment-vir/common';
2
- import { type CSSResult } from 'element-vir';
2
+ import { CSSResult } from 'element-vir';
3
3
  import { type CssVarName, type SingleCssVarDefinition } from 'lit-css-vars';
4
4
  import { type RequireAtLeastOne } from 'type-fest';
5
- /**
6
- * Reference another color from this same definition inside {@link ColorInitValue}
7
- *
8
- * @category Internal
9
- */
10
- export type ColorInitReference = RequireAtLeastOne<{
11
- refForeground: CssVarName;
12
- refBackground: CssVarName;
13
- refDefaultBackground: true;
14
- refDefaultForeground: true;
15
- }>;
16
- /**
17
- * All possible types for {@link ColorInit}.
18
- *
19
- * @category Internal
20
- */
21
- export type ColorInitValue = string | number | CSSResult | ColorInitReference | SingleCssVarDefinition;
22
- /**
23
- * An individual theme color init.
24
- *
25
- * @category Internal
26
- */
27
- export type ColorInit = RequireAtLeastOne<{
28
- foreground: ColorInitValue;
29
- background: ColorInitValue;
30
- }>;
5
+ import { type ColorInit, type ColorInitReference, type ColorInitValue, type ColorThemeInit, type ColorThemeOverride } from './color-theme-init.js';
31
6
  /**
32
7
  * Same as {@link ColorInit} but without references.
33
8
  *
@@ -52,12 +27,6 @@ export type ColorThemeColor<Init extends ColorInit = ColorInit, Name extends Css
52
27
  name: Name;
53
28
  init: Init;
54
29
  };
55
- /**
56
- * Base input type for the type parameter in {@link defineColorTheme}.
57
- *
58
- * @category Internal
59
- */
60
- export type ColorThemeInit = Record<CssVarName, ColorInit>;
61
30
  /**
62
31
  * A finalized color theme, output from {@link defineColorTheme}.
63
32
  *
@@ -69,7 +38,7 @@ export type ColorTheme<Init extends ColorThemeInit = ColorThemeInit> = {
69
38
  /** The original init object for this theme. */
70
39
  init: {
71
40
  colors: Init;
72
- default: RequiredAndNotNull<NoRefColorInit>;
41
+ default: Readonly<DefaultColorThemeInit>;
73
42
  };
74
43
  };
75
44
  /**
@@ -80,7 +49,7 @@ export type ColorTheme<Init extends ColorThemeInit = ColorThemeInit> = {
80
49
  export type AllColorThemeColors<Init extends ColorThemeInit = ColorThemeInit> = {
81
50
  [ColorName in keyof Init as ColorName extends CssVarName ? ColorName : never]: ColorName extends CssVarName ? Init[ColorName] extends ColorInit ? ColorThemeColor<Init[ColorName], ColorName> : never : never;
82
51
  } & {
83
- [themeDefaultKey]: ColorThemeColor<RequiredAndNotNull<NoRefColorInit>, typeof themeDefaultKey>;
52
+ [themeDefaultKey]: ColorThemeColor<DefaultColorThemeInit, typeof themeDefaultKey>;
84
53
  };
85
54
  /** @category Internal */
86
55
  export declare function noRefColorInitToString(init: Values<NoRefColorInit>): string;
@@ -89,7 +58,15 @@ export declare function noRefColorInitToString(init: Values<NoRefColorInit>): st
89
58
  *
90
59
  * @category Internal
91
60
  */
92
- export declare function createColorCssVarDefault(fromName: string, init: ColorInitValue, defaultInit: RequiredAndNotNull<NoRefColorInit>, colorsInit: ColorThemeInit): string | number | CSSResult;
61
+ export declare function createColorCssVarDefault(fromName: string, init: ColorInitValue, defaultInit: Readonly<DefaultColorThemeInit>, colorsInit: ColorThemeInit): string | number | CSSResult;
62
+ /**
63
+ * Default theme init for {@link defineColorTheme}.
64
+ *
65
+ * @category Internal
66
+ */
67
+ export type DefaultColorThemeInit = RequiredAndNotNull<NoRefColorInit> & {
68
+ prefix: string;
69
+ };
93
70
  /**
94
71
  * Default foreground/background color theme used in {@link ColorTheme}. Do not define a theme color
95
72
  * with this name!
@@ -97,9 +74,18 @@ export declare function createColorCssVarDefault(fromName: string, init: ColorIn
97
74
  * @category Internal
98
75
  */
99
76
  export declare const themeDefaultKey = "theme-default";
77
+ /**
78
+ * Set all color theme CSS vars on the given element. If no override is given, the theme color
79
+ * default values are assigned.
80
+ *
81
+ * @category Color Theme
82
+ */
83
+ export declare function applyColorTheme<const Theme extends ColorTheme>(
84
+ /** This should usually be the top-level `html` element. */
85
+ element: HTMLElement, fullTheme: Theme, themeOverride?: ColorThemeOverride | undefined): void;
100
86
  /**
101
87
  * Define a color theme.
102
88
  *
103
89
  * @category Color Theme
104
90
  */
105
- export declare function defineColorTheme<const Init extends ColorThemeInit>(defaultInit: RequiredAndNotNull<NoRefColorInit>, allColorsInit: Init): ColorTheme<Init>;
91
+ export declare function defineColorTheme<const Init extends ColorThemeInit>(defaultInit: Readonly<DefaultColorThemeInit>, allColorsInit: Init): ColorTheme<Init>;
@@ -1,9 +1,10 @@
1
1
  import { assert, check } from '@augment-vir/assert';
2
- import { getObjectTypedEntries, log, } from '@augment-vir/common';
3
- import { defineCssVars, } from 'lit-css-vars';
2
+ import { getObjectTypedEntries, getObjectTypedValues, log, mapObjectValues, } from '@augment-vir/common';
3
+ import { CSSResult } from 'element-vir';
4
+ import { CssVarSyntaxName, defineCssVars, setCssVarValue, } from 'lit-css-vars';
4
5
  /** @category Internal */
5
6
  export function noRefColorInitToString(init) {
6
- if (check.isPrimitive(init) || '_$cssResult$' in init) {
7
+ if (check.isPrimitive(init) || init instanceof CSSResult) {
7
8
  return String(init);
8
9
  }
9
10
  else {
@@ -16,14 +17,16 @@ export function noRefColorInitToString(init) {
16
17
  * @category Internal
17
18
  */
18
19
  export function createColorCssVarDefault(fromName, init, defaultInit, colorsInit) {
19
- if (check.isPrimitive(init) || '_$cssResult$' in init) {
20
+ const defaultForegroundKey = `${defaultInit.prefix}-default-fg`;
21
+ const defaultBackgroundKey = `${defaultInit.prefix}-default-bg`;
22
+ if (check.isPrimitive(init) || init instanceof CSSResult) {
20
23
  return init;
21
24
  }
22
25
  else if ('refDefaultBackground' in init) {
23
- return `var(--default-bg, ${noRefColorInitToString(defaultInit.background)})`;
26
+ return `var(--${defaultBackgroundKey}, ${noRefColorInitToString(defaultInit.background)})`;
24
27
  }
25
28
  else if ('refDefaultForeground' in init) {
26
- return `var(--default-fg, ${noRefColorInitToString(defaultInit.foreground)})`;
29
+ return `var(--${defaultForegroundKey}, ${noRefColorInitToString(defaultInit.foreground)})`;
27
30
  }
28
31
  else if ('refBackground' in init || 'refForeground' in init) {
29
32
  const referenceKey = check.hasKey(init, 'refBackground')
@@ -39,8 +42,8 @@ export function createColorCssVarDefault(fromName, init, defaultInit, colorsInit
39
42
  }
40
43
  const colorValue = referenced[layerKey] ||
41
44
  (layerKey === 'foreground'
42
- ? createColorCssVarDefault('default-fg', defaultInit.foreground, defaultInit, colorsInit)
43
- : createColorCssVarDefault('default-bg', defaultInit.background, defaultInit, colorsInit));
45
+ ? createColorCssVarDefault(defaultForegroundKey, defaultInit.foreground, defaultInit, colorsInit)
46
+ : createColorCssVarDefault(defaultBackgroundKey, defaultInit.background, defaultInit, colorsInit));
44
47
  return `var(--${reference}-${layerKey === 'foreground' ? 'fg' : 'bg'}, ${createColorCssVarDefault(reference, colorValue, defaultInit, colorsInit)})`;
45
48
  }
46
49
  else {
@@ -54,6 +57,39 @@ export function createColorCssVarDefault(fromName, init, defaultInit, colorsInit
54
57
  * @category Internal
55
58
  */
56
59
  export const themeDefaultKey = 'theme-default';
60
+ /**
61
+ * Set all color theme CSS vars on the given element. If no override is given, the theme color
62
+ * default values are assigned.
63
+ *
64
+ * @category Color Theme
65
+ */
66
+ export function applyColorTheme(
67
+ /** This should usually be the top-level `html` element. */
68
+ element, fullTheme, themeOverride) {
69
+ getObjectTypedValues(fullTheme.colors).forEach((themeColor) => {
70
+ applyIndividualThemeColorValue({
71
+ element,
72
+ layerKey: 'background',
73
+ themeColor,
74
+ themeOverride,
75
+ });
76
+ applyIndividualThemeColorValue({
77
+ element,
78
+ layerKey: 'foreground',
79
+ themeColor,
80
+ themeOverride,
81
+ });
82
+ });
83
+ }
84
+ function applyIndividualThemeColorValue({ element, layerKey, themeOverride, themeColor, }) {
85
+ const override = themeOverride?.overrides[String(themeColor[layerKey].name)];
86
+ const value = override || themeColor[layerKey].default;
87
+ setCssVarValue({
88
+ forCssVar: themeColor[layerKey],
89
+ onElement: element,
90
+ toValue: value,
91
+ });
92
+ }
57
93
  /**
58
94
  * Define a color theme.
59
95
  *
@@ -64,30 +100,59 @@ export function defineColorTheme(defaultInit, allColorsInit) {
64
100
  if (themeDefaultKey in allColorsInit) {
65
101
  throw new Error(`Cannot define theme color by name '${themeDefaultKey}', it is used internally.`);
66
102
  }
67
- const defaultColors = defineCssVars({
68
- 'default-fg': createColorCssVarDefault('default-fg', defaultInit.foreground, defaultInit, allColorsInit),
69
- 'default-bg': createColorCssVarDefault('default-bg', defaultInit.background, defaultInit, allColorsInit),
70
- 'default-inverse-fg': createColorCssVarDefault('default-inverse-fg', defaultInit.background, defaultInit, allColorsInit),
71
- 'default-inverse-bg': createColorCssVarDefault('default-inverse-bg', defaultInit.foreground, defaultInit, allColorsInit),
103
+ const defaultForegroundKey = `${defaultInit.prefix}-default-fg`;
104
+ const defaultBackgroundKey = `${defaultInit.prefix}-default-bg`;
105
+ const inverseDefaultForegroundKey = `${defaultInit.prefix}-default-inverse-fg`;
106
+ const inverseDefaultBackgroundKey = `${defaultInit.prefix}-default-inverse-bg`;
107
+ const defaultColorsInit = mapObjectValues({
108
+ [defaultForegroundKey]: createColorCssVarDefault(defaultForegroundKey, defaultInit.foreground, defaultInit, allColorsInit),
109
+ [defaultBackgroundKey]: createColorCssVarDefault(defaultBackgroundKey, defaultInit.background, defaultInit, allColorsInit),
110
+ [inverseDefaultForegroundKey]: createColorCssVarDefault(inverseDefaultForegroundKey, defaultInit.background, defaultInit, allColorsInit),
111
+ [inverseDefaultBackgroundKey]: createColorCssVarDefault(inverseDefaultBackgroundKey, defaultInit.foreground, defaultInit, allColorsInit),
112
+ }, (key, value) => {
113
+ return {
114
+ default: value,
115
+ initialValue: 'transparent',
116
+ syntax: CssVarSyntaxName.Color,
117
+ };
72
118
  });
119
+ const defaultColors = defineCssVars(defaultColorsInit);
73
120
  const cssVarsSetup = getObjectTypedEntries(allColorsInit).reduce((accum, [colorName, colorInit,]) => {
74
121
  const names = createCssVarNames(colorName);
75
- accum[names.foreground] = colorInit.foreground
122
+ const foreground = colorInit.foreground
76
123
  ? createColorCssVarDefault([
77
124
  colorName,
78
125
  'foreground',
79
126
  ].join(' '), colorInit.foreground, defaultInit, allColorsInit)
80
- : `var(${defaultColors['default-fg'].name}, ${defaultColors['default-fg'].default})`;
81
- accum[names.background] = colorInit.background
127
+ : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
128
+ `var(${defaultColors[defaultForegroundKey].name}, ${defaultColors[defaultForegroundKey].default})`;
129
+ const background = colorInit.background
82
130
  ? createColorCssVarDefault([
83
131
  colorName,
84
132
  'background',
85
133
  ].join(' '), colorInit.background, defaultInit, allColorsInit)
86
- : `var(${defaultColors['default-bg'].name}, ${defaultColors['default-bg'].default})`;
87
- accum[names.foregroundInverse] =
88
- `var(--${names.background}, ${accum[names.background]})`;
89
- accum[names.backgroundInverse] =
90
- `var(--${names.foreground}, ${accum[names.foreground]})`;
134
+ : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
135
+ `var(${defaultColors[defaultBackgroundKey].name}, ${defaultColors[defaultBackgroundKey].default})`;
136
+ accum[names.foreground] = {
137
+ default: foreground,
138
+ initialValue: 'transparent',
139
+ syntax: CssVarSyntaxName.Color,
140
+ };
141
+ accum[names.background] = {
142
+ default: background,
143
+ initialValue: 'transparent',
144
+ syntax: CssVarSyntaxName.Color,
145
+ };
146
+ accum[names.foregroundInverse] = {
147
+ default: `var(--${names.background})`,
148
+ initialValue: 'transparent',
149
+ syntax: CssVarSyntaxName.Color,
150
+ };
151
+ accum[names.backgroundInverse] = {
152
+ default: `var(--${names.foreground})`,
153
+ initialValue: 'transparent',
154
+ syntax: CssVarSyntaxName.Color,
155
+ };
91
156
  return accum;
92
157
  }, {});
93
158
  /**
@@ -123,15 +188,19 @@ export function defineColorTheme(defaultInit, allColorsInit) {
123
188
  };
124
189
  });
125
190
  const themeDefaultColors = {
126
- foreground: defaultColors['default-fg'],
127
- background: defaultColors['default-bg'],
191
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
192
+ foreground: defaultColors[defaultForegroundKey],
193
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
194
+ background: defaultColors[defaultBackgroundKey],
128
195
  init: defaultInit,
129
196
  name: themeDefaultKey,
130
197
  };
131
198
  const themeDefaultInverseColors = {
132
199
  ...themeDefaultColors,
133
- foreground: defaultColors['default-inverse-fg'],
134
- background: defaultColors['default-inverse-bg'],
200
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
201
+ foreground: defaultColors[inverseDefaultForegroundKey],
202
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
203
+ background: defaultColors[inverseDefaultBackgroundKey],
135
204
  };
136
205
  return {
137
206
  colors: {
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from './color-theme/build-color-theme.js';
2
- export * from './color-theme/color-css.js';
3
2
  export * from './color-theme/color-palette-book-pages.js';
4
3
  export * from './color-theme/color-theme-book-pages.js';
5
4
  export * from './color-theme/color-theme-code.js';
5
+ export * from './color-theme/color-theme-init.js';
6
6
  export * from './color-theme/color-theme-override.js';
7
7
  export * from './color-theme/color-theme.js';
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from './color-theme/build-color-theme.js';
2
- export * from './color-theme/color-css.js';
3
2
  export * from './color-theme/color-palette-book-pages.js';
4
3
  export * from './color-theme/color-theme-book-pages.js';
5
4
  export * from './color-theme/color-theme-code.js';
5
+ export * from './color-theme/color-theme-init.js';
6
6
  export * from './color-theme/color-theme-override.js';
7
7
  export * from './color-theme/color-theme.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "theme-vir",
3
- "version": "28.18.2",
3
+ "version": "28.18.4",
4
4
  "description": "Create an entire web theme.",
5
5
  "keywords": [
6
6
  "design",
@@ -43,29 +43,29 @@
43
43
  "test:update": "virmator test web update"
44
44
  },
45
45
  "dependencies": {
46
- "@augment-vir/assert": "^31.58.0",
47
- "@augment-vir/common": "^31.58.0",
48
- "@electrovir/color": "^1.7.0",
46
+ "@augment-vir/assert": "^31.59.0",
47
+ "@augment-vir/common": "^31.59.0",
48
+ "@electrovir/color": "^1.7.5",
49
49
  "apca-w3": "^0.1.9",
50
- "lit-css-vars": "^3.0.11",
50
+ "lit-css-vars": "^3.2.2",
51
51
  "type-fest": "^5.4.1"
52
52
  },
53
53
  "devDependencies": {
54
- "@augment-vir/test": "^31.58.0",
54
+ "@augment-vir/test": "^31.59.0",
55
55
  "@types/apca-w3": "^0.1.3",
56
56
  "@web/dev-server-esbuild": "^1.0.4",
57
57
  "@web/test-runner": "^0.20.2",
58
58
  "@web/test-runner-commands": "^0.9.0",
59
59
  "@web/test-runner-playwright": "^0.11.1",
60
60
  "@web/test-runner-visual-regression": "^0.10.0",
61
- "element-book": "^26.15.0",
62
- "element-vir": "^26.14.1",
61
+ "element-book": "^26.15.3",
62
+ "element-vir": "^26.14.2",
63
63
  "esbuild": "^0.27.2",
64
64
  "istanbul-smart-text-reporter": "^1.1.5",
65
65
  "markdown-code-example-inserter": "^3.0.3",
66
66
  "typedoc": "^0.28.16",
67
67
  "typescript": "5.9.3",
68
- "vira": "^28.19.5",
68
+ "vira": "^28.19.7",
69
69
  "vite": "^7.3.1",
70
70
  "vite-tsconfig-paths": "^6.0.4"
71
71
  },
@@ -1,9 +0,0 @@
1
- import { type CSSResult } from 'element-vir';
2
- import { type ColorThemeColor } from './color-theme.js';
3
- /**
4
- * Creates foreground and background CSS code. The foreground color is applied to the CSS `color`
5
- * property and the background color is applied to the CSS `background-color` property.
6
- *
7
- * @category Color Theme
8
- */
9
- export declare function colorCss(color: Readonly<ColorThemeColor>): CSSResult;
@@ -1,13 +0,0 @@
1
- import { css } from 'element-vir';
2
- /**
3
- * Creates foreground and background CSS code. The foreground color is applied to the CSS `color`
4
- * property and the background color is applied to the CSS `background-color` property.
5
- *
6
- * @category Color Theme
7
- */
8
- export function colorCss(color) {
9
- return css `
10
- color: ${color.foreground.value};
11
- background-color: ${color.background.value};
12
- `;
13
- }