@usequeek/theme-kit 0.1.42 → 0.1.43
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 +59 -0
package/package.json
CHANGED
package/utils/brand.ts
CHANGED
|
@@ -2,13 +2,16 @@ import type { CSSProperties } from 'react';
|
|
|
2
2
|
import type {
|
|
3
3
|
Brand,
|
|
4
4
|
DesignBorderWeight,
|
|
5
|
+
DesignColorScheme,
|
|
5
6
|
DesignDensity,
|
|
6
7
|
DesignElevation,
|
|
7
8
|
DesignHeadingCase,
|
|
8
9
|
DesignImageFilter,
|
|
10
|
+
DesignImageFit,
|
|
9
11
|
DesignImageRadius,
|
|
10
12
|
DesignMotion,
|
|
11
13
|
DesignRadius,
|
|
14
|
+
DesignScaleRatio,
|
|
12
15
|
DesignTokenColor,
|
|
13
16
|
DesignTokenImage,
|
|
14
17
|
DesignTokenShape,
|
|
@@ -137,6 +140,62 @@ const IMAGE_FILTER_MAP: Record<DesignImageFilter, string> = {
|
|
|
137
140
|
warm: 'sepia(0.35) saturate(1.1) contrast(0.98)',
|
|
138
141
|
};
|
|
139
142
|
|
|
143
|
+
/** Reject both extra values and missing union members at compile time. */
|
|
144
|
+
function exhaustiveChoices<Union extends string | number>() {
|
|
145
|
+
return <const Values extends readonly Union[]>(
|
|
146
|
+
values: Values & ([Union] extends [Values[number]] ? unknown : never),
|
|
147
|
+
): Readonly<Values> => Object.freeze(values);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** These private, exhaustive Record tables have only their declared string keys. */
|
|
151
|
+
function tableKeys<Table extends Record<string, unknown>>(table: Table): Array<Extract<keyof Table, string>> {
|
|
152
|
+
return Object.keys(table) as Array<Extract<keyof Table, string>>;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Allowed semantic token values, shared by renderers and external validation tools. */
|
|
156
|
+
export const DESIGN_TOKEN_CHOICES = Object.freeze({
|
|
157
|
+
'color.scheme': exhaustiveChoices<DesignColorScheme>()(['light', 'dark']),
|
|
158
|
+
'type.scale_ratio': exhaustiveChoices<DesignScaleRatio>()([1.125, 1.2, 1.25, 1.333]),
|
|
159
|
+
'type.heading_case': exhaustiveChoices<DesignHeadingCase>()(tableKeys(CASE_MAP)),
|
|
160
|
+
'space.density': exhaustiveChoices<DesignDensity>()(tableKeys(DENSITY_UNIT_REM)),
|
|
161
|
+
'shape.radius': exhaustiveChoices<DesignRadius>()(tableKeys(RADIUS_SCALE)),
|
|
162
|
+
'shape.border_weight': exhaustiveChoices<DesignBorderWeight>()(tableKeys(BORDER_WEIGHT_PX)),
|
|
163
|
+
elevation: exhaustiveChoices<DesignElevation>()(tableKeys(ELEVATION_SHADOWS)),
|
|
164
|
+
motion: exhaustiveChoices<DesignMotion>()(tableKeys(MOTION_TIMING)),
|
|
165
|
+
'image.radius': exhaustiveChoices<DesignImageRadius>()(tableKeys(IMAGE_RADIUS_MAP)),
|
|
166
|
+
'image.fit': exhaustiveChoices<DesignImageFit>()(['cover', 'contain']),
|
|
167
|
+
'image.filter': exhaustiveChoices<DesignImageFilter>()(tableKeys(IMAGE_FILTER_MAP)),
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Check only the enumerated leaves; missing/undefined values are valid partials.
|
|
172
|
+
* Read own data properties only, so malformed input cannot invoke getters.
|
|
173
|
+
* Unreadable leaves (including throwing proxy traps) are treated as absent.
|
|
174
|
+
*/
|
|
175
|
+
export function findInvalidDesignTokens(tokens: unknown): Array<{
|
|
176
|
+
path: string; value: unknown; allowed: readonly (string | number)[];
|
|
177
|
+
}> {
|
|
178
|
+
const invalid: ReturnType<typeof findInvalidDesignTokens> = [];
|
|
179
|
+
for (const [path, allowed] of Object.entries(DESIGN_TOKEN_CHOICES)) {
|
|
180
|
+
let value: unknown = tokens;
|
|
181
|
+
try {
|
|
182
|
+
for (const key of path.split('.')) {
|
|
183
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
184
|
+
value = undefined;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
value = Object.getOwnPropertyDescriptor(value, key)?.value;
|
|
188
|
+
}
|
|
189
|
+
} catch {
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (value !== undefined && !(allowed as readonly unknown[]).includes(value)) {
|
|
193
|
+
invalid.push({ path, value, allowed });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return invalid;
|
|
197
|
+
}
|
|
198
|
+
|
|
140
199
|
/** n exponents for the --fs-xs..--fs-3xl ladder, base*ratio^n. */
|
|
141
200
|
const FONT_SIZE_STEPS: Array<[string, number]> = [
|
|
142
201
|
['xs', -2],
|