@allons-y/course-kit 0.0.0-stage → 0.2.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.
Files changed (137) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +90 -2
  3. package/assets/fonts/instrument-sans-italic-var.woff2 +0 -0
  4. package/assets/fonts/instrument-sans-var.woff2 +0 -0
  5. package/assets/fonts/pt-mono.woff2 +0 -0
  6. package/assets/scripts/copy.js +99 -0
  7. package/assets/scripts/deck.js +161 -0
  8. package/assets/scripts/email.js +19 -0
  9. package/assets/scripts/practice.js +545 -0
  10. package/assets/scripts/theme-panel.js +647 -0
  11. package/assets/scripts/theme-restore.js +17 -0
  12. package/assets/styles/base.css +2091 -0
  13. package/assets/styles/deck.css +968 -0
  14. package/assets/styles/font.css +36 -0
  15. package/assets/styles/notes.css +137 -0
  16. package/assets/styles/print.css +428 -0
  17. package/assets/styles/semantics.css +101 -0
  18. package/components/practice/check-controls.webc +50 -0
  19. package/components/practice/choice-list.webc +86 -0
  20. package/components/practice/cite-panel.webc +108 -0
  21. package/components/practice/practice-figure.webc +47 -0
  22. package/components/practice/practice-fill.webc +154 -0
  23. package/components/practice/practice-grid.webc +82 -0
  24. package/components/practice/practice-judge.webc +165 -0
  25. package/components/practice/practice-match.webc +178 -0
  26. package/components/practice/practice-question.webc +141 -0
  27. package/components/practice/practice-selfcheck.webc +40 -0
  28. package/components/practice/practice-terms.webc +95 -0
  29. package/components/practice/practice-tree.webc +91 -0
  30. package/components/practice/practice-worksheet.webc +369 -0
  31. package/components/site/copyright.webc +13 -0
  32. package/components/site/course-documents.webc +27 -0
  33. package/components/site/course-now.webc +393 -0
  34. package/components/site/course-summary.webc +136 -0
  35. package/components/site/demo-weeks.webc +91 -0
  36. package/components/site/glossary-entry.webc +31 -0
  37. package/components/site/glossary-panel.webc +1482 -0
  38. package/components/site/glossary.webc +45 -0
  39. package/components/site/grading.webc +50 -0
  40. package/components/site/instructor.webc +432 -0
  41. package/components/site/masthead.webc +147 -0
  42. package/components/site/nav-links.webc +174 -0
  43. package/components/site/policies.webc +17 -0
  44. package/components/site/practice-index.webc +31 -0
  45. package/components/site/schedule-overview.webc +145 -0
  46. package/components/site/schedule-weeks.webc +589 -0
  47. package/components/site/site-footer.webc +119 -0
  48. package/components/site/slide-timetable.webc +152 -0
  49. package/components/site/syllabus.webc +370 -0
  50. package/components/ui/arrow-link.webc +19 -0
  51. package/components/ui/assigned-list.webc +101 -0
  52. package/components/ui/callout.webc +11 -0
  53. package/components/ui/card-list.webc +124 -0
  54. package/components/ui/codepen-embed.webc +123 -0
  55. package/components/ui/download-button.webc +49 -0
  56. package/components/ui/eyebrow.webc +18 -0
  57. package/components/ui/icons/github.webc +6 -0
  58. package/components/ui/icons/google.webc +9 -0
  59. package/components/ui/link-list.webc +130 -0
  60. package/components/ui/link-section.webc +40 -0
  61. package/components/ui/meeting-card.webc +73 -0
  62. package/components/ui/meeting-decks.webc +38 -0
  63. package/components/ui/table-rubric.webc +251 -0
  64. package/components/ui/today-link.webc +43 -0
  65. package/components/ui/today-marker.webc +149 -0
  66. package/components/ui/week-block.webc +53 -0
  67. package/examples/starter/_data/course.json +20 -0
  68. package/examples/starter/_data/dates.json +16 -0
  69. package/examples/starter/_data/grading.json +6 -0
  70. package/examples/starter/_data/instructor.json +7 -0
  71. package/examples/starter/_data/policies.json +9 -0
  72. package/examples/starter/_data/scale.json +8 -0
  73. package/examples/starter/_data/schedule.json +22 -0
  74. package/examples/starter/_data/theme.json +17 -0
  75. package/examples/starter/content/index.webc +12 -0
  76. package/examples/starter/content/schedule.webc +12 -0
  77. package/examples/starter/content/slides/index.11tydata.js +5 -0
  78. package/examples/starter/content/slides/index.webc +12 -0
  79. package/examples/starter/content/slides/week-1-day-1.md +40 -0
  80. package/examples/starter/eleventy.config.js +8 -0
  81. package/glue/deck-notes.data.js +41 -0
  82. package/glue/deck-notes.webc +32 -0
  83. package/glue/lesson-day.data.js +17 -0
  84. package/glue/lesson-day.webc +0 -0
  85. package/glue/lesson-plans.data.js +13 -0
  86. package/glue/lesson-plans.webc +2 -0
  87. package/index.js +6 -0
  88. package/layouts/base.webc +79 -0
  89. package/layouts/deck.webc +46 -0
  90. package/layouts/demo.webc +16 -0
  91. package/layouts/document.webc +10 -0
  92. package/layouts/lesson-detail.webc +109 -0
  93. package/layouts/lesson.webc +71 -0
  94. package/layouts/notes.webc +23 -0
  95. package/layouts/worksheet.webc +52 -0
  96. package/lib/assets.js +42 -0
  97. package/lib/bundles.js +79 -0
  98. package/lib/check.js +110 -0
  99. package/lib/components.js +19 -0
  100. package/lib/copy.js +656 -0
  101. package/lib/data/build.js +22 -0
  102. package/lib/data/fonts.js +16 -0
  103. package/lib/data/glossary.js +304 -0
  104. package/lib/data/index.js +8 -0
  105. package/lib/data/lessons.js +173 -0
  106. package/lib/data/project.js +22 -0
  107. package/lib/data/read.js +61 -0
  108. package/lib/data/site.js +70 -0
  109. package/lib/data/term.js +80 -0
  110. package/lib/extensions.js +70 -0
  111. package/lib/files.js +14 -0
  112. package/lib/filters.js +584 -0
  113. package/lib/fit.js +106 -0
  114. package/lib/glue.js +26 -0
  115. package/lib/lesson-materials.js +66 -0
  116. package/lib/markdown.js +568 -0
  117. package/lib/minify.js +53 -0
  118. package/lib/paths.js +5 -0
  119. package/lib/pdf.js +134 -0
  120. package/lib/plugin.js +231 -0
  121. package/lib/plugins.js +34 -0
  122. package/lib/postcss.js +93 -0
  123. package/lib/theme-server.js +88 -0
  124. package/lib/theme.js +185 -0
  125. package/lib/worksheet-dialect.js +415 -0
  126. package/lib/worksheets.js +276 -0
  127. package/package.json +119 -4
  128. package/schemas/course.schema.json +161 -0
  129. package/schemas/dates.schema.json +165 -0
  130. package/schemas/grading.schema.json +59 -0
  131. package/schemas/instructor.schema.json +76 -0
  132. package/schemas/policies.schema.json +90 -0
  133. package/schemas/project.schema.json +209 -0
  134. package/schemas/scale.schema.json +68 -0
  135. package/schemas/schedule.schema.json +260 -0
  136. package/schemas/theme.schema.json +704 -0
  137. package/schemas/worksheet.schema.json +449 -0
@@ -0,0 +1,647 @@
1
+ /**
2
+ * Dev-only theme panel: preview color tokens on the live page and save them to
3
+ * `_data/theme.json` through the dev server. See docs/theme-panel.md.
4
+ */
5
+
6
+ const ENDPOINT = "/.course-kit/theme";
7
+
8
+ /** `t("color", { n: 2 })`: a `themePanel` string with its `{name}` placeholders filled. */
9
+ const t = (key, vars = {}) => window.ckCopy.t("themePanel", key, vars);
10
+ const STORE = "course-kit:theme-panel";
11
+
12
+ /** Plain-language groups for the most used tokens; every token is also under "Every color". */
13
+ const GROUPS = [
14
+ { legend: "brand", tokens: [["accent", "brandColor"], ["accent-text", "brandColorForText"], ["on-accent", "textOnBrandColor"], ["accent-secondary", "wayfindingColor"], ["on-accent-secondary", "textOnWayfindingColor"]] },
15
+ { legend: "page", tokens: [["page", "pageBackground"], ["surface", "cardsAndPanels"], ["border", "linesAndBorders"]] },
16
+ { legend: "text", tokens: [["text", "bodyText"], ["text-muted", "secondaryText"], ["link", "links"]] },
17
+ ];
18
+
19
+ /** Text and background pairs whose contrast is reported, shown beside the first token. */
20
+ const CONTRAST = [
21
+ ["text", "page", 4.5],
22
+ ["text-muted", "page", 4.5],
23
+ ["accent-text", "page", 4.5],
24
+ ["link", "page", 4.5],
25
+ ["on-accent", "accent", 4.5],
26
+ ];
27
+
28
+ const read = (storage, key, fallback) => {
29
+ try {
30
+ const value = storage.getItem(`${STORE}:${key}`);
31
+ return value === null ? fallback : JSON.parse(value);
32
+ } catch {
33
+ return fallback;
34
+ }
35
+ };
36
+ const write = (storage, key, value) => {
37
+ try {
38
+ if (value === undefined) storage.removeItem(`${STORE}:${key}`);
39
+ else storage.setItem(`${STORE}:${key}`, JSON.stringify(value));
40
+ } catch {
41
+ // storage blocked: the panel still works, it just forgets.
42
+ }
43
+ };
44
+
45
+ /** @param {unknown} value a color or { light, dark } */
46
+ const toCss = (value) => (typeof value === "string" ? value : `light-dark(${value.light}, ${value.dark})`);
47
+ const clone = (value) => structuredClone(value);
48
+ const hex = ([r, g, b]) => `#${[r, g, b].map((n) => n.toString(16).padStart(2, "0")).join("")}`;
49
+
50
+ /** WCAG relative luminance of 8-bit sRGB. */
51
+ const luminance = ([r, g, b]) => {
52
+ const channel = (c) => {
53
+ const s = c / 255;
54
+ return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
55
+ };
56
+ return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
57
+ };
58
+ const ratio = (a, b) => {
59
+ const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
60
+ return (hi + 0.05) / (lo + 0.05);
61
+ };
62
+ /** A translucent color painted over an opaque one. */
63
+ const over = ([r, g, b, a], [br, bg, bb]) => {
64
+ const t = a / 255;
65
+ return [Math.round(r * t + br * (1 - t)), Math.round(g * t + bg * (1 - t)), Math.round(b * t + bb * (1 - t)), 255];
66
+ };
67
+
68
+ const STYLES = `
69
+ :host {
70
+ all: initial;
71
+ color-scheme: light dark;
72
+ --tp-bg: light-dark(#fff, #1d1f27);
73
+ --tp-text: light-dark(#1d1f27, #eceef4);
74
+ --tp-muted: light-dark(#555b6e, #a9aec0);
75
+ --tp-line: light-dark(#c4c8d4, #454959);
76
+ --tp-focus: light-dark(#0b57d0, #8ab4f8);
77
+ --tp-good: light-dark(#1d6b3c, #86d7a4);
78
+ --tp-bad: light-dark(#a3261a, #ff9d8f);
79
+ --tp-gap: 0.75rem;
80
+ font: 0.875rem/1.45 system-ui, sans-serif;
81
+ }
82
+
83
+ * { box-sizing: border-box; }
84
+
85
+ :focus-visible {
86
+ outline: 2px solid var(--tp-focus);
87
+ outline-offset: 2px;
88
+ }
89
+
90
+ button, input, select { font: inherit; color: inherit; }
91
+
92
+ button {
93
+ min-block-size: 2rem;
94
+ min-inline-size: 2rem;
95
+ padding-inline: 0.75rem;
96
+ border: 1px solid var(--tp-line);
97
+ border-radius: 0.375rem;
98
+ background: var(--tp-bg);
99
+ cursor: pointer;
100
+ }
101
+
102
+ button:disabled { opacity: 0.5; cursor: not-allowed; }
103
+
104
+ [hidden] { display: none !important; }
105
+
106
+ .launcher {
107
+ position: fixed;
108
+ inset-inline-end: 0;
109
+ inset-block-start: 50%;
110
+ translate: 0 -50%;
111
+ z-index: 2147483646;
112
+ writing-mode: vertical-rl;
113
+ padding: 0.75rem 0.4rem;
114
+ border-inline-end: 0;
115
+ border-start-end-radius: 0;
116
+ border-end-end-radius: 0;
117
+ color: var(--tp-text);
118
+ box-shadow: 0 2px 8px rgb(0 0 0 / 20%);
119
+ }
120
+
121
+ .panel {
122
+ position: fixed;
123
+ inset: auto;
124
+ inset-inline-end: var(--tp-gap);
125
+ inset-block-start: 50%;
126
+ translate: 0 -50%;
127
+ margin: 0;
128
+ z-index: 2147483647;
129
+ inline-size: min(22rem, 100vw - 2 * var(--tp-gap));
130
+ max-block-size: calc(100dvh - 2 * var(--tp-gap));
131
+ overflow: auto;
132
+ overscroll-behavior: contain;
133
+ scrollbar-gutter: stable;
134
+ padding: 0;
135
+ border: 1px solid var(--tp-line);
136
+ border-radius: 0.5rem;
137
+ background: var(--tp-bg);
138
+ color: var(--tp-text);
139
+ box-shadow: 0 8px 28px rgb(0 0 0 / 25%);
140
+ container-type: inline-size;
141
+
142
+ opacity: 1;
143
+ transform: none;
144
+ transition-property: opacity, transform, display, overlay;
145
+ transition-duration: 0.2s;
146
+ transition-timing-function: ease-out;
147
+ transition-behavior: allow-discrete;
148
+ }
149
+
150
+ .panel:not([popover]) { display: block; }
151
+
152
+ .panel:popover-open {
153
+ @starting-style {
154
+ opacity: 0;
155
+ transform: translateX(1rem);
156
+ }
157
+ }
158
+
159
+ .panel[popover]:not(:popover-open) {
160
+ opacity: 0;
161
+ transform: translateX(1rem);
162
+ }
163
+
164
+ @media (width < 40rem) {
165
+ .panel {
166
+ inset-inline: 0;
167
+ inset-block: auto 0;
168
+ translate: none;
169
+ inline-size: 100%;
170
+ max-block-size: 70dvh;
171
+ border-end-start-radius: 0;
172
+ border-end-end-radius: 0;
173
+ padding-block-end: env(safe-area-inset-bottom);
174
+ }
175
+ }
176
+
177
+ .main > summary {
178
+ position: sticky;
179
+ inset-block-start: 0;
180
+ z-index: 1;
181
+ display: flex;
182
+ align-items: center;
183
+ gap: 0.5rem;
184
+ padding-block: 0.5rem;
185
+ padding-inline: 0.75rem 3rem;
186
+ background: var(--tp-bg);
187
+ border-block-end: 1px solid var(--tp-line);
188
+ cursor: pointer;
189
+ }
190
+
191
+ .main:not([open]) > summary { border-block-end: 0; }
192
+
193
+ h2 { margin: 0; font-size: 1rem; }
194
+
195
+ .flag { font-size: 0.75rem; color: var(--tp-muted); }
196
+
197
+ .close {
198
+ position: absolute;
199
+ inset-block-start: 0.375rem;
200
+ inset-inline-end: 0.375rem;
201
+ z-index: 2;
202
+ padding: 0;
203
+ }
204
+
205
+ .body {
206
+ display: grid;
207
+ gap: 0.75rem;
208
+ padding: 0.75rem;
209
+ }
210
+
211
+ fieldset {
212
+ margin: 0;
213
+ padding: 0.5rem 0.75rem 0.75rem;
214
+ border: 1px solid var(--tp-line);
215
+ border-radius: 0.375rem;
216
+ display: grid;
217
+ gap: 0.5rem;
218
+ }
219
+
220
+ legend { padding-inline: 0.25rem; font-weight: 600; }
221
+
222
+ .modes { display: flex; gap: 1rem; }
223
+
224
+ .row {
225
+ display: grid;
226
+ grid-template-columns: 2.25rem 1fr;
227
+ grid-template-areas: "swatch label" "swatch value" ". contrast";
228
+ column-gap: 0.5rem;
229
+ align-items: center;
230
+ }
231
+
232
+ .row label { grid-area: label; }
233
+ .row input[type="color"] {
234
+ grid-area: swatch;
235
+ inline-size: 2.25rem;
236
+ block-size: 2.25rem;
237
+ padding: 0;
238
+ border: 1px solid var(--tp-line);
239
+ border-radius: 0.375rem;
240
+ background: none;
241
+ forced-color-adjust: none;
242
+ }
243
+ .row input[type="text"] {
244
+ grid-area: value;
245
+ inline-size: 100%;
246
+ min-block-size: 1.75rem;
247
+ padding-inline: 0.375rem;
248
+ border: 1px solid var(--tp-line);
249
+ border-radius: 0.25rem;
250
+ background: var(--tp-bg);
251
+ font-family: ui-monospace, monospace;
252
+ font-size: 0.8125rem;
253
+ }
254
+ .row input[type="text"]:user-invalid { border-color: var(--tp-bad); }
255
+ .row output { grid-area: contrast; font-size: 0.75rem; }
256
+ .row output[data-pass="true"] { color: var(--tp-good); }
257
+ .row output[data-pass="false"] { color: var(--tp-bad); }
258
+ .role { grid-column: 2; font-size: 0.75rem; color: var(--tp-muted); }
259
+
260
+ .advanced > summary { cursor: pointer; font-weight: 600; }
261
+ .advanced[open] > summary { margin-block-end: 0.5rem; }
262
+ .advanced > div { display: grid; gap: 0.75rem; }
263
+
264
+ .stops { display: grid; gap: 0.5rem; }
265
+ .stop { display: flex; gap: 0.5rem; align-items: center; }
266
+
267
+ menu {
268
+ display: flex;
269
+ flex-wrap: wrap;
270
+ gap: 0.5rem;
271
+ margin: 0;
272
+ padding: 0;
273
+ list-style: none;
274
+ }
275
+
276
+ .save { font-weight: 600; }
277
+
278
+ .status { min-block-size: 1.25rem; font-size: 0.8125rem; }
279
+
280
+ .probe { position: absolute; inline-size: 0; block-size: 0; overflow: hidden; }
281
+
282
+ @container (inline-size < 16rem) {
283
+ .row { grid-template-columns: 1fr; grid-template-areas: "label" "swatch" "value" "contrast"; }
284
+ .role { grid-column: 1; }
285
+ }
286
+
287
+ @supports (interpolate-size: allow-keywords) {
288
+ :host { interpolate-size: allow-keywords; }
289
+ .main::details-content {
290
+ block-size: 0;
291
+ overflow: clip;
292
+ transition: block-size 0.2s ease-out, content-visibility 0.2s;
293
+ transition-behavior: allow-discrete;
294
+ }
295
+ .main[open]::details-content { block-size: auto; }
296
+ }
297
+
298
+ @media (prefers-reduced-motion: reduce) {
299
+ .panel, .panel:popover-open, .panel[popover]:not(:popover-open) { transform: none; transition-duration: 0.01s; }
300
+ .main::details-content { transition: none; }
301
+ }
302
+ `;
303
+
304
+ class ThemePanel extends HTMLElement {
305
+ async connectedCallback() {
306
+ if (this.shadowRoot) return;
307
+ let data;
308
+ try {
309
+ const response = await fetch(ENDPOINT, { cache: "no-store" });
310
+ if (!response.ok) throw new Error(String(response.status));
311
+ data = await response.json();
312
+ } catch {
313
+ // No dev server endpoint (a static build): there is nothing to save to.
314
+ this.remove();
315
+ return;
316
+ }
317
+ this.context = this.getAttribute("context") === "deck" ? "deck" : "site";
318
+ this.tokens = data.tokens.filter((token) => token[this.context]);
319
+ this.roles = Object.fromEntries(this.tokens.map((token) => [token.key, token.role]));
320
+ this.defaultGradient = data.gradient;
321
+ this.file = data.file;
322
+ this.saved = data.theme;
323
+ this.draft = read(sessionStorage, "draft", null) ?? clone(this.saved);
324
+ this.history = [];
325
+ this.render();
326
+ this.applyPreview();
327
+ this.refresh();
328
+ }
329
+
330
+ /** @returns {string} */
331
+ get mode() {
332
+ const forced = document.documentElement.style.colorScheme;
333
+ if (forced === "light" || forced === "dark") return forced;
334
+ return matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
335
+ }
336
+
337
+ render() {
338
+ const root = this.attachShadow({ mode: "open" });
339
+ const supportsPopover = "popover" in HTMLElement.prototype;
340
+ root.innerHTML = `
341
+ <style>${STYLES}</style>
342
+ <button type="button" class="launcher" ${supportsPopover ? 'popovertarget="panel" popovertargetaction="show"' : ""}>${t("theme")}</button>
343
+ <aside class="panel" id="panel" ${supportsPopover ? 'popover="manual"' : "hidden"} aria-labelledby="title">
344
+ <details class="main" open>
345
+ <summary><h2 id="title">${t("theme")}</h2><span class="flag" hidden>${t("unsavedChanges")}</span></summary>
346
+ <div class="body">
347
+ <fieldset class="modes-set">
348
+ <legend>${t("preview")}</legend>
349
+ <div class="modes">
350
+ <label><input type="radio" name="mode" value="light"> ${t("light")}</label>
351
+ <label><input type="radio" name="mode" value="dark"> ${t("dark")}</label>
352
+ </div>
353
+ </fieldset>
354
+ ${GROUPS.map((group) => this.fieldset(t(group.legend), group.tokens.filter(([key]) => this.roles[key]).map(([key, label]) => [key, t(label)]))).join("")}
355
+ <details class="advanced" name="theme-panel-section">
356
+ <summary>${t("everyColor")}</summary>
357
+ <div>
358
+ ${this.fieldset(t("allTokens"), this.tokens.map((token) => [token.key, token.role]), true)}
359
+ ${this.context === "site" ? `<fieldset><legend>${t("gradient")}</legend><div class="stops"></div><button type="button" data-action="add-stop">${t("addAColor")}</button></fieldset>` : ""}
360
+ </div>
361
+ </details>
362
+ <menu>
363
+ <li><button type="button" data-action="undo">${t("undo")}</button></li>
364
+ <li><button type="button" data-action="reset">${t("reset")}</button></li>
365
+ <li><button type="button" class="save" data-action="save">${t("save")}</button></li>
366
+ <li><button type="button" data-action="download">${t("download")}</button></li>
367
+ </menu>
368
+ <output class="status" role="status"></output>
369
+ </div>
370
+ </details>
371
+ <button type="button" class="close" aria-label="${t("hideTheThemePanel")}" ${supportsPopover ? 'popovertarget="panel" popovertargetaction="hide"' : ""}>&times;</button>
372
+ </aside>
373
+ <div class="probe" aria-hidden="true"></div>`;
374
+
375
+ this.panel = root.querySelector(".panel");
376
+ this.main = root.querySelector(".main");
377
+ this.status = root.querySelector(".status");
378
+
379
+ const open = read(localStorage, "open", false);
380
+ const expanded = read(localStorage, "expanded", true);
381
+ this.main.open = expanded;
382
+ if (open) this.show();
383
+
384
+ if (!supportsPopover) {
385
+ root.querySelector(".launcher").addEventListener("click", () => this.show());
386
+ root.querySelector(".close").addEventListener("click", () => this.hide());
387
+ }
388
+ const launcher = root.querySelector(".launcher");
389
+ this.panel.addEventListener("toggle", (event) => {
390
+ launcher.hidden = event.newState === "open";
391
+ write(localStorage, "open", event.newState === "open");
392
+ });
393
+ this.main.addEventListener("toggle", () => write(localStorage, "expanded", this.main.open));
394
+ this.panel.addEventListener("keydown", (event) => {
395
+ if (event.key === "Escape" && this.main.open) {
396
+ event.preventDefault();
397
+ this.main.open = false;
398
+ this.main.querySelector("summary").focus();
399
+ }
400
+ });
401
+
402
+ for (const radio of root.querySelectorAll('input[name="mode"]')) {
403
+ radio.checked = radio.value === this.mode;
404
+ radio.addEventListener("change", () => {
405
+ document.documentElement.style.colorScheme = radio.value;
406
+ this.refresh();
407
+ });
408
+ }
409
+
410
+ root.addEventListener("input", (event) => {
411
+ const input = event.target;
412
+ if (!input.dataset.key) return;
413
+ if (input.type === "color") this.set(input.dataset.key, input.value);
414
+ });
415
+ root.addEventListener("change", (event) => {
416
+ const input = event.target;
417
+ if (input.type !== "text") return;
418
+ if (input.dataset.stop !== undefined) return this.setStop(Number(input.dataset.stop), input.value.trim());
419
+ if (!input.dataset.key) return;
420
+ const value = input.value.trim();
421
+ input.setCustomValidity(CSS.supports("color", value) ? "" : t("notACssColor"));
422
+ if (input.checkValidity()) this.set(input.dataset.key, value);
423
+ else input.reportValidity();
424
+ });
425
+ root.addEventListener("click", (event) => {
426
+ const action = event.target.closest("[data-action]")?.dataset.action;
427
+ if (action) this[action === "add-stop" ? "addStop" : action === "remove-stop" ? "removeStop" : action](event.target.closest("[data-action]"));
428
+ });
429
+ }
430
+
431
+ /**
432
+ * @param {string} legend
433
+ * @param {[string, string][]} tokens key and label
434
+ * @param {boolean} [roles] label rows by token name and show the role
435
+ */
436
+ fieldset(legend, tokens, roles = false) {
437
+ const rows = tokens.map(([key, label]) => {
438
+ const id = `${roles ? "all" : "main"}-${key}`;
439
+ const name = roles ? `--color-${key}` : label;
440
+ return `<div class="row">
441
+ <label for="${id}">${name}</label>
442
+ <input type="color" id="${id}" data-key="${key}" title="${this.roles[key]}">
443
+ <input type="text" data-key="${key}" aria-label="${t("cssValue", { name })}" spellcheck="false" autocomplete="off">
444
+ ${roles ? `<span class="role">${label}</span>` : `<output for="${id}" data-contrast="${key}"></output>`}
445
+ </div>`;
446
+ });
447
+ return `<fieldset><legend>${legend}</legend>${rows.join("")}</fieldset>`;
448
+ }
449
+
450
+ show() {
451
+ if (this.panel.showPopover) this.panel.showPopover();
452
+ else this.panel.hidden = false;
453
+ this.shadowRoot.querySelector(".launcher").hidden = true;
454
+ write(localStorage, "open", true);
455
+ }
456
+
457
+ hide() {
458
+ if (this.panel.hidePopover) this.panel.hidePopover();
459
+ else this.panel.hidden = true;
460
+ this.shadowRoot.querySelector(".launcher").hidden = false;
461
+ write(localStorage, "open", false);
462
+ }
463
+
464
+ /** The scope a key is edited in: this context's section if it already holds the key, else the top level. */
465
+ scope(key) {
466
+ return this.draft[this.context] && key in this.draft[this.context] ? this.draft[this.context] : this.draft;
467
+ }
468
+
469
+ /** The resolved RGBA of a CSS color in one mode. */
470
+ resolve(value, mode) {
471
+ const probe = this.shadowRoot.querySelector(".probe");
472
+ probe.style.colorScheme = mode;
473
+ probe.style.color = "transparent";
474
+ probe.style.color = value;
475
+ const canvas = (this.canvas ??= new OffscreenCanvas(1, 1));
476
+ const context = (this.context2d ??= canvas.getContext("2d", { willReadFrequently: true }));
477
+ context.clearRect(0, 0, 1, 1);
478
+ context.fillStyle = getComputedStyle(probe).color;
479
+ context.fillRect(0, 0, 1, 1);
480
+ return [...context.getImageData(0, 0, 1, 1).data];
481
+ }
482
+
483
+ /** Set one token for the previewed mode, keeping the other mode as it currently looks. */
484
+ set(key, value) {
485
+ this.remember();
486
+ const scope = this.scope(key);
487
+ const other = this.mode === "light" ? "dark" : "light";
488
+ const current = scope[key];
489
+ const otherValue = current && typeof current === "object" ? current[other] : hex(this.resolve(`var(--color-${key})`, other));
490
+ scope[key] = this.mode === "light" ? { light: value, dark: otherValue } : { light: otherValue, dark: value };
491
+ this.changed();
492
+ }
493
+
494
+ gradient() {
495
+ return this.draft.gradient ?? this.defaultGradient;
496
+ }
497
+
498
+ setStop(index, value) {
499
+ if (!CSS.supports("color", value)) return;
500
+ this.remember();
501
+ const stops = clone(this.gradient());
502
+ stops[index] = value;
503
+ this.draft.gradient = stops;
504
+ this.changed();
505
+ }
506
+
507
+ addStop() {
508
+ const stops = clone(this.gradient());
509
+ if (stops.length >= 4) return;
510
+ this.remember();
511
+ this.draft.gradient = [...stops, stops.at(-1)];
512
+ this.changed();
513
+ }
514
+
515
+ removeStop(button) {
516
+ const stops = clone(this.gradient());
517
+ if (stops.length <= 1) return;
518
+ this.remember();
519
+ stops.splice(Number(button.dataset.stop), 1);
520
+ this.draft.gradient = stops;
521
+ this.changed();
522
+ }
523
+
524
+ remember() {
525
+ this.history.push(clone(this.draft));
526
+ }
527
+
528
+ undo() {
529
+ if (!this.history.length) return;
530
+ this.draft = this.history.pop();
531
+ this.changed();
532
+ }
533
+
534
+ reset() {
535
+ this.remember();
536
+ this.draft = clone(this.saved);
537
+ this.changed();
538
+ this.say(t("backToTheSaved"));
539
+ }
540
+
541
+ changed() {
542
+ write(sessionStorage, "draft", this.dirty ? this.draft : undefined);
543
+ this.applyPreview();
544
+ this.refresh();
545
+ }
546
+
547
+ get dirty() {
548
+ return JSON.stringify(this.draft) !== JSON.stringify(this.saved);
549
+ }
550
+
551
+ /** Write the draft onto the page as inline custom properties. */
552
+ applyPreview() {
553
+ const style = document.documentElement.style;
554
+ for (const token of this.tokens) {
555
+ const value = this.draft[this.context]?.[token.key] ?? this.draft[token.key];
556
+ if (value === undefined) style.removeProperty(`--color-${token.key}`);
557
+ else style.setProperty(`--color-${token.key}`, toCss(value));
558
+ }
559
+ if (this.context === "site") {
560
+ const stops = this.gradient();
561
+ for (let index = 0; index < 4; index++) {
562
+ if (index < stops.length) style.setProperty(`--color-gradient-${index + 1}`, toCss(stops[index]));
563
+ else style.removeProperty(`--color-gradient-${index + 1}`);
564
+ }
565
+ const names = stops.map((_, index) => `var(--color-gradient-${index + 1})`);
566
+ if (names.length === 1) names.push(names[0]);
567
+ style.setProperty("--gradient-stops", names.join(", "));
568
+ }
569
+ }
570
+
571
+ /** Update every input, contrast result and button to match the draft and the previewed mode. */
572
+ refresh() {
573
+ const root = this.shadowRoot;
574
+ const mode = this.mode;
575
+ for (const radio of root.querySelectorAll('input[name="mode"]')) radio.checked = radio.value === mode;
576
+ for (const input of root.querySelectorAll("input[data-key]")) {
577
+ const key = input.dataset.key;
578
+ const rgba = this.resolve(`var(--color-${key})`, mode);
579
+ if (input.type === "color") input.value = hex(rgba);
580
+ else if (input !== root.activeElement) {
581
+ const value = this.scope(key)[key];
582
+ input.value = value === undefined ? (rgba[3] === 255 ? hex(rgba) : `rgb(${rgba.slice(0, 3).join(" ")} / ${(rgba[3] / 255).toFixed(2)})`) : typeof value === "string" ? value : value[mode];
583
+ input.setCustomValidity("");
584
+ }
585
+ }
586
+ for (const output of root.querySelectorAll("output[data-contrast]")) {
587
+ const pair = CONTRAST.find(([text]) => text === output.dataset.contrast);
588
+ if (!pair || !this.roles[pair[1]]) {
589
+ output.textContent = "";
590
+ continue;
591
+ }
592
+ const [text, background, minimum] = pair;
593
+ const back = over(this.resolve(`var(--color-${background})`, mode), [255, 255, 255]);
594
+ const value = ratio(over(this.resolve(`var(--color-${text})`, mode), back), back);
595
+ const pass = value >= minimum;
596
+ output.dataset.pass = String(pass);
597
+ output.textContent = t("contrast", { ratio: value.toFixed(1), ground: background === "page" ? t("thePage") : t("theBrandColor"), verdict: pass ? t("passes") : t("below", { minimum }) });
598
+ }
599
+ const stops = root.querySelector(".stops");
600
+ if (stops) {
601
+ const list = this.gradient();
602
+ stops.innerHTML = list
603
+ .map(
604
+ (stop, index) => `<div class="stop">
605
+ <label for="stop-${index}">${t("color", { n: index + 1 })}</label>
606
+ <input type="text" id="stop-${index}" data-stop="${index}" value="${toCss(stop)}" spellcheck="false" autocomplete="off">
607
+ <button type="button" data-action="remove-stop" data-stop="${index}" aria-label="${t("removeColor", { n: index + 1 })}" ${list.length <= 1 ? "disabled" : ""}>&minus;</button>
608
+ </div>`,
609
+ )
610
+ .join("");
611
+ root.querySelector('[data-action="add-stop"]').disabled = list.length >= 4;
612
+ }
613
+ root.querySelector('[data-action="undo"]').disabled = !this.history.length;
614
+ root.querySelector('[data-action="reset"]').disabled = !this.dirty;
615
+ root.querySelector(".flag").hidden = !this.dirty;
616
+ }
617
+
618
+ async save() {
619
+ this.say(t("saving"));
620
+ try {
621
+ const response = await fetch(ENDPOINT, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(this.draft) });
622
+ const result = await response.json();
623
+ if (!response.ok) throw new Error(result.error);
624
+ this.saved = clone(this.draft);
625
+ this.history = [];
626
+ write(sessionStorage, "draft", undefined);
627
+ this.refresh();
628
+ this.say(t("savedToThe", { file: result.file }));
629
+ } catch (error) {
630
+ this.say(t("notSaved", { message: error.message }));
631
+ }
632
+ }
633
+
634
+ download() {
635
+ const blob = new Blob([`${JSON.stringify(this.draft, null, "\t")}\n`], { type: "application/json" });
636
+ const link = Object.assign(document.createElement("a"), { href: URL.createObjectURL(blob), download: "theme.json" });
637
+ link.click();
638
+ URL.revokeObjectURL(link.href);
639
+ this.say(t("downloadedPutItIn", { folder: this.file.replace(/theme\.json$/, "") }));
640
+ }
641
+
642
+ say(message) {
643
+ this.status.textContent = message;
644
+ }
645
+ }
646
+
647
+ if (!customElements.get("theme-panel")) customElements.define("theme-panel", ThemePanel);
@@ -0,0 +1,17 @@
1
+ /*
2
+ * The deck's saved light/dark choice, applied before first paint so a deck
3
+ * never flashes the wrong scheme. Only the deck layout inlines this, through
4
+ * the `head` bundle bucket; the site and worksheets follow the system.
5
+ */
6
+ try {
7
+ const theme = localStorage.getItem("theme");
8
+ if (theme === "light" || theme === "dark") {
9
+ document.documentElement.style.colorScheme = theme;
10
+ document.documentElement.dataset.theme = theme;
11
+ for (const link of document.querySelectorAll("link[data-prism-theme]")) {
12
+ link.media = link.dataset.prismTheme === theme ? "all" : "not all";
13
+ }
14
+ }
15
+ } catch {
16
+ // Storage can be unavailable (private mode, blocked cookies); follow system.
17
+ }