@astl/tokens 0.0.0 → 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.
@@ -0,0 +1,204 @@
1
+ /**
2
+ * The built-in style templates, for `definePlayer({ styleTemplates })`, `createEngine({ styleTemplates })` and
3
+ * `createAuthoring({ styleTemplates })`. Each sets the whole light and dark colour scheme (so any document template
4
+ * combined with any component template keeps every contrast pair) and passes `checkStyleTemplate`.
5
+ */
6
+ import en from './messages/en.json' with { type: 'json' };
7
+ import pl from './messages/pl.json' with { type: 'json' };
8
+ import type { StyleTemplateShape, TemplateCatalog, TemplateTheme } from './templates.js';
9
+ import { PALETTE, tokens } from './tokens.js';
10
+
11
+ /**
12
+ * The group palette of a theme. The built-in looks keep it as it is: it passes on their backgrounds too, and groups
13
+ * keep their colours from one look to another.
14
+ */
15
+ function palette(theme: TemplateTheme): Readonly<Record<string, string>> {
16
+ const colors = tokens.themes[theme];
17
+ return Object.fromEntries(
18
+ PALETTE.flatMap((color) => [
19
+ [color, colors[color]],
20
+ [`${color}-soft`, colors[`${color}-soft`]],
21
+ ]),
22
+ );
23
+ }
24
+
25
+ /** Label and description of one template from the flat catalogs (`<name>.label`, `<name>.description`). */
26
+ function catalogs(name: string): readonly TemplateCatalog[] {
27
+ const of = (locale: string, messages: Readonly<Record<string, string>>): TemplateCatalog => ({
28
+ namespace: `astl.style.${name}`,
29
+ locale,
30
+ messages: { label: messages[`${name}.label`] ?? '', description: messages[`${name}.description`] ?? '' },
31
+ });
32
+ return [of('en', en), of('pl', pl)];
33
+ }
34
+
35
+ const ROUNDED =
36
+ 'ui-rounded, "SF Pro Rounded", "Hiragino Maru Gothic ProN", Nunito, "Varela Round", system-ui, sans-serif';
37
+ /** Headings only: a bold rounded face that ships with macOS and Office, else the rounded stack. */
38
+ const ROUNDED_BOLD =
39
+ 'ui-rounded, "Arial Rounded MT Bold", "SF Pro Rounded", Nunito, "Varela Round", system-ui, sans-serif';
40
+ const BOOK = '"Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif';
41
+
42
+ /** The built-in types with a `prompt` part: the notebook look sets their questions in the book face. */
43
+ const PROMPTS = [
44
+ 'astl-categorise',
45
+ 'astl-choice',
46
+ 'astl-crossword',
47
+ 'astl-hotspot',
48
+ 'astl-image-drop',
49
+ 'astl-image-label',
50
+ 'astl-long-answer',
51
+ 'astl-mark-words',
52
+ 'astl-match',
53
+ 'astl-memory',
54
+ 'astl-order',
55
+ 'astl-true-false',
56
+ 'astl-word-search',
57
+ ];
58
+
59
+ /** The "funny" look: rounder, bolder, more colourful, but calm enough for reading. */
60
+ const playful: StyleTemplateShape = {
61
+ name: 'playful',
62
+ version: 1,
63
+ scopes: ['document', 'component'],
64
+ messages: 'astl.style.playful',
65
+ catalogs: catalogs('playful'),
66
+ tokens: {
67
+ 'font-body': ROUNDED,
68
+ 'font-display': ROUNDED_BOLD,
69
+ 'radius-s': '6px',
70
+ 'radius-m': '16px',
71
+ 'radius-l': '24px',
72
+ 'outline-width': '3px',
73
+ },
74
+ light: {
75
+ canvas: '#f5f0ff',
76
+ surface: '#ffffff',
77
+ 'surface-raised': '#ffffff',
78
+ ink: '#1f1830',
79
+ 'ink-muted': '#544a6b',
80
+ line: '#d8ccf3',
81
+ outline: '#7556b8',
82
+ accent: '#7428c9',
83
+ 'on-accent': '#ffffff',
84
+ 'accent-soft': '#f0e6ff',
85
+ focus: '#b3136a',
86
+ success: '#17733f',
87
+ 'success-soft': '#e3f5ea',
88
+ 'on-success': '#ffffff',
89
+ danger: '#b3261e',
90
+ 'danger-soft': '#fdebea',
91
+ warning: '#8a4b00',
92
+ 'warning-soft': '#fff0db',
93
+ selected: '#1f5bc6',
94
+ 'selected-soft': '#e8effc',
95
+ ...palette('light'),
96
+ },
97
+ dark: {
98
+ canvas: '#150f1f',
99
+ surface: '#211830',
100
+ 'surface-raised': '#2a1f3d',
101
+ ink: '#f3eeff',
102
+ 'ink-muted': '#c3b6de',
103
+ line: '#45385e',
104
+ outline: '#a492cf',
105
+ accent: '#c7a2ff',
106
+ 'on-accent': '#1c0b36',
107
+ 'accent-soft': '#33214f',
108
+ focus: '#ff8fd0',
109
+ success: '#70d498',
110
+ 'success-soft': '#15301f',
111
+ 'on-success': '#06200f',
112
+ danger: '#ff8f85',
113
+ 'danger-soft': '#3d1716',
114
+ warning: '#f6bf66',
115
+ 'warning-soft': '#3a290d',
116
+ selected: '#8fb8ff',
117
+ 'selected-soft': '#16284a',
118
+ ...palette('dark'),
119
+ },
120
+ parts: {
121
+ ...Object.fromEntries(
122
+ ['astl-choice', 'astl-true-false', 'astl-match'].map((tag) => [
123
+ tag,
124
+ { option: { 'box-shadow': '0 3px 0 var(--astl-line)' } },
125
+ ]),
126
+ ),
127
+ // The dropdown's list lifts like the cards, and its option in focus rounds. Its own parts only: a select's `option`
128
+ // is also a button of the buttons variant, whose pick ring is an inset shadow.
129
+ 'astl-select': {
130
+ listbox: { 'box-shadow': '0 3px 0 var(--astl-line)' },
131
+ 'option-active': { 'border-radius': 'var(--astl-radius-s)' },
132
+ },
133
+ },
134
+ };
135
+
136
+ /** Paper-like: cream sheets on a kraft desk, ink-blue text, a book face for questions, small corners. */
137
+ const notebook: StyleTemplateShape = {
138
+ name: 'notebook',
139
+ version: 1,
140
+ scopes: ['document', 'component'],
141
+ messages: 'astl.style.notebook',
142
+ catalogs: catalogs('notebook'),
143
+ tokens: {
144
+ 'font-display': BOOK,
145
+ 'radius-s': '2px',
146
+ 'radius-m': '3px',
147
+ 'radius-l': '4px',
148
+ 'radius-pill': '4px',
149
+ // Printed exercises separate the words to choose from: "mat / hat".
150
+ 'separator-display': 'inline',
151
+ },
152
+ light: {
153
+ canvas: '#ece5d4',
154
+ surface: '#fffdf5',
155
+ 'surface-raised': '#fffbee',
156
+ ink: '#1c2333',
157
+ 'ink-muted': '#485166',
158
+ line: '#b4cbea',
159
+ outline: '#5c6c8c',
160
+ accent: '#1c4ba3',
161
+ 'on-accent': '#ffffff',
162
+ 'accent-soft': '#e5edfa',
163
+ focus: '#2152b0',
164
+ success: '#1d6b3a',
165
+ 'success-soft': '#e6f2e6',
166
+ 'on-success': '#ffffff',
167
+ danger: '#ad2a22',
168
+ 'danger-soft': '#fbe9e4',
169
+ warning: '#844b00',
170
+ 'warning-soft': '#fbeed6',
171
+ selected: '#6c3796',
172
+ 'selected-soft': '#f2e9f8',
173
+ ...palette('light'),
174
+ },
175
+ dark: {
176
+ canvas: '#12151b',
177
+ surface: '#1b1f27',
178
+ 'surface-raised': '#232833',
179
+ ink: '#e8ecf4',
180
+ 'ink-muted': '#adb5c6',
181
+ line: '#384760',
182
+ outline: '#8491a8',
183
+ accent: '#9bbfff',
184
+ 'on-accent': '#0c1a33',
185
+ 'accent-soft': '#1d2a43',
186
+ focus: '#a9c8ff',
187
+ success: '#76cf92',
188
+ 'success-soft': '#15301e',
189
+ 'on-success': '#06200f',
190
+ danger: '#ff8f85',
191
+ 'danger-soft': '#3b1816',
192
+ warning: '#f0be66',
193
+ 'warning-soft': '#382a10',
194
+ selected: '#d4abf2',
195
+ 'selected-soft': '#2d2340',
196
+ ...palette('dark'),
197
+ },
198
+ parts: Object.fromEntries(
199
+ PROMPTS.map((tag) => [tag, { prompt: { 'font-family': 'var(--astl-font-display)' } }]),
200
+ ),
201
+ };
202
+
203
+ /** `playful` and `notebook`, usable on documents and on every component. The default look is no template at all. */
204
+ export const builtInStyleTemplates: readonly StyleTemplateShape[] = [playful, notebook];
package/src/css.ts ADDED
@@ -0,0 +1,48 @@
1
+ import { type ColorScheme, type Theme, tokens } from './tokens.js';
2
+
3
+ /** `selector[attr]`, written `:host([attr])` for shadow hosts (`:host[attr]` never matches). */
4
+ function withAttribute(selector: string, attribute: string): string {
5
+ if (selector === ':host') return `:host(${attribute})`;
6
+ const host = /^:host\((.*)\)$/.exec(selector);
7
+ if (host) return `:host(${host[1]}${attribute})`;
8
+ return `${selector}${attribute}`;
9
+ }
10
+
11
+ function declarations(values: Readonly<Record<string, string>>): string {
12
+ return Object.entries(values)
13
+ .map(([name, value]) => ` --astl-${name}: ${value};\n`)
14
+ .join('');
15
+ }
16
+
17
+ function rule(selector: string, body: string): string {
18
+ return `${selector} {\n${body}}\n`;
19
+ }
20
+
21
+ function theme(name: Theme, scheme: 'light' | 'dark', values: ColorScheme): string {
22
+ return ` color-scheme: ${scheme};\n${declarations(values)}${declarations(tokens.themeScale[name])}`;
23
+ }
24
+
25
+ /**
26
+ * The token stylesheet for `selector` (comma-separated selectors allowed, `:host` included):
27
+ * light by default, `data-theme="dark" | "hc"` themes, `data-theme="auto"` following `prefers-color-scheme`,
28
+ * system colours under `forced-colors: active` and no motion under `prefers-reduced-motion: reduce`.
29
+ */
30
+ export function tokensCss(selector: string): string {
31
+ const parts = selector
32
+ .split(',')
33
+ .map((s) => s.trim())
34
+ .filter((s) => s !== '');
35
+ const base = parts.join(', ');
36
+ const themed = (value: string) => parts.map((s) => withAttribute(s, `[data-theme="${value}"]`)).join(', ');
37
+ // `[data-theme]` gives the overrides below the same specificity as the theme rules, so they win by order.
38
+ const any = `${base}, ${parts.map((s) => withAttribute(s, '[data-theme]')).join(', ')}`;
39
+ const { light, dark, hc } = tokens.themes;
40
+ return [
41
+ rule(base, `${declarations(tokens.scale)}${theme('light', 'light', light)}`),
42
+ rule(themed('dark'), theme('dark', 'dark', dark)),
43
+ rule(themed('hc'), theme('hc', 'light', hc)),
44
+ `@media (prefers-color-scheme: dark) {\n${rule(themed('auto'), theme('dark', 'dark', dark))}}\n`,
45
+ `@media (forced-colors: active) {\n${rule(any, declarations(tokens.forced))}}\n`,
46
+ `@media (prefers-reduced-motion: reduce) {\n${rule(any, ' --astl-duration-fast: 0ms;\n --astl-duration-normal: 0ms;\n')}}\n`,
47
+ ].join('');
48
+ }
package/src/index.ts ADDED
@@ -0,0 +1,32 @@
1
+ export { builtInStyleTemplates } from './built-in-templates.js';
2
+ export { tokensCss } from './css.js';
3
+ export type {
4
+ CheckStyleTemplateOptions,
5
+ StyleTemplateProblem,
6
+ StyleTemplateShape,
7
+ TemplateCatalog,
8
+ TemplateScope,
9
+ TemplateTheme,
10
+ } from './templates.js';
11
+ export {
12
+ checkStyleTemplate,
13
+ contrastRatio,
14
+ isSafeStyleValue,
15
+ PART_PROPERTIES,
16
+ TEMPLATE_NAME,
17
+ templateColors,
18
+ } from './templates.js';
19
+ export type {
20
+ ColorScheme,
21
+ ColorToken,
22
+ PaletteColor,
23
+ PaletteSlot,
24
+ PaletteToken,
25
+ ScaleToken,
26
+ Theme,
27
+ ThemeScaleToken,
28
+ ThemeSetting,
29
+ TokenName,
30
+ Tokens,
31
+ } from './tokens.js';
32
+ export { PALETTE, TEXT_PAIRS, THEMES, tokens, UI_PAIRS } from './tokens.js';
@@ -0,0 +1,6 @@
1
+ {
2
+ "playful.label": "Playful",
3
+ "playful.description": "Rounder shapes, bolder lines and brighter colours, still calm enough to read.",
4
+ "notebook.label": "Notebook",
5
+ "notebook.description": "Cream paper, ink-blue text and small corners, like a school notebook."
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "playful.label": "Zabawny",
3
+ "playful.description": "Bardziej zaokrąglone kształty, grubsze linie i żywsze kolory, a tekst nadal czyta się spokojnie.",
4
+ "notebook.label": "Zeszyt",
5
+ "notebook.description": "Kremowy papier, granatowy tekst i małe zaokrąglenia, jak w szkolnym zeszycie."
6
+ }