@dloizides/theme-web 1.4.0 → 1.6.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/CHANGELOG.md +8 -0
- package/README.md +4 -0
- package/dist/index.d.mts +46 -1
- package/dist/index.d.ts +46 -1
- package/dist/index.js +171 -63
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +170 -64
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.6.0
|
|
4
|
+
|
|
5
|
+
FINREG-BRAND-1 "Logo without its background and one palette from the logo": optional `ResolvedTheme.semanticText` holds error, warning, success and info text colours per mode, each readable on that mode's surfaces. Existing `semantic` fills are unchanged.
|
|
6
|
+
|
|
7
|
+
## 1.5.0
|
|
8
|
+
|
|
9
|
+
INVOICE-5 "Edit invoices before sending, shared background and menu hover": app background presets. `TenantThemeConfig.backdrop` takes one `BackdropPreset` (`plain` default, `paper`, `ledger`, `horizon`, `tint`); `resolveTheme` puts the result on `theme.backdrop`, and `resolveBackdrop(theme, preset)` resolves one directly. Every colour comes from the theme; page and band inks clear 4.5:1 plus the 0.25 margin. No preset set = today's flat background.
|
|
10
|
+
|
|
3
11
|
## 1.4.0
|
|
4
12
|
|
|
5
13
|
INVOICE-3 "Invoice interface over a user-chosen folder": `ThemeProvider` gains `forcedMode`, which holds one mode whatever the OS or a stored preference says.
|
package/README.md
CHANGED
|
@@ -75,6 +75,10 @@ badgeColors(theme.semantic.info)
|
|
|
75
75
|
plus `AA_CONTRAST_MARGIN`, so dark seeds keep a rich `600` and pale seeds fall through to `900`.
|
|
76
76
|
The result is mode-independent (semantic scales are seeded from the config alone).
|
|
77
77
|
|
|
78
|
+
## App background — `backdrop`
|
|
79
|
+
|
|
80
|
+
Set one value: `{ ...config, backdrop: BackdropPreset.Horizon }`. `theme.backdrop` then holds `color`, an optional web CSS `image` + `imageSize`, `text` / `textSecondary` inks that clear AA on `color`, a `band` (horizon only: colour, inks, `overlap` of 56 px compact / 64 px wide) and `liftSurfaces` (cards get a shadow). Presets: `plain` (default, unchanged), `paper` (surface + border grain), `ledger` (divider rules every 32 px), `horizon` (brand band behind the header), `tint` (lightest brand shade, darkest in dark mode).
|
|
81
|
+
|
|
78
82
|
## Asserting your own palette
|
|
79
83
|
|
|
80
84
|
`contrastRatio`, `meetsContrast`, `relativeLuminance`, the `AA_*` thresholds and
|
package/dist/index.d.mts
CHANGED
|
@@ -13,6 +13,16 @@ declare const ThemeMode: {
|
|
|
13
13
|
};
|
|
14
14
|
type ThemeMode = (typeof ThemeMode)[keyof typeof ThemeMode];
|
|
15
15
|
|
|
16
|
+
/** App background preset, chosen by one config value. `Plain` keeps today's flat background. */
|
|
17
|
+
declare const BackdropPreset: {
|
|
18
|
+
readonly Plain: "plain";
|
|
19
|
+
readonly Paper: "paper";
|
|
20
|
+
readonly Ledger: "ledger";
|
|
21
|
+
readonly Horizon: "horizon";
|
|
22
|
+
readonly Tint: "tint";
|
|
23
|
+
};
|
|
24
|
+
type BackdropPreset = (typeof BackdropPreset)[keyof typeof BackdropPreset];
|
|
25
|
+
|
|
16
26
|
/**
|
|
17
27
|
* Theme type system — promoted verbatim (shapes unchanged) from the duplicated
|
|
18
28
|
* erevna-web / katalogos-web `src/theme/types`, so a migrated app's
|
|
@@ -82,6 +92,8 @@ interface TenantThemeConfig {
|
|
|
82
92
|
dark: ThemeModeColors;
|
|
83
93
|
typography?: TypographyConfig;
|
|
84
94
|
branding: BrandingConfig;
|
|
95
|
+
/** App background preset; omitted = `plain`, today's flat background. */
|
|
96
|
+
backdrop?: BackdropPreset;
|
|
85
97
|
}
|
|
86
98
|
/** Full shade palettes for brand colors. Type alias — see {@link ColorScale}. */
|
|
87
99
|
type ResolvedPalette = {
|
|
@@ -96,6 +108,12 @@ type ResolvedSemanticColors = {
|
|
|
96
108
|
error: ColorScale;
|
|
97
109
|
info: ColorScale;
|
|
98
110
|
};
|
|
111
|
+
interface ResolvedSemanticText {
|
|
112
|
+
success: string;
|
|
113
|
+
warning: string;
|
|
114
|
+
error: string;
|
|
115
|
+
info: string;
|
|
116
|
+
}
|
|
99
117
|
/** Resolved typography configuration. */
|
|
100
118
|
interface ResolvedTypography {
|
|
101
119
|
fontFamily: string;
|
|
@@ -111,9 +129,33 @@ interface ResolvedTheme {
|
|
|
111
129
|
colors: ThemeModeColors;
|
|
112
130
|
palette: ResolvedPalette;
|
|
113
131
|
semantic: ResolvedSemanticColors;
|
|
132
|
+
semanticText?: ResolvedSemanticText;
|
|
114
133
|
typography: ResolvedTypography;
|
|
115
134
|
mode: ThemeMode;
|
|
116
135
|
branding: ResolvedBranding;
|
|
136
|
+
/** Resolved app background; set by `resolveTheme`. */
|
|
137
|
+
backdrop?: ResolvedBackdrop;
|
|
138
|
+
}
|
|
139
|
+
/** Brand band of the `horizon` backdrop, with how far content overlaps its lower edge. */
|
|
140
|
+
interface ResolvedBackdropBand {
|
|
141
|
+
color: string;
|
|
142
|
+
text: string;
|
|
143
|
+
textSecondary: string;
|
|
144
|
+
overlap: {
|
|
145
|
+
compact: number;
|
|
146
|
+
wide: number;
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/** App background: base colour, optional web CSS image, inks that clear AA on it. */
|
|
150
|
+
interface ResolvedBackdrop {
|
|
151
|
+
preset: BackdropPreset;
|
|
152
|
+
color: string;
|
|
153
|
+
image: string | null;
|
|
154
|
+
imageSize: string | null;
|
|
155
|
+
text: string;
|
|
156
|
+
textSecondary: string;
|
|
157
|
+
band: ResolvedBackdropBand | null;
|
|
158
|
+
liftSurfaces: boolean;
|
|
117
159
|
}
|
|
118
160
|
|
|
119
161
|
/**
|
|
@@ -212,6 +254,9 @@ declare const DEFAULT_THEME_CONFIG: TenantThemeConfig;
|
|
|
212
254
|
/** Project a design-token set onto a `TenantThemeConfig`. */
|
|
213
255
|
declare function fromDesignTokens(tokens: DesignTokens): TenantThemeConfig;
|
|
214
256
|
|
|
257
|
+
/** Resolve a backdrop preset to theme-derived fills whose text inks clear WCAG AA. */
|
|
258
|
+
declare function resolveBackdrop(theme: ResolvedTheme, preset?: BackdropPreset): ResolvedBackdrop;
|
|
259
|
+
|
|
215
260
|
/**
|
|
216
261
|
* Palette Generator — promoted verbatim (algorithm + constants unchanged) from
|
|
217
262
|
* the duplicated erevna-web / katalogos-web `theme/utils/palette-generator.ts`.
|
|
@@ -333,4 +378,4 @@ declare function contrastRatio(foreground: string, background: string): number;
|
|
|
333
378
|
/** True when `foreground` on `background` meets `requiredRatio`. */
|
|
334
379
|
declare function meetsContrast(foreground: string, background: string, requiredRatio: number): boolean;
|
|
335
380
|
|
|
336
|
-
export { AA_CONTRAST_MARGIN, AA_LARGE_TEXT_RATIO, AA_NON_TEXT_RATIO, AA_NORMAL_TEXT_RATIO, BADGE_TINT_SHADE, type BadgeColors, type BrandingConfig, type ColorScale, DEFAULT_DARK_MODE_STORAGE_KEY, DEFAULT_THEME_CONFIG, DarkModePreference, type ResolvedBranding, type ResolvedPalette, type ResolvedSemanticColors, type ResolvedTheme, type ResolvedTypography, type SemanticColorOverrides, type TenantThemeConfig, ThemeContext, type ThemeContextValue, ThemeMode, type ThemeModeColors, ThemeProvider, type ThemeProviderProps, type TypographyConfig, type UseDarkModeReturn, badgeColors, contrastRatio, darken, fromDesignTokens, generateColorScale, generateThemePalette, hexToHsl, hslToHex, isValidHex, lighten, meetsContrast, relativeLuminance, resolveTheme, useDarkMode, useTheme };
|
|
381
|
+
export { AA_CONTRAST_MARGIN, AA_LARGE_TEXT_RATIO, AA_NON_TEXT_RATIO, AA_NORMAL_TEXT_RATIO, BADGE_TINT_SHADE, BackdropPreset, type BadgeColors, type BrandingConfig, type ColorScale, DEFAULT_DARK_MODE_STORAGE_KEY, DEFAULT_THEME_CONFIG, DarkModePreference, type ResolvedBackdrop, type ResolvedBackdropBand, type ResolvedBranding, type ResolvedPalette, type ResolvedSemanticColors, type ResolvedSemanticText, type ResolvedTheme, type ResolvedTypography, type SemanticColorOverrides, type TenantThemeConfig, ThemeContext, type ThemeContextValue, ThemeMode, type ThemeModeColors, ThemeProvider, type ThemeProviderProps, type TypographyConfig, type UseDarkModeReturn, badgeColors, contrastRatio, darken, fromDesignTokens, generateColorScale, generateThemePalette, hexToHsl, hslToHex, isValidHex, lighten, meetsContrast, relativeLuminance, resolveBackdrop, resolveTheme, useDarkMode, useTheme };
|
package/dist/index.d.ts
CHANGED
|
@@ -13,6 +13,16 @@ declare const ThemeMode: {
|
|
|
13
13
|
};
|
|
14
14
|
type ThemeMode = (typeof ThemeMode)[keyof typeof ThemeMode];
|
|
15
15
|
|
|
16
|
+
/** App background preset, chosen by one config value. `Plain` keeps today's flat background. */
|
|
17
|
+
declare const BackdropPreset: {
|
|
18
|
+
readonly Plain: "plain";
|
|
19
|
+
readonly Paper: "paper";
|
|
20
|
+
readonly Ledger: "ledger";
|
|
21
|
+
readonly Horizon: "horizon";
|
|
22
|
+
readonly Tint: "tint";
|
|
23
|
+
};
|
|
24
|
+
type BackdropPreset = (typeof BackdropPreset)[keyof typeof BackdropPreset];
|
|
25
|
+
|
|
16
26
|
/**
|
|
17
27
|
* Theme type system — promoted verbatim (shapes unchanged) from the duplicated
|
|
18
28
|
* erevna-web / katalogos-web `src/theme/types`, so a migrated app's
|
|
@@ -82,6 +92,8 @@ interface TenantThemeConfig {
|
|
|
82
92
|
dark: ThemeModeColors;
|
|
83
93
|
typography?: TypographyConfig;
|
|
84
94
|
branding: BrandingConfig;
|
|
95
|
+
/** App background preset; omitted = `plain`, today's flat background. */
|
|
96
|
+
backdrop?: BackdropPreset;
|
|
85
97
|
}
|
|
86
98
|
/** Full shade palettes for brand colors. Type alias — see {@link ColorScale}. */
|
|
87
99
|
type ResolvedPalette = {
|
|
@@ -96,6 +108,12 @@ type ResolvedSemanticColors = {
|
|
|
96
108
|
error: ColorScale;
|
|
97
109
|
info: ColorScale;
|
|
98
110
|
};
|
|
111
|
+
interface ResolvedSemanticText {
|
|
112
|
+
success: string;
|
|
113
|
+
warning: string;
|
|
114
|
+
error: string;
|
|
115
|
+
info: string;
|
|
116
|
+
}
|
|
99
117
|
/** Resolved typography configuration. */
|
|
100
118
|
interface ResolvedTypography {
|
|
101
119
|
fontFamily: string;
|
|
@@ -111,9 +129,33 @@ interface ResolvedTheme {
|
|
|
111
129
|
colors: ThemeModeColors;
|
|
112
130
|
palette: ResolvedPalette;
|
|
113
131
|
semantic: ResolvedSemanticColors;
|
|
132
|
+
semanticText?: ResolvedSemanticText;
|
|
114
133
|
typography: ResolvedTypography;
|
|
115
134
|
mode: ThemeMode;
|
|
116
135
|
branding: ResolvedBranding;
|
|
136
|
+
/** Resolved app background; set by `resolveTheme`. */
|
|
137
|
+
backdrop?: ResolvedBackdrop;
|
|
138
|
+
}
|
|
139
|
+
/** Brand band of the `horizon` backdrop, with how far content overlaps its lower edge. */
|
|
140
|
+
interface ResolvedBackdropBand {
|
|
141
|
+
color: string;
|
|
142
|
+
text: string;
|
|
143
|
+
textSecondary: string;
|
|
144
|
+
overlap: {
|
|
145
|
+
compact: number;
|
|
146
|
+
wide: number;
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/** App background: base colour, optional web CSS image, inks that clear AA on it. */
|
|
150
|
+
interface ResolvedBackdrop {
|
|
151
|
+
preset: BackdropPreset;
|
|
152
|
+
color: string;
|
|
153
|
+
image: string | null;
|
|
154
|
+
imageSize: string | null;
|
|
155
|
+
text: string;
|
|
156
|
+
textSecondary: string;
|
|
157
|
+
band: ResolvedBackdropBand | null;
|
|
158
|
+
liftSurfaces: boolean;
|
|
117
159
|
}
|
|
118
160
|
|
|
119
161
|
/**
|
|
@@ -212,6 +254,9 @@ declare const DEFAULT_THEME_CONFIG: TenantThemeConfig;
|
|
|
212
254
|
/** Project a design-token set onto a `TenantThemeConfig`. */
|
|
213
255
|
declare function fromDesignTokens(tokens: DesignTokens): TenantThemeConfig;
|
|
214
256
|
|
|
257
|
+
/** Resolve a backdrop preset to theme-derived fills whose text inks clear WCAG AA. */
|
|
258
|
+
declare function resolveBackdrop(theme: ResolvedTheme, preset?: BackdropPreset): ResolvedBackdrop;
|
|
259
|
+
|
|
215
260
|
/**
|
|
216
261
|
* Palette Generator — promoted verbatim (algorithm + constants unchanged) from
|
|
217
262
|
* the duplicated erevna-web / katalogos-web `theme/utils/palette-generator.ts`.
|
|
@@ -333,4 +378,4 @@ declare function contrastRatio(foreground: string, background: string): number;
|
|
|
333
378
|
/** True when `foreground` on `background` meets `requiredRatio`. */
|
|
334
379
|
declare function meetsContrast(foreground: string, background: string, requiredRatio: number): boolean;
|
|
335
380
|
|
|
336
|
-
export { AA_CONTRAST_MARGIN, AA_LARGE_TEXT_RATIO, AA_NON_TEXT_RATIO, AA_NORMAL_TEXT_RATIO, BADGE_TINT_SHADE, type BadgeColors, type BrandingConfig, type ColorScale, DEFAULT_DARK_MODE_STORAGE_KEY, DEFAULT_THEME_CONFIG, DarkModePreference, type ResolvedBranding, type ResolvedPalette, type ResolvedSemanticColors, type ResolvedTheme, type ResolvedTypography, type SemanticColorOverrides, type TenantThemeConfig, ThemeContext, type ThemeContextValue, ThemeMode, type ThemeModeColors, ThemeProvider, type ThemeProviderProps, type TypographyConfig, type UseDarkModeReturn, badgeColors, contrastRatio, darken, fromDesignTokens, generateColorScale, generateThemePalette, hexToHsl, hslToHex, isValidHex, lighten, meetsContrast, relativeLuminance, resolveTheme, useDarkMode, useTheme };
|
|
381
|
+
export { AA_CONTRAST_MARGIN, AA_LARGE_TEXT_RATIO, AA_NON_TEXT_RATIO, AA_NORMAL_TEXT_RATIO, BADGE_TINT_SHADE, BackdropPreset, type BadgeColors, type BrandingConfig, type ColorScale, DEFAULT_DARK_MODE_STORAGE_KEY, DEFAULT_THEME_CONFIG, DarkModePreference, type ResolvedBackdrop, type ResolvedBackdropBand, type ResolvedBranding, type ResolvedPalette, type ResolvedSemanticColors, type ResolvedSemanticText, type ResolvedTheme, type ResolvedTypography, type SemanticColorOverrides, type TenantThemeConfig, ThemeContext, type ThemeContextValue, ThemeMode, type ThemeModeColors, ThemeProvider, type ThemeProviderProps, type TypographyConfig, type UseDarkModeReturn, badgeColors, contrastRatio, darken, fromDesignTokens, generateColorScale, generateThemePalette, hexToHsl, hslToHex, isValidHex, lighten, meetsContrast, relativeLuminance, resolveBackdrop, resolveTheme, useDarkMode, useTheme };
|
package/dist/index.js
CHANGED
|
@@ -209,13 +209,179 @@ function generateThemePalette(config) {
|
|
|
209
209
|
};
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
+
// src/BackdropPreset.ts
|
|
213
|
+
var BackdropPreset = {
|
|
214
|
+
Plain: "plain",
|
|
215
|
+
Paper: "paper",
|
|
216
|
+
Ledger: "ledger",
|
|
217
|
+
Horizon: "horizon",
|
|
218
|
+
Tint: "tint"
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// src/contrast.ts
|
|
222
|
+
var AA_NORMAL_TEXT_RATIO = 4.5;
|
|
223
|
+
var AA_LARGE_TEXT_RATIO = 3;
|
|
224
|
+
var AA_NON_TEXT_RATIO = 3;
|
|
225
|
+
var AA_CONTRAST_MARGIN = 0.25;
|
|
226
|
+
var SRGB_CHANNEL_MAX = 255;
|
|
227
|
+
var GAMMA_LINEAR_THRESHOLD = 0.03928;
|
|
228
|
+
var GAMMA_LINEAR_DIVISOR = 12.92;
|
|
229
|
+
var GAMMA_OFFSET = 0.055;
|
|
230
|
+
var GAMMA_DIVISOR = 1.055;
|
|
231
|
+
var GAMMA_EXPONENT = 2.4;
|
|
232
|
+
var LUMA_COEFFICIENT_RED = 0.2126;
|
|
233
|
+
var LUMA_COEFFICIENT_GREEN = 0.7152;
|
|
234
|
+
var LUMA_COEFFICIENT_BLUE = 0.0722;
|
|
235
|
+
var CONTRAST_FLARE = 0.05;
|
|
236
|
+
var HEX_RADIX = 16;
|
|
237
|
+
var HEX_PAIR_LENGTH = 2;
|
|
238
|
+
var HEX_SHORTHAND_LENGTH = 3;
|
|
239
|
+
var HEX_FULL_LENGTH = 6;
|
|
240
|
+
var CHANNEL_OFFSET_RED = 0;
|
|
241
|
+
var CHANNEL_OFFSET_GREEN = 2;
|
|
242
|
+
var CHANNEL_OFFSET_BLUE = 4;
|
|
243
|
+
function normalizeHexDigits(hex) {
|
|
244
|
+
const clean = hex.trim().replace("#", "").toLowerCase();
|
|
245
|
+
if (clean.length === HEX_SHORTHAND_LENGTH)
|
|
246
|
+
return `${clean[0] ?? ""}${clean[0] ?? ""}${clean[1] ?? ""}${clean[1] ?? ""}${clean[2] ?? ""}${clean[2] ?? ""}`;
|
|
247
|
+
return clean;
|
|
248
|
+
}
|
|
249
|
+
function parseChannel(digits, offset) {
|
|
250
|
+
const pair = digits.slice(offset, offset + HEX_PAIR_LENGTH);
|
|
251
|
+
return parseInt(pair, HEX_RADIX) / SRGB_CHANNEL_MAX;
|
|
252
|
+
}
|
|
253
|
+
function linearizeChannel(channel) {
|
|
254
|
+
if (channel <= GAMMA_LINEAR_THRESHOLD) return channel / GAMMA_LINEAR_DIVISOR;
|
|
255
|
+
return Math.pow((channel + GAMMA_OFFSET) / GAMMA_DIVISOR, GAMMA_EXPONENT);
|
|
256
|
+
}
|
|
257
|
+
function relativeLuminance(hex) {
|
|
258
|
+
const digits = normalizeHexDigits(hex);
|
|
259
|
+
if (digits.length !== HEX_FULL_LENGTH || !/^[0-9a-f]{6}$/.test(digits))
|
|
260
|
+
throw new Error(`relativeLuminance: not a hex colour: ${hex}`);
|
|
261
|
+
const red = linearizeChannel(parseChannel(digits, CHANNEL_OFFSET_RED));
|
|
262
|
+
const green = linearizeChannel(parseChannel(digits, CHANNEL_OFFSET_GREEN));
|
|
263
|
+
const blue = linearizeChannel(parseChannel(digits, CHANNEL_OFFSET_BLUE));
|
|
264
|
+
return LUMA_COEFFICIENT_RED * red + LUMA_COEFFICIENT_GREEN * green + LUMA_COEFFICIENT_BLUE * blue;
|
|
265
|
+
}
|
|
266
|
+
function contrastRatio(foreground, background) {
|
|
267
|
+
const first = relativeLuminance(foreground);
|
|
268
|
+
const second = relativeLuminance(background);
|
|
269
|
+
const lighter = Math.max(first, second);
|
|
270
|
+
const darker = Math.min(first, second);
|
|
271
|
+
return (lighter + CONTRAST_FLARE) / (darker + CONTRAST_FLARE);
|
|
272
|
+
}
|
|
273
|
+
function meetsContrast(foreground, background, requiredRatio) {
|
|
274
|
+
return contrastRatio(foreground, background) >= requiredRatio;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// src/ThemeMode.ts
|
|
278
|
+
var ThemeMode = {
|
|
279
|
+
Light: "light",
|
|
280
|
+
Dark: "dark"
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
// src/resolveBackdrop.ts
|
|
284
|
+
var SAFE_TEXT_RATIO = AA_NORMAL_TEXT_RATIO + AA_CONTRAST_MARGIN;
|
|
285
|
+
var LEDGER_RULE_SPACING_PX = 32;
|
|
286
|
+
var LEDGER_RULE_WIDTH_PX = 1;
|
|
287
|
+
var PAPER_GRAIN_SIZE_PX = 6;
|
|
288
|
+
var BAND_OVERLAP_COMPACT_PX = 56;
|
|
289
|
+
var BAND_OVERLAP_WIDE_PX = 64;
|
|
290
|
+
function readable(candidates, background, fallback) {
|
|
291
|
+
return candidates.find((ink) => contrastRatio(ink, background) >= SAFE_TEXT_RATIO) ?? fallback;
|
|
292
|
+
}
|
|
293
|
+
function strongest(candidates, background) {
|
|
294
|
+
const ranked = [...candidates].sort((a, b) => contrastRatio(b, background) - contrastRatio(a, background));
|
|
295
|
+
return ranked[0] ?? background;
|
|
296
|
+
}
|
|
297
|
+
function ledgerFill(theme) {
|
|
298
|
+
const ruleStart = LEDGER_RULE_SPACING_PX - LEDGER_RULE_WIDTH_PX;
|
|
299
|
+
const rule = theme.colors.divider;
|
|
300
|
+
return {
|
|
301
|
+
color: theme.colors.background,
|
|
302
|
+
image: `linear-gradient(to bottom, transparent ${ruleStart}px, ${rule} ${ruleStart}px)`,
|
|
303
|
+
imageSize: `100% ${LEDGER_RULE_SPACING_PX}px`
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
function paperFill(theme) {
|
|
307
|
+
return {
|
|
308
|
+
color: theme.colors.surface,
|
|
309
|
+
image: `radial-gradient(${theme.colors.border} 0.5px, transparent 0.5px)`,
|
|
310
|
+
imageSize: `${PAPER_GRAIN_SIZE_PX}px ${PAPER_GRAIN_SIZE_PX}px`
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
function flatFill(color) {
|
|
314
|
+
return { color, image: null, imageSize: null };
|
|
315
|
+
}
|
|
316
|
+
function tintShade(theme) {
|
|
317
|
+
return theme.mode === ThemeMode.Dark ? theme.palette.primary["900"] : theme.palette.primary["50"];
|
|
318
|
+
}
|
|
319
|
+
function fillFor(theme, preset) {
|
|
320
|
+
if (preset === BackdropPreset.Ledger) return ledgerFill(theme);
|
|
321
|
+
if (preset === BackdropPreset.Paper) return paperFill(theme);
|
|
322
|
+
if (preset === BackdropPreset.Horizon) return flatFill(theme.colors.surface);
|
|
323
|
+
if (preset === BackdropPreset.Tint) return flatFill(tintShade(theme));
|
|
324
|
+
return flatFill(theme.colors.background);
|
|
325
|
+
}
|
|
326
|
+
function bandFor(theme) {
|
|
327
|
+
const color = theme.palette.primary["500"];
|
|
328
|
+
const { primary } = theme.palette;
|
|
329
|
+
const text = strongest([theme.colors.surfaceElevated, theme.colors.background, theme.colors.text, primary["50"]], color);
|
|
330
|
+
return {
|
|
331
|
+
color,
|
|
332
|
+
text,
|
|
333
|
+
textSecondary: readable([primary["100"], primary["200"], primary["800"], primary["900"]], color, text),
|
|
334
|
+
overlap: { compact: BAND_OVERLAP_COMPACT_PX, wide: BAND_OVERLAP_WIDE_PX }
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function safeFill(theme, preset) {
|
|
338
|
+
const fill = fillFor(theme, preset);
|
|
339
|
+
const textFits = contrastRatio(theme.colors.text, fill.color) >= SAFE_TEXT_RATIO;
|
|
340
|
+
return textFits ? fill : flatFill(theme.colors.background);
|
|
341
|
+
}
|
|
342
|
+
function resolveBackdrop(theme, preset = BackdropPreset.Plain) {
|
|
343
|
+
const fill = safeFill(theme, preset);
|
|
344
|
+
return {
|
|
345
|
+
preset,
|
|
346
|
+
...fill,
|
|
347
|
+
text: theme.colors.text,
|
|
348
|
+
textSecondary: readable([theme.colors.textSecondary], fill.color, theme.colors.text),
|
|
349
|
+
band: preset === BackdropPreset.Horizon ? bandFor(theme) : null,
|
|
350
|
+
liftSurfaces: preset === BackdropPreset.Tint || preset === BackdropPreset.Horizon
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// src/semanticText.ts
|
|
355
|
+
var REQUIRED_TEXT_RATIO = AA_NORMAL_TEXT_RATIO + AA_CONTRAST_MARGIN;
|
|
356
|
+
var LIGHT_SURFACE_LADDER = ["500", "600", "700", "800", "900"];
|
|
357
|
+
var DARK_SURFACE_LADDER = ["500", "400", "300", "200", "100", "50"];
|
|
358
|
+
function worstRatio(ink, grounds) {
|
|
359
|
+
return Math.min(...grounds.map((ground) => contrastRatio(ink, ground)));
|
|
360
|
+
}
|
|
361
|
+
function readableInk(scale, grounds, ladder) {
|
|
362
|
+
const inks = ladder.map((step) => scale[step]).filter(isValidHex);
|
|
363
|
+
const legible = inks.find((ink) => worstRatio(ink, grounds) >= REQUIRED_TEXT_RATIO);
|
|
364
|
+
const strongest2 = [...inks].sort((a, b) => worstRatio(b, grounds) - worstRatio(a, grounds))[0];
|
|
365
|
+
return legible ?? strongest2 ?? scale["500"];
|
|
366
|
+
}
|
|
367
|
+
function resolveSemanticText(semantic, colors, mode) {
|
|
368
|
+
const grounds = [colors.background, colors.surface, colors.surfaceElevated].filter(isValidHex);
|
|
369
|
+
const ladder = mode === ThemeMode.Dark ? DARK_SURFACE_LADDER : LIGHT_SURFACE_LADDER;
|
|
370
|
+
return {
|
|
371
|
+
success: readableInk(semantic.success, grounds, ladder),
|
|
372
|
+
warning: readableInk(semantic.warning, grounds, ladder),
|
|
373
|
+
error: readableInk(semantic.error, grounds, ladder),
|
|
374
|
+
info: readableInk(semantic.info, grounds, ladder)
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
212
378
|
// src/resolveTheme.ts
|
|
213
379
|
var DEFAULT_FONT_FAMILY = "System";
|
|
214
380
|
var DEFAULT_HEADING_SCALE = 1;
|
|
215
381
|
function resolveTheme(config, mode) {
|
|
216
382
|
const effectiveConfig = config ?? DEFAULT_THEME_CONFIG;
|
|
217
383
|
const paletteResult = generateThemePalette(effectiveConfig);
|
|
218
|
-
|
|
384
|
+
const theme = {
|
|
219
385
|
colors: effectiveConfig[mode],
|
|
220
386
|
palette: {
|
|
221
387
|
primary: paletteResult.primary,
|
|
@@ -223,6 +389,7 @@ function resolveTheme(config, mode) {
|
|
|
223
389
|
accent: paletteResult.accent
|
|
224
390
|
},
|
|
225
391
|
semantic: paletteResult.semantic,
|
|
392
|
+
semanticText: resolveSemanticText(paletteResult.semantic, effectiveConfig[mode], mode),
|
|
226
393
|
typography: {
|
|
227
394
|
fontFamily: effectiveConfig.typography?.fontFamily ?? DEFAULT_FONT_FAMILY,
|
|
228
395
|
headingScale: effectiveConfig.typography?.headingScale ?? DEFAULT_HEADING_SCALE
|
|
@@ -233,14 +400,9 @@ function resolveTheme(config, mode) {
|
|
|
233
400
|
faviconUrl: effectiveConfig.branding.faviconContentId
|
|
234
401
|
}
|
|
235
402
|
};
|
|
403
|
+
return { ...theme, backdrop: resolveBackdrop(theme, effectiveConfig.backdrop) };
|
|
236
404
|
}
|
|
237
405
|
var ThemeContext = react.createContext(null);
|
|
238
|
-
|
|
239
|
-
// src/ThemeMode.ts
|
|
240
|
-
var ThemeMode = {
|
|
241
|
-
Light: "light",
|
|
242
|
-
Dark: "dark"
|
|
243
|
-
};
|
|
244
406
|
var MEDIA_QUERY = "(prefers-color-scheme: dark)";
|
|
245
407
|
var DEFAULT_DARK_MODE_STORAGE_KEY = "DARK_MODE_PREFERENCE";
|
|
246
408
|
function getMatchMedia() {
|
|
@@ -398,62 +560,6 @@ function fromDesignTokens(tokens) {
|
|
|
398
560
|
return config;
|
|
399
561
|
}
|
|
400
562
|
|
|
401
|
-
// src/contrast.ts
|
|
402
|
-
var AA_NORMAL_TEXT_RATIO = 4.5;
|
|
403
|
-
var AA_LARGE_TEXT_RATIO = 3;
|
|
404
|
-
var AA_NON_TEXT_RATIO = 3;
|
|
405
|
-
var AA_CONTRAST_MARGIN = 0.25;
|
|
406
|
-
var SRGB_CHANNEL_MAX = 255;
|
|
407
|
-
var GAMMA_LINEAR_THRESHOLD = 0.03928;
|
|
408
|
-
var GAMMA_LINEAR_DIVISOR = 12.92;
|
|
409
|
-
var GAMMA_OFFSET = 0.055;
|
|
410
|
-
var GAMMA_DIVISOR = 1.055;
|
|
411
|
-
var GAMMA_EXPONENT = 2.4;
|
|
412
|
-
var LUMA_COEFFICIENT_RED = 0.2126;
|
|
413
|
-
var LUMA_COEFFICIENT_GREEN = 0.7152;
|
|
414
|
-
var LUMA_COEFFICIENT_BLUE = 0.0722;
|
|
415
|
-
var CONTRAST_FLARE = 0.05;
|
|
416
|
-
var HEX_RADIX = 16;
|
|
417
|
-
var HEX_PAIR_LENGTH = 2;
|
|
418
|
-
var HEX_SHORTHAND_LENGTH = 3;
|
|
419
|
-
var HEX_FULL_LENGTH = 6;
|
|
420
|
-
var CHANNEL_OFFSET_RED = 0;
|
|
421
|
-
var CHANNEL_OFFSET_GREEN = 2;
|
|
422
|
-
var CHANNEL_OFFSET_BLUE = 4;
|
|
423
|
-
function normalizeHexDigits(hex) {
|
|
424
|
-
const clean = hex.trim().replace("#", "").toLowerCase();
|
|
425
|
-
if (clean.length === HEX_SHORTHAND_LENGTH)
|
|
426
|
-
return `${clean[0] ?? ""}${clean[0] ?? ""}${clean[1] ?? ""}${clean[1] ?? ""}${clean[2] ?? ""}${clean[2] ?? ""}`;
|
|
427
|
-
return clean;
|
|
428
|
-
}
|
|
429
|
-
function parseChannel(digits, offset) {
|
|
430
|
-
const pair = digits.slice(offset, offset + HEX_PAIR_LENGTH);
|
|
431
|
-
return parseInt(pair, HEX_RADIX) / SRGB_CHANNEL_MAX;
|
|
432
|
-
}
|
|
433
|
-
function linearizeChannel(channel) {
|
|
434
|
-
if (channel <= GAMMA_LINEAR_THRESHOLD) return channel / GAMMA_LINEAR_DIVISOR;
|
|
435
|
-
return Math.pow((channel + GAMMA_OFFSET) / GAMMA_DIVISOR, GAMMA_EXPONENT);
|
|
436
|
-
}
|
|
437
|
-
function relativeLuminance(hex) {
|
|
438
|
-
const digits = normalizeHexDigits(hex);
|
|
439
|
-
if (digits.length !== HEX_FULL_LENGTH || !/^[0-9a-f]{6}$/.test(digits))
|
|
440
|
-
throw new Error(`relativeLuminance: not a hex colour: ${hex}`);
|
|
441
|
-
const red = linearizeChannel(parseChannel(digits, CHANNEL_OFFSET_RED));
|
|
442
|
-
const green = linearizeChannel(parseChannel(digits, CHANNEL_OFFSET_GREEN));
|
|
443
|
-
const blue = linearizeChannel(parseChannel(digits, CHANNEL_OFFSET_BLUE));
|
|
444
|
-
return LUMA_COEFFICIENT_RED * red + LUMA_COEFFICIENT_GREEN * green + LUMA_COEFFICIENT_BLUE * blue;
|
|
445
|
-
}
|
|
446
|
-
function contrastRatio(foreground, background) {
|
|
447
|
-
const first = relativeLuminance(foreground);
|
|
448
|
-
const second = relativeLuminance(background);
|
|
449
|
-
const lighter = Math.max(first, second);
|
|
450
|
-
const darker = Math.min(first, second);
|
|
451
|
-
return (lighter + CONTRAST_FLARE) / (darker + CONTRAST_FLARE);
|
|
452
|
-
}
|
|
453
|
-
function meetsContrast(foreground, background, requiredRatio) {
|
|
454
|
-
return contrastRatio(foreground, background) >= requiredRatio;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
563
|
// src/badgeColors.ts
|
|
458
564
|
var BADGE_TEXT_REQUIRED_RATIO = AA_NORMAL_TEXT_RATIO + AA_CONTRAST_MARGIN;
|
|
459
565
|
var BADGE_TINT_SHADE = "100";
|
|
@@ -472,6 +578,7 @@ exports.AA_LARGE_TEXT_RATIO = AA_LARGE_TEXT_RATIO;
|
|
|
472
578
|
exports.AA_NON_TEXT_RATIO = AA_NON_TEXT_RATIO;
|
|
473
579
|
exports.AA_NORMAL_TEXT_RATIO = AA_NORMAL_TEXT_RATIO;
|
|
474
580
|
exports.BADGE_TINT_SHADE = BADGE_TINT_SHADE;
|
|
581
|
+
exports.BackdropPreset = BackdropPreset;
|
|
475
582
|
exports.DEFAULT_DARK_MODE_STORAGE_KEY = DEFAULT_DARK_MODE_STORAGE_KEY;
|
|
476
583
|
exports.DEFAULT_THEME_CONFIG = DEFAULT_THEME_CONFIG;
|
|
477
584
|
exports.DarkModePreference = DarkModePreference;
|
|
@@ -490,6 +597,7 @@ exports.isValidHex = isValidHex;
|
|
|
490
597
|
exports.lighten = lighten;
|
|
491
598
|
exports.meetsContrast = meetsContrast;
|
|
492
599
|
exports.relativeLuminance = relativeLuminance;
|
|
600
|
+
exports.resolveBackdrop = resolveBackdrop;
|
|
493
601
|
exports.resolveTheme = resolveTheme;
|
|
494
602
|
exports.useDarkMode = useDarkMode;
|
|
495
603
|
exports.useTheme = useTheme;
|