@wistia/kaleidoscope 0.0.5 → 0.0.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/CHANGELOG.md +18 -0
- package/dist/color-BY2NrNVz.js +148 -0
- package/dist/color-BY2NrNVz.js.map +1 -0
- package/dist/{color-BD12TVX4.d.ts → color-CwLKMi6Y.d.ts} +4 -25
- package/dist/color-CwLKMi6Y.d.ts.map +1 -0
- package/dist/engine-B00ZVZ0d.d.ts +85 -0
- package/dist/engine-B00ZVZ0d.d.ts.map +1 -0
- package/dist/engine-DeOUPRa0.js +661 -0
- package/dist/engine-DeOUPRa0.js.map +1 -0
- package/dist/entries/color.d.ts +1 -1
- package/dist/entries/color.js +3 -2
- package/dist/entries/engine.d.ts +3 -0
- package/dist/entries/engine.js +10 -0
- package/dist/entries/styleRegistry.d.ts +2 -2
- package/dist/entries/styleRegistry.js +2 -3
- package/dist/entries/validate.d.ts +1 -1
- package/dist/entries/validate.js +2 -3
- package/dist/entries/vocabulary.d.ts +1 -1
- package/dist/entries/vocabulary.js +3 -2
- package/dist/{color-C99gJSgH.js → getColorLightnessClassification--FSkjXEx.js} +29 -178
- package/dist/getColorLightnessClassification--FSkjXEx.js.map +1 -0
- package/dist/guards-t_cf_IOV.js +24 -0
- package/dist/guards-t_cf_IOV.js.map +1 -0
- package/dist/{index-CG6dL9XZ.d.ts → index-BOJCY_MU.d.ts} +4 -32
- package/dist/index-BOJCY_MU.d.ts.map +1 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.js +9 -6
- package/dist/styleRegistry-A0nrsHk-.d.ts +15 -0
- package/dist/styleRegistry-A0nrsHk-.d.ts.map +1 -0
- package/dist/{styleRegistry-BoviV8Ue.js → styleRegistry-CT4jvu4h.js} +41 -42
- package/dist/styleRegistry-CT4jvu4h.js.map +1 -0
- package/dist/{styleVocabulary-Kl8p6TDk.js → styleVocabulary-DcxbkEvK.js} +19 -80
- package/dist/styleVocabulary-DcxbkEvK.js.map +1 -0
- package/dist/typography-BA9Pz8Im.js +65 -0
- package/dist/typography-BA9Pz8Im.js.map +1 -0
- package/dist/{validate-V28YRNxt.d.ts → validate-Bqx__f3d.d.ts} +2 -6
- package/dist/validate-Bqx__f3d.d.ts.map +1 -0
- package/dist/{validate-C_h6gEXp.js → validate-D-OR7_3g.js} +20 -46
- package/dist/validate-D-OR7_3g.js.map +1 -0
- package/package.json +19 -14
- package/schema/style-v1.json +7 -2
- package/styles/aurora-dark.json +3 -2
- package/styles/aurora-light.json +3 -2
- package/styles/glass-dark.json +3 -2
- package/styles/glass-light.json +3 -2
- package/styles/opaque-bright.json +3 -2
- package/styles/opaque-heavy.json +3 -2
- package/styles/opaque-light.json +3 -2
- package/styles/opaque-medium.json +3 -2
- package/styles/opaque-minimal.json +3 -2
- package/dist/color-BD12TVX4.d.ts.map +0 -1
- package/dist/color-C99gJSgH.js.map +0 -1
- package/dist/index-CG6dL9XZ.d.ts.map +0 -1
- package/dist/styleRegistry-BoviV8Ue.js.map +0 -1
- package/dist/styleRegistry-DNVNmafi.d.ts +0 -35
- package/dist/styleRegistry-DNVNmafi.d.ts.map +0 -1
- package/dist/styleVocabulary-Kl8p6TDk.js.map +0 -1
- package/dist/validate-C_h6gEXp.js.map +0 -1
- package/dist/validate-V28YRNxt.d.ts.map +0 -1
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
|
|
2
|
+
/*
|
|
3
|
+
* @license @wistia/kaleidoscope v0.0.6
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2026, Wistia, Inc. and its affiliates.
|
|
6
|
+
*
|
|
7
|
+
* This source code is unlicensed, all rights reserved.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { c as getBestColorForText, d as colorContrastRatiosByShape, f as Color, h as clamp, n as buildBrandedColorScale, o as getBrandLightBackgroundColor, s as getBrandDarkBackgroundColor, t as getColorLightnessClassification, u as adjustColorForProperContrast } from "./getColorLightnessClassification--FSkjXEx.js";
|
|
11
|
+
import { l as ORNAMENTAL_BORDER_STYLES, n as TYPE_ROLES, o as STACKABLE_ON, s as SURFACE_NAMES } from "./typography-BA9Pz8Im.js";
|
|
12
|
+
import { n as isOneOf } from "./guards-t_cf_IOV.js";
|
|
13
|
+
// src/brandTokens.ts
|
|
14
|
+
/** The Brand Kit tokens a Style document can reference, each as `$<name>`. */
|
|
15
|
+
const BRAND_TOKEN_NAMES = [
|
|
16
|
+
"primaryColor",
|
|
17
|
+
"pageBackgroundColor",
|
|
18
|
+
"borderRadius",
|
|
19
|
+
"headingFontFamily",
|
|
20
|
+
"bodyFontFamily",
|
|
21
|
+
"buttonFontFamily"
|
|
22
|
+
];
|
|
23
|
+
/** What a missing Brand Kit token falls back to: Wistia's own brand. */
|
|
24
|
+
const DEFAULT_BRAND_TOKENS = {
|
|
25
|
+
primaryColor: "#2949e5",
|
|
26
|
+
pageBackgroundColor: "#ffffff",
|
|
27
|
+
borderRadius: 20,
|
|
28
|
+
headingFontFamily: "GT Walsheim",
|
|
29
|
+
bodyFontFamily: "Inter",
|
|
30
|
+
buttonFontFamily: "Inter"
|
|
31
|
+
};
|
|
32
|
+
// src/private/engine/constants/color.ts
|
|
33
|
+
/** Degrees a `relation` turns a hue, measured around the Lab a/b plane. Placeholder. */
|
|
34
|
+
const RELATION_HUE_DEGREES = {
|
|
35
|
+
same: 0,
|
|
36
|
+
analogous: 30,
|
|
37
|
+
triad: 120,
|
|
38
|
+
complementary: 180
|
|
39
|
+
};
|
|
40
|
+
/** WCAG non-text contrast: below this a fill does not read as a shape against its parent. */
|
|
41
|
+
const INVERT_BELOW_CONTRAST = colorContrastRatiosByShape.nonText;
|
|
42
|
+
/** How far low-emphasis text fades from the surface's text color. Placeholder. */
|
|
43
|
+
const LOW_EMPHASIS_TEXT_ALPHA = .7;
|
|
44
|
+
// src/private/engine/scale.ts
|
|
45
|
+
const MAX_CACHED_SCALES = 600;
|
|
46
|
+
const scaleCache = /* @__PURE__ */ new Map();
|
|
47
|
+
/** The branded scale through `color`, cached because every surface of every Style asks for it. */
|
|
48
|
+
const scaleOf = (color) => {
|
|
49
|
+
const hex = color.toHex();
|
|
50
|
+
let scale = scaleCache.get(hex);
|
|
51
|
+
if (scale === void 0) {
|
|
52
|
+
if (scaleCache.size >= MAX_CACHED_SCALES) scaleCache.clear();
|
|
53
|
+
scale = buildBrandedColorScale(new Color(hex));
|
|
54
|
+
scaleCache.set(hex, scale);
|
|
55
|
+
}
|
|
56
|
+
return scale;
|
|
57
|
+
};
|
|
58
|
+
const lastStep = (scale) => scale.steps - 1;
|
|
59
|
+
const stepAt = (scale, index) => scale.atStep(clamp(index, 0, lastStep(scale)));
|
|
60
|
+
const nearestStep = (scale, color) => clamp(Math.round(scale.stepOf(color)), 0, lastStep(scale));
|
|
61
|
+
/**
|
|
62
|
+
* `offset` steps along `scale` from `color`, away from its lightness: a dark color steps lighter,
|
|
63
|
+
* and a light or midtone one steps darker. The color decides, not its place on the scale, so a
|
|
64
|
+
* page steps the same way whatever the brand.
|
|
65
|
+
*/
|
|
66
|
+
const stepAlong = (scale, color, offset) => {
|
|
67
|
+
const position = nearestStep(scale, color);
|
|
68
|
+
const lightness = getColorLightnessClassification(color);
|
|
69
|
+
return stepAt(scale, position + (lightness === "dark" || lightness === "veryDark" ? -1 : 1) * offset);
|
|
70
|
+
};
|
|
71
|
+
const stepFrom = (color, offset) => stepAlong(scaleOf(color), color, offset);
|
|
72
|
+
/**
|
|
73
|
+
* `offset` steps from `color` along its scale, away from `text`, so their contrast grows; a negative
|
|
74
|
+
* offset steps towards it. A color with no room left on that side steps the other way, so it still
|
|
75
|
+
* visibly changes.
|
|
76
|
+
*/
|
|
77
|
+
const stepAwayFrom = (color, text, offset) => {
|
|
78
|
+
const scale = scaleOf(color);
|
|
79
|
+
const position = nearestStep(scale, color);
|
|
80
|
+
const away = position + (text.luminance() > color.luminance() ? offset : -offset);
|
|
81
|
+
return stepAt(scale, away >= 0 && away <= lastStep(scale) ? away : position * 2 - away);
|
|
82
|
+
};
|
|
83
|
+
// src/private/engine/anchors.ts
|
|
84
|
+
const isTokenRef = (value) => value.startsWith("$");
|
|
85
|
+
/** A token the Brand Kit leaves out takes its default; a token no Brand Kit has is an error. */
|
|
86
|
+
const tokenValue = (tokens, ref) => {
|
|
87
|
+
const name = ref.slice(1);
|
|
88
|
+
if (!isOneOf(BRAND_TOKEN_NAMES, name)) throw new Error(`${ref} is not a Brand Kit token`);
|
|
89
|
+
return tokens[name] ?? DEFAULT_BRAND_TOKENS[name];
|
|
90
|
+
};
|
|
91
|
+
const tokenColor = (tokens, ref) => new Color(String(tokenValue(tokens, ref)));
|
|
92
|
+
/** Turns a hue around the Lab a/b plane. */
|
|
93
|
+
const rotateHue = (color, degrees) => {
|
|
94
|
+
if (degrees === 0) return color;
|
|
95
|
+
const lab = color.toLab();
|
|
96
|
+
const radians = degrees * Math.PI / 180;
|
|
97
|
+
const cos = Math.cos(radians);
|
|
98
|
+
const sin = Math.sin(radians);
|
|
99
|
+
return Color.fromLab({
|
|
100
|
+
lightness: lab.lightness,
|
|
101
|
+
a: lab.a * cos - lab.b * sin,
|
|
102
|
+
b: lab.a * sin + lab.b * cos
|
|
103
|
+
}, color.a);
|
|
104
|
+
};
|
|
105
|
+
const resolveSingleColor = (value, tokens) => {
|
|
106
|
+
if (typeof value === "string") return isTokenRef(value) ? tokenColor(tokens, value) : new Color(value);
|
|
107
|
+
const related = rotateHue(tokenColor(tokens, value.from), RELATION_HUE_DEGREES[value.relation ?? "same"]);
|
|
108
|
+
if (value.tone === "dark") return getBrandDarkBackgroundColor(related);
|
|
109
|
+
if (value.tone === "light") return getBrandLightBackgroundColor(related);
|
|
110
|
+
return related;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* A toned anchor is a point on its token's scale, so stepping from it should walk that scale.
|
|
114
|
+
* Rebuilding a scale from a pale tone would keep almost none of the brand's hue.
|
|
115
|
+
*/
|
|
116
|
+
const scaleBeforeTone = (value, tokens) => {
|
|
117
|
+
if (value === void 0 || typeof value === "string" || value.tone === void 0) return null;
|
|
118
|
+
return scaleOf(rotateHue(tokenColor(tokens, value.from), RELATION_HUE_DEGREES[value.relation ?? "same"]));
|
|
119
|
+
};
|
|
120
|
+
const fontFrom = (value, tokens) => isTokenRef(value) ? String(tokenValue(tokens, value)) : value;
|
|
121
|
+
const resolveAnchors = (style, tokens) => {
|
|
122
|
+
const { colors, dimensions, fonts } = style.anchors;
|
|
123
|
+
const dimension = (value) => typeof value === "number" ? value : Number(tokenValue(tokens, value));
|
|
124
|
+
return {
|
|
125
|
+
tokens,
|
|
126
|
+
dimension,
|
|
127
|
+
borderRadius: dimension(dimensions.borderRadius),
|
|
128
|
+
colors: {
|
|
129
|
+
pageBackground: resolveSingleColor(colors.pageBackground, tokens),
|
|
130
|
+
primary: resolveSingleColor(colors.primary, tokens),
|
|
131
|
+
ground: colors.ground === void 0 ? null : resolveSingleColor(colors.ground, tokens)
|
|
132
|
+
},
|
|
133
|
+
scales: {
|
|
134
|
+
pageBackground: scaleBeforeTone(colors.pageBackground, tokens),
|
|
135
|
+
primary: scaleBeforeTone(colors.primary, tokens),
|
|
136
|
+
ground: scaleBeforeTone(colors.ground, tokens)
|
|
137
|
+
},
|
|
138
|
+
fonts: {
|
|
139
|
+
"anchors.fonts.heading": fontFrom(fonts.heading, tokens),
|
|
140
|
+
"anchors.fonts.body": fontFrom(fonts.body, tokens),
|
|
141
|
+
"anchors.fonts.button": fontFrom(fonts.button, tokens)
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
const ANCHOR_BY_REF = {
|
|
146
|
+
"anchors.colors.pageBackground": "pageBackground",
|
|
147
|
+
"anchors.colors.primary": "primary",
|
|
148
|
+
"anchors.colors.ground": "ground"
|
|
149
|
+
};
|
|
150
|
+
const anchorName = (ref) => ANCHOR_BY_REF[ref];
|
|
151
|
+
const anchorColor = (ref, anchors) => {
|
|
152
|
+
const color = anchors.colors[ANCHOR_BY_REF[ref]];
|
|
153
|
+
if (color === null) throw new Error(`${ref} is referenced but not declared`);
|
|
154
|
+
return color;
|
|
155
|
+
};
|
|
156
|
+
/** The scale stepping from an anchor walks: its token's if it's toned, or else its own. */
|
|
157
|
+
const anchorScale = (ref, anchors) => anchors.scales[anchorName(ref)] ?? scaleOf(anchorColor(ref, anchors));
|
|
158
|
+
// src/private/engine/rules/border.ts
|
|
159
|
+
/** The page sits on nothing and has no edge, so it carries no border. */
|
|
160
|
+
const BORDER_SLOTS = {
|
|
161
|
+
surfaces: SURFACE_NAMES.filter((name) => name !== "page"),
|
|
162
|
+
initialValues: {
|
|
163
|
+
"border-width": "0",
|
|
164
|
+
"border-style": "none",
|
|
165
|
+
"border-color": "transparent"
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
/** A `width` of 0 keeps the initial values, so the surface has no edge whatever its source. */
|
|
169
|
+
const borderSlots = (border, solid, anchors) => {
|
|
170
|
+
if (border === void 0 || border.width === 0) return {};
|
|
171
|
+
const { offset, source, style, width } = border;
|
|
172
|
+
const from = source === "surface" ? solid : anchorColor(source, anchors);
|
|
173
|
+
return {
|
|
174
|
+
"border-width": `${width}px`,
|
|
175
|
+
"border-style": style,
|
|
176
|
+
"border-color": stepFrom(from, offset).toHex()
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
/** An ornament is a drawn tile rather than a CSS border, and draws whatever the width. */
|
|
180
|
+
const borderGaps = ({ border }) => ORNAMENTAL_BORDER_STYLES.some((ornament) => ornament === border?.style) ? [`border.style: ${border?.style}`] : [];
|
|
181
|
+
// src/private/engine/constants/elevation.ts
|
|
182
|
+
/**
|
|
183
|
+
* The shadow each hardness casts at each depth. A pairing missing here is one the engine does not
|
|
184
|
+
* resolve yet. `soft · low` is from the Phase 1 carousel sandbox.
|
|
185
|
+
*/
|
|
186
|
+
const SHADOWS = { soft: { low: {
|
|
187
|
+
x: 0,
|
|
188
|
+
y: 8,
|
|
189
|
+
blur: 32,
|
|
190
|
+
alpha: .2
|
|
191
|
+
} } };
|
|
192
|
+
// src/private/engine/rules/elevation.ts
|
|
193
|
+
const ELEVATION_SLOTS = {
|
|
194
|
+
surfaces: SURFACE_NAMES,
|
|
195
|
+
initialValues: { "box-shadow": "none" }
|
|
196
|
+
};
|
|
197
|
+
const elevationSlots = ({ depth, hardness }) => {
|
|
198
|
+
const shadow = depth === "none" ? void 0 : SHADOWS[hardness]?.[depth];
|
|
199
|
+
if (shadow === void 0) return {};
|
|
200
|
+
const { alpha, blur, x, y } = shadow;
|
|
201
|
+
return { "box-shadow": `${x}px ${y}px ${blur}px rgba(0, 0, 0, ${alpha})` };
|
|
202
|
+
};
|
|
203
|
+
const elevationGaps = ({ elevation: { depth, hardness, source } }) => {
|
|
204
|
+
if (depth === "none") return [];
|
|
205
|
+
return [...SHADOWS[hardness]?.[depth] === void 0 ? [`elevation: ${hardness} ${depth}`] : [], ...source === "shade" ? [] : [`elevation.source: ${JSON.stringify(source)}`]];
|
|
206
|
+
};
|
|
207
|
+
// src/private/engine/kebab.ts
|
|
208
|
+
/** `buttonPrimary` → `button-primary`, as surface names appear in variable names. */
|
|
209
|
+
const kebab = (name) => name.replaceAll(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
|
|
210
|
+
// src/private/engine/surfaces.ts
|
|
211
|
+
/** What the engine knows about each surface beyond what a Style document says. */
|
|
212
|
+
const SURFACES = {
|
|
213
|
+
page: {
|
|
214
|
+
holdsContent: true,
|
|
215
|
+
interactive: false,
|
|
216
|
+
mayContainForm: false
|
|
217
|
+
},
|
|
218
|
+
panel: {
|
|
219
|
+
holdsContent: true,
|
|
220
|
+
interactive: false,
|
|
221
|
+
mayContainForm: true
|
|
222
|
+
},
|
|
223
|
+
card: {
|
|
224
|
+
holdsContent: true,
|
|
225
|
+
interactive: true,
|
|
226
|
+
mayContainForm: false
|
|
227
|
+
},
|
|
228
|
+
buttonPrimary: {
|
|
229
|
+
holdsContent: false,
|
|
230
|
+
interactive: true,
|
|
231
|
+
mayContainForm: false
|
|
232
|
+
},
|
|
233
|
+
buttonSecondary: {
|
|
234
|
+
holdsContent: false,
|
|
235
|
+
interactive: true,
|
|
236
|
+
mayContainForm: false
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
/** Every surface group, parents before the surfaces that sit on them. */
|
|
240
|
+
const SURFACE_GROUPS = SURFACE_NAMES.flatMap((surface) => {
|
|
241
|
+
const parents = STACKABLE_ON[surface];
|
|
242
|
+
if (parents.length <= 1) return [{
|
|
243
|
+
surface,
|
|
244
|
+
parent: parents[0] ?? null,
|
|
245
|
+
prefix: kebab(surface)
|
|
246
|
+
}];
|
|
247
|
+
return parents.map((parent) => ({
|
|
248
|
+
surface,
|
|
249
|
+
parent,
|
|
250
|
+
prefix: `${kebab(surface)}-on-${kebab(parent)}`
|
|
251
|
+
}));
|
|
252
|
+
});
|
|
253
|
+
// src/private/engine/slots.ts
|
|
254
|
+
const VARIABLE_PREFIX = "--wistia-kaleidoscope-";
|
|
255
|
+
const surfacesWhere = (flag) => SURFACE_NAMES.filter((name) => SURFACES[name][flag]);
|
|
256
|
+
const surfaceVariable = (prefix, slot) => `${VARIABLE_PREFIX}${prefix}-${slot}`;
|
|
257
|
+
const typeVariable = (role, slot) => `${VARIABLE_PREFIX}type-${role}-${slot}`;
|
|
258
|
+
// src/private/engine/treatments/flat.ts
|
|
259
|
+
/**
|
|
260
|
+
* A step away from the text, unless that lands too close to the resting fill or the parent to be
|
|
261
|
+
* seen. Then a step towards it, which relies on the hover text staying readable.
|
|
262
|
+
*/
|
|
263
|
+
const hoverFill = (solid, text, parent) => {
|
|
264
|
+
const away = new Color(stepAwayFrom(solid, text, 1).toHex());
|
|
265
|
+
return away.contrast(solid) >= 1.1 && (parent === null || away.contrast(parent.solid) >= 1.1) ? away : stepAwayFrom(solid, text, -1);
|
|
266
|
+
};
|
|
267
|
+
/** The slots that paint `solid` as a flat fill on `parent`. */
|
|
268
|
+
const flatFill = (solid, scale, parent) => ({
|
|
269
|
+
solid,
|
|
270
|
+
scale,
|
|
271
|
+
fill: { "background-color": solid.toHex() },
|
|
272
|
+
hover: (text) => {
|
|
273
|
+
const solidHover = hoverFill(solid, text, parent);
|
|
274
|
+
return {
|
|
275
|
+
solid: solidHover,
|
|
276
|
+
slots: { "background-color-hover": solidHover.toHex() }
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
/** A single opaque color, stepped along its source's scale. */
|
|
281
|
+
const flat = ({ anchors, parent, settings }) => {
|
|
282
|
+
const { from, offset, source } = settings.backgroundColor;
|
|
283
|
+
const sourceColor = source === "parent" ? parent?.solid ?? anchorColor("anchors.colors.pageBackground", anchors) : anchorColor(source, anchors);
|
|
284
|
+
const scale = source === "parent" ? parent?.scale ?? scaleOf(sourceColor) : anchorScale(source, anchors);
|
|
285
|
+
const start = from === "parent" && parent !== null ? parent.solid : sourceColor;
|
|
286
|
+
let solid = stepAlong(scale, start, offset);
|
|
287
|
+
if (parent !== null && solid.contrast(parent.solid) < 1.1) solid = stepAlong(scale, start, offset + 1);
|
|
288
|
+
return flatFill(solid, scale, parent);
|
|
289
|
+
};
|
|
290
|
+
// src/private/engine/treatments/index.ts
|
|
291
|
+
/** The treatments the engine can paint. A Style using any other is not supported yet. */
|
|
292
|
+
const TREATMENT_FUNCTIONS = { flat };
|
|
293
|
+
// src/private/engine/rules/fill.ts
|
|
294
|
+
/**
|
|
295
|
+
* A surface's fill. `background-image` holds layers drawn over `background-color` — glass sheens,
|
|
296
|
+
* gradients — and `backdrop-filter` blurs what is behind a translucent fill.
|
|
297
|
+
*/
|
|
298
|
+
const FILL_SLOTS = {
|
|
299
|
+
surfaces: SURFACE_NAMES,
|
|
300
|
+
initialValues: {
|
|
301
|
+
"background-color": "transparent",
|
|
302
|
+
"background-image": "none",
|
|
303
|
+
"backdrop-filter": "none"
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
const HOVER_FILL_SLOTS = {
|
|
307
|
+
surfaces: surfacesWhere("interactive"),
|
|
308
|
+
initialValues: {
|
|
309
|
+
"background-color-hover": "transparent",
|
|
310
|
+
"background-image-hover": "none"
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
const fillGaps = ({ treatment }) => Object.hasOwn(TREATMENT_FUNCTIONS, treatment) ? [] : [`treatment: ${treatment}`];
|
|
314
|
+
// src/private/engine/constants/motion.ts
|
|
315
|
+
/** How long a surface takes to move between states. Placeholder. */
|
|
316
|
+
const TRANSITION_DURATIONS = {
|
|
317
|
+
none: null,
|
|
318
|
+
quick: "120ms",
|
|
319
|
+
smooth: "240ms"
|
|
320
|
+
};
|
|
321
|
+
/** The properties a state change animates. */
|
|
322
|
+
const TRANSITIONED_PROPERTIES = [
|
|
323
|
+
"background-color",
|
|
324
|
+
"box-shadow",
|
|
325
|
+
"border-color",
|
|
326
|
+
"color"
|
|
327
|
+
];
|
|
328
|
+
const TRANSITION_EASING = "ease";
|
|
329
|
+
// src/private/engine/rules/motion.ts
|
|
330
|
+
const MOTION_SLOTS = {
|
|
331
|
+
surfaces: SURFACE_NAMES,
|
|
332
|
+
initialValues: {
|
|
333
|
+
transition: "none",
|
|
334
|
+
animation: "none"
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
const motionSlots = ({ transition }) => {
|
|
338
|
+
const duration = TRANSITION_DURATIONS[transition];
|
|
339
|
+
if (duration === null) return {};
|
|
340
|
+
return { transition: TRANSITIONED_PROPERTIES.map((property) => `${property} ${duration} ${TRANSITION_EASING}`).join(", ") };
|
|
341
|
+
};
|
|
342
|
+
/** No ambient motion yet. */
|
|
343
|
+
const motionGaps = ({ motion: { ambient } }) => ambient === "none" ? [] : [`motion.ambient: ${ambient}`];
|
|
344
|
+
// src/private/engine/rules/panelInput.ts
|
|
345
|
+
/** The form fields inside a panel. */
|
|
346
|
+
const PANEL_INPUT_SLOTS = {
|
|
347
|
+
surfaces: surfacesWhere("mayContainForm"),
|
|
348
|
+
initialValues: {
|
|
349
|
+
"input-background-color": "transparent",
|
|
350
|
+
"input-background-color-focus": "transparent",
|
|
351
|
+
"input-border-color-focus": "transparent"
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
/** Inputs step off the panel's fill, and take the primary color when focused. */
|
|
355
|
+
const panelInputSlots = (solid, anchors) => ({
|
|
356
|
+
"input-background-color": stepFrom(solid, 1).toHex(),
|
|
357
|
+
"input-background-color-focus": stepFrom(solid, 2).toHex(),
|
|
358
|
+
"input-border-color-focus": anchorColor("anchors.colors.primary", anchors).toHex()
|
|
359
|
+
});
|
|
360
|
+
// src/private/engine/constants/radius.ts
|
|
361
|
+
/** How the radius anchor scales on each surface. Placeholder. */
|
|
362
|
+
const RADIUS_FACTORS = {
|
|
363
|
+
page: 0,
|
|
364
|
+
panel: 1,
|
|
365
|
+
card: .7,
|
|
366
|
+
buttonPrimary: .5,
|
|
367
|
+
buttonSecondary: .5
|
|
368
|
+
};
|
|
369
|
+
// src/private/engine/rules/radius.ts
|
|
370
|
+
const RADIUS_SLOTS = {
|
|
371
|
+
surfaces: SURFACE_NAMES,
|
|
372
|
+
initialValues: { "border-radius": "0" }
|
|
373
|
+
};
|
|
374
|
+
const radiusSlots = (surface, { source }, anchors) => ({ "border-radius": source === "anchors.dimensions.borderRadius" ? `${Math.round(anchors.borderRadius * RADIUS_FACTORS[surface])}px` : `${anchors.dimension(source)}px` });
|
|
375
|
+
// src/private/engine/rules/text.ts
|
|
376
|
+
const TEXT_SLOTS = {
|
|
377
|
+
surfaces: SURFACE_NAMES,
|
|
378
|
+
initialValues: { "text-color": "canvastext" }
|
|
379
|
+
};
|
|
380
|
+
const CONTENT_TEXT_SLOTS = {
|
|
381
|
+
surfaces: surfacesWhere("holdsContent"),
|
|
382
|
+
initialValues: {
|
|
383
|
+
"heading-text-color": "canvastext",
|
|
384
|
+
"low-emphasis-text-color": "canvastext"
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
const HOVER_TEXT_SLOTS = {
|
|
388
|
+
surfaces: surfacesWhere("interactive"),
|
|
389
|
+
initialValues: { "text-color-hover": "canvastext" }
|
|
390
|
+
};
|
|
391
|
+
const CONTENT_HOVER_TEXT_SLOTS = {
|
|
392
|
+
surfaces: surfacesWhere("interactive").filter((surface) => SURFACES[surface].holdsContent),
|
|
393
|
+
initialValues: {
|
|
394
|
+
"heading-text-color-hover": "canvastext",
|
|
395
|
+
"low-emphasis-text-color-hover": "canvastext"
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
const isPreference = (textColor) => typeof textColor === "object" && "source" in textColor;
|
|
399
|
+
/**
|
|
400
|
+
* `derive` allows text color to be calculated by the engine so we can pick a color with appropriate contrast.
|
|
401
|
+
* A preferred color starts from a reference point (a color anchor) and adjusts it until it meets required contrast rules.
|
|
402
|
+
*/
|
|
403
|
+
const resolveTextColor = (textColor, solid, anchors) => {
|
|
404
|
+
if (!isPreference(textColor)) return getBestColorForText({ backgroundColor: solid });
|
|
405
|
+
const { offset, source } = textColor;
|
|
406
|
+
const preferred = stepAlong(anchorScale(source, anchors), anchorColor(source, anchors), offset);
|
|
407
|
+
return adjustColorForProperContrast({
|
|
408
|
+
foregroundColor: preferred,
|
|
409
|
+
backgroundColor: solid
|
|
410
|
+
});
|
|
411
|
+
};
|
|
412
|
+
const textSlots = (surface, text) => ({
|
|
413
|
+
"text-color": text.toHex(),
|
|
414
|
+
...SURFACES[surface].holdsContent ? {
|
|
415
|
+
"heading-text-color": text.toHex(),
|
|
416
|
+
"low-emphasis-text-color": text.withAlpha(LOW_EMPHASIS_TEXT_ALPHA).toRgba()
|
|
417
|
+
} : {}
|
|
418
|
+
});
|
|
419
|
+
/** The same text slots with a `-hover` suffix, moved only as far as the hovered fill needs. */
|
|
420
|
+
const hoverTextSlots = (surface, text, solidHover) => {
|
|
421
|
+
const readable = adjustColorForProperContrast({
|
|
422
|
+
foregroundColor: text,
|
|
423
|
+
backgroundColor: solidHover
|
|
424
|
+
});
|
|
425
|
+
return Object.fromEntries(Object.entries(textSlots(surface, readable)).map(([slot, value]) => [`${slot}-hover`, value]));
|
|
426
|
+
};
|
|
427
|
+
const textGaps = ({ textColor }) => textColor === "derive" || isPreference(textColor) ? [] : [`textColor: ${JSON.stringify(textColor)}`];
|
|
428
|
+
// src/private/engine/constants/typography.ts
|
|
429
|
+
/** Letter spacing for `case: upper`, which reads cramped at the spacing set for mixed case. */
|
|
430
|
+
const UPPERCASE_LETTER_SPACING = "0.05em";
|
|
431
|
+
// src/private/engine/rules/typography.ts
|
|
432
|
+
/**
|
|
433
|
+
* `font-family` has no fixed initial value — it is up to the browser — so its unused value is the
|
|
434
|
+
* generic family instead.
|
|
435
|
+
*/
|
|
436
|
+
const TYPE_INITIAL_VALUES = {
|
|
437
|
+
"font-family": "sans-serif",
|
|
438
|
+
"font-size": "medium",
|
|
439
|
+
"font-weight": "normal",
|
|
440
|
+
"text-transform": "none",
|
|
441
|
+
"letter-spacing": "normal"
|
|
442
|
+
};
|
|
443
|
+
const TYPOGRAPHY_CONTRACT = Object.fromEntries(TYPE_ROLES.flatMap((role) => Object.entries(TYPE_INITIAL_VALUES).map(([slot, value]) => [typeVariable(role, slot), value])));
|
|
444
|
+
const typographyVariables = ({ typography }, anchors) => Object.fromEntries(TYPE_ROLES.flatMap((role) => {
|
|
445
|
+
const type = typography[role];
|
|
446
|
+
const upper = type.case === "upper";
|
|
447
|
+
return [
|
|
448
|
+
[typeVariable(role, "font-family"), anchors.fonts[type.font]],
|
|
449
|
+
[typeVariable(role, "font-size"), `${type.fontSize}px`],
|
|
450
|
+
[typeVariable(role, "font-weight"), String(type.fontWeight)],
|
|
451
|
+
[typeVariable(role, "text-transform"), upper ? "uppercase" : "none"],
|
|
452
|
+
[typeVariable(role, "letter-spacing"), upper ? UPPERCASE_LETTER_SPACING : "normal"]
|
|
453
|
+
];
|
|
454
|
+
}));
|
|
455
|
+
// src/private/engine/contract.ts
|
|
456
|
+
/** Every rule's surface slots. A rule that adds slots adds its fragment here. */
|
|
457
|
+
const SURFACE_SLOT_FRAGMENTS = [
|
|
458
|
+
FILL_SLOTS,
|
|
459
|
+
HOVER_FILL_SLOTS,
|
|
460
|
+
TEXT_SLOTS,
|
|
461
|
+
CONTENT_TEXT_SLOTS,
|
|
462
|
+
HOVER_TEXT_SLOTS,
|
|
463
|
+
CONTENT_HOVER_TEXT_SLOTS,
|
|
464
|
+
PANEL_INPUT_SLOTS,
|
|
465
|
+
BORDER_SLOTS,
|
|
466
|
+
ELEVATION_SLOTS,
|
|
467
|
+
RADIUS_SLOTS,
|
|
468
|
+
MOTION_SLOTS
|
|
469
|
+
];
|
|
470
|
+
const surfaceContract = () => Object.fromEntries(SURFACE_GROUPS.flatMap(({ prefix, surface }) => SURFACE_SLOT_FRAGMENTS.filter(({ surfaces }) => surfaces.includes(surface)).flatMap(({ initialValues }) => Object.entries(initialValues).map(([slot, value]) => [surfaceVariable(prefix, slot), value]))));
|
|
471
|
+
// src/kaleidoscopeContract.ts
|
|
472
|
+
/**
|
|
473
|
+
* Every variable the engine emits, with the value it takes when a Style doesn't use the rule
|
|
474
|
+
* behind it: the CSS initial value of the property it feeds, or the value that draws nothing.
|
|
475
|
+
* Every Style emits exactly these names, so embeds and hosted pages can rely on each one.
|
|
476
|
+
*
|
|
477
|
+
* Surface variables are `--wistia-kaleidoscope-<surface>-<slot>`. A surface that can sit on several
|
|
478
|
+
* parents is emitted once per parent, as `<surface>-on-<parent>`: `button-primary-on-panel`. Slots
|
|
479
|
+
* are named after the CSS property they feed — `background-color`, `border-radius`, `box-shadow` —
|
|
480
|
+
* with a `-hover` or `-focus` suffix for a state. There are two exceptions: `text-color`,
|
|
481
|
+
* `heading-text-color` and `low-emphasis-text-color` all feed `color`, and a panel's input slots
|
|
482
|
+
* carry an `input-` prefix, as in `input-background-color`.
|
|
483
|
+
*
|
|
484
|
+
* Typography variables are `--wistia-kaleidoscope-type-<role>-<property>`, for the `heading`,
|
|
485
|
+
* `body` and `button` roles.
|
|
486
|
+
*/
|
|
487
|
+
const KALEIDOSCOPE_CONTRACT = {
|
|
488
|
+
...surfaceContract(),
|
|
489
|
+
...TYPOGRAPHY_CONTRACT
|
|
490
|
+
};
|
|
491
|
+
// src/private/engine/rules/invert.ts
|
|
492
|
+
/**
|
|
493
|
+
* `invert: when-indistinct`: if a surface's background fill color is too close to its parent background fill,
|
|
494
|
+
* invert: when-indistinct allows the fill and text color to be inverted. This provides better contrast between the surface
|
|
495
|
+
* and its parent while also preserving legible contrast between the surface and its text color.
|
|
496
|
+
*/
|
|
497
|
+
const invertWhenIndistinct = ({ backgroundColor: { invert }, treatment }, treated, text, parent) => {
|
|
498
|
+
if (invert !== "when-indistinct" || treatment !== "flat" || parent === null) return {
|
|
499
|
+
text,
|
|
500
|
+
treated
|
|
501
|
+
};
|
|
502
|
+
const fillContrast = treated.solid.contrast(parent.solid);
|
|
503
|
+
return fillContrast < INVERT_BELOW_CONTRAST && text.contrast(parent.solid) > fillContrast ? {
|
|
504
|
+
text: treated.solid,
|
|
505
|
+
treated: flatFill(text, null, parent)
|
|
506
|
+
} : {
|
|
507
|
+
text,
|
|
508
|
+
treated
|
|
509
|
+
};
|
|
510
|
+
};
|
|
511
|
+
// src/private/engine/evaluateSurfaces.ts
|
|
512
|
+
/**
|
|
513
|
+
* One surface on one parent. A slot a Style doesn't use is left out, so it keeps the contract's
|
|
514
|
+
* initial value.
|
|
515
|
+
*/
|
|
516
|
+
const evaluateSurface = (style, surface, anchors, parent) => {
|
|
517
|
+
const settings = style.surfaces[surface];
|
|
518
|
+
const treatment = TREATMENT_FUNCTIONS[settings.treatment];
|
|
519
|
+
if (treatment === void 0) throw new Error(`The engine cannot paint a ${settings.treatment} ${surface} yet`);
|
|
520
|
+
const painted = treatment({
|
|
521
|
+
anchors,
|
|
522
|
+
parent,
|
|
523
|
+
settings,
|
|
524
|
+
surface
|
|
525
|
+
});
|
|
526
|
+
const { text, treated } = invertWhenIndistinct(settings, painted, resolveTextColor(settings.textColor, painted.solid, anchors), parent);
|
|
527
|
+
const spec = SURFACES[surface];
|
|
528
|
+
const hover = treated.hover(text);
|
|
529
|
+
return {
|
|
530
|
+
solid: treated.solid,
|
|
531
|
+
scale: treated.scale,
|
|
532
|
+
slots: {
|
|
533
|
+
...treated.fill,
|
|
534
|
+
...spec.interactive ? {
|
|
535
|
+
...hover.slots,
|
|
536
|
+
...hoverTextSlots(surface, text, hover.solid)
|
|
537
|
+
} : {},
|
|
538
|
+
...textSlots(surface, text),
|
|
539
|
+
...spec.mayContainForm ? panelInputSlots(treated.solid, anchors) : {},
|
|
540
|
+
...borderSlots(settings.border, treated.solid, anchors),
|
|
541
|
+
...elevationSlots(settings.elevation),
|
|
542
|
+
...radiusSlots(surface, settings.radius, anchors),
|
|
543
|
+
...motionSlots(settings.motion)
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
};
|
|
547
|
+
/**
|
|
548
|
+
* Evaluates every surface group parents first, so each surface knows the fill it sits on, and
|
|
549
|
+
* names each slot after its group.
|
|
550
|
+
*/
|
|
551
|
+
const evaluateSurfaces = (style, anchors) => {
|
|
552
|
+
const containers = /* @__PURE__ */ new Map();
|
|
553
|
+
const variables = {};
|
|
554
|
+
for (const { parent, prefix, surface } of SURFACE_GROUPS) {
|
|
555
|
+
const evaluated = evaluateSurface(style, surface, anchors, parent === null ? null : containers.get(parent) ?? null);
|
|
556
|
+
containers.set(surface, evaluated);
|
|
557
|
+
for (const [slot, value] of Object.entries(evaluated.slots)) variables[surfaceVariable(prefix, slot)] = value;
|
|
558
|
+
}
|
|
559
|
+
return variables;
|
|
560
|
+
};
|
|
561
|
+
// src/private/engine/rules/texture.ts
|
|
562
|
+
/** No textures yet, so no texture slots: the Style that first uses one adds them. */
|
|
563
|
+
const textureGaps = ({ texture: { kind } }) => kind === "none" ? [] : [`texture.kind: ${kind}`];
|
|
564
|
+
// src/private/engine/support.ts
|
|
565
|
+
const GAP_FINDERS = [
|
|
566
|
+
fillGaps,
|
|
567
|
+
textGaps,
|
|
568
|
+
borderGaps,
|
|
569
|
+
elevationGaps,
|
|
570
|
+
motionGaps,
|
|
571
|
+
textureGaps
|
|
572
|
+
];
|
|
573
|
+
/** The rules in `style` the engine cannot resolve yet, as `<surface>.<setting>: <value>`. */
|
|
574
|
+
const findUnsupportedRules = (style) => SURFACE_NAMES.flatMap((surface) => GAP_FINDERS.flatMap((findGaps) => findGaps(style.surfaces[surface])).map((gap) => `${surface}.${gap}`));
|
|
575
|
+
// src/getKaleidoscopeStyles.ts
|
|
576
|
+
const resolveAgainstDefaultValues = (resolved) => ({
|
|
577
|
+
...KALEIDOSCOPE_CONTRACT,
|
|
578
|
+
...resolved
|
|
579
|
+
});
|
|
580
|
+
/**
|
|
581
|
+
* Resolves a Style against a Brand Kit into the `--wistia-kaleidoscope-*` variables that style an
|
|
582
|
+
* embed or hosted page. The record always holds every name in {@link KALEIDOSCOPE_CONTRACT}.
|
|
583
|
+
*
|
|
584
|
+
* A Brand Kit token the Style references but `brandTokens` leaves out takes its default from
|
|
585
|
+
* `DEFAULT_BRAND_TOKENS`.
|
|
586
|
+
*
|
|
587
|
+
* @throws when the Style uses a rule the engine cannot resolve yet, or references a token that is
|
|
588
|
+
* not a Brand Kit token.
|
|
589
|
+
*
|
|
590
|
+
* @example
|
|
591
|
+
* ```ts
|
|
592
|
+
* const variables = getKaleidoscopeStyles(OPAQUE_MINIMAL, { primaryColor: '#5b4cf0' });
|
|
593
|
+
* variables['--wistia-kaleidoscope-button-primary-on-page-background-color']; // '#5b4cf0'
|
|
594
|
+
* ```
|
|
595
|
+
*/
|
|
596
|
+
const getKaleidoscopeStyles = (style, brandTokens = {}) => {
|
|
597
|
+
const unsupported = findUnsupportedRules(style);
|
|
598
|
+
if (unsupported.length > 0) throw new Error(`The engine cannot resolve "${style.id}" yet: ${unsupported.join(", ")}`);
|
|
599
|
+
const anchors = resolveAnchors(style, brandTokens);
|
|
600
|
+
return resolveAgainstDefaultValues({
|
|
601
|
+
...evaluateSurfaces(style, anchors),
|
|
602
|
+
...typographyVariables(style, anchors)
|
|
603
|
+
});
|
|
604
|
+
};
|
|
605
|
+
// src/serializeKaleidoscopeStyles.ts
|
|
606
|
+
const NAME_SUFFIX = /^[a-z0-9-]+$/;
|
|
607
|
+
/** Ends the declaration or rule, or the `<style>` element, or escapes what follows, or `!important`. */
|
|
608
|
+
const UNSAFE_IN_VALUE = /[{};<\\!\p{Cc}]|\/\*|\*\//u;
|
|
609
|
+
const UNSAFE_IN_SELECTOR = /[{};<\\@\p{Cc}]|\/\*|\*\//u;
|
|
610
|
+
const QUOTED = /"[^"]*"|'[^']*'/g;
|
|
611
|
+
const CLOSER_OF = {
|
|
612
|
+
"(": ")",
|
|
613
|
+
"[": "]"
|
|
614
|
+
};
|
|
615
|
+
const CLOSERS = new Set(Object.values(CLOSER_OF));
|
|
616
|
+
/** An unclosed quote or bracket swallows the declarations and rules after it. */
|
|
617
|
+
const isBalanced = (text) => {
|
|
618
|
+
const unquoted = text.replaceAll(QUOTED, "");
|
|
619
|
+
const expected = [];
|
|
620
|
+
if (/["']/.test(unquoted)) return false;
|
|
621
|
+
for (const char of unquoted) {
|
|
622
|
+
const closer = CLOSER_OF[char];
|
|
623
|
+
if (closer !== void 0) expected.push(closer);
|
|
624
|
+
else if (CLOSERS.has(char) && expected.pop() !== char) return false;
|
|
625
|
+
}
|
|
626
|
+
return expected.length === 0;
|
|
627
|
+
};
|
|
628
|
+
const assertSafe = (label, text, unsafe) => {
|
|
629
|
+
if (text.trim() === "" || unsafe.test(text) || !isBalanced(text)) throw new Error(`Cannot serialize ${label} safely: ${JSON.stringify(text)}`);
|
|
630
|
+
};
|
|
631
|
+
const assertName = (name) => {
|
|
632
|
+
if (!name.startsWith("--wistia-kaleidoscope-") || !NAME_SUFFIX.test(name.slice(22))) throw new Error(`Cannot serialize variable name safely: ${JSON.stringify(name)}`);
|
|
633
|
+
};
|
|
634
|
+
/**
|
|
635
|
+
* Writes the record from `getKaleidoscopeStyles()` as a CSS rule, for a server-rendered `<style>`
|
|
636
|
+
* or for injecting at runtime.
|
|
637
|
+
*
|
|
638
|
+
* Text that could leave its declaration, its rule or the `<style>` element is refused rather than
|
|
639
|
+
* escaped. The engine's own values never need it, so such text is a font family from a Brand Kit
|
|
640
|
+
* that should not reach the page, and an escaped version would no longer name a real font.
|
|
641
|
+
*
|
|
642
|
+
* @throws when `selector`, a variable name or a value is unsafe. Callers rendering customer Brand
|
|
643
|
+
* Kits should catch this and fall back.
|
|
644
|
+
*
|
|
645
|
+
* @example
|
|
646
|
+
* ```ts
|
|
647
|
+
* serializeKaleidoscopeStyles(getKaleidoscopeStyles(OPAQUE_MINIMAL), '.my-scope');
|
|
648
|
+
* // '.my-scope {\n --wistia-kaleidoscope-page-background-color: #ffffff;\n …\n}'
|
|
649
|
+
* ```
|
|
650
|
+
*/
|
|
651
|
+
const serializeKaleidoscopeStyles = (variables, selector) => {
|
|
652
|
+
assertSafe("selector", selector, UNSAFE_IN_SELECTOR);
|
|
653
|
+
return `${selector} {\n${Object.entries(variables).map(([name, value]) => {
|
|
654
|
+
assertName(name);
|
|
655
|
+
assertSafe(`the value of ${name}`, value, UNSAFE_IN_VALUE);
|
|
656
|
+
return ` ${name}: ${value};`;
|
|
657
|
+
}).join("\n")}\n}`;
|
|
658
|
+
};
|
|
659
|
+
export { BRAND_TOKEN_NAMES as a, VARIABLE_PREFIX as i, getKaleidoscopeStyles as n, DEFAULT_BRAND_TOKENS as o, KALEIDOSCOPE_CONTRACT as r, serializeKaleidoscopeStyles as t };
|
|
660
|
+
|
|
661
|
+
//# sourceMappingURL=engine-DeOUPRa0.js.map
|