@cognitap/radiant 0.0.3 → 0.0.5
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 +69 -0
- package/README.md +28 -15
- package/dist/library/Card.d.ts.map +1 -1
- package/dist/library/Card.js +8 -6
- package/dist/library/Card.js.map +1 -1
- package/dist/library/Checkbox.js +2 -2
- package/dist/library/Checkbox.js.map +1 -1
- package/dist/library/Collapsible.d.ts.map +1 -1
- package/dist/library/Collapsible.js +5 -4
- package/dist/library/Collapsible.js.map +1 -1
- package/dist/library/Dialog.d.ts.map +1 -1
- package/dist/library/Dialog.js +8 -6
- package/dist/library/Dialog.js.map +1 -1
- package/dist/library/Field.js +4 -4
- package/dist/library/Field.js.map +1 -1
- package/dist/library/Input.d.ts.map +1 -1
- package/dist/library/Input.js +4 -3
- package/dist/library/Input.js.map +1 -1
- package/dist/library/Link.d.ts.map +1 -1
- package/dist/library/Link.js +0 -1
- package/dist/library/Link.js.map +1 -1
- package/dist/library/RadioGroup.js +2 -2
- package/dist/library/RadioGroup.js.map +1 -1
- package/dist/library/ThemeScope.js +1 -1
- package/dist/library/ThemeScope.js.map +1 -1
- package/dist/library/actionStyles.js +2 -2
- package/dist/library/actionStyles.js.map +1 -1
- package/dist/library/cn.d.ts +2 -1
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +22 -1
- package/dist/library/cn.js.map +1 -1
- package/dist/library/color/colorFamilies.d.ts +3 -0
- package/dist/library/color/colorFamilies.d.ts.map +1 -0
- package/dist/library/color/colorFamilies.js +20 -0
- package/dist/library/color/colorFamilies.js.map +1 -0
- package/dist/library/color/generateColorWall.d.ts +54 -0
- package/dist/library/color/generateColorWall.d.ts.map +1 -0
- package/dist/library/color/generateColorWall.js +231 -0
- package/dist/library/color/generateColorWall.js.map +1 -0
- package/dist/library/color/index.d.ts +5 -0
- package/dist/library/color/index.d.ts.map +1 -0
- package/dist/library/color/index.js +3 -0
- package/dist/library/color/index.js.map +1 -0
- package/dist/library/color/oklch.d.ts +17 -0
- package/dist/library/color/oklch.d.ts.map +1 -0
- package/dist/library/color/oklch.js +79 -0
- package/dist/library/color/oklch.js.map +1 -0
- package/dist/library/color/recommendColorPalette.d.ts +65 -0
- package/dist/library/color/recommendColorPalette.d.ts.map +1 -0
- package/dist/library/color/recommendColorPalette.js +263 -0
- package/dist/library/color/recommendColorPalette.js.map +1 -0
- package/dist/library/index.d.ts +10 -2
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +5 -1
- package/dist/library/index.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/defineTheme.d.ts.map +1 -1
- package/dist/library/themes/defineTheme.js +5 -1
- package/dist/library/themes/defineTheme.js.map +1 -1
- package/dist/library/themes/index.d.ts +4 -0
- package/dist/library/themes/index.d.ts.map +1 -1
- package/dist/library/themes/index.js +2 -0
- package/dist/library/themes/index.js.map +1 -1
- package/dist/library/themes/themeDocument.d.ts +16 -0
- package/dist/library/themes/themeDocument.d.ts.map +1 -0
- package/dist/library/themes/themeDocument.js +332 -0
- package/dist/library/themes/themeDocument.js.map +1 -0
- package/dist/library/themes/themeDocumentTypes.d.ts +80 -0
- package/dist/library/themes/themeDocumentTypes.d.ts.map +1 -0
- package/dist/library/themes/themeDocumentTypes.js +2 -0
- package/dist/library/themes/themeDocumentTypes.js.map +1 -0
- package/dist/library/themes/tokens.d.ts +7 -0
- package/dist/library/themes/tokens.d.ts.map +1 -1
- package/dist/library/themes/tokens.js +99 -0
- package/dist/library/themes/tokens.js.map +1 -1
- package/dist/library/themes/updateThemeAppearance.d.ts +29 -0
- package/dist/library/themes/updateThemeAppearance.d.ts.map +1 -0
- package/dist/library/themes/updateThemeAppearance.js +138 -0
- package/dist/library/themes/updateThemeAppearance.js.map +1 -0
- package/dist/library/typography/index.d.ts +72 -0
- package/dist/library/typography/index.d.ts.map +1 -0
- package/dist/library/typography/index.js +160 -0
- package/dist/library/typography/index.js.map +1 -0
- package/dist/library/typography/types.d.ts +78 -0
- package/dist/library/typography/types.d.ts.map +1 -0
- package/dist/library/typography/types.js +2 -0
- package/dist/library/typography/types.js.map +1 -0
- package/docs/system.md +20 -0
- package/docs/theming.md +616 -0
- package/package.json +17 -5
- package/src/Card.tsx +8 -6
- package/src/Checkbox.tsx +2 -2
- package/src/Collapsible.tsx +5 -4
- package/src/Dialog.tsx +8 -6
- package/src/Field.tsx +4 -4
- package/src/Input.tsx +4 -3
- package/src/Link.tsx +0 -1
- package/src/RadioGroup.tsx +2 -2
- package/src/ThemeScope.tsx +1 -1
- package/src/actionStyles.ts +2 -2
- package/src/cn.ts +25 -1
- package/src/color/colorFamilies.ts +19 -0
- package/src/color/generateColorWall.ts +358 -0
- package/src/color/index.ts +11 -0
- package/src/color/oklch.ts +95 -0
- package/src/color/recommendColorPalette.ts +375 -0
- package/src/index.ts +40 -0
- package/src/styles.css +1 -0
- package/src/themes/baseline.css +131 -0
- package/src/themes/defineTheme.ts +5 -1
- package/src/themes/index.ts +25 -0
- package/src/themes/themeDocument.ts +370 -0
- package/src/themes/themeDocumentTypes.ts +88 -0
- package/src/themes/tokens.ts +137 -0
- package/src/themes/updateThemeAppearance.ts +172 -0
- package/src/typography/index.ts +199 -0
- package/src/typography/types.ts +92 -0
- package/src/typography.css +336 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { ColorPalette } from '../color/recommendColorPalette.js'
|
|
2
|
+
import { tokenDefaults, type ThemeTokens } from './tokens.js'
|
|
3
|
+
import { defineThemeDocument, updateThemeDocument } from './themeDocument.js'
|
|
4
|
+
import type { ThemeDocument, ThemeSection } from './themeDocumentTypes.js'
|
|
5
|
+
|
|
6
|
+
/** Bounded workshop controls; dimensions are derived from the neutral baseline, not compounded. */
|
|
7
|
+
export interface ThemeGeometrySettings {
|
|
8
|
+
/** 0.5–2; scales shared gaps and panel padding, retaining rem units. */
|
|
9
|
+
readonly spacing?: number
|
|
10
|
+
/** 0.75–1.5; scales all three control heights, inline padding and indicators, not fonts. */
|
|
11
|
+
readonly density?: number
|
|
12
|
+
/** Pixels, 0–32. Checkbox still caps its inherited radius; Radio stays circular. */
|
|
13
|
+
readonly controlRadius?: number
|
|
14
|
+
/** Pixels, 0–48. */
|
|
15
|
+
readonly surfaceRadius?: number
|
|
16
|
+
/** Pixels, 0–4. */
|
|
17
|
+
readonly controlBorderWidth?: number
|
|
18
|
+
readonly surfaceBorderWidth?: number
|
|
19
|
+
readonly borderStyle?: 'solid' | 'dashed' | 'dotted'
|
|
20
|
+
/** 0 = none, 1 = baseline, 2 = raised, 3 = floating. Passive and overlay shadows only. */
|
|
21
|
+
readonly elevation?: 0 | 1 | 2 | 3
|
|
22
|
+
/** 0–0.9; black modal backdrop. Panel filtering remains a component exception. */
|
|
23
|
+
readonly backdropOpacity?: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ThemeAppearanceSettings {
|
|
27
|
+
readonly version?: 1
|
|
28
|
+
readonly palette?: Pick<ColorPalette, 'version' | 'color' | 'surface'>
|
|
29
|
+
readonly geometry?: ThemeSection<ThemeGeometrySettings>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const bounds = {
|
|
33
|
+
spacing: [0.5, 2],
|
|
34
|
+
density: [0.75, 1.5],
|
|
35
|
+
controlRadius: [0, 32],
|
|
36
|
+
surfaceRadius: [0, 48],
|
|
37
|
+
controlBorderWidth: [0, 4],
|
|
38
|
+
surfaceBorderWidth: [0, 4],
|
|
39
|
+
elevation: [0, 3],
|
|
40
|
+
backdropOpacity: [0, 0.9]
|
|
41
|
+
} as const
|
|
42
|
+
function record(value: unknown, path: string): asserts value is Record<string, unknown> {
|
|
43
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
44
|
+
throw new TypeError(`${path} must be an object.`)
|
|
45
|
+
}
|
|
46
|
+
function keys(value: object, allowed: readonly string[], path: string) {
|
|
47
|
+
for (const key of Object.keys(value))
|
|
48
|
+
if (!allowed.includes(key)) throw new TypeError(`Unknown ${path}.${key}.`)
|
|
49
|
+
}
|
|
50
|
+
function validate(value: unknown, path: string): asserts value is ThemeGeometrySettings {
|
|
51
|
+
record(value, path)
|
|
52
|
+
keys(value, [...Object.keys(bounds), 'borderStyle'], path)
|
|
53
|
+
for (const [key, item] of Object.entries(value)) {
|
|
54
|
+
if (item === undefined) continue
|
|
55
|
+
if (key === 'borderStyle') {
|
|
56
|
+
if (typeof item === 'string' && ['solid', 'dashed', 'dotted'].includes(item)) continue
|
|
57
|
+
} else {
|
|
58
|
+
const [min, max] = bounds[key as keyof typeof bounds]
|
|
59
|
+
if (
|
|
60
|
+
typeof item === 'number' &&
|
|
61
|
+
Number.isFinite(item) &&
|
|
62
|
+
item >= min &&
|
|
63
|
+
item <= max &&
|
|
64
|
+
(key !== 'elevation' || Number.isInteger(item))
|
|
65
|
+
)
|
|
66
|
+
continue
|
|
67
|
+
throw new TypeError(
|
|
68
|
+
`${path}.${key} must be a finite number from ${min} to ${max}${key === 'elevation' ? ' (integer)' : ''}.`
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
throw new TypeError(`${path}.borderStyle must be solid, dashed or dotted.`)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const scaled = (rem: string, factor: number) =>
|
|
75
|
+
`${Number((parseFloat(rem) * factor).toFixed(6))}rem`
|
|
76
|
+
|
|
77
|
+
function geometryTokens(settings: ThemeGeometrySettings, mode: 'light' | 'dark'): ThemeTokens {
|
|
78
|
+
const base = tokenDefaults[mode]
|
|
79
|
+
const spacing: Record<string, string> = {}
|
|
80
|
+
const control: Record<string, unknown> = {}
|
|
81
|
+
const surface: Record<string, string> = {}
|
|
82
|
+
if (settings.spacing !== undefined) {
|
|
83
|
+
for (const [key, value] of Object.entries(base.spacing))
|
|
84
|
+
spacing[key] = scaled(value, settings.spacing)
|
|
85
|
+
for (const key of ['padding', 'gap'] as const)
|
|
86
|
+
surface[key] = scaled(base.surface[key], settings.spacing)
|
|
87
|
+
}
|
|
88
|
+
if (settings.density !== undefined)
|
|
89
|
+
for (const size of ['small', 'medium', 'large'] as const) {
|
|
90
|
+
const metrics: Record<string, string> = {}
|
|
91
|
+
for (const key of ['height', 'paddingInline', 'indicatorSize'] as const)
|
|
92
|
+
metrics[key] = scaled(base.control[size][key], settings.density)
|
|
93
|
+
control[size] = metrics
|
|
94
|
+
}
|
|
95
|
+
for (const [key, target] of [
|
|
96
|
+
['Radius', 'radius'],
|
|
97
|
+
['BorderWidth', 'borderWidth']
|
|
98
|
+
] as const) {
|
|
99
|
+
const a = settings[`control${key}`],
|
|
100
|
+
b = settings[`surface${key}`]
|
|
101
|
+
if (a !== undefined) control[target] = `${a}px`
|
|
102
|
+
if (b !== undefined) surface[target] = `${b}px`
|
|
103
|
+
}
|
|
104
|
+
if (settings.borderStyle !== undefined)
|
|
105
|
+
control.borderStyle = surface.borderStyle = settings.borderStyle
|
|
106
|
+
if (settings.elevation !== undefined) {
|
|
107
|
+
const level = settings.elevation
|
|
108
|
+
surface.shadow =
|
|
109
|
+
level === 0
|
|
110
|
+
? 'none'
|
|
111
|
+
: level === 1
|
|
112
|
+
? base.surface.shadow
|
|
113
|
+
: `0 ${level * 2}px ${level * 6}px -2px rgb(0 0 0 / ${mode === 'light' ? 0.12 : 0.4})`
|
|
114
|
+
surface.overlayShadow =
|
|
115
|
+
level === 0
|
|
116
|
+
? 'none'
|
|
117
|
+
: level === 1
|
|
118
|
+
? base.surface.overlayShadow
|
|
119
|
+
: `0 ${level * 12}px ${level * 32}px -16px rgb(0 0 0 / ${mode === 'light' ? 0.3 : 0.6})`
|
|
120
|
+
}
|
|
121
|
+
if (settings.backdropOpacity !== undefined)
|
|
122
|
+
surface.backdrop = `rgb(0 0 0 / ${settings.backdropOpacity})`
|
|
123
|
+
return {
|
|
124
|
+
...(Object.keys(spacing).length ? { spacing } : {}),
|
|
125
|
+
...(Object.keys(control).length ? { control } : {}),
|
|
126
|
+
...(Object.keys(surface).length ? { surface } : {})
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Apply generated color/canvas atomically and geometry leaves without erasing authored exceptions. */
|
|
131
|
+
export function updateThemeAppearance(
|
|
132
|
+
document: ThemeDocument,
|
|
133
|
+
settings: ThemeAppearanceSettings
|
|
134
|
+
): ThemeDocument {
|
|
135
|
+
record(settings, 'appearance')
|
|
136
|
+
keys(settings, ['version', 'palette', 'geometry'], 'appearance')
|
|
137
|
+
if (settings.version !== undefined && settings.version !== 1)
|
|
138
|
+
throw new TypeError('Unsupported appearance.version; expected 1.')
|
|
139
|
+
let next = defineThemeDocument(document)
|
|
140
|
+
if (settings.palette !== undefined) {
|
|
141
|
+
record(settings.palette, 'appearance.palette')
|
|
142
|
+
if (settings.palette.version !== 1) throw new TypeError('appearance.palette.version must be 1.')
|
|
143
|
+
record(settings.palette.color, 'appearance.palette.color')
|
|
144
|
+
record(settings.palette.surface, 'appearance.palette.surface')
|
|
145
|
+
next = updateThemeDocument(next, { color: settings.palette.color }, 'replace')
|
|
146
|
+
next = updateThemeDocument(next, { surface: settings.palette.surface })
|
|
147
|
+
}
|
|
148
|
+
const geometry = settings.geometry
|
|
149
|
+
if (geometry === undefined) return next
|
|
150
|
+
record(geometry, 'appearance.geometry')
|
|
151
|
+
keys(geometry, ['values', 'modes'], 'appearance.geometry')
|
|
152
|
+
if (geometry.values !== undefined) validate(geometry.values, 'appearance.geometry.values')
|
|
153
|
+
if (geometry.modes !== undefined) {
|
|
154
|
+
record(geometry.modes, 'appearance.geometry.modes')
|
|
155
|
+
keys(geometry.modes, ['light', 'dark'], 'appearance.geometry.modes')
|
|
156
|
+
}
|
|
157
|
+
for (const mode of ['light', 'dark'] as const) {
|
|
158
|
+
if (geometry.modes?.[mode] !== undefined)
|
|
159
|
+
validate(geometry.modes[mode], `appearance.geometry.modes.${mode}`)
|
|
160
|
+
const values = { ...geometry.values }
|
|
161
|
+
for (const [key, value] of Object.entries(geometry.modes?.[mode] ?? {}))
|
|
162
|
+
if (value !== undefined) Object.assign(values, { [key]: value })
|
|
163
|
+
const tokens = geometryTokens(values, mode)
|
|
164
|
+
next = updateThemeDocument(
|
|
165
|
+
next,
|
|
166
|
+
Object.fromEntries(
|
|
167
|
+
Object.entries(tokens).map(([group, value]) => [group, { modes: { [mode]: value } }])
|
|
168
|
+
)
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
return next
|
|
172
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import {
|
|
2
|
+
freezeTree,
|
|
3
|
+
typographyRoles,
|
|
4
|
+
typographyStyles,
|
|
5
|
+
typographyTokens
|
|
6
|
+
} from '../themes/tokens.js'
|
|
7
|
+
import type {
|
|
8
|
+
TypographyAssignment,
|
|
9
|
+
TypographyFont,
|
|
10
|
+
TypographyInput,
|
|
11
|
+
TypographyProfile,
|
|
12
|
+
TypographyProseRhythm,
|
|
13
|
+
TypographyResult
|
|
14
|
+
} from './types.js'
|
|
15
|
+
export type * from './types.js'
|
|
16
|
+
export { typographyRoles }
|
|
17
|
+
|
|
18
|
+
const inter = { family: 'Inter, system-ui, sans-serif', profile: 'inter' } as const
|
|
19
|
+
const serif = { family: '"Source Serif 4", Georgia, serif', profile: 'source-serif-4' } as const
|
|
20
|
+
const plex = { family: '"IBM Plex Sans", system-ui, sans-serif', profile: 'plex-sans' } as const
|
|
21
|
+
const mono = { family: '"IBM Plex Mono", ui-monospace, monospace', profile: 'plex-mono' } as const
|
|
22
|
+
export const typographyPresets = freezeTree({
|
|
23
|
+
inter: { name: 'Inter', fonts: { body: inter, headings: inter, ui: inter, code: mono } },
|
|
24
|
+
'source-serif': {
|
|
25
|
+
name: 'Source Serif 4 + Inter',
|
|
26
|
+
fonts: { body: serif, headings: serif, ui: inter, code: mono }
|
|
27
|
+
},
|
|
28
|
+
plex: {
|
|
29
|
+
name: 'Plex Sans + Plex Mono',
|
|
30
|
+
fonts: { body: plex, headings: plex, ui: plex, code: mono }
|
|
31
|
+
}
|
|
32
|
+
} as const)
|
|
33
|
+
const assignments: readonly TypographyAssignment[] = ['body', 'headings', 'ui', 'code']
|
|
34
|
+
const profiles: readonly TypographyProfile[] = [
|
|
35
|
+
'inter',
|
|
36
|
+
'source-serif-4',
|
|
37
|
+
'plex-sans',
|
|
38
|
+
'plex-mono',
|
|
39
|
+
'manual'
|
|
40
|
+
]
|
|
41
|
+
const bodyProfiles: Partial<Record<TypographyProfile, readonly [number, number, number]>> = {
|
|
42
|
+
inter: [20, 1.6, 64],
|
|
43
|
+
'source-serif-4': [21, 1.65, 62],
|
|
44
|
+
'plex-sans': [20, 1.6, 62]
|
|
45
|
+
}
|
|
46
|
+
const weights: Partial<Record<TypographyProfile, readonly [number, number]>> = {
|
|
47
|
+
inter: [100, 900],
|
|
48
|
+
'source-serif-4': [200, 900],
|
|
49
|
+
'plex-sans': [100, 700],
|
|
50
|
+
'plex-mono': [100, 700]
|
|
51
|
+
}
|
|
52
|
+
function record(value: unknown, path: string): void {
|
|
53
|
+
if (
|
|
54
|
+
!value ||
|
|
55
|
+
typeof value !== 'object' ||
|
|
56
|
+
Array.isArray(value) ||
|
|
57
|
+
![Object.prototype, null].includes(Object.getPrototypeOf(value))
|
|
58
|
+
)
|
|
59
|
+
throw new TypeError(`${path} must be a plain object.`)
|
|
60
|
+
}
|
|
61
|
+
function keys(value: object, allowed: readonly string[], path: string) {
|
|
62
|
+
for (const key of Object.keys(value))
|
|
63
|
+
if (!allowed.includes(key)) throw new TypeError(`Unknown key: ${path}.${key}.`)
|
|
64
|
+
}
|
|
65
|
+
function number(value: unknown, min: number, max: number, path: string) {
|
|
66
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < min || value > max)
|
|
67
|
+
throw new TypeError(`${path} must be a finite number from ${min} to ${max}.`)
|
|
68
|
+
}
|
|
69
|
+
function metrics(value: object, bounds: Record<string, readonly [number, number]>, path: string) {
|
|
70
|
+
record(value, path)
|
|
71
|
+
keys(value, Object.keys(bounds), path)
|
|
72
|
+
for (const [key, item] of Object.entries(value))
|
|
73
|
+
if (item !== undefined) number(item, ...bounds[key], `${path}.${key}`)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Pure, server-safe calculations. Input omits automatic metrics; output never loads a font. */
|
|
77
|
+
export function generateTypography(input: TypographyInput = {}): TypographyResult {
|
|
78
|
+
record(input, 'typography')
|
|
79
|
+
keys(input, ['preset', 'fonts', 'prose', 'ui', 'styles'], 'typography')
|
|
80
|
+
const preset = input.preset === undefined ? 'inter' : input.preset
|
|
81
|
+
if (typeof preset !== 'string' || !Object.hasOwn(typographyPresets, preset))
|
|
82
|
+
throw new TypeError('Unknown typography.preset.')
|
|
83
|
+
const fonts = { ...typographyPresets[preset].fonts } as Record<
|
|
84
|
+
TypographyAssignment,
|
|
85
|
+
TypographyFont
|
|
86
|
+
>
|
|
87
|
+
if (input.fonts !== undefined) {
|
|
88
|
+
record(input.fonts, 'typography.fonts')
|
|
89
|
+
keys(input.fonts, assignments, 'typography.fonts')
|
|
90
|
+
for (const key of assignments) {
|
|
91
|
+
const font = input.fonts[key]
|
|
92
|
+
if (font === undefined) continue
|
|
93
|
+
record(font, `typography.fonts.${key}`)
|
|
94
|
+
keys(font, ['family', 'profile'], `typography.fonts.${key}`)
|
|
95
|
+
if (
|
|
96
|
+
typeof font.family !== 'string' ||
|
|
97
|
+
!font.family.trim() ||
|
|
98
|
+
/[;{}@]|url\s*\(/i.test(font.family) ||
|
|
99
|
+
[...font.family].some(
|
|
100
|
+
character => character.charCodeAt(0) < 32 || character.charCodeAt(0) === 127
|
|
101
|
+
)
|
|
102
|
+
)
|
|
103
|
+
throw new TypeError(`typography.fonts.${key}.family must be a CSS font-family list.`)
|
|
104
|
+
if (font.profile !== undefined && !profiles.includes(font.profile))
|
|
105
|
+
throw new TypeError(`Unknown typography.fonts.${key}.profile.`)
|
|
106
|
+
fonts[key] = { family: font.family, profile: font.profile ?? 'manual' }
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const manualProse = input.prose === undefined ? {} : input.prose,
|
|
110
|
+
manualUI = input.ui === undefined ? {} : input.ui
|
|
111
|
+
metrics(
|
|
112
|
+
manualProse,
|
|
113
|
+
{
|
|
114
|
+
size: [12, 40],
|
|
115
|
+
lineHeight: [1, 3],
|
|
116
|
+
paragraphSpacing: [0, 4],
|
|
117
|
+
measure: [20, 120],
|
|
118
|
+
headingScale: [1.1, 1.5]
|
|
119
|
+
},
|
|
120
|
+
'typography.prose'
|
|
121
|
+
)
|
|
122
|
+
metrics(manualUI, { size: [12, 32], lineHeight: [1, 3] }, 'typography.ui')
|
|
123
|
+
const profile = bodyProfiles[fonts.body.profile ?? 'manual']
|
|
124
|
+
const prose: TypographyProseRhythm = {
|
|
125
|
+
size: profile?.[0] ?? 20,
|
|
126
|
+
lineHeight: profile?.[1] ?? 1.65,
|
|
127
|
+
measure: profile?.[2] ?? 64,
|
|
128
|
+
paragraphSpacing: 1.25,
|
|
129
|
+
headingScale: 1.25,
|
|
130
|
+
...defined(manualProse)
|
|
131
|
+
}
|
|
132
|
+
const ui = { size: 16, lineHeight: 1.5, ...defined(manualUI) }
|
|
133
|
+
const styles = typographyStyles(prose, ui)
|
|
134
|
+
const overrides = input.styles === undefined ? {} : input.styles
|
|
135
|
+
record(overrides, 'typography.styles')
|
|
136
|
+
keys(overrides, typographyRoles, 'typography.styles')
|
|
137
|
+
for (const role of typographyRoles) {
|
|
138
|
+
const override = overrides[role]
|
|
139
|
+
if (override === undefined) continue
|
|
140
|
+
record(override, `typography.styles.${role}`)
|
|
141
|
+
const { assignment, ...numeric } = override
|
|
142
|
+
if (assignment !== undefined && !assignments.includes(assignment))
|
|
143
|
+
throw new TypeError(`Unknown typography.styles.${role}.assignment.`)
|
|
144
|
+
metrics(
|
|
145
|
+
numeric,
|
|
146
|
+
{
|
|
147
|
+
narrow: [8, 256],
|
|
148
|
+
wide: [8, 512],
|
|
149
|
+
weight: [1, 1000],
|
|
150
|
+
lineHeight: [0.8, 3],
|
|
151
|
+
tracking: [-0.2, 0.5],
|
|
152
|
+
measure: [0, 120]
|
|
153
|
+
},
|
|
154
|
+
`typography.styles.${role}`
|
|
155
|
+
)
|
|
156
|
+
styles[role] = { ...styles[role], ...defined(override) }
|
|
157
|
+
if (styles[role].wide < styles[role].narrow)
|
|
158
|
+
throw new TypeError(`typography.styles.${role}.wide must be at least narrow.`)
|
|
159
|
+
}
|
|
160
|
+
const warnings: TypographyResult['warnings'][number][] = []
|
|
161
|
+
for (const role of typographyRoles) {
|
|
162
|
+
const s = styles[role],
|
|
163
|
+
range = weights[fonts[s.assignment].profile ?? 'manual']
|
|
164
|
+
if (range && (s.weight < range[0] || s.weight > range[1]))
|
|
165
|
+
warnings.push({ role, requestedWeight: s.weight, supportedWeights: range })
|
|
166
|
+
}
|
|
167
|
+
return freezeTree({
|
|
168
|
+
input: {
|
|
169
|
+
preset,
|
|
170
|
+
fonts,
|
|
171
|
+
prose: defined(manualProse),
|
|
172
|
+
ui: defined(manualUI),
|
|
173
|
+
styles: Object.fromEntries(
|
|
174
|
+
Object.entries(overrides)
|
|
175
|
+
.filter(([, v]) => v !== undefined)
|
|
176
|
+
.map(([k, v]) => [k, defined(v!)])
|
|
177
|
+
)
|
|
178
|
+
},
|
|
179
|
+
prose,
|
|
180
|
+
ui,
|
|
181
|
+
styles,
|
|
182
|
+
tokens: typographyTokens(
|
|
183
|
+
styles,
|
|
184
|
+
Object.fromEntries(assignments.map(key => [key, fonts[key].family])) as Record<
|
|
185
|
+
TypographyAssignment,
|
|
186
|
+
string
|
|
187
|
+
>,
|
|
188
|
+
prose.paragraphSpacing
|
|
189
|
+
),
|
|
190
|
+
recommendations: {
|
|
191
|
+
body: !!profile,
|
|
192
|
+
ui: fonts.ui.profile === 'inter' || fonts.ui.profile === 'plex-sans'
|
|
193
|
+
},
|
|
194
|
+
warnings
|
|
195
|
+
})
|
|
196
|
+
}
|
|
197
|
+
function defined<T extends object>(value: T): T {
|
|
198
|
+
return Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined)) as T
|
|
199
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** Stable text styles; presentation is independent of the element's HTML semantics. */
|
|
2
|
+
export type TypographyRole =
|
|
3
|
+
| 'prose-title'
|
|
4
|
+
| 'prose-heading'
|
|
5
|
+
| 'prose-subheading'
|
|
6
|
+
| 'prose-body'
|
|
7
|
+
| 'prose-lead'
|
|
8
|
+
| 'prose-small'
|
|
9
|
+
| 'prose-quote'
|
|
10
|
+
| 'ui-title'
|
|
11
|
+
| 'ui-body'
|
|
12
|
+
| 'ui-label'
|
|
13
|
+
| 'ui-supporting'
|
|
14
|
+
| 'ui-caption'
|
|
15
|
+
| 'ui-metric'
|
|
16
|
+
| 'ui-eyebrow'
|
|
17
|
+
| 'display'
|
|
18
|
+
| 'code'
|
|
19
|
+
|
|
20
|
+
export type TypographyAssignment = 'body' | 'headings' | 'ui' | 'code'
|
|
21
|
+
export type TypographyProfile = 'inter' | 'source-serif-4' | 'plex-sans' | 'plex-mono' | 'manual'
|
|
22
|
+
export type TypographyPreset = 'inter' | 'source-serif' | 'plex'
|
|
23
|
+
|
|
24
|
+
export interface TypographyFont {
|
|
25
|
+
/** CSS font-family list; choosing it never loads a font. */
|
|
26
|
+
readonly family: string
|
|
27
|
+
/** Explicit provenance of a studied font. Omitted for an unfamiliar/manual font. */
|
|
28
|
+
readonly profile?: TypographyProfile
|
|
29
|
+
}
|
|
30
|
+
export interface TypographyProseRhythm {
|
|
31
|
+
/** Reference pixels at a 16px root, emitted as rem. */
|
|
32
|
+
readonly size: number
|
|
33
|
+
readonly lineHeight: number
|
|
34
|
+
readonly paragraphSpacing: number
|
|
35
|
+
readonly measure: number
|
|
36
|
+
readonly headingScale: number
|
|
37
|
+
}
|
|
38
|
+
export interface TypographyUIRhythm {
|
|
39
|
+
readonly size: number
|
|
40
|
+
readonly lineHeight: number
|
|
41
|
+
}
|
|
42
|
+
export interface TypographyStyle {
|
|
43
|
+
readonly assignment: TypographyAssignment
|
|
44
|
+
/** Reference pixels at a 16px root. */
|
|
45
|
+
readonly narrow: number
|
|
46
|
+
readonly wide: number
|
|
47
|
+
readonly weight: number
|
|
48
|
+
readonly lineHeight: number
|
|
49
|
+
/** em; zero is deliberately not the old global negative tracking. */
|
|
50
|
+
readonly tracking: number
|
|
51
|
+
/** ch; zero means the layout owns measure. */
|
|
52
|
+
readonly measure: number
|
|
53
|
+
}
|
|
54
|
+
export interface TypographyInput {
|
|
55
|
+
readonly preset?: TypographyPreset
|
|
56
|
+
readonly fonts?: Readonly<Partial<Record<TypographyAssignment, TypographyFont>>>
|
|
57
|
+
/** Only manual settings belong here; omission keeps a profile-derived default live. */
|
|
58
|
+
readonly prose?: Partial<TypographyProseRhythm>
|
|
59
|
+
readonly ui?: Partial<TypographyUIRhythm>
|
|
60
|
+
readonly styles?: Readonly<Partial<Record<TypographyRole, Partial<TypographyStyle>>>>
|
|
61
|
+
}
|
|
62
|
+
export interface TypographyRoleTokens {
|
|
63
|
+
readonly family: string
|
|
64
|
+
readonly size: string
|
|
65
|
+
readonly fluidSize: string
|
|
66
|
+
readonly weight: number | string
|
|
67
|
+
readonly lineHeight: string
|
|
68
|
+
readonly letterSpacing: string
|
|
69
|
+
readonly measure: string
|
|
70
|
+
}
|
|
71
|
+
export interface TypographyTokens {
|
|
72
|
+
readonly roles: Readonly<Record<TypographyRole, TypographyRoleTokens>>
|
|
73
|
+
readonly flow: {
|
|
74
|
+
readonly paragraph: string
|
|
75
|
+
readonly headingBefore: string
|
|
76
|
+
readonly headingAfter: string
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export interface TypographyResult {
|
|
80
|
+
/** Replayable input retains manual/automatic ownership rather than pinning computed defaults. */
|
|
81
|
+
readonly input: TypographyInput
|
|
82
|
+
readonly prose: TypographyProseRhythm
|
|
83
|
+
readonly ui: TypographyUIRhythm
|
|
84
|
+
readonly styles: Readonly<Record<TypographyRole, TypographyStyle>>
|
|
85
|
+
readonly tokens: TypographyTokens
|
|
86
|
+
readonly recommendations: { readonly body: boolean; readonly ui: boolean }
|
|
87
|
+
readonly warnings: readonly {
|
|
88
|
+
readonly role: TypographyRole
|
|
89
|
+
readonly requestedWeight: number
|
|
90
|
+
readonly supportedWeights: readonly [number, number]
|
|
91
|
+
}[]
|
|
92
|
+
}
|