@usequeek/theme-kit 0.1.31 → 0.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/utils/brand.ts +50 -7
package/package.json
CHANGED
package/utils/brand.ts
CHANGED
|
@@ -227,6 +227,29 @@ function pickReadableTextColor(hex: string): string {
|
|
|
227
227
|
return onWhite >= onBlack ? '#ffffff' : '#000000';
|
|
228
228
|
}
|
|
229
229
|
|
|
230
|
+
/** How far a failing white-on-accent pairing may be deepened before it stops being the vendor's colour: past
|
|
231
|
+
* this the label flips to black instead (a pale yellow keeps its look, with black text). */
|
|
232
|
+
const MAX_ACCENT_DEEPENING = 0.25;
|
|
233
|
+
|
|
234
|
+
/** The smallest deepening of `accent` (by `shade`) at which `onAccent` reads at the AA minimum, or null within the bound. */
|
|
235
|
+
function deepenAccentFor(accent: string, onAccent: string): string | null {
|
|
236
|
+
for (let step = 1; step <= MAX_ACCENT_DEEPENING * 100; step += 1) {
|
|
237
|
+
const candidate = shade(accent, step / 100);
|
|
238
|
+
if ((contrastRatio(candidate, onAccent) ?? 0) >= MIN_ON_ACCENT_CONTRAST) return candidate;
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** `fill` itself, or its deepened form when a lighter `label` fails 4.5:1 on it and a bounded deepening fixes that. */
|
|
244
|
+
function readableFill(fill: string, label: string): string {
|
|
245
|
+
// A legacy colour set can leave the primary unset: nothing to deepen.
|
|
246
|
+
if (!fill || !label) return fill;
|
|
247
|
+
const ratio = contrastRatio(fill, label);
|
|
248
|
+
if (ratio === null || ratio >= MIN_ON_ACCENT_CONTRAST) return fill;
|
|
249
|
+
if ((relativeLuminance(label) ?? 0) <= (relativeLuminance(fill) ?? 0)) return fill;
|
|
250
|
+
return deepenAccentFor(fill, label) ?? fill;
|
|
251
|
+
}
|
|
252
|
+
|
|
230
253
|
/**
|
|
231
254
|
* Guards against `on_accent` silently pairing with an `accent` it was never
|
|
232
255
|
* designed for. `accent` and `on_accent` are independent fields (see
|
|
@@ -236,12 +259,23 @@ function pickReadableTextColor(hex: string): string {
|
|
|
236
259
|
* shape at all, so `on_accent` falls through to the THEME's own default,
|
|
237
260
|
* tuned for the theme's own accent color, not the vendor's. Only corrects
|
|
238
261
|
* when the pairing actually fails contrast; a theme's own (already-tuned)
|
|
239
|
-
* accent/on_accent pair is never touched.
|
|
240
|
-
|
|
262
|
+
* accent/on_accent pair is never touched.
|
|
263
|
+
*
|
|
264
|
+
* A light label on a mid-tone accent (the default theme's white on #229879) keeps its label: the FILL a button
|
|
265
|
+
* paints under it is deepened just enough to read, and returned. `--brand-accent` itself is never changed, so
|
|
266
|
+
* links, rings and tints keep the vendor's colour. Otherwise the label flips to black or white, as before.
|
|
267
|
+
* Returns the fill (the accent itself when nothing was deepened; only a deepened fill is emitted as `--brand-accent-fill`). */
|
|
268
|
+
function ensureReadableOnAccent(color: DesignTokenColor & { accent: string; on_accent: string }, deepen = true): string {
|
|
241
269
|
const ratio = contrastRatio(color.accent, color.on_accent);
|
|
242
|
-
if (ratio
|
|
243
|
-
|
|
270
|
+
if (ratio === null || ratio >= MIN_ON_ACCENT_CONTRAST) return color.accent;
|
|
271
|
+
|
|
272
|
+
if (deepen && (relativeLuminance(color.on_accent) ?? 0) > (relativeLuminance(color.accent) ?? 0)) {
|
|
273
|
+
const deepened = deepenAccentFor(color.accent, color.on_accent);
|
|
274
|
+
if (deepened !== null) return deepened;
|
|
244
275
|
}
|
|
276
|
+
|
|
277
|
+
color.on_accent = pickReadableTextColor(color.accent);
|
|
278
|
+
return color.accent;
|
|
245
279
|
}
|
|
246
280
|
|
|
247
281
|
function px(value: number): string {
|
|
@@ -368,9 +402,12 @@ function emitImageVars(
|
|
|
368
402
|
* entire groups (or being `null`/`undefined` altogether) still produces
|
|
369
403
|
* every var with a sane value — never throws, never omits a role.
|
|
370
404
|
*/
|
|
371
|
-
export function expandDesignTokens(tokens?: DesignTokens | null): Record<string, string> {
|
|
405
|
+
export function expandDesignTokens(tokens?: DesignTokens | null, { explicitLabel = false }: { explicitLabel?: boolean } = {}): Record<string, string> {
|
|
372
406
|
const color = { ...DEFAULT_COLOR, ...tokens?.color };
|
|
373
|
-
|
|
407
|
+
// A label the merchant chose is theirs: no fill is deepened under it (the derived label flip still applies to --brand-on-accent).
|
|
408
|
+
const accentFill = ensureReadableOnAccent(color, !explicitLabel);
|
|
409
|
+
// --brand-button-bg / --brand-button-text are one pair too (the bg is the primary): the same rule keeps that pair readable.
|
|
410
|
+
const buttonBg = explicitLabel ? color.primary : readableFill(color.primary, color.on_accent);
|
|
374
411
|
const type = { ...DEFAULT_TYPE, ...tokens?.type };
|
|
375
412
|
const space = { ...DEFAULT_SPACE, ...tokens?.space };
|
|
376
413
|
const shape = { ...DEFAULT_SHAPE, ...tokens?.shape };
|
|
@@ -382,6 +419,12 @@ export function expandDesignTokens(tokens?: DesignTokens | null): Record<string,
|
|
|
382
419
|
|
|
383
420
|
// COLOR — every role, fixing the missing-12 bug.
|
|
384
421
|
emitColorVars(color, vars);
|
|
422
|
+
// Only when the fill had to deepen: every other store's markup is byte-identical, and the CSS falls back to --brand-accent.
|
|
423
|
+
if (accentFill !== color.accent) vars['--brand-accent-fill'] = accentFill;
|
|
424
|
+
if (buttonBg !== color.primary) {
|
|
425
|
+
vars['--brand-button-bg'] = buttonBg;
|
|
426
|
+
vars['--brand-button-hover-bg'] = shade(buttonBg, 0.08);
|
|
427
|
+
}
|
|
385
428
|
|
|
386
429
|
// TYPE
|
|
387
430
|
emitTypeVars(type, vars);
|
|
@@ -503,7 +546,7 @@ export function getBrandCssVariables(
|
|
|
503
546
|
config?: StorefrontConfig | null,
|
|
504
547
|
): CSSProperties {
|
|
505
548
|
const tokens = resolveEffectiveTokens(brand, config);
|
|
506
|
-
const vars = expandDesignTokens(tokens);
|
|
549
|
+
const vars = expandDesignTokens(tokens, { explicitLabel: !!config?.colors?.button_text });
|
|
507
550
|
|
|
508
551
|
if (config?.footer?.bg) vars['--brand-footer-bg'] = config.footer.bg;
|
|
509
552
|
if (config?.footer?.text) vars['--brand-footer-text'] = config.footer.text;
|