@bilursag/solidui-tokens 0.0.0-stage → 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 +54 -0
- package/README.md +19 -2
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +77 -0
- package/dist/cli.js.map +1 -0
- package/dist/color.d.ts +20 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +65 -0
- package/dist/color.js.map +1 -0
- package/dist/css.d.ts +51 -0
- package/dist/css.d.ts.map +1 -0
- package/dist/css.js +137 -0
- package/dist/css.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/scales.d.ts +159 -0
- package/dist/scales.d.ts.map +1 -0
- package/dist/scales.js +112 -0
- package/dist/scales.js.map +1 -0
- package/dist/tailwind-preset.cjs +144 -0
- package/dist/theme.d.ts +84 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +182 -0
- package/dist/theme.js.map +1 -0
- package/dist/web.css +228 -0
- package/package.json +54 -4
- package/src/cli.ts +82 -0
- package/src/color.ts +72 -0
- package/src/css.ts +152 -0
- package/src/index.ts +12 -0
- package/src/scales.ts +134 -0
- package/src/theme.ts +239 -0
package/src/css.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns a theme into what each platform consumes:
|
|
3
|
+
* - Web (Tailwind v4): CSS variables (`--sui-*`) + an `@theme inline` block + utilities.
|
|
4
|
+
* - Native (NativeWind / Tailwind v3): "r g b" variables + a Tailwind preset.
|
|
5
|
+
*/
|
|
6
|
+
import { isHex, rgbChannels } from './color.js';
|
|
7
|
+
import { font, motion, radius, size, typeScale, type ColorScheme } from './scales.js';
|
|
8
|
+
import { colorNames, defaultTheme, type Theme } from './theme.js';
|
|
9
|
+
|
|
10
|
+
export const CSS_PREFIX = '--sui-';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The theme's variables for one scheme as a plain object, e.g. for a React `style` prop:
|
|
14
|
+
* <div style={themeVars(createTheme({ brand }), 'light')}>…</div>
|
|
15
|
+
* Lets a page re-brand an area at runtime without generating CSS.
|
|
16
|
+
*/
|
|
17
|
+
export function themeVars(theme: Theme, scheme: ColorScheme): Record<string, string> {
|
|
18
|
+
const c = theme.colors[scheme];
|
|
19
|
+
const [a, b] = theme.gradients[scheme].brand;
|
|
20
|
+
return {
|
|
21
|
+
...Object.fromEntries(colorNames.map((n) => [`${CSS_PREFIX}${n}`, c[n]])),
|
|
22
|
+
[`${CSS_PREFIX}brand-gradient`]: `linear-gradient(135deg, ${a}, ${b})`,
|
|
23
|
+
...Object.fromEntries(Object.entries(theme.shadows[scheme]).map(([k, v]) => [`${CSS_PREFIX}shadow-${k}`, v])),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function schemeVars(theme: Theme, scheme: ColorScheme, indent = ' ') {
|
|
28
|
+
return Object.entries(themeVars(theme, scheme)).map(([k, v]) => `${indent}${k}: ${v};`).join('\n');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Only the variables that change between themes: colors, gradient, shadows and fonts.
|
|
33
|
+
* Import it after `@bilursag/solidui-react/styles.css` to re-brand the components.
|
|
34
|
+
* `.dark` / `.light` match the classes the components already respond to.
|
|
35
|
+
*/
|
|
36
|
+
export function themeToCss(theme: Theme) {
|
|
37
|
+
return `:root {
|
|
38
|
+
${schemeVars(theme, 'light')}
|
|
39
|
+
${CSS_PREFIX}font-rounded: ${theme.fonts.rounded};
|
|
40
|
+
${CSS_PREFIX}font-heading: ${theme.fonts.heading};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.dark {
|
|
44
|
+
${schemeVars(theme, 'dark')}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/* Forces the light scheme inside a dark page (side-by-side previews, for instance).
|
|
48
|
+
It comes after .dark so it wins when nested. */
|
|
49
|
+
.light {
|
|
50
|
+
${schemeVars(theme, 'light')}
|
|
51
|
+
}
|
|
52
|
+
`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The complete Tailwind v4 stylesheet: theme variables + @theme + utilities. */
|
|
56
|
+
export function webCss(theme: Theme = defaultTheme) {
|
|
57
|
+
return `${themeToCss(theme).replace(
|
|
58
|
+
':root {\n',
|
|
59
|
+
`:root {
|
|
60
|
+
${CSS_PREFIX}tracking-heading: ${font.headingTracking};
|
|
61
|
+
${CSS_PREFIX}ease-spring: ${motion.easeSpring};
|
|
62
|
+
${CSS_PREFIX}ease-fade: ${motion.easeFade};
|
|
63
|
+
${CSS_PREFIX}dur-fast: ${motion.durationFast}ms;
|
|
64
|
+
${CSS_PREFIX}dur-fade: ${motion.durationFade}ms;
|
|
65
|
+
${CSS_PREFIX}dur-spring: ${motion.durationSpring}ms;
|
|
66
|
+
${CSS_PREFIX}press-pill: ${motion.pressScale.pill};
|
|
67
|
+
${CSS_PREFIX}press-icon: ${motion.pressScale.icon};
|
|
68
|
+
`,
|
|
69
|
+
)}
|
|
70
|
+
@media (prefers-reduced-motion: reduce) {
|
|
71
|
+
:root { ${CSS_PREFIX}dur-spring: 0ms; ${CSS_PREFIX}dur-fade: 0ms; ${CSS_PREFIX}dur-fast: 0ms; }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@theme inline {
|
|
75
|
+
${colorNames.map((n) => ` --color-${n}: var(${CSS_PREFIX}${n});`).join('\n')}
|
|
76
|
+
${Object.entries(radius).map(([k, v]) => ` --radius-${k}: ${v}px;`).join('\n')}
|
|
77
|
+
${Object.entries(size).map(([k, v]) => ` --spacing-${k}: ${v}px;`).join('\n')}
|
|
78
|
+
--shadow-soft: var(${CSS_PREFIX}shadow-soft);
|
|
79
|
+
--shadow-raised: var(${CSS_PREFIX}shadow-raised);
|
|
80
|
+
--shadow-overlay: var(${CSS_PREFIX}shadow-overlay);
|
|
81
|
+
--font-rounded: var(${CSS_PREFIX}font-rounded);
|
|
82
|
+
--font-heading: var(${CSS_PREFIX}font-heading);
|
|
83
|
+
--tracking-heading: var(${CSS_PREFIX}tracking-heading);
|
|
84
|
+
${Object.entries(typeScale)
|
|
85
|
+
.map(([k, t]) =>
|
|
86
|
+
[
|
|
87
|
+
` --text-${k}: ${t.size}px;`,
|
|
88
|
+
` --text-${k}--line-height: ${t.lineHeight}px;`,
|
|
89
|
+
'tracking' in t ? ` --text-${k}--letter-spacing: var(${CSS_PREFIX}tracking-heading);` : '',
|
|
90
|
+
].filter(Boolean).join('\n'),
|
|
91
|
+
)
|
|
92
|
+
.join('\n')}
|
|
93
|
+
--ease-spring: var(${CSS_PREFIX}ease-spring);
|
|
94
|
+
--ease-fade: var(${CSS_PREFIX}ease-fade);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@utility bg-brand-gradient {
|
|
98
|
+
background-image: var(${CSS_PREFIX}brand-gradient);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
@utility focus-ring {
|
|
102
|
+
outline: 2px solid var(${CSS_PREFIX}focus);
|
|
103
|
+
outline-offset: 2px;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
@utility press-spring {
|
|
107
|
+
transition:
|
|
108
|
+
scale var(${CSS_PREFIX}dur-spring) var(${CSS_PREFIX}ease-spring),
|
|
109
|
+
background-color var(${CSS_PREFIX}dur-fast) var(${CSS_PREFIX}ease-fade),
|
|
110
|
+
box-shadow var(${CSS_PREFIX}dur-fast) var(${CSS_PREFIX}ease-fade),
|
|
111
|
+
color var(${CSS_PREFIX}dur-fast) var(${CSS_PREFIX}ease-fade),
|
|
112
|
+
opacity var(${CSS_PREFIX}dur-fade) var(${CSS_PREFIX}ease-fade);
|
|
113
|
+
}
|
|
114
|
+
`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Variables for NativeWind's `vars()`: `{ '--sui-brand': '75 72 224', … }`.
|
|
119
|
+
* Only hex colors: NativeWind needs RGB channels for opacity modifiers (bg-brand/50).
|
|
120
|
+
*/
|
|
121
|
+
export function nativeVars(theme: Theme, scheme: ColorScheme): Record<string, string> {
|
|
122
|
+
const c = theme.colors[scheme];
|
|
123
|
+
return Object.fromEntries(colorNames.filter((n) => isHex(c[n])).map((n) => [`${CSS_PREFIX}${n}`, rgbChannels(c[n])]));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Tailwind v3 preset for NativeWind: colors → variables, radii, heights, fonts and the type scale. */
|
|
127
|
+
export function tailwindPreset() {
|
|
128
|
+
const c = defaultTheme.colors.light;
|
|
129
|
+
return {
|
|
130
|
+
theme: {
|
|
131
|
+
extend: {
|
|
132
|
+
colors: Object.fromEntries(
|
|
133
|
+
colorNames.filter((n) => isHex(c[n])).map((n) => [n, `rgb(var(${CSS_PREFIX}${n}) / <alpha-value>)`]),
|
|
134
|
+
),
|
|
135
|
+
borderRadius: Object.fromEntries(Object.entries(radius).map(([k, v]) => [k, `${v}px`])),
|
|
136
|
+
spacing: Object.fromEntries(Object.entries(size).map(([k, v]) => [k, `${v}px`])),
|
|
137
|
+
fontFamily: {
|
|
138
|
+
rounded: [font.native.rounded.medium],
|
|
139
|
+
...Object.fromEntries(Object.entries(font.native.rounded).map(([w, f]) => [`rounded-${w}`, [f]])),
|
|
140
|
+
heading: [font.native.heading.bold],
|
|
141
|
+
...Object.fromEntries(Object.entries(font.native.heading).map(([w, f]) => [`heading-${w}`, [f]])),
|
|
142
|
+
},
|
|
143
|
+
letterSpacing: { heading: font.headingTracking },
|
|
144
|
+
// Size + line height in px. Title tracking goes through the
|
|
145
|
+
// tracking-heading class (in <Text>), not here.
|
|
146
|
+
fontSize: Object.fromEntries(
|
|
147
|
+
Object.entries(typeScale).map(([k, t]) => [k, [`${t.size}px`, { lineHeight: `${t.lineHeight}px` }]]),
|
|
148
|
+
),
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SolidUI design tokens — the single source of truth for @bilursag/solidui-react (web)
|
|
3
|
+
* and @bilursag/solidui-native (React Native).
|
|
4
|
+
*
|
|
5
|
+
* Look and feel inspired by Plush UI (https://plush-ui.pages.dev, MIT): light
|
|
6
|
+
* surfaces with soft shadows instead of borders, pill-shaped controls, rounded
|
|
7
|
+
* type and spring motion.
|
|
8
|
+
*/
|
|
9
|
+
export * from './scales.js';
|
|
10
|
+
export * from './color.js';
|
|
11
|
+
export * from './theme.js';
|
|
12
|
+
export * from './css.js';
|
package/src/scales.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SolidUI design tokens — the single source of truth for @bilursag/solidui-react (web)
|
|
3
|
+
* and @bilursag/solidui-native (React Native).
|
|
4
|
+
*
|
|
5
|
+
* Look and feel inspired by Plush UI (https://plush-ui.pages.dev, MIT): light
|
|
6
|
+
* surfaces with soft shadows instead of borders, pill-shaped controls, rounded
|
|
7
|
+
* type and spring motion.
|
|
8
|
+
*
|
|
9
|
+
* Colors live in a *theme* (see ./theme.ts): neutrals and states are fixed and
|
|
10
|
+
* WCAG-checked; the brand group is derived from one color with `createTheme`.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export type ColorScheme = 'light' | 'dark';
|
|
14
|
+
|
|
15
|
+
/** Radii (px). A pill is half the control height. */
|
|
16
|
+
export const radius = {
|
|
17
|
+
item: 10,
|
|
18
|
+
check: 8,
|
|
19
|
+
row: 16,
|
|
20
|
+
card: 24,
|
|
21
|
+
sheet: 32,
|
|
22
|
+
panel: 40,
|
|
23
|
+
pill: 9999,
|
|
24
|
+
} as const;
|
|
25
|
+
|
|
26
|
+
/** Control heights (px): buttons, inputs, tabs, chips. */
|
|
27
|
+
export const size = {
|
|
28
|
+
'control-xs': 36,
|
|
29
|
+
'control-sm': 44,
|
|
30
|
+
'control-md': 52,
|
|
31
|
+
'control-lg': 56,
|
|
32
|
+
'control-xl': 64,
|
|
33
|
+
} as const;
|
|
34
|
+
|
|
35
|
+
/** CSS shadows. A shadow is invisible on dark backgrounds, so dark mode adds a 1 px light ring. */
|
|
36
|
+
export const shadows: Record<ColorScheme, { soft: string; raised: string; overlay: string }> = {
|
|
37
|
+
light: {
|
|
38
|
+
soft: '0 1px 2px rgba(20, 20, 20, 0.04), 0 8px 24px rgba(20, 20, 20, 0.05)',
|
|
39
|
+
raised: '0 1px 3px rgba(20, 20, 20, 0.14), 0 4px 12px rgba(20, 20, 20, 0.08)',
|
|
40
|
+
overlay: '0 2px 8px rgba(20, 20, 20, 0.08), 0 24px 64px rgba(20, 20, 20, 0.16)',
|
|
41
|
+
},
|
|
42
|
+
dark: {
|
|
43
|
+
soft: '0 0 0 1px rgba(255, 255, 255, 0.06), 0 8px 24px rgba(0, 0, 0, 0.35)',
|
|
44
|
+
raised: '0 0 0 1px rgba(255, 255, 255, 0.10), 0 4px 12px rgba(0, 0, 0, 0.45)',
|
|
45
|
+
overlay: '0 0 0 1px rgba(255, 255, 255, 0.08), 0 24px 64px rgba(0, 0, 0, 0.6)',
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Two families, both under the SIL Open Font License 1.1:
|
|
51
|
+
* - Text (`rounded`): Open Runde, for everything except large titles.
|
|
52
|
+
* - Titles (`heading`): Montserrat, only from ~22 px up and at 600–800 —
|
|
53
|
+
* it is wide and looks cramped at small sizes.
|
|
54
|
+
* React Native does not synthesize weights for custom fonts, so native has one family per weight.
|
|
55
|
+
*/
|
|
56
|
+
export const font = {
|
|
57
|
+
web: {
|
|
58
|
+
rounded: '"Open Runde", ui-rounded, "SF Pro Rounded", system-ui, sans-serif',
|
|
59
|
+
heading: '"Montserrat Variable", "Montserrat", ui-sans-serif, system-ui, sans-serif',
|
|
60
|
+
},
|
|
61
|
+
native: {
|
|
62
|
+
rounded: {
|
|
63
|
+
regular: 'OpenRunde-Regular',
|
|
64
|
+
medium: 'OpenRunde-Medium',
|
|
65
|
+
semibold: 'OpenRunde-Semibold',
|
|
66
|
+
bold: 'OpenRunde-Bold',
|
|
67
|
+
},
|
|
68
|
+
heading: {
|
|
69
|
+
semibold: 'Montserrat-SemiBold',
|
|
70
|
+
bold: 'Montserrat-Bold',
|
|
71
|
+
extrabold: 'Montserrat-ExtraBold',
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
/** Montserrat is wide: large titles are tightened slightly. */
|
|
75
|
+
headingTracking: '-0.02em',
|
|
76
|
+
} as const;
|
|
77
|
+
|
|
78
|
+
export type FontWeight = 'regular' | 'medium' | 'semibold' | 'bold' | 'extrabold';
|
|
79
|
+
type TypeStyle = {
|
|
80
|
+
/** px */
|
|
81
|
+
size: number;
|
|
82
|
+
/** px */
|
|
83
|
+
lineHeight: number;
|
|
84
|
+
family: 'heading' | 'rounded';
|
|
85
|
+
weight: FontWeight;
|
|
86
|
+
/** Only Montserrat titles are tightened (font.headingTracking). */
|
|
87
|
+
tracking?: 'heading';
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Semantic type scale: size and line height travel together.
|
|
92
|
+
* Web: `text-display`, `text-title`… (Tailwind v4). Native: the same classes
|
|
93
|
+
* (NativeWind) or the numbers for StyleSheet.
|
|
94
|
+
* Coexists with Tailwind's numeric scale; the large steps match it:
|
|
95
|
+
* title = 2xl, heading = xl, body = base, label = sm.
|
|
96
|
+
* `family` and `weight` are each style's default (used by <Text> on native).
|
|
97
|
+
*/
|
|
98
|
+
export const typeScale = {
|
|
99
|
+
display: { size: 34, lineHeight: 40, family: 'heading', weight: 'extrabold', tracking: 'heading' },
|
|
100
|
+
title: { size: 24, lineHeight: 32, family: 'heading', weight: 'bold', tracking: 'heading' },
|
|
101
|
+
heading: { size: 20, lineHeight: 28, family: 'rounded', weight: 'semibold' },
|
|
102
|
+
body: { size: 16, lineHeight: 24, family: 'rounded', weight: 'medium' },
|
|
103
|
+
'body-sm': { size: 15, lineHeight: 21, family: 'rounded', weight: 'medium' },
|
|
104
|
+
label: { size: 14, lineHeight: 20, family: 'rounded', weight: 'semibold' },
|
|
105
|
+
caption: { size: 13, lineHeight: 18, family: 'rounded', weight: 'medium' },
|
|
106
|
+
/** Counters and tiny badges. Not for running text. */
|
|
107
|
+
micro: { size: 11, lineHeight: 14, family: 'rounded', weight: 'semibold' },
|
|
108
|
+
btn: { size: 17, lineHeight: 22, family: 'rounded', weight: 'semibold' },
|
|
109
|
+
} as const satisfies Record<string, TypeStyle>;
|
|
110
|
+
export type TypeStyleName = keyof typeof typeScale;
|
|
111
|
+
|
|
112
|
+
/** Size, line height and tracking in px for React Native StyleSheet (the family is per weight, separately). */
|
|
113
|
+
export function textMetrics(name: TypeStyleName) {
|
|
114
|
+
const t: TypeStyle = typeScale[name];
|
|
115
|
+
return {
|
|
116
|
+
fontSize: t.size,
|
|
117
|
+
lineHeight: t.lineHeight,
|
|
118
|
+
...(t.tracking ? { letterSpacing: Math.round(t.size * parseFloat(font.headingTracking) * 100) / 100 } : null),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export const motion = {
|
|
123
|
+
durationFast: 150,
|
|
124
|
+
durationFade: 220,
|
|
125
|
+
durationSpring: 600,
|
|
126
|
+
/** Spring curve with ~3 % overshoot (from Plush UI) for CSS `transition`. */
|
|
127
|
+
easeSpring:
|
|
128
|
+
'linear(0, 0.062, 0.203, 0.372, 0.538, 0.683, 0.801, 0.89, 0.952, 0.993, 1.016, 1.028, 1.031, 1.029, 1.025, 1.02, 1.015, 1.01, 1.006, 1.004, 1.002, 1)',
|
|
129
|
+
easeFade: 'cubic-bezier(0.2, 0, 0, 1)',
|
|
130
|
+
/** Press scale: pills give a little, round buttons more. */
|
|
131
|
+
pressScale: { pill: 0.97, icon: 0.92 },
|
|
132
|
+
/** Reanimated equivalent (`withSpring`). */
|
|
133
|
+
spring: { damping: 15, stiffness: 320, mass: 1 },
|
|
134
|
+
} as const;
|
package/src/theme.ts
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Themes. Neutrals and status colors are fixed (and WCAG-checked); the brand
|
|
3
|
+
* group (brand, hover, press, tint, ink, on-brand, focus) is derived from a
|
|
4
|
+
* single color, so `createTheme({ brand: '#0a7d5a' })` is a complete theme.
|
|
5
|
+
*/
|
|
6
|
+
import { contrast, fromHsl, mix, shiftUntil, toHsl } from './color.js';
|
|
7
|
+
import { font, shadows, type ColorScheme } from './scales.js';
|
|
8
|
+
|
|
9
|
+
const neutralsLight = {
|
|
10
|
+
// Surfaces, back to front
|
|
11
|
+
canvas: '#fbfbfa',
|
|
12
|
+
panel: '#f3f3f1',
|
|
13
|
+
surface: '#ffffff',
|
|
14
|
+
'surface-hover': '#ffffff',
|
|
15
|
+
'surface-press': '#f3f3f1',
|
|
16
|
+
fill: '#f0f0ee',
|
|
17
|
+
'fill-strong': '#e8e8e5',
|
|
18
|
+
'fill-press': '#e0e0dc',
|
|
19
|
+
line: '#e4e4e1',
|
|
20
|
+
|
|
21
|
+
// Ink. ink and ink-2 = text. ink-3 = icons, placeholders and disabled states
|
|
22
|
+
// only: it meets 3:1, not 4.5:1, so never use it for text people must read.
|
|
23
|
+
ink: '#141414',
|
|
24
|
+
'ink-2': '#67676b',
|
|
25
|
+
'ink-3': '#88888d',
|
|
26
|
+
|
|
27
|
+
// Status
|
|
28
|
+
success: '#1f9d55',
|
|
29
|
+
'success-tint': '#e6f6ec',
|
|
30
|
+
'success-ink': '#157a41',
|
|
31
|
+
danger: '#d93a3f',
|
|
32
|
+
'danger-hover': '#c33035',
|
|
33
|
+
'on-danger': '#ffffff',
|
|
34
|
+
'danger-tint': '#fdecec',
|
|
35
|
+
'danger-ink': '#c33035',
|
|
36
|
+
warning: '#f5a524',
|
|
37
|
+
'warning-tint': '#fef4e2',
|
|
38
|
+
'warning-ink': '#8a5a00',
|
|
39
|
+
|
|
40
|
+
scrim: 'rgba(20, 20, 20, 0.32)',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type Neutrals = typeof neutralsLight;
|
|
44
|
+
|
|
45
|
+
const neutralsDark: Neutrals = {
|
|
46
|
+
canvas: '#0e0f12',
|
|
47
|
+
panel: '#15171b',
|
|
48
|
+
surface: '#1a1d22',
|
|
49
|
+
'surface-hover': '#20242a',
|
|
50
|
+
'surface-press': '#262a31',
|
|
51
|
+
fill: '#23262c',
|
|
52
|
+
'fill-strong': '#2b2f36',
|
|
53
|
+
'fill-press': '#333840',
|
|
54
|
+
line: '#2e3239',
|
|
55
|
+
|
|
56
|
+
ink: '#f3f4f6',
|
|
57
|
+
'ink-2': '#a1a6b0',
|
|
58
|
+
'ink-3': '#7c818b',
|
|
59
|
+
|
|
60
|
+
success: '#2fc26b',
|
|
61
|
+
'success-tint': '#12291c',
|
|
62
|
+
'success-ink': '#5fd893',
|
|
63
|
+
danger: '#d93a3f',
|
|
64
|
+
// Darker, not lighter: with white text on top, lightening lowers the contrast.
|
|
65
|
+
'danger-hover': '#c33035',
|
|
66
|
+
'on-danger': '#ffffff',
|
|
67
|
+
'danger-tint': '#2d1517',
|
|
68
|
+
'danger-ink': '#ff8a8d',
|
|
69
|
+
warning: '#f5a524',
|
|
70
|
+
'warning-tint': '#2c2210',
|
|
71
|
+
'warning-ink': '#ffc766',
|
|
72
|
+
|
|
73
|
+
scrim: 'rgba(0, 0, 0, 0.6)',
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const brandKeys = ['brand', 'brand-hover', 'brand-press', 'on-brand', 'brand-tint', 'brand-ink', 'brand-2', 'focus'] as const;
|
|
77
|
+
type BrandGroup = Record<(typeof brandKeys)[number], string>;
|
|
78
|
+
|
|
79
|
+
export type Palette = Neutrals & BrandGroup;
|
|
80
|
+
export type ColorToken = keyof Palette;
|
|
81
|
+
|
|
82
|
+
/** Every color token, in a stable order (neutrals, then brand). */
|
|
83
|
+
export const colorNames = [...Object.keys(neutralsLight), ...brandKeys] as ColorToken[];
|
|
84
|
+
|
|
85
|
+
export interface Theme {
|
|
86
|
+
colors: Record<ColorScheme, Palette>;
|
|
87
|
+
gradients: Record<ColorScheme, { brand: [string, string] }>;
|
|
88
|
+
shadows: typeof shadows;
|
|
89
|
+
/** CSS font stacks (web). Native fonts are configured in the Tailwind preset. */
|
|
90
|
+
fonts: { rounded: string; heading: string };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
type PerScheme<T> = T | { light: T; dark: T };
|
|
94
|
+
const perScheme = <T,>(v: PerScheme<T>, s: ColorScheme): T =>
|
|
95
|
+
typeof v === 'object' && v !== null && 'light' in v ? v[s] : (v as T);
|
|
96
|
+
|
|
97
|
+
export interface ThemeOptions {
|
|
98
|
+
/** The brand color (6-digit hex). One value derives both schemes; pass `{ light, dark }` to pick each. */
|
|
99
|
+
brand?: PerScheme<string>;
|
|
100
|
+
/** Second color of the brand gradient. Defaults to the hue 45° ahead (45° back for reds), adjusted so on-brand stays legible. */
|
|
101
|
+
accent?: PerScheme<string>;
|
|
102
|
+
/** Exact overrides per token, applied after derivation (escape hatch). */
|
|
103
|
+
colors?: Partial<Record<ColorScheme, Partial<Palette>>>;
|
|
104
|
+
/** CSS font stacks for the web. */
|
|
105
|
+
fonts?: Partial<Theme['fonts']>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export const DEFAULT_BRAND = '#4b48e0';
|
|
109
|
+
|
|
110
|
+
const WHITE = '#ffffff';
|
|
111
|
+
const BLACK = '#000000';
|
|
112
|
+
|
|
113
|
+
/** White or near-black, whichever reads better on `bg` (white wins ties at 4.5:1). */
|
|
114
|
+
function pickOn(bg: string, darkText: string) {
|
|
115
|
+
if (contrast(WHITE, bg) >= 4.5) return WHITE;
|
|
116
|
+
return contrast(darkText, bg) > contrast(WHITE, bg) ? darkText : WHITE;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function deriveBrand(scheme: ColorScheme, base: string, accent: string | undefined, exact: boolean): BrandGroup {
|
|
120
|
+
const n = scheme === 'light' ? neutralsLight : neutralsDark;
|
|
121
|
+
let brand = base;
|
|
122
|
+
if (scheme === 'dark' && !exact) {
|
|
123
|
+
// A single brand color usually needs a touch more light to hold its own on near-black,
|
|
124
|
+
// unless that would cost white text its contrast.
|
|
125
|
+
const lighter = mix(base, WHITE, 0.1);
|
|
126
|
+
if (!(contrast(WHITE, base) >= 4.5 && contrast(WHITE, lighter) < 4.5)) brand = lighter;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const onBrand = pickOn(brand, scheme === 'light' ? n.ink : neutralsDark.canvas);
|
|
130
|
+
// Hover/press move away from the text color, so contrast only goes up.
|
|
131
|
+
const away = onBrand === WHITE ? BLACK : WHITE;
|
|
132
|
+
const step = scheme === 'light' ? 0.12 : 0.08;
|
|
133
|
+
const tint = scheme === 'light' ? mix(brand, WHITE, 0.9) : mix(brand, n.canvas, 0.8);
|
|
134
|
+
// Brand-colored text (links, ghost labels) on every surface it can sit on.
|
|
135
|
+
const ink = shiftUntil(brand, scheme === 'light' ? BLACK : WHITE, [n.surface, n.canvas, n.fill, tint], 4.5);
|
|
136
|
+
|
|
137
|
+
let accentColor = accent;
|
|
138
|
+
if (!accentColor) {
|
|
139
|
+
const [h, s, l] = toHsl(base);
|
|
140
|
+
// The neighbouring hue 45° ahead, unless it lands on orange/yellow and has to be
|
|
141
|
+
// darkened for white text: darkened oranges turn brown, so go 45° back instead.
|
|
142
|
+
const ahead = (h + 45) % 360;
|
|
143
|
+
const hue = away === BLACK && ahead >= 15 && ahead <= 75 ? (h + 315) % 360 : ahead;
|
|
144
|
+
accentColor = fromHsl(hue, Math.min(1, s * 1.05), Math.min(0.62, Math.max(0.5, l + 0.08)));
|
|
145
|
+
if (scheme === 'dark') accentColor = mix(accentColor, WHITE, 0.08);
|
|
146
|
+
// Text sits on the gradient, so its far end must hold on-brand as well as the brand does.
|
|
147
|
+
accentColor = shiftUntil(accentColor, away, [onBrand], 4.5);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
brand,
|
|
152
|
+
'brand-hover': mix(brand, away, step),
|
|
153
|
+
'brand-press': mix(brand, away, step * 2),
|
|
154
|
+
'on-brand': onBrand,
|
|
155
|
+
'brand-tint': tint,
|
|
156
|
+
'brand-ink': ink,
|
|
157
|
+
'brand-2': accentColor,
|
|
158
|
+
focus: ink,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Builds a complete theme from a brand color. Check it with `checkContrast(theme)`. */
|
|
163
|
+
export function createTheme(options: ThemeOptions = {}): Theme {
|
|
164
|
+
const colors = {} as Record<ColorScheme, Palette>;
|
|
165
|
+
const gradients = {} as Theme['gradients'];
|
|
166
|
+
for (const s of ['light', 'dark'] as ColorScheme[]) {
|
|
167
|
+
const base = perScheme(options.brand ?? DEFAULT_BRAND, s);
|
|
168
|
+
const accent = options.accent === undefined ? undefined : perScheme(options.accent, s);
|
|
169
|
+
// With { light, dark } each value is used as is; a single value is lightened for dark mode.
|
|
170
|
+
const group = deriveBrand(s, base, accent, typeof options.brand === 'object');
|
|
171
|
+
colors[s] = { ...(s === 'light' ? neutralsLight : neutralsDark), ...group, ...options.colors?.[s] };
|
|
172
|
+
gradients[s] = { brand: [colors[s].brand, colors[s]['brand-2']] };
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
colors,
|
|
176
|
+
gradients,
|
|
177
|
+
shadows,
|
|
178
|
+
fonts: { rounded: font.web.rounded, heading: font.web.heading, ...options.fonts },
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** The theme SolidUI ships with. */
|
|
183
|
+
export const defaultTheme: Theme = createTheme();
|
|
184
|
+
|
|
185
|
+
/** Shortcuts to the default theme (handy in JS: icon colors, charts…). */
|
|
186
|
+
export const colors = defaultTheme.colors;
|
|
187
|
+
export const gradients = defaultTheme.gradients;
|
|
188
|
+
|
|
189
|
+
/** Text/background pairs checked against WCAG (4.5 = AA body text, 3 = icons and large text). */
|
|
190
|
+
export const contrastChecks: Array<[ColorToken, ColorToken, number]> = [
|
|
191
|
+
['ink', 'canvas', 4.5],
|
|
192
|
+
['ink', 'surface', 4.5],
|
|
193
|
+
['ink-2', 'surface', 4.5],
|
|
194
|
+
['ink-2', 'canvas', 4.5],
|
|
195
|
+
['ink-2', 'fill', 4.5],
|
|
196
|
+
['ink-2', 'fill-strong', 4.5],
|
|
197
|
+
['ink-3', 'surface', 3],
|
|
198
|
+
['ink-3', 'canvas', 3],
|
|
199
|
+
['ink-3', 'fill', 3],
|
|
200
|
+
['on-brand', 'brand', 4.5],
|
|
201
|
+
['on-brand', 'brand-hover', 4.5],
|
|
202
|
+
['on-brand', 'brand-press', 4.5],
|
|
203
|
+
// The gradient's far end: text on bg-brand-gradient should be large (titles), so 3:1.
|
|
204
|
+
['on-brand', 'brand-2', 3],
|
|
205
|
+
['brand-ink', 'surface', 4.5],
|
|
206
|
+
['brand-ink', 'canvas', 4.5],
|
|
207
|
+
['brand-ink', 'fill', 4.5],
|
|
208
|
+
['brand-ink', 'brand-tint', 4.5],
|
|
209
|
+
['focus', 'surface', 3],
|
|
210
|
+
['focus', 'canvas', 3],
|
|
211
|
+
['on-danger', 'danger', 4.5],
|
|
212
|
+
['on-danger', 'danger-hover', 4.5],
|
|
213
|
+
['danger-ink', 'surface', 4.5],
|
|
214
|
+
['danger-ink', 'canvas', 4.5],
|
|
215
|
+
['danger-ink', 'danger-tint', 4.5],
|
|
216
|
+
['success-ink', 'surface', 4.5],
|
|
217
|
+
['success-ink', 'success-tint', 4.5],
|
|
218
|
+
['warning-ink', 'surface', 4.5],
|
|
219
|
+
['warning-ink', 'warning-tint', 4.5],
|
|
220
|
+
];
|
|
221
|
+
|
|
222
|
+
export interface ContrastResult {
|
|
223
|
+
scheme: ColorScheme;
|
|
224
|
+
fg: ColorToken;
|
|
225
|
+
bg: ColorToken;
|
|
226
|
+
ratio: number;
|
|
227
|
+
min: number;
|
|
228
|
+
pass: boolean;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Runs every pair in `contrastChecks` on both schemes. */
|
|
232
|
+
export function checkContrast(theme: Theme = defaultTheme): ContrastResult[] {
|
|
233
|
+
return (['light', 'dark'] as ColorScheme[]).flatMap((scheme) =>
|
|
234
|
+
contrastChecks.map(([fg, bg, min]) => {
|
|
235
|
+
const ratio = contrast(theme.colors[scheme][fg], theme.colors[scheme][bg]);
|
|
236
|
+
return { scheme, fg, bg, ratio, min, pass: ratio >= min };
|
|
237
|
+
}),
|
|
238
|
+
);
|
|
239
|
+
}
|