@elastic/distillate 0.0.0 → 0.1.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/LICENSE.txt +93 -0
- package/NOTICE.txt +30 -0
- package/README.md +142 -0
- package/THIRD_PARTY_LICENSES.md +768 -0
- package/dist/cjs/collector.js +420 -0
- package/dist/cjs/collector.js.map +1 -0
- package/dist/cjs/css_scan.js +186 -0
- package/dist/cjs/css_scan.js.map +1 -0
- package/dist/cjs/dom_sink.js +40 -0
- package/dist/cjs/dom_sink.js.map +1 -0
- package/dist/cjs/emotion.js +189 -0
- package/dist/cjs/emotion.js.map +1 -0
- package/dist/cjs/engine.js +79 -0
- package/dist/cjs/engine.js.map +1 -0
- package/dist/cjs/environment.js +9 -0
- package/dist/cjs/environment.js.map +1 -0
- package/dist/cjs/hash.js +86 -0
- package/dist/cjs/hash.js.map +1 -0
- package/dist/cjs/idents.js +23 -0
- package/dist/cjs/idents.js.map +1 -0
- package/dist/cjs/index.js +42 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/instance.js +34 -0
- package/dist/cjs/instance.js.map +1 -0
- package/dist/cjs/local_vars.js +100 -0
- package/dist/cjs/local_vars.js.map +1 -0
- package/dist/cjs/minify.js +77 -0
- package/dist/cjs/minify.js.map +1 -0
- package/dist/cjs/names.js +77 -0
- package/dist/cjs/names.js.map +1 -0
- package/dist/cjs/nesting.js +406 -0
- package/dist/cjs/nesting.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/runtime.js +257 -0
- package/dist/cjs/runtime.js.map +1 -0
- package/dist/cjs/styles/authoring.js +169 -0
- package/dist/cjs/styles/authoring.js.map +1 -0
- package/dist/cjs/styles/declarations.js +139 -0
- package/dist/cjs/styles/declarations.js.map +1 -0
- package/dist/cjs/styles/index.js +32 -0
- package/dist/cjs/styles/index.js.map +1 -0
- package/dist/cjs/styles/module.js +233 -0
- package/dist/cjs/styles/module.js.map +1 -0
- package/dist/cjs/styles/registry.js +187 -0
- package/dist/cjs/styles/registry.js.map +1 -0
- package/dist/cjs/styles/types.js +9 -0
- package/dist/cjs/styles/types.js.map +1 -0
- package/dist/cjs/testing.js +37 -0
- package/dist/cjs/testing.js.map +1 -0
- package/dist/cjs/theme.js +249 -0
- package/dist/cjs/theme.js.map +1 -0
- package/dist/cjs/tokens/contextual_var.js +39 -0
- package/dist/cjs/tokens/contextual_var.js.map +1 -0
- package/dist/cjs/tokens/index.js +24 -0
- package/dist/cjs/tokens/index.js.map +1 -0
- package/dist/cjs/tokens/kind.js +14 -0
- package/dist/cjs/tokens/kind.js.map +1 -0
- package/dist/cjs/tokens/light_dark.js +42 -0
- package/dist/cjs/tokens/light_dark.js.map +1 -0
- package/dist/cjs/tokens/scale_token.js +28 -0
- package/dist/cjs/tokens/scale_token.js.map +1 -0
- package/dist/cjs/tokens/theme_token.js +28 -0
- package/dist/cjs/tokens/theme_token.js.map +1 -0
- package/dist/cjs/var_invariant.js +115 -0
- package/dist/cjs/var_invariant.js.map +1 -0
- package/dist/collector.d.ts +84 -0
- package/dist/collector.d.ts.map +1 -0
- package/dist/collector.js +416 -0
- package/dist/collector.js.map +1 -0
- package/dist/css_scan.d.ts +57 -0
- package/dist/css_scan.d.ts.map +1 -0
- package/dist/css_scan.js +180 -0
- package/dist/css_scan.js.map +1 -0
- package/dist/dom_sink.d.ts +27 -0
- package/dist/dom_sink.d.ts.map +1 -0
- package/dist/dom_sink.js +36 -0
- package/dist/dom_sink.js.map +1 -0
- package/dist/emotion.d.ts +47 -0
- package/dist/emotion.d.ts.map +1 -0
- package/dist/emotion.js +184 -0
- package/dist/emotion.js.map +1 -0
- package/dist/engine.d.ts +41 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +75 -0
- package/dist/engine.js.map +1 -0
- package/dist/environment.d.ts +43 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +8 -0
- package/dist/environment.js.map +1 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +82 -0
- package/dist/hash.js.map +1 -0
- package/dist/idents.d.ts +9 -0
- package/dist/idents.d.ts.map +1 -0
- package/dist/idents.js +19 -0
- package/dist/idents.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/instance.d.ts +16 -0
- package/dist/instance.d.ts.map +1 -0
- package/dist/instance.js +30 -0
- package/dist/instance.js.map +1 -0
- package/dist/local_vars.d.ts +75 -0
- package/dist/local_vars.d.ts.map +1 -0
- package/dist/local_vars.js +92 -0
- package/dist/local_vars.js.map +1 -0
- package/dist/minify.d.ts +11 -0
- package/dist/minify.d.ts.map +1 -0
- package/dist/minify.js +73 -0
- package/dist/minify.js.map +1 -0
- package/dist/names.d.ts +39 -0
- package/dist/names.d.ts.map +1 -0
- package/dist/names.js +70 -0
- package/dist/names.js.map +1 -0
- package/dist/nesting.d.ts +59 -0
- package/dist/nesting.d.ts.map +1 -0
- package/dist/nesting.js +400 -0
- package/dist/nesting.js.map +1 -0
- package/dist/runtime.d.ts +36 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +252 -0
- package/dist/runtime.js.map +1 -0
- package/dist/styles/authoring.d.ts +59 -0
- package/dist/styles/authoring.d.ts.map +1 -0
- package/dist/styles/authoring.js +155 -0
- package/dist/styles/authoring.js.map +1 -0
- package/dist/styles/declarations.d.ts +10 -0
- package/dist/styles/declarations.d.ts.map +1 -0
- package/dist/styles/declarations.js +132 -0
- package/dist/styles/declarations.js.map +1 -0
- package/dist/styles/index.d.ts +7 -0
- package/dist/styles/index.d.ts.map +1 -0
- package/dist/styles/index.js +12 -0
- package/dist/styles/index.js.map +1 -0
- package/dist/styles/module.d.ts +5 -0
- package/dist/styles/module.d.ts.map +1 -0
- package/dist/styles/module.js +229 -0
- package/dist/styles/module.js.map +1 -0
- package/dist/styles/registry.d.ts +48 -0
- package/dist/styles/registry.d.ts.map +1 -0
- package/dist/styles/registry.js +182 -0
- package/dist/styles/registry.js.map +1 -0
- package/dist/styles/types.d.ts +211 -0
- package/dist/styles/types.d.ts.map +1 -0
- package/dist/styles/types.js +8 -0
- package/dist/styles/types.js.map +1 -0
- package/dist/testing.d.ts +10 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +31 -0
- package/dist/testing.js.map +1 -0
- package/dist/theme.d.ts +85 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +240 -0
- package/dist/theme.js.map +1 -0
- package/dist/tokens/contextual_var.d.ts +35 -0
- package/dist/tokens/contextual_var.d.ts.map +1 -0
- package/dist/tokens/contextual_var.js +33 -0
- package/dist/tokens/contextual_var.js.map +1 -0
- package/dist/tokens/index.d.ts +5 -0
- package/dist/tokens/index.d.ts.map +1 -0
- package/dist/tokens/index.js +11 -0
- package/dist/tokens/index.js.map +1 -0
- package/dist/tokens/kind.d.ts +2 -0
- package/dist/tokens/kind.d.ts.map +1 -0
- package/dist/tokens/kind.js +10 -0
- package/dist/tokens/kind.js.map +1 -0
- package/dist/tokens/light_dark.d.ts +20 -0
- package/dist/tokens/light_dark.d.ts.map +1 -0
- package/dist/tokens/light_dark.js +37 -0
- package/dist/tokens/light_dark.js.map +1 -0
- package/dist/tokens/scale_token.d.ts +20 -0
- package/dist/tokens/scale_token.d.ts.map +1 -0
- package/dist/tokens/scale_token.js +23 -0
- package/dist/tokens/scale_token.js.map +1 -0
- package/dist/tokens/theme_token.d.ts +20 -0
- package/dist/tokens/theme_token.d.ts.map +1 -0
- package/dist/tokens/theme_token.js +23 -0
- package/dist/tokens/theme_token.js.map +1 -0
- package/dist/var_invariant.d.ts +25 -0
- package/dist/var_invariant.d.ts.map +1 -0
- package/dist/var_invariant.js +108 -0
- package/dist/var_invariant.js.map +1 -0
- package/docs/concepts/authoring.md +115 -0
- package/docs/concepts/collection.md +66 -0
- package/docs/concepts/distillery.md +47 -0
- package/docs/concepts/index.md +18 -0
- package/docs/concepts/naming-and-output.md +123 -0
- package/docs/concepts/ordering.md +61 -0
- package/docs/concepts/single-copy.md +103 -0
- package/docs/concepts/tokens.md +99 -0
- package/docs/docset.yml +53 -0
- package/docs/examples/01-stylesheet.ts +20 -0
- package/docs/examples/02-artifact.ts +26 -0
- package/docs/examples/03-variants.ts +40 -0
- package/docs/examples/04-emotion.ts +33 -0
- package/docs/examples/05-var-invariant.ts +31 -0
- package/docs/examples/06-payload.ts +85 -0
- package/docs/examples/fixture.ts +22 -0
- package/docs/getting-started/index.md +14 -0
- package/docs/getting-started/installation.md +30 -0
- package/docs/getting-started/quick-start.md +61 -0
- package/docs/getting-started/your-first-stylesheet.md +57 -0
- package/docs/guides/compact-artifacts.md +87 -0
- package/docs/guides/emotion-migration.md +60 -0
- package/docs/guides/index.md +17 -0
- package/docs/guides/name-collisions.md +37 -0
- package/docs/guides/non-css-surfaces.md +73 -0
- package/docs/guides/react-renderer.md +158 -0
- package/docs/guides/theming.md +107 -0
- package/docs/guides/var-invariant.md +24 -0
- package/docs/guides/vs-emotion.md +61 -0
- package/docs/index.md +73 -0
- package/docs/logo.svg +7 -0
- package/docs/playground/index.html +12 -0
- package/docs/playground/package.json +23 -0
- package/docs/playground/src/app/app.tsx +109 -0
- package/docs/playground/src/app/css_panel.tsx +119 -0
- package/docs/playground/src/app/editor_panel.tsx +87 -0
- package/docs/playground/src/app/index.ts +8 -0
- package/docs/playground/src/app/monaco_css_editor.tsx +84 -0
- package/docs/playground/src/app/monaco_source_editor.tsx +132 -0
- package/docs/playground/src/app/preview_panel.tsx +99 -0
- package/docs/playground/src/app/tokens_panel.tsx +117 -0
- package/docs/playground/src/app/types.ts +10 -0
- package/docs/playground/src/examples/snippets.test.ts +28 -0
- package/docs/playground/src/examples/snippets.ts +418 -0
- package/docs/playground/src/lib/compile.test.ts +157 -0
- package/docs/playground/src/lib/compile.ts +242 -0
- package/docs/playground/src/lib/demo_environment.test.ts +123 -0
- package/docs/playground/src/lib/demo_environment.ts +140 -0
- package/docs/playground/src/lib/editor_lib.test.ts +39 -0
- package/docs/playground/src/lib/editor_lib.ts +142 -0
- package/docs/playground/src/lib/format_css.test.ts +41 -0
- package/docs/playground/src/lib/format_css.ts +71 -0
- package/docs/playground/src/main.tsx +25 -0
- package/docs/playground/src/setup/chrome.test.ts +47 -0
- package/docs/playground/src/setup/chrome.ts +365 -0
- package/docs/playground/src/setup/monaco_env.ts +64 -0
- package/docs/playground/tsconfig.json +22 -0
- package/docs/playground/vite.config.ts +37 -0
- package/docs/playground/vitest.config.ts +35 -0
- package/docs/playground.md +28 -0
- package/docs/reference/api/emotion.md +372 -0
- package/docs/reference/api/index-1.md +2180 -0
- package/docs/reference/api/index.md +9 -0
- package/docs/reference/api/testing.md +92 -0
- package/docs/reference/api.md +12 -0
- package/docs/reference/entry-points.md +125 -0
- package/docs/reference/environment.md +60 -0
- package/docs/reference/index.md +11 -0
- package/docs/reference/okf-map.md +92 -0
- package/package.json +127 -4
- package/src/collector.ts +526 -0
- package/src/css_scan.ts +263 -0
- package/src/dom_sink.ts +65 -0
- package/src/emotion.ts +311 -0
- package/src/engine.ts +191 -0
- package/src/environment.ts +56 -0
- package/src/hash.ts +111 -0
- package/src/idents.ts +21 -0
- package/src/index.ts +93 -0
- package/src/instance.ts +49 -0
- package/src/local_vars.ts +220 -0
- package/src/minify.ts +87 -0
- package/src/names.ts +129 -0
- package/src/nesting.ts +596 -0
- package/src/runtime.ts +428 -0
- package/src/styles/authoring.ts +244 -0
- package/src/styles/declarations.ts +172 -0
- package/src/styles/index.ts +58 -0
- package/src/styles/module.ts +396 -0
- package/src/styles/registry.ts +261 -0
- package/src/styles/types.ts +283 -0
- package/src/testing.ts +48 -0
- package/src/theme.ts +454 -0
- package/src/tokens/contextual_var.ts +72 -0
- package/src/tokens/index.ts +17 -0
- package/src/tokens/kind.ts +13 -0
- package/src/tokens/light_dark.ts +58 -0
- package/src/tokens/scale_token.ts +41 -0
- package/src/tokens/theme_token.ts +42 -0
- package/src/var_invariant.ts +147 -0
package/src/theme.ts
ADDED
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
3
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
4
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
5
|
+
* 2.0.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ThemeVarDefinition } from './environment';
|
|
9
|
+
import { cssVarName } from './names';
|
|
10
|
+
import {
|
|
11
|
+
type CssToken,
|
|
12
|
+
isScaleToken,
|
|
13
|
+
isSchemePair,
|
|
14
|
+
lightDark,
|
|
15
|
+
type ScaleToken,
|
|
16
|
+
type SchemePair,
|
|
17
|
+
themeToken,
|
|
18
|
+
} from './tokens';
|
|
19
|
+
|
|
20
|
+
/** Theme-tree keys omit `-` so hyphen-joined custom properties reverse uniquely. */
|
|
21
|
+
const THEME_KEY_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
22
|
+
|
|
23
|
+
/** One authoring leaf: invariant string, {@link lightDark} pair, or {@link ScaleToken}. */
|
|
24
|
+
export type ThemeLeaf = string | SchemePair | ScaleToken;
|
|
25
|
+
|
|
26
|
+
/** Nested authoring tree. Keys must match `/^[A-Za-z_][A-Za-z0-9_]*$/`. */
|
|
27
|
+
export type ThemeTree = {
|
|
28
|
+
readonly [key: string]: ThemeLeaf | ThemeTree;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Per-scheme value tree accepted by {@link zipSchemes}. */
|
|
32
|
+
export type SchemeValueTree = {
|
|
33
|
+
readonly [key: string]: string | ScaleToken | SchemeValueTree;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** Token tree derived from an authoring {@link ThemeTree}. */
|
|
37
|
+
export type TokensOf<T> = T extends ScaleToken
|
|
38
|
+
? ScaleToken
|
|
39
|
+
: T extends SchemePair | string
|
|
40
|
+
? CssToken
|
|
41
|
+
: T extends object
|
|
42
|
+
? { readonly [K in keyof T]: TokensOf<T[K]> }
|
|
43
|
+
: never;
|
|
44
|
+
|
|
45
|
+
/** Literal-value tree for one scheme. {@link ScaleToken} leaves use `.value`. */
|
|
46
|
+
export type ValuesOf<T> = T extends ScaleToken
|
|
47
|
+
? string
|
|
48
|
+
: T extends SchemePair | string
|
|
49
|
+
? string
|
|
50
|
+
: T extends object
|
|
51
|
+
? { readonly [K in keyof T]: ValuesOf<T[K]> }
|
|
52
|
+
: never;
|
|
53
|
+
|
|
54
|
+
/** Slash-delimited theme-var paths (excludes {@link ScaleToken} leaves). */
|
|
55
|
+
export type PathsOf<T, Prefix extends string = ''> = T extends ScaleToken
|
|
56
|
+
? never
|
|
57
|
+
: T extends SchemePair | string
|
|
58
|
+
? Prefix extends ''
|
|
59
|
+
? never
|
|
60
|
+
: Prefix
|
|
61
|
+
: T extends object
|
|
62
|
+
? {
|
|
63
|
+
[K in keyof T & string]: PathsOf<
|
|
64
|
+
T[K],
|
|
65
|
+
Prefix extends '' ? K : `${Prefix}/${K}`
|
|
66
|
+
>;
|
|
67
|
+
}[keyof T & string]
|
|
68
|
+
: never;
|
|
69
|
+
|
|
70
|
+
/** {@link zipSchemes} output: equal strings collapse; differing strings become {@link SchemePair}. */
|
|
71
|
+
export type Zipped<T> = T extends ScaleToken
|
|
72
|
+
? ScaleToken
|
|
73
|
+
: T extends string
|
|
74
|
+
? string | SchemePair
|
|
75
|
+
: T extends object
|
|
76
|
+
? { readonly [K in keyof T]: Zipped<T[K]> }
|
|
77
|
+
: never;
|
|
78
|
+
|
|
79
|
+
/** Partial variation of a {@link ThemeTree}. Leaves may change kind between `string` and {@link SchemePair}. */
|
|
80
|
+
export type DeepPartialTheme<T> = T extends ScaleToken
|
|
81
|
+
? ScaleToken
|
|
82
|
+
: T extends SchemePair | string
|
|
83
|
+
? string | SchemePair
|
|
84
|
+
: T extends object
|
|
85
|
+
? { readonly [K in keyof T]?: DeepPartialTheme<T[K]> }
|
|
86
|
+
: never;
|
|
87
|
+
|
|
88
|
+
/** Media-conditioned variation. `media` is intrinsic; it is not a selector. */
|
|
89
|
+
export interface MediaThemeVariation<T extends ThemeTree = ThemeTree> {
|
|
90
|
+
/** Media query wrapping this variation's emitted diffs. */
|
|
91
|
+
readonly media: string;
|
|
92
|
+
/** Partial variation of the base theme. */
|
|
93
|
+
readonly variation: DeepPartialTheme<T>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Named variation: a partial of the base, or a media-conditioned partial. */
|
|
97
|
+
export type ThemeVariation<T extends ThemeTree = ThemeTree> =
|
|
98
|
+
DeepPartialTheme<T> | MediaThemeVariation<T>;
|
|
99
|
+
|
|
100
|
+
/** One named variation resolved against the base at construction. */
|
|
101
|
+
export interface ResolvedThemeVariation {
|
|
102
|
+
/** Name passed in `variations`. */
|
|
103
|
+
readonly name: string;
|
|
104
|
+
/** Intrinsic media query, when declared. */
|
|
105
|
+
readonly media?: string;
|
|
106
|
+
/** Base theme vars with this variation applied. */
|
|
107
|
+
readonly themeVars: Readonly<Record<string, ThemeVarDefinition>>;
|
|
108
|
+
/** Paths whose serialized value differs from the base. */
|
|
109
|
+
readonly diffs: Readonly<Record<string, ThemeVarDefinition>>;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Folds two per-scheme trees into one authoring tree. Equal string leaves stay bare; differing strings become {@link lightDark}. {@link ScaleToken} leaves must agree.
|
|
114
|
+
*
|
|
115
|
+
* @param light Light-scheme value tree.
|
|
116
|
+
* @param dark Dark-scheme value tree.
|
|
117
|
+
* @throws If shapes, leaf kinds, or scale values disagree, or if a differing string pair is not a CSS `<color>`.
|
|
118
|
+
*/
|
|
119
|
+
export const zipSchemes = <T extends SchemeValueTree>(
|
|
120
|
+
light: T,
|
|
121
|
+
dark: T
|
|
122
|
+
): Zipped<T> => zipNode(light, dark, '') as Zipped<T>;
|
|
123
|
+
|
|
124
|
+
/** Walks `theme` into a {@link CssToken} / {@link ScaleToken} tree and a `themeVars` registry. */
|
|
125
|
+
export const deriveTheme = <T extends ThemeTree>(
|
|
126
|
+
prefix: string,
|
|
127
|
+
theme: T
|
|
128
|
+
): { tokens: TokensOf<T>; themeVars: Record<string, ThemeVarDefinition> } => {
|
|
129
|
+
const themeVars = dict<ThemeVarDefinition>();
|
|
130
|
+
const tokens = walk(theme, '', prefix, themeVars) as TokensOf<T>;
|
|
131
|
+
return { tokens, themeVars };
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Nested literal values for one scheme. Walks `theme` so {@link ScaleToken} leaves resolve to `.value`.
|
|
136
|
+
*
|
|
137
|
+
* @param theme Authoring tree that produced `themeVars`.
|
|
138
|
+
* @param themeVars Base registry from {@link Distillery.themeVars}.
|
|
139
|
+
* @param scheme Which side of each {@link ThemeVarDefinition} to read.
|
|
140
|
+
* @param variation When set, overridden paths come from `variation.diffs`; everything else stays on `themeVars`.
|
|
141
|
+
*/
|
|
142
|
+
export const resolveThemeValues = <T extends ThemeTree>(
|
|
143
|
+
theme: T,
|
|
144
|
+
themeVars: Readonly<Record<string, ThemeVarDefinition>>,
|
|
145
|
+
scheme: 'light' | 'dark',
|
|
146
|
+
variation?: ResolvedThemeVariation
|
|
147
|
+
): ValuesOf<T> => {
|
|
148
|
+
const vars = variation
|
|
149
|
+
? Object.assign(dict<ThemeVarDefinition>(), themeVars, variation.diffs)
|
|
150
|
+
: themeVars;
|
|
151
|
+
return walkValues(theme, '', vars, scheme) as ValuesOf<T>;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/** Looks up a declared variation. @throws If `name` is not in `variations`. */
|
|
155
|
+
export const requireThemeVariation = (
|
|
156
|
+
variations: Readonly<Record<string, ResolvedThemeVariation>> | undefined,
|
|
157
|
+
name: string
|
|
158
|
+
): ResolvedThemeVariation => {
|
|
159
|
+
const resolved =
|
|
160
|
+
variations && Object.hasOwn(variations, name)
|
|
161
|
+
? variations[name]
|
|
162
|
+
: undefined;
|
|
163
|
+
if (!resolved) {
|
|
164
|
+
const declared = Object.keys(variations ?? {});
|
|
165
|
+
const suffix =
|
|
166
|
+
declared.length > 0
|
|
167
|
+
? ` Declared: ${declared.join(', ')}.`
|
|
168
|
+
: ' No variations were declared.';
|
|
169
|
+
throw new Error(`Unknown variation "${name}".${suffix}`);
|
|
170
|
+
}
|
|
171
|
+
return resolved;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/** Serialized `light` / `light-dark(light, dark)` form used in emission and diffs. */
|
|
175
|
+
export const serializedThemeValue = ({
|
|
176
|
+
light,
|
|
177
|
+
dark,
|
|
178
|
+
}: ThemeVarDefinition): string =>
|
|
179
|
+
light === dark ? light : `light-dark(${light},${dark})`;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Resolves named `variations` against `base`. Variations extend the base only (no chains).
|
|
183
|
+
*
|
|
184
|
+
* @throws If a variation introduces an unknown path, a leaf-kind mismatch, or a diverging {@link ScaleToken}.
|
|
185
|
+
*/
|
|
186
|
+
export const resolveThemeVariations = (
|
|
187
|
+
prefix: string,
|
|
188
|
+
base: ThemeTree,
|
|
189
|
+
baseVars: Readonly<Record<string, ThemeVarDefinition>>,
|
|
190
|
+
variations: Readonly<Record<string, ThemeVariation>> | undefined
|
|
191
|
+
): Readonly<Record<string, ResolvedThemeVariation>> => {
|
|
192
|
+
if (!variations) {
|
|
193
|
+
return {};
|
|
194
|
+
}
|
|
195
|
+
const out = dict<ResolvedThemeVariation>();
|
|
196
|
+
for (const [name, declaration] of Object.entries(variations)) {
|
|
197
|
+
const { media, variation } = unwrapThemeVariation(declaration, name);
|
|
198
|
+
const merged = applyVariation(base, variation, name, '') as ThemeTree;
|
|
199
|
+
const { themeVars } = deriveTheme(prefix, merged);
|
|
200
|
+
const diffs = dict<ThemeVarDefinition>();
|
|
201
|
+
for (const [path, definition] of Object.entries(themeVars)) {
|
|
202
|
+
const baseDefinition = baseVars[path];
|
|
203
|
+
if (
|
|
204
|
+
!baseDefinition ||
|
|
205
|
+
serializedThemeValue(definition) !==
|
|
206
|
+
serializedThemeValue(baseDefinition)
|
|
207
|
+
) {
|
|
208
|
+
diffs[path] = definition;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
out[name] = Object.freeze({
|
|
212
|
+
name,
|
|
213
|
+
themeVars: Object.freeze(themeVars),
|
|
214
|
+
diffs: Object.freeze(diffs),
|
|
215
|
+
...(media ? { media } : {}),
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return Object.freeze(out);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const unwrapThemeVariation = (
|
|
222
|
+
declaration: ThemeVariation,
|
|
223
|
+
name: string
|
|
224
|
+
): { media?: string; variation: DeepPartialTheme<ThemeTree> } => {
|
|
225
|
+
if (!isMediaThemeVariation(declaration)) {
|
|
226
|
+
return { variation: declaration };
|
|
227
|
+
}
|
|
228
|
+
const extra = Object.keys(declaration).filter(
|
|
229
|
+
(key) => key !== 'media' && key !== 'variation'
|
|
230
|
+
);
|
|
231
|
+
if (extra.length > 0) {
|
|
232
|
+
throw new Error(
|
|
233
|
+
`Variation "${name}" media declaration cannot include keys ${extra.map((key) => `"${key}"`).join(', ')}.`
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
if (declaration.media.trim().length === 0) {
|
|
237
|
+
throw new Error(`Variation "${name}" has an empty media query.`);
|
|
238
|
+
}
|
|
239
|
+
return { media: declaration.media, variation: declaration.variation };
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const isMediaThemeVariation = (
|
|
243
|
+
value: ThemeVariation
|
|
244
|
+
): value is MediaThemeVariation => {
|
|
245
|
+
if (!isPlainObject(value)) {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
const candidate = value;
|
|
249
|
+
return (
|
|
250
|
+
typeof candidate.media === 'string' && isPlainObject(candidate.variation)
|
|
251
|
+
);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const applyVariation = (
|
|
255
|
+
base: unknown,
|
|
256
|
+
variation: unknown,
|
|
257
|
+
variationName: string,
|
|
258
|
+
path: string
|
|
259
|
+
): unknown => {
|
|
260
|
+
if (variation === undefined) {
|
|
261
|
+
return base;
|
|
262
|
+
}
|
|
263
|
+
if (isScaleToken(base) || isScaleToken(variation)) {
|
|
264
|
+
if (!isScaleToken(base) || !isScaleToken(variation)) {
|
|
265
|
+
throw new Error(
|
|
266
|
+
`Variation "${variationName}": "${path}" is a ScaleToken in one tree and not the other.`
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
if (base.value !== variation.value || base.cq !== variation.cq) {
|
|
270
|
+
throw new Error(
|
|
271
|
+
`Variation "${variationName}": ScaleToken at "${path}" disagrees with the base (scale tokens inline and cannot vary by variation).`
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
return base;
|
|
275
|
+
}
|
|
276
|
+
if (isSchemePair(variation) || typeof variation === 'string') {
|
|
277
|
+
if (typeof base !== 'string' && !isSchemePair(base)) {
|
|
278
|
+
throw new Error(
|
|
279
|
+
`Variation "${variationName}": "${path}" is a leaf in the variation and a group in the base.`
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
return variation;
|
|
283
|
+
}
|
|
284
|
+
if (isThemeGroup(variation) && isThemeGroup(base)) {
|
|
285
|
+
const out = Object.assign(dict<unknown>(), base);
|
|
286
|
+
for (const [key, child] of Object.entries(variation)) {
|
|
287
|
+
if (!Object.hasOwn(base, key)) {
|
|
288
|
+
throw new Error(
|
|
289
|
+
`Variation "${variationName}": unknown path "${childPath(path, key)}".`
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
out[key] = applyVariation(
|
|
293
|
+
base[key],
|
|
294
|
+
child,
|
|
295
|
+
variationName,
|
|
296
|
+
childPath(path, key)
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
301
|
+
throw new Error(
|
|
302
|
+
`Variation "${variationName}": "${path || '(root)'}" is not a valid partial.`
|
|
303
|
+
);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const isThemeGroup = (value: unknown): value is Record<string, unknown> =>
|
|
307
|
+
isPlainObject(value) && !isSchemePair(value) && !isScaleToken(value);
|
|
308
|
+
|
|
309
|
+
const walkValues = (
|
|
310
|
+
node: unknown,
|
|
311
|
+
path: string,
|
|
312
|
+
themeVars: Readonly<Record<string, ThemeVarDefinition>>,
|
|
313
|
+
scheme: 'light' | 'dark'
|
|
314
|
+
): unknown => {
|
|
315
|
+
if (isScaleToken(node)) {
|
|
316
|
+
assertNotRoot(path);
|
|
317
|
+
return node.value;
|
|
318
|
+
}
|
|
319
|
+
if (isSchemePair(node) || typeof node === 'string') {
|
|
320
|
+
assertNotRoot(path);
|
|
321
|
+
const definition = themeVars[path];
|
|
322
|
+
if (!definition) {
|
|
323
|
+
throw new Error(`No theme var for "${path}".`);
|
|
324
|
+
}
|
|
325
|
+
return definition[scheme];
|
|
326
|
+
}
|
|
327
|
+
if (isPlainObject(node)) {
|
|
328
|
+
const out = dict<unknown>();
|
|
329
|
+
for (const [key, child] of Object.entries(node)) {
|
|
330
|
+
out[key] = walkValues(child, childPath(path, key), themeVars, scheme);
|
|
331
|
+
}
|
|
332
|
+
return Object.freeze(out);
|
|
333
|
+
}
|
|
334
|
+
const label = path === '' ? 'theme root' : `"${path}"`;
|
|
335
|
+
throw new Error(
|
|
336
|
+
`Theme leaf at ${label} must be a string, lightDark(...), or ScaleToken.`
|
|
337
|
+
);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
const walk = (
|
|
341
|
+
node: unknown,
|
|
342
|
+
path: string,
|
|
343
|
+
prefix: string,
|
|
344
|
+
themeVars: Record<string, ThemeVarDefinition>
|
|
345
|
+
): unknown => {
|
|
346
|
+
if (isScaleToken(node)) {
|
|
347
|
+
assertNotRoot(path);
|
|
348
|
+
return node;
|
|
349
|
+
}
|
|
350
|
+
if (isSchemePair(node)) {
|
|
351
|
+
assertNotRoot(path);
|
|
352
|
+
return registerCssToken(path, prefix, node.light, node.dark, themeVars);
|
|
353
|
+
}
|
|
354
|
+
if (typeof node === 'string') {
|
|
355
|
+
assertNotRoot(path);
|
|
356
|
+
return registerCssToken(path, prefix, node, node, themeVars);
|
|
357
|
+
}
|
|
358
|
+
if (isPlainObject(node)) {
|
|
359
|
+
const out = dict<unknown>();
|
|
360
|
+
for (const [key, child] of Object.entries(node)) {
|
|
361
|
+
assertThemeKey(key);
|
|
362
|
+
const childPath = path === '' ? key : `${path}/${key}`;
|
|
363
|
+
out[key] = walk(child, childPath, prefix, themeVars);
|
|
364
|
+
}
|
|
365
|
+
return Object.freeze(out);
|
|
366
|
+
}
|
|
367
|
+
const label = path === '' ? 'theme root' : `"${path}"`;
|
|
368
|
+
throw new Error(
|
|
369
|
+
`Theme leaf at ${label} must be a string, lightDark(...), or ScaleToken.`
|
|
370
|
+
);
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
const registerCssToken = (
|
|
374
|
+
path: string,
|
|
375
|
+
prefix: string,
|
|
376
|
+
light: string,
|
|
377
|
+
dark: string,
|
|
378
|
+
themeVars: Record<string, ThemeVarDefinition>
|
|
379
|
+
): CssToken => {
|
|
380
|
+
const cssVar = cssVarName(prefix, path);
|
|
381
|
+
themeVars[path] = { path, cssVar, light, dark };
|
|
382
|
+
return themeToken(path, cssVar);
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
const zipNode = (light: unknown, dark: unknown, path: string): unknown => {
|
|
386
|
+
if (isScaleToken(light) || isScaleToken(dark)) {
|
|
387
|
+
if (!isScaleToken(light) || !isScaleToken(dark)) {
|
|
388
|
+
throw new Error(
|
|
389
|
+
`zipSchemes: "${path}" is a ScaleToken in one scheme and not the other.`
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
if (light.value !== dark.value || light.cq !== dark.cq) {
|
|
393
|
+
throw new Error(
|
|
394
|
+
`zipSchemes: ScaleToken at "${path}" disagrees across schemes.`
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
return light;
|
|
398
|
+
}
|
|
399
|
+
if (typeof light === 'string' || typeof dark === 'string') {
|
|
400
|
+
if (typeof light !== 'string' || typeof dark !== 'string') {
|
|
401
|
+
throw new Error(
|
|
402
|
+
`zipSchemes: "${path}" is a string in one scheme and not the other.`
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
return light === dark ? light : lightDark(light, dark);
|
|
406
|
+
}
|
|
407
|
+
if (isPlainObject(light) && isPlainObject(dark)) {
|
|
408
|
+
for (const key of Object.keys(light)) {
|
|
409
|
+
if (!Object.hasOwn(dark, key)) {
|
|
410
|
+
throw new Error(
|
|
411
|
+
`zipSchemes: "${childPath(path, key)}" is missing in the dark tree.`
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
for (const key of Object.keys(dark)) {
|
|
416
|
+
if (!Object.hasOwn(light, key)) {
|
|
417
|
+
throw new Error(
|
|
418
|
+
`zipSchemes: "${childPath(path, key)}" is missing in the light tree.`
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
const out = dict<unknown>();
|
|
423
|
+
for (const key of Object.keys(light)) {
|
|
424
|
+
out[key] = zipNode(light[key], dark[key], childPath(path, key));
|
|
425
|
+
}
|
|
426
|
+
return out;
|
|
427
|
+
}
|
|
428
|
+
throw new Error(
|
|
429
|
+
`zipSchemes: "${path || '(root)'}" must be a string, ScaleToken, or nested object.`
|
|
430
|
+
);
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
const childPath = (path: string, key: string): string =>
|
|
434
|
+
path === '' ? key : `${path}/${key}`;
|
|
435
|
+
|
|
436
|
+
const assertNotRoot = (path: string): void => {
|
|
437
|
+
if (path === '') {
|
|
438
|
+
throw new Error('Theme root must be an object.');
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
const assertThemeKey = (key: string): void => {
|
|
443
|
+
if (!THEME_KEY_RE.test(key)) {
|
|
444
|
+
throw new Error(
|
|
445
|
+
`Theme key "${key}" must match /^[A-Za-z_][A-Za-z0-9_]*$/ (hyphens are rejected so path segments reverse uniquely).`
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const dict = <T>(): Record<string, T> =>
|
|
451
|
+
Object.create(null) as Record<string, T>;
|
|
452
|
+
|
|
453
|
+
const isPlainObject = (value: unknown): value is Record<string, unknown> =>
|
|
454
|
+
Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
3
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
4
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
5
|
+
* 2.0.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { hasKind } from './kind';
|
|
9
|
+
|
|
10
|
+
/** Represents a contextual CSS variable name. Stringifies to its custom property name. */
|
|
11
|
+
export interface ContextualCssVarName {
|
|
12
|
+
/** Brand for {@link isContextualCssVarName}. */
|
|
13
|
+
readonly __kind: 'contextual-var-name';
|
|
14
|
+
/** Group/key path for this variable. */
|
|
15
|
+
readonly path: `vars/${string}`;
|
|
16
|
+
/** Actual emitted CSS custom property name. */
|
|
17
|
+
readonly name: `--${string}`;
|
|
18
|
+
/** Stringifies to {@link ContextualCssVarName.name}. */
|
|
19
|
+
toString(): string;
|
|
20
|
+
/** Template interpolation; same as {@link ContextualCssVarName.toString}. */
|
|
21
|
+
[Symbol.toPrimitive](hint: string): string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Represents a contextual CSS variable. Stringifies to `var(--...)`. */
|
|
25
|
+
export interface ContextualCssVar {
|
|
26
|
+
/** Brand for {@link isContextualCssVar}. */
|
|
27
|
+
readonly __kind: 'contextual-var';
|
|
28
|
+
/** Group/key path for this variable. */
|
|
29
|
+
readonly path: `vars/${string}`;
|
|
30
|
+
/** Wrapper yielding just the custom property name. */
|
|
31
|
+
readonly name: ContextualCssVarName;
|
|
32
|
+
/** Usable CSS `var(...)` reference string. */
|
|
33
|
+
readonly ref: `var(--${string})`;
|
|
34
|
+
/** Stringifies to {@link ContextualCssVar.ref}. */
|
|
35
|
+
toString(): string;
|
|
36
|
+
/** Template interpolation; same as {@link ContextualCssVar.toString}. */
|
|
37
|
+
[Symbol.toPrimitive](hint: string): string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Shared contextual var. Stringifies to `var(<prop>)`; `.name` is the ident. */
|
|
41
|
+
export const contextualVar = (
|
|
42
|
+
path: `vars/${string}`,
|
|
43
|
+
prop: `--${string}`
|
|
44
|
+
): ContextualCssVar => {
|
|
45
|
+
const ref: `var(--${string})` = `var(${prop})`;
|
|
46
|
+
const propToString = (): string => prop;
|
|
47
|
+
const refToString = (): string => ref;
|
|
48
|
+
const name: ContextualCssVarName = {
|
|
49
|
+
__kind: 'contextual-var-name',
|
|
50
|
+
path,
|
|
51
|
+
name: prop,
|
|
52
|
+
toString: propToString,
|
|
53
|
+
[Symbol.toPrimitive]: propToString,
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
__kind: 'contextual-var',
|
|
57
|
+
path,
|
|
58
|
+
name,
|
|
59
|
+
ref,
|
|
60
|
+
toString: refToString,
|
|
61
|
+
[Symbol.toPrimitive]: refToString,
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** `true` when `value` is a {@link ContextualCssVar}. @param value Value to test. */
|
|
66
|
+
export const isContextualCssVar = (value: unknown): value is ContextualCssVar =>
|
|
67
|
+
hasKind(value, 'contextual-var');
|
|
68
|
+
|
|
69
|
+
/** `true` when `value` is a {@link ContextualCssVarName}. @param value Value to test. */
|
|
70
|
+
export const isContextualCssVarName = (
|
|
71
|
+
value: unknown
|
|
72
|
+
): value is ContextualCssVarName => hasKind(value, 'contextual-var-name');
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
3
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
4
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
5
|
+
* 2.0.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
type ContextualCssVar,
|
|
10
|
+
type ContextualCssVarName,
|
|
11
|
+
contextualVar,
|
|
12
|
+
isContextualCssVar,
|
|
13
|
+
isContextualCssVarName,
|
|
14
|
+
} from './contextual_var';
|
|
15
|
+
export { type SchemePair, isSchemePair, lightDark } from './light_dark';
|
|
16
|
+
export { type ScaleToken, cq, isScaleToken, scaleToken } from './scale_token';
|
|
17
|
+
export { type CssToken, isCssToken, themeToken } from './theme_token';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
3
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
4
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
5
|
+
* 2.0.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const hasKind = (value: unknown, kind: string): boolean =>
|
|
9
|
+
Boolean(
|
|
10
|
+
value &&
|
|
11
|
+
typeof value === 'object' &&
|
|
12
|
+
(value as { __kind?: string }).__kind === kind
|
|
13
|
+
);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
3
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
4
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
5
|
+
* 2.0.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { hasKind } from './kind';
|
|
9
|
+
|
|
10
|
+
/** Scheme-varying color pair. Compiles to `light-dark(light, dark)`. */
|
|
11
|
+
export interface SchemePair {
|
|
12
|
+
/** Brand for {@link isSchemePair}. */
|
|
13
|
+
readonly __kind: 'scheme-pair';
|
|
14
|
+
/** Light color-scheme value. Must be a CSS `<color>`. */
|
|
15
|
+
readonly light: string;
|
|
16
|
+
/** Dark color-scheme value. Must be a CSS `<color>`. */
|
|
17
|
+
readonly dark: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** `true` when `value` is a {@link SchemePair}. @param value Value to test. */
|
|
21
|
+
export const isSchemePair = (value: unknown): value is SchemePair =>
|
|
22
|
+
hasKind(value, 'scheme-pair');
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Scheme-varying color pair. Both sides must be CSS `<color>` values.
|
|
26
|
+
*
|
|
27
|
+
* @param light Light color-scheme value.
|
|
28
|
+
* @param dark Dark color-scheme value.
|
|
29
|
+
* @throws If either value is not a CSS `<color>`.
|
|
30
|
+
*/
|
|
31
|
+
export const lightDark = (light: string, dark: string): SchemePair => {
|
|
32
|
+
assertCssColor(light, 'light');
|
|
33
|
+
assertCssColor(dark, 'dark');
|
|
34
|
+
return { __kind: 'scheme-pair', light, dark };
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const isCssColor = (value: string): boolean => {
|
|
38
|
+
const trimmed = value.trim();
|
|
39
|
+
if (/^#(?:[\da-f]{3}|[\da-f]{4}|[\da-f]{6}|[\da-f]{8})$/i.test(trimmed)) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
if (/^(?:transparent|currentcolor)$/i.test(trimmed)) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
return (
|
|
46
|
+
/^(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color-mix|color)\(/i.test(
|
|
47
|
+
trimmed
|
|
48
|
+
) && trimmed.endsWith(')')
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const assertCssColor = (value: string, side: 'light' | 'dark'): void => {
|
|
53
|
+
if (!isCssColor(value)) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
`lightDark() ${side} value "${value}" is not a CSS <color>; light-dark() would be dropped by the browser.`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
3
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
4
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
5
|
+
* 2.0.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { hasKind } from './kind';
|
|
9
|
+
|
|
10
|
+
/** Represents a CSS scale size token. Stringifies to its literal value. */
|
|
11
|
+
export interface ScaleToken {
|
|
12
|
+
/** Brand for {@link isScaleToken}. */
|
|
13
|
+
readonly __kind: 'scale-token';
|
|
14
|
+
/** Literal CSS value, inlined into declarations. */
|
|
15
|
+
readonly value: string;
|
|
16
|
+
/** Container-query-relative variant of `value`. */
|
|
17
|
+
readonly cq: string;
|
|
18
|
+
/** Stringifies to {@link ScaleToken.value}. */
|
|
19
|
+
toString(): string;
|
|
20
|
+
/** Template interpolation; same as {@link ScaleToken.toString}. */
|
|
21
|
+
[Symbol.toPrimitive](hint: string): string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Literal size token. Stringifies to `value`; `cq` is the container-relative variant. */
|
|
25
|
+
export const scaleToken = (value: string, cq: string): ScaleToken => {
|
|
26
|
+
const valueToString = (): string => value;
|
|
27
|
+
return {
|
|
28
|
+
__kind: 'scale-token',
|
|
29
|
+
value,
|
|
30
|
+
cq,
|
|
31
|
+
toString: valueToString,
|
|
32
|
+
[Symbol.toPrimitive]: valueToString,
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** Alias of {@link scaleToken} for theme-tree authoring. */
|
|
37
|
+
export const cq = scaleToken;
|
|
38
|
+
|
|
39
|
+
/** `true` when `value` is a {@link ScaleToken}. @param value Value to test. */
|
|
40
|
+
export const isScaleToken = (value: unknown): value is ScaleToken =>
|
|
41
|
+
hasKind(value, 'scale-token');
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
3
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
4
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
5
|
+
* 2.0.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { hasKind } from './kind';
|
|
9
|
+
|
|
10
|
+
/** Represents a CSS theme token. Stringifies to `var(--...)`. */
|
|
11
|
+
export interface CssToken {
|
|
12
|
+
/** Brand for {@link isCssToken}. */
|
|
13
|
+
readonly __kind: 'theme-token';
|
|
14
|
+
/** Path string mapping to this token in the environment. */
|
|
15
|
+
readonly path: string;
|
|
16
|
+
/** Actual emitted CSS custom property name. */
|
|
17
|
+
readonly cssVar: `--${string}`;
|
|
18
|
+
/** Usable CSS `var(...)` reference string. */
|
|
19
|
+
readonly ref: `var(--${string})`;
|
|
20
|
+
/** Stringifies to {@link CssToken.ref}. */
|
|
21
|
+
toString(): string;
|
|
22
|
+
/** Template interpolation; same as {@link CssToken.toString}. */
|
|
23
|
+
[Symbol.toPrimitive](hint: string): string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Theme token that stringifies to `var(<cssVar>)`. */
|
|
27
|
+
export const themeToken = (path: string, cssVar: `--${string}`): CssToken => {
|
|
28
|
+
const ref: `var(--${string})` = `var(${cssVar})`;
|
|
29
|
+
const refToString = (): string => ref;
|
|
30
|
+
return {
|
|
31
|
+
__kind: 'theme-token',
|
|
32
|
+
path,
|
|
33
|
+
cssVar,
|
|
34
|
+
ref,
|
|
35
|
+
toString: refToString,
|
|
36
|
+
[Symbol.toPrimitive]: refToString,
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** `true` when `value` is a {@link CssToken}. @param value Value to test. */
|
|
41
|
+
export const isCssToken = (value: unknown): value is CssToken =>
|
|
42
|
+
hasKind(value, 'theme-token');
|