@tjakoen/grain 0.1.13 → 0.1.15

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/README.md CHANGED
@@ -181,6 +181,11 @@ data-attributes, no composition-root wiring and no AI layer required:
181
181
  trigger (if it's an `<a href>`, the href is the full image, so it stays a real link with no JS);
182
182
  a `[data-lightbox-group]` ancestor turns its triggers into one gallery with prev/next + a dot rail.
183
183
  Caption comes from `data-lightbox-caption` or the child `<img>` alt.
184
+ - **`scripts/presentation.js`** + the `presentation` organism: a deck. Slides, staged fragments, a
185
+ fit-to-box pass so a dense slide scales instead of overflowing, an overview grid, present mode, a
186
+ synced presenter window, and a print sheet. Opt in with `data-deck`. It owns which slide is up and
187
+ how far into it you are, and nothing else: an app hangs its own figures off one event,
188
+ `presentation:slide`.
184
189
  - **`scripts/drawer.js`** + the `drawer` organism: the modal side panel. `[data-drawer-open="id"]`
185
190
  opens, `[data-drawer-close]` (the scrim carries it) closes, and the script owns the modal
186
191
  obligations — focus into the panel, Tab trapped, the rest of the page `inert`, focus returned to
@@ -0,0 +1,62 @@
1
+ // grain/components/doc-links.test.ts — a doc example must not invent a route.
2
+ //
3
+ // Why this exists. The catalog renders these examples as real markup, so a root-absolute href in a
4
+ // component doc becomes a real link on every consumer that mounts the catalog. Point one at a page
5
+ // that only exists in the example's imagination and you have handed every consumer a dead link,
6
+ // which their export walk will find long after the person who wrote the example has left.
7
+ //
8
+ // It happened: /clients/acme, /clients/borden and /tickets shipped in the admin-surface docs and
9
+ // broke the portfolio's Pages deploy the first time it consumed that version. The fix was to point
10
+ // fictional destinations at in-page anchors. This test is that fix, made mechanical.
11
+ //
12
+ // A doc may still link a route the REFERENCE CONSUMER really serves (the portfolio), and those are
13
+ // listed below, deliberately, one by one.
14
+ import { test, expect } from "bun:test";
15
+ import { readdirSync, readFileSync, statSync } from "node:fs";
16
+ import { join } from "node:path";
17
+
18
+ const here = new URL(".", import.meta.url).pathname;
19
+
20
+ // Two kinds of allowed reference, and the difference matters.
21
+ //
22
+ // Assets GRAIN itself ships, at the mount points a consumer wires up (/assets, /scripts). These
23
+ // resolve for anyone who mounted the package the way the README says to.
24
+ const SHIPPED_ASSETS = new Set([
25
+ "/assets/figure-sample.svg",
26
+ "/scripts/terminal.js",
27
+ ]);
28
+
29
+ // Routes the REFERENCE CONSUMER really serves. Adding one is a decision: it means every consumer
30
+ // mounting grain's catalog is expected to serve that path too, or accept a dead link on their site.
31
+ const REAL_ROUTES = new Set([
32
+ "/",
33
+ "/calendar",
34
+ "/grain",
35
+ "/notes",
36
+ "/notes/ten-times-zero",
37
+ "/notes/the-browser-grew-up",
38
+ "/notes/the-browser-grew-up.md",
39
+ ]);
40
+
41
+ function docs(dir: string, out: string[] = []): string[] {
42
+ for (const name of readdirSync(dir)) {
43
+ const full = join(dir, name);
44
+ if (statSync(full).isDirectory()) docs(full, out);
45
+ else if (name.endsWith(".md")) out.push(full);
46
+ }
47
+ return out;
48
+ }
49
+
50
+ test("no component doc links at a route that does not exist", () => {
51
+ const offenders: string[] = [];
52
+ for (const file of docs(here)) {
53
+ const md = readFileSync(file, "utf8");
54
+ for (const m of md.matchAll(/(?:href|src)="(\/[^"#][^"]*)"/g)) {
55
+ const ref = m[1];
56
+ if (ref.startsWith("//")) continue; // protocol-relative, not a route
57
+ if (SHIPPED_ASSETS.has(ref) || REAL_ROUTES.has(ref)) continue;
58
+ offenders.push(`${file.slice(here.length)}: ${ref}`);
59
+ }
60
+ }
61
+ expect(offenders).toEqual([]);
62
+ });
@@ -23,13 +23,13 @@ the rows; nothing breaks, but you don't get what the demo shows.
23
23
  <table class="data-table">
24
24
  <thead><tr><th>Name</th><th>Code</th><th>Owner</th></tr></thead>
25
25
  <tbody>
26
- <tr data-href="/clients/acme">
27
- <td><a href="/clients/acme">Acme Advisory</a><br><span class="data-table__sub">since 2021</span></td>
26
+ <tr data-href="#acme">
27
+ <td><a href="#acme">Acme Advisory</a><br><span class="data-table__sub">since 2021</span></td>
28
28
  <td>acme</td>
29
29
  <td><span class="data-table__sub">jane@acme.test</span></td>
30
30
  </tr>
31
- <tr data-href="/clients/borden">
32
- <td><a href="/clients/borden">Borden &amp; Co</a></td>
31
+ <tr data-href="#borden">
32
+ <td><a href="#borden">Borden &amp; Co</a></td>
33
33
  <td>borden</td>
34
34
  <td><span class="data-table__sub">ops@borden.test</span></td>
35
35
  </tr>
@@ -0,0 +1,265 @@
1
+ /* grain/components/organisms/presentation: the PRESENTATION organism.
2
+ *
3
+ * A deck: full-bleed slides, staged fragments, a fit-to-box pass, an overview grid, present mode,
4
+ * a speaker-notes strip, a synced presenter window, and a print sheet. It knows nothing about any
5
+ * particular talk: the app owns the slides and every figure on them.
6
+ *
7
+ * Sizing is one number. `--u0` is derived from the deck's own box with container query units, and
8
+ * `--u` is that times `--fit`, the shrink factor the script computes per slide. Every size below is
9
+ * a multiple of `--u`, so a slide that is too tall scales as one piece instead of reflowing.
10
+ *
11
+ * Behaviour: grain/scripts/presentation.js. Opt a deck in with `data-deck` on the root; without
12
+ * the script the markup still renders whichever slide carries `data-current`, which is what the
13
+ * static export and a print run get.
14
+ */
15
+
16
+ .presentation {
17
+ container-type: size;
18
+ /* tuned for a deck that owns the whole window. A deck docked in a pane can raise this. */
19
+ --u0: min(0.86cqw, 1.44cqh);
20
+ --fit: 1;
21
+ --u: calc(var(--u0) * var(--fit));
22
+ position: relative;
23
+ height: 100%;
24
+ min-height: 0;
25
+ overflow: hidden;
26
+ background: var(--color-bg);
27
+ color: var(--color-fg);
28
+ }
29
+
30
+
31
+ .presentation[data-present] {
32
+ position: fixed; inset: 0; z-index: var(--z-presentation, 8000);
33
+ height: 100dvh;
34
+ }
35
+
36
+
37
+ .presentation__slide {
38
+ position: absolute; inset: 0;
39
+ display: none;
40
+ flex-direction: column; justify-content: center;
41
+
42
+ padding: calc(var(--u) * 8) calc(var(--u) * 9) calc(var(--u) * 8);
43
+ box-sizing: border-box; overflow: hidden;
44
+
45
+ --u: calc(var(--u0) * var(--fit, 1));
46
+ }
47
+ .presentation__slide[data-current] { display: flex; }
48
+
49
+
50
+ .presentation__body { display: flex; flex-direction: column; width: 100%; min-width: 0; }
51
+ .presentation__slide[data-align="top"] { justify-content: flex-start; }
52
+ .presentation__slide[data-align="center"] { align-items: center; text-align: center; }
53
+ .presentation__slide[data-align="center"] .presentation__body { align-items: center; text-align: center; }
54
+
55
+
56
+
57
+ .presentation .slide-eyebrow {
58
+ font-size: calc(var(--u) * 1.55); letter-spacing: 0.14em; text-transform: uppercase;
59
+ color: var(--color-muted); margin: 0 0 calc(var(--u) * 3);
60
+ }
61
+ .presentation .slide-head {
62
+ font-size: calc(var(--u) * 6.2); line-height: var(--leading-tight); letter-spacing: -0.015em;
63
+ font-weight: var(--font-weight-regular); margin: 0; max-width: 20ch;
64
+ }
65
+ .presentation .slide-head[data-size="sm"] { font-size: calc(var(--u) * 4.4); max-width: 26ch; }
66
+ .presentation .slide-head[data-size="xl"] { font-size: calc(var(--u) * 8.4); max-width: 15ch; }
67
+ .presentation .slide-lede {
68
+ font-size: calc(var(--u) * 2.5); line-height: 1.45; color: var(--color-muted);
69
+ max-width: 46ch; margin: calc(var(--u) * 4) 0 0;
70
+ }
71
+ .presentation .slide-lede[data-tone="loud"] { color: var(--color-fg); }
72
+ .presentation .slide-rule { border: 0; border-top: var(--rule); margin: calc(var(--u) * 3.5) 0 0;
73
+ max-width: calc(var(--u) * 60); }
74
+ .presentation .slide-sig { font-size: calc(var(--u) * 1.9); color: var(--color-muted); margin: calc(var(--u) * 3) 0 0; }
75
+ .presentation .slide-sig b { color: var(--color-fg); font-weight: var(--font-weight-regular); }
76
+
77
+
78
+ .presentation .quote { font-size: calc(var(--u) * 6.6); line-height: 1.15; letter-spacing: -0.01em;
79
+ max-width: 22ch; margin: 0; text-wrap: balance; }
80
+ .presentation .quote-mark { display: block; font-size: calc(var(--u) * 2); color: var(--color-muted);
81
+ letter-spacing: 0.14em; text-transform: uppercase; margin-bottom: calc(var(--u) * 3); }
82
+
83
+
84
+ .presentation .beats { list-style: none; margin: calc(var(--u) * 4) 0 0; padding: 0;
85
+ display: flex; flex-direction: column; gap: calc(var(--u) * 2.4); }
86
+ .presentation .beats li { font-size: calc(var(--u) * 3.4); line-height: 1.25; max-width: 30ch; }
87
+ .presentation .beats[data-size="sm"] li { font-size: calc(var(--u) * 2.6); }
88
+
89
+ .presentation .sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
90
+ overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
91
+
92
+
93
+
94
+ .presentation__slide[data-current] { animation: pres-in 0.34s cubic-bezier(0.22, 0.61, 0.36, 1) both; }
95
+ .presentation[data-dir="back"] .presentation__slide[data-current] { animation-name: pres-in-back; }
96
+ @keyframes pres-in { from { opacity: 0; transform: translateX(calc(var(--u) * 2.2)); } }
97
+ @keyframes pres-in-back { from { opacity: 0; transform: translateX(calc(var(--u) * -2.2)); } }
98
+
99
+
100
+ /* No type swap on arrival. An earlier build faded and re-faced every landing slide for a third of
101
+ a second, and from the back of a room that reads as a rendering glitch rather than a signal. The
102
+ script still sets [data-settling] on arrival, so a consumer that wants the effect can style it.
103
+ Deliberate grain, on [data-grade="grain"], is untouched and is the honest signal. */
104
+
105
+ .presentation__slide [data-grade="grain"] { --type-font: var(--font-grain); }
106
+
107
+
108
+
109
+ .presentation .frag { opacity: 0; transition: opacity 0.32s ease, transform 0.32s ease; }
110
+ .presentation .frag.is-on { opacity: 1; transform: none; }
111
+ .presentation .frag[data-frag="rise"], .presentation .frag:not([data-frag]) { transform: translateY(calc(var(--u) * 0.9)); }
112
+ .presentation .frag[data-frag="fade"] { transform: none; }
113
+ .presentation .frag[data-frag="grow"] { transform: scaleX(0.82); transform-origin: left center; }
114
+ .presentation .frag[data-frag="dim"].is-on { opacity: 0.55; }
115
+
116
+
117
+ .presentation [data-draw] {
118
+ stroke-dasharray: var(--draw-len, 1000);
119
+ stroke-dashoffset: var(--draw-len, 1000);
120
+ transition: stroke-dashoffset 0.9s cubic-bezier(0.4, 0, 0.2, 1);
121
+ }
122
+ .presentation [data-draw].is-drawn { stroke-dashoffset: 0; }
123
+
124
+ .presentation [data-lit] { opacity: 0; transition: opacity 0.4s ease 0.35s; }
125
+ .presentation [data-lit].is-drawn { opacity: 1; }
126
+
127
+ @media (prefers-reduced-motion: reduce) {
128
+ .presentation__slide[data-current] { animation: none; }
129
+ .presentation .frag, .presentation [data-draw], .presentation [data-lit] { transition: none; }
130
+ .presentation .frag { transform: none; }
131
+ }
132
+
133
+
134
+
135
+ .presentation__dots {
136
+ position: absolute; left: calc(var(--u) * 11.7); bottom: calc(var(--u) * 3.4);
137
+ display: flex; gap: calc(var(--u) * 0.85); align-items: center;
138
+ }
139
+ .presentation__dot {
140
+ width: calc(var(--u) * 0.62); height: calc(var(--u) * 0.62); border-radius: 50%;
141
+ background: var(--ink-faint); border: 0; padding: 0; cursor: pointer;
142
+ transition: transform 0.24s ease, background 0.24s ease;
143
+ }
144
+ .presentation__dot[data-seen] { background: var(--color-muted); }
145
+ .presentation__dot[data-now] { background: var(--color-accent); transform: scale(1.9); }
146
+ .presentation__dot:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 3px; }
147
+
148
+
149
+ .presentation__bar {
150
+ position: absolute; left: calc(var(--u) * 11.7); right: calc(var(--u) * 11.7); top: calc(var(--u) * 3);
151
+ display: flex; justify-content: space-between; align-items: baseline;
152
+ gap: calc(var(--u) * 3); min-width: 0;
153
+ }
154
+ .presentation__meta, .presentation__hint, .presentation__legend {
155
+ margin: 0; font-size: calc(var(--u) * 1.4); color: var(--ink-faint);
156
+ display: flex; gap: calc(var(--u) * 2.2); align-items: baseline; min-width: 0;
157
+ }
158
+ .presentation__hint { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
159
+ .presentation__legend { justify-content: flex-end; flex-wrap: wrap; row-gap: calc(var(--u) * 0.6); }
160
+
161
+ .presentation__meta { flex: 0 0 auto; }
162
+ .presentation__hint kbd { font-family: var(--font-mono); font-size: 0.95em; }
163
+ .presentation__legend b, .presentation__meta b { font-weight: var(--font-weight-regular); color: var(--color-muted); }
164
+ .presentation__meta a { color: inherit; text-decoration: none; }
165
+ .presentation__meta a:hover { color: var(--color-muted); text-decoration: underline; }
166
+
167
+ .presentation__controls {
168
+ position: absolute; right: calc(var(--u) * 11.7); bottom: calc(var(--u) * 2.4);
169
+ display: flex; align-items: center; gap: calc(var(--u) * 0.8);
170
+ }
171
+ .presentation__ctl {
172
+ display: inline-flex; align-items: center; justify-content: center;
173
+ min-width: calc(var(--u) * 3.4); height: calc(var(--u) * 3.4); padding: 0 calc(var(--u) * 1.1);
174
+ background: transparent; border: 1px solid var(--color-line); border-radius: var(--radius-sm);
175
+ color: var(--color-muted); font: inherit; font-size: calc(var(--u) * 1.5); line-height: 1; cursor: pointer;
176
+ transition: border-color 0.18s ease, color 0.18s ease, background 0.18s ease;
177
+ }
178
+ .presentation__ctl:hover { border-color: var(--hairline); color: var(--color-fg); background: var(--paper-2); }
179
+ .presentation__ctl:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
180
+ .presentation__ctl[aria-pressed="true"] { border-color: var(--color-accent); color: var(--color-accent); }
181
+ .presentation__ctl:disabled { opacity: 0.3; cursor: default; }
182
+ .presentation__ctl[data-wide] { font-family: var(--font-mono); font-size: calc(var(--u) * 1.35);
183
+ letter-spacing: 0.04em; border-color: transparent; }
184
+ .presentation__ctl[data-wide]:hover { background: transparent; border-color: transparent; }
185
+
186
+
187
+ .presentation[data-present] .presentation__hint { display: none; }
188
+
189
+
190
+ [data-present-toggle] .on-present { display: none; }
191
+ .presentation[data-present] [data-present-toggle] .off-present { display: none; }
192
+ .presentation[data-present] [data-present-toggle] .on-present { display: inline; }
193
+
194
+
195
+ .presentation__grid {
196
+ position: absolute; inset: 0; overflow: auto; padding: calc(var(--u) * 4);
197
+ display: none; grid-template-columns: repeat(auto-fill, minmax(calc(var(--u) * 22), 1fr));
198
+ gap: calc(var(--u) * 2); background: var(--color-bg); align-content: start;
199
+ }
200
+ .presentation[data-view="grid"] .presentation__slide { display: none; }
201
+ .presentation[data-view="grid"] .presentation__grid { display: grid; }
202
+ .presentation__cell {
203
+ border: 1px solid var(--color-line); border-radius: var(--radius-md);
204
+ padding: calc(var(--u) * 1.8); text-align: left; background: transparent;
205
+ font: inherit; color: inherit; cursor: pointer; transition: border-color 0.2s ease;
206
+ }
207
+ .presentation__cell:hover { border-color: var(--hairline); }
208
+ .presentation__cell[data-now] { border-color: var(--color-accent); }
209
+ .presentation__n { font-family: var(--font-mono); font-size: calc(var(--u) * 1.3); color: var(--color-muted); }
210
+ .presentation__t { font-size: calc(var(--u) * 1.7); margin-top: calc(var(--u) * 0.8); line-height: 1.25; }
211
+
212
+
213
+
214
+ .presentation__notes {
215
+ position: absolute; left: 0; right: 0; bottom: 0; display: none;
216
+ background: var(--panel); border-top: 1px solid var(--color-line);
217
+ padding: calc(var(--u) * 2) calc(var(--u) * 11.7); max-height: 32cqh; overflow: auto;
218
+ }
219
+ .presentation[data-notes] .presentation__notes { display: block; }
220
+ .presentation__notes li { font-size: calc(var(--u) * 1.7); line-height: 1.5; color: var(--color-muted); }
221
+ .presentation__slide .slide-notes { display: none; }
222
+
223
+
224
+ .presenter { display: none; }
225
+ [data-mode="presenter"] .presenter {
226
+ display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto 1fr;
227
+ align-content: start; gap: 1.5rem; padding: 1.5rem; min-height: 100dvh;
228
+ box-sizing: border-box; font-size: 1rem;
229
+ }
230
+ .presenter__now { grid-column: 1 / -1; display: flex; justify-content: space-between;
231
+ align-items: baseline; border-bottom: var(--rule); padding-bottom: 0.75rem; }
232
+ .presenter__clock { font-family: var(--font-mono); font-size: 2.6rem; }
233
+ .presenter__clock[data-over] { color: var(--color-accent); }
234
+ .presenter__pos { font-family: var(--font-mono); color: var(--color-muted); }
235
+ .presenter__title { font-size: 1.8rem; margin: 0 0 0.75rem; }
236
+ .presenter__notes li { font-size: 1.15rem; line-height: 1.55; margin-bottom: 0.55rem; }
237
+ .presenter__next h3 { font-size: 0.95rem; letter-spacing: 0.12em; text-transform: uppercase;
238
+ color: var(--color-muted); margin: 0 0 0.5rem; }
239
+ .presenter__next p { color: var(--color-muted); }
240
+ .presenter__hint { grid-column: 1 / -1; margin: 0; font-size: 0.85rem; color: var(--ink-faint);
241
+ letter-spacing: 0.08em; text-transform: uppercase; }
242
+
243
+
244
+
245
+ @media print {
246
+ .presentation {
247
+ position: static; height: auto; overflow: visible; container-type: normal;
248
+ --u0: min(1.05vw, 1.75vh);
249
+ }
250
+ .presentation__dots, .presentation__meta, .presentation__hint, .presentation__legend,
251
+ .presentation__notes, .presentation__grid, .presentation__controls, .presentation__bar { display: none !important; }
252
+ .presentation__slide {
253
+ display: flex !important; position: relative; inset: auto;
254
+ width: 100%; height: 100vh; box-sizing: border-box;
255
+
256
+ padding: calc(var(--u) * 7) calc(var(--u) * 9);
257
+ page-break-after: always; break-after: page; animation: none !important;
258
+ }
259
+ .presentation__slide:last-of-type { page-break-after: auto; break-after: auto; }
260
+ .presentation .frag { opacity: 1 !important; transform: none !important; }
261
+ .presentation [data-draw] { stroke-dashoffset: 0 !important; }
262
+ .presentation [data-lit] { opacity: 1 !important; }
263
+
264
+ body::before { display: none !important; }
265
+ }
@@ -0,0 +1,128 @@
1
+ # Presentation
2
+
3
+ A **deck**. Full-bleed slides, staged fragments, an overview grid, present mode, a speaker-notes
4
+ strip, a second-window presenter view, and a print sheet that lands everything first.
5
+
6
+ It knows nothing about any particular talk. The organism owns *which slide is up and how far into
7
+ it we are*; the app owns the slides, the figures, and every animation on them. That split is the
8
+ whole design: one seam, no registry, no per-slide config.
9
+
10
+ CSS plus one island, `grain/scripts/presentation.js`. Load it and the markup below is live with no
11
+ per-page wiring.
12
+
13
+ ## Markup
14
+
15
+ ```html
16
+ <div class="presentation" data-present data-deck data-hash data-minutes="20" data-channel="my-talk">
17
+
18
+ <div class="presentation__bar">
19
+ <p class="presentation__hint">← → move · <kbd>D</kbd> light/dark · <kbd>.</kbd> blank</p>
20
+ </div>
21
+
22
+ <section class="presentation__slide" data-title="Title">
23
+ <p class="slide-eyebrow">An eyebrow</p>
24
+ <h1 class="slide-head" data-size="xl">The claim</h1>
25
+ <ul class="beats">
26
+ <li class="frag">Lands on the first press</li>
27
+ <li class="frag">Then this one</li>
28
+ </ul>
29
+ <ul class="slide-notes" hidden>
30
+ <li>What to say here. Never rendered on the slide.</li>
31
+ </ul>
32
+ </section>
33
+
34
+ <nav class="presentation__dots" data-dots aria-label="Slides"></nav>
35
+ <div class="presentation__controls">
36
+ <button class="presentation__ctl" type="button" data-ctl="prev" aria-label="Back">‹</button>
37
+ <span class="presentation__ctl" data-wide aria-hidden="true"><span data-pos>1 / 1</span></span>
38
+ <button class="presentation__ctl" type="button" data-ctl="next" aria-label="Forward">›</button>
39
+ <button class="presentation__ctl" type="button" data-ctl="grid" aria-pressed="false">⊞</button>
40
+ <button class="presentation__ctl" type="button" data-ctl="notes" aria-pressed="false">☰</button>
41
+ <button class="presentation__ctl" type="button" data-ctl="presenter">◫</button>
42
+ <button class="presentation__ctl" type="button" data-present-toggle>⤢</button>
43
+ </div>
44
+ <div class="presentation__notes" data-inline-notes aria-live="polite"></div>
45
+ <div class="presentation__grid" data-grid></div>
46
+ </div>
47
+ ```
48
+
49
+ The dot strip and the overview grid are filled by the script from the slides themselves, so a deck
50
+ never maintains two lists that can disagree. `data-title` is what shows in both, and in the
51
+ presenter window.
52
+
53
+ The catalog renders this example flat rather than live, and that is the honest thing to do: a deck
54
+ sizes itself from its own box with `container-type: size` and lays its slides out absolutely, so
55
+ one dropped into a catalog panel collapses to nothing. Read the code, or open a real deck.
56
+
57
+ **Opt in with `data-deck`.** Without the script the markup still renders whichever slide carries
58
+ `data-current`, which is what a static export and a print run get.
59
+
60
+ ## Sizing is one number
61
+
62
+ `--u0` comes from the deck's own box in container query units, and `--u` is `--u0 × --fit`. Every
63
+ size in the component is a multiple of `--u`.
64
+
65
+ `--fit` is the shrink factor the script measures per slide: it drops the factor until the slide's
66
+ body fits its box, so a dense slide scales as one piece instead of reflowing into a mess. Anything
67
+ marked `aria-expanded="false"` is measured **open**, so clicking a card on stage can never overflow
68
+ a slide that fit a moment ago.
69
+
70
+ Two consequences worth knowing. A slide sitting near `--fit: 0.6` is not a rendering bug, it is the
71
+ component telling you the slide carries two ideas and wants to be two slides. And fragments only
72
+ change opacity and transform, never layout, so one measurement per slide holds for every step.
73
+
74
+ ## Fragments and figures
75
+
76
+ `.frag` starts hidden and lands on a press. `data-frag` picks how: `rise` (the default), `fade`,
77
+ `grow`, or `dim` for a beat that lands quieter than the ones above it.
78
+
79
+ Figures get two hooks that need no per-deck script. `[data-draw]` draws an SVG stroke in, using a
80
+ `--draw-len` set to the path length. `[data-lit]` fades an element up once its slide arrives, or
81
+ once its own fragment lands if it sits inside one.
82
+
83
+ Slides can also declare steps that no fragment represents, for an app-driven animation:
84
+ `data-steps="1"` adds a press that fires the seam without changing the markup.
85
+
86
+ ## The seam
87
+
88
+ One event on the deck element, and it is the entire contract:
89
+
90
+ ```js
91
+ deck.addEventListener("presentation:slide", (e) => {
92
+ const { index, step, slide, title, entered, total } = e.detail;
93
+ });
94
+ ```
95
+
96
+ `entered` is true on the first event for a slide and false on later steps within it, so an app can
97
+ start an animation once and advance a figure per step. A second event, `presentation:print`, fires
98
+ before printing so an app can land its own figures in their final state.
99
+
100
+ ## Keys, and giving one back
101
+
102
+ Arrows and space move, `Home` and `End` jump, `O` opens the overview, `N` toggles notes, `P` opens
103
+ the presenter window, `F` presents, and `.` blanks the screen. A swipe is an arrow key.
104
+
105
+ A slide can claim a key for its own control with `data-cede="ArrowUp ArrowDown"`, and the deck
106
+ yields it while that slide is current. A guess marker the room drives with the up and down arrows
107
+ is the case this exists for.
108
+
109
+ ## The presenter window
110
+
111
+ `P` opens the same page again with `?presenter=1`. It shows the current title, its notes, what is
112
+ next, and a clock that turns accent past `data-minutes`. The two windows stay in step over a
113
+ `BroadcastChannel` named by `data-channel`, and arrows pressed in the presenter window drive the
114
+ main one. The audience never sees it.
115
+
116
+ ## Print
117
+
118
+ `Cmd+P` lands every fragment and every figure first, then lays one slide per page. The component
119
+ re-runs its fit pass under print rules, because print swaps `--u0` to viewport units and a deck
120
+ that skipped that step would print every slide at the screen's factor.
121
+
122
+ ## Grade
123
+
124
+ A deck follows grain's grade mechanism like anything else: `data-grade="grain"` on a line the
125
+ machine drafted. The organism deliberately does **not** re-face type on arrival. An earlier build
126
+ did, and from the back of a room a third of a second of shifting letterforms reads as a glitch
127
+ rather than a signal. The script still sets `data-settling` while a slide lands, so a consumer that
128
+ wants the effect can style it.
@@ -0,0 +1,75 @@
1
+ // grain/components/organisms/presentation/presentation.test.ts — DRIFT GUARD for the deck's CSS.
2
+ //
3
+ // Scope, stated honestly: this package has no DOM in test, so these are grep-style contract guards
4
+ // in the drawer.test.ts mould. They catch an obligation being DELETED, which is the realistic
5
+ // regression here, not one being subtly wrong. Whether a slide actually fits its box on a
6
+ // projector is measured in a browser against a consumer, and no assertion in this file claims
7
+ // otherwise.
8
+ import { test, expect } from "bun:test";
9
+ import { readFileSync } from "node:fs";
10
+ import { join } from "node:path";
11
+
12
+ const here = new URL(".", import.meta.url).pathname;
13
+ const css = readFileSync(join(here, "presentation.css"), "utf8");
14
+ const md = readFileSync(join(here, "presentation.md"), "utf8");
15
+
16
+ test("every size is a multiple of --u, so a dense slide scales as one piece", () => {
17
+ expect(css).toMatch(/--u:\s*calc\(var\(--u0\)\s*\*\s*var\(--fit/);
18
+ // a raw px or rem font-size inside a slide would survive the fit pass and break the scaling
19
+ const slideBlock = css.slice(css.indexOf(".presentation .slide-eyebrow"), css.indexOf(".presentation .sr-only"));
20
+ expect(slideBlock).not.toMatch(/font-size:\s*\d+(\.\d+)?(px|rem)/);
21
+ });
22
+
23
+ test("the deck is its own container, or the container query units size against the page", () => {
24
+ expect(css).toMatch(/\.presentation\s*\{[^}]*container-type:\s*size/);
25
+ expect(css).toMatch(/--u0:\s*min\([\d.]+cqw,\s*[\d.]+cqh\)/);
26
+ });
27
+
28
+ test("a slide with no JS still renders: display is driven by [data-current]", () => {
29
+ expect(css).toMatch(/\.presentation__slide\s*\{[^}]*display:\s*none/);
30
+ expect(css).toMatch(/\.presentation__slide\[data-current\]\s*\{\s*display:\s*flex/);
31
+ });
32
+
33
+ test("fragments move opacity and transform only, never layout", () => {
34
+ const frag = css.match(/\.presentation \.frag\s*\{([^}]*)\}/);
35
+ expect(frag).not.toBeNull();
36
+ expect(frag![1]).toMatch(/transition:\s*opacity[^;]*transform/);
37
+ expect(frag![1]).not.toMatch(/display|height|margin|padding/);
38
+ });
39
+
40
+ test("speaker notes never render on the slide itself", () => {
41
+ expect(css).toMatch(/\.presentation__slide \.slide-notes\s*\{\s*display:\s*none/);
42
+ });
43
+
44
+ test("print lands the figures and re-derives the unit from the page, not the screen", () => {
45
+ const printBlock = css.slice(css.indexOf("@media print"));
46
+ expect(printBlock).toMatch(/--u0:\s*min\([\d.]+vw,\s*[\d.]+vh\)/);
47
+ expect(printBlock).toMatch(/\.presentation \.frag\s*\{\s*opacity:\s*1\s*!important/);
48
+ expect(printBlock).toMatch(/\[data-draw\]\s*\{\s*stroke-dashoffset:\s*0\s*!important/);
49
+ expect(printBlock).toMatch(/page-break-after:\s*always/);
50
+ });
51
+
52
+ test("the chrome is hidden in print, so a printed deck is slides and nothing else", () => {
53
+ const printBlock = css.slice(css.indexOf("@media print"));
54
+ for (const part of ["__dots", "__controls", "__notes", "__grid", "__bar"]) {
55
+ expect(printBlock).toContain(`.presentation${part}`);
56
+ }
57
+ });
58
+
59
+ test("arrival does not re-face type, and the .md says why", () => {
60
+ // a consumer may style [data-settling]; the component must not ship the swap itself
61
+ expect(css).not.toMatch(/\[data-settling\][^{]*\{[^}]*font-family/);
62
+ expect(md).toMatch(/data-settling/);
63
+ });
64
+
65
+ test("the .md documents the one seam an app is allowed to use", () => {
66
+ expect(md).toMatch(/presentation:slide/);
67
+ expect(md).toMatch(/presentation:print/);
68
+ expect(md).toMatch(/entered/);
69
+ });
70
+
71
+ test("no em-dashes and no hardcoded colors, per the standards this package follows", () => {
72
+ expect(css).not.toContain("\u2014");
73
+ expect(md).not.toContain("\u2014");
74
+ expect(css).not.toMatch(/:\s*#[0-9a-fA-F]{3,8}\b/);
75
+ });
@@ -39,7 +39,7 @@ fails silently, so the promise here is only "an input that belongs in a topbar".
39
39
 
40
40
  ```html
41
41
  <header class="app-shell__topbar">
42
- <span class="topbar-crumbs"><a href="/tickets">Tickets</a> / <strong>Annual Review</strong></span>
42
+ <span class="topbar-crumbs"><a href="#tickets">Tickets</a> / <strong>Annual Review</strong></span>
43
43
  <div class="topbar-search"><input type="search" placeholder="Filter…" aria-label="Filter"></div>
44
44
  <div class="topbar-ctl">
45
45
  <button class="icon-btn" data-variant="ghost" data-toggle-scheme aria-label="Toggle light / dark">◐</button>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tjakoen/grain",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "GRAIN — an AI-interaction design system where every surface is operable by both a human and an AI through one shared vocabulary.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -0,0 +1,315 @@
1
+ // grain/scripts/presentation.js: the PRESENTATION island, the behaviour half of the
2
+ // organisms/presentation surface.
3
+ //
4
+ // It owns the deck and nothing else: which slide is current, which fragments have landed, the
5
+ // fit-to-box pass, the dot matrix, the overview grid, present mode, the notes strip, the presenter
6
+ // window, and print. It knows nothing about any particular talk.
7
+ //
8
+ // THE SEAM. A consuming app hooks its own per-slide behaviour by listening on the deck element:
9
+ //
10
+ // deck.addEventListener("presentation:slide", (e) => {
11
+ // const { index, step, slide, title, entered } = e.detail;
12
+ // });
13
+ //
14
+ // `entered` is true on the first event for a slide (the arrival) and false on later steps within
15
+ // it, so an app can start an animation once and advance figures per step. That is the whole
16
+ // contract: no registry, no per-slide config, no framework.
17
+ //
18
+ // OPT-IN: mark the root `.presentation` with `data-deck`. Zero-JS the markup still renders the
19
+ // first slide (author it with `data-current`), which is what the static export and a print run get.
20
+ (() => {
21
+ "use strict";
22
+ if (window.grain && window.grain.presentation) return; // idempotent, like the other islands
23
+
24
+ const SETTLE_MS = 420; // how long a slide reads as "in transit" before its type commits to clean
25
+
26
+ function init() {
27
+ for (const deck of document.querySelectorAll(".presentation[data-deck]")) mount(deck);
28
+ }
29
+
30
+ function mount(deck) {
31
+ const slides = [...deck.querySelectorAll(".presentation__slide")];
32
+ if (!slides.length) return;
33
+ const dotsEl = deck.querySelector("[data-dots]");
34
+ const posEl = deck.querySelector("[data-pos]");
35
+ const gridEl = deck.querySelector("[data-grid]");
36
+ const stripEl = deck.querySelector("[data-inline-notes]");
37
+ const total = slides.length;
38
+ const channel = "BroadcastChannel" in window
39
+ ? new BroadcastChannel(deck.dataset.channel || "grain.presentation") : null;
40
+ const isPresenter = new URLSearchParams(location.search).get("presenter") === "1";
41
+
42
+ let at = 0, step = 0, settleTimer = null;
43
+
44
+ const frags = (s) => [...s.querySelectorAll(".frag")];
45
+ const steps = (s) => frags(s).length + (Number(s.dataset.steps) || 0);
46
+ const notesOf = (s) => [...(s.querySelector(".slide-notes")?.children || [])].map((li) => li.innerHTML);
47
+
48
+ /* ---- fit: shrink a dense slide until it fits its own box -------------------
49
+ Fragments only change opacity and transform, never layout, so one measurement per slide
50
+ holds for every step. Any [aria-expanded] card is measured OPEN, so clicking one on stage
51
+ can never overflow. */
52
+ slides.forEach((s) => {
53
+ if (s.querySelector(".presentation__body")) return;
54
+ const body = document.createElement("div");
55
+ body.className = "presentation__body";
56
+ [...s.children].forEach((c) => { if (!c.classList.contains("slide-notes")) body.append(c); });
57
+ s.prepend(body);
58
+ });
59
+
60
+ function fit(s) {
61
+ const body = s.querySelector(".presentation__body");
62
+ if (!body) return;
63
+ const shown = s.hasAttribute("data-current");
64
+ if (!shown) { s.style.display = "flex"; s.style.visibility = "hidden"; }
65
+ const collapsed = [...s.querySelectorAll('[aria-expanded="false"]')];
66
+ collapsed.forEach((c) => c.setAttribute("aria-expanded", "true"));
67
+ // measure the WORST case, which is the slide mid-settle: the grain face and its extra
68
+ // letter-spacing are wider than the clean one, so a slide that fits when settled can still
69
+ // overflow on the way in. Measuring settled would ship exactly that silent overflow.
70
+ const wasSettling = s.hasAttribute("data-settling");
71
+ s.setAttribute("data-settling", "");
72
+ s.style.setProperty("--fit", "1");
73
+ const cs = getComputedStyle(s);
74
+ const room = () => s.clientHeight - parseFloat(cs.paddingTop) - parseFloat(cs.paddingBottom);
75
+ let f = 1;
76
+ while (f > 0.5 && body.scrollHeight > room()) {
77
+ f = Math.round((f - 0.04) * 100) / 100;
78
+ s.style.setProperty("--fit", String(f));
79
+ }
80
+ collapsed.forEach((c) => c.setAttribute("aria-expanded", "false"));
81
+ if (!wasSettling) s.removeAttribute("data-settling");
82
+ if (!shown) { s.style.display = ""; s.style.visibility = ""; }
83
+ }
84
+ const fitAll = () => slides.forEach(fit);
85
+
86
+ /* ---- paint ------------------------------------------------------------- */
87
+ function paint(entered) {
88
+ slides.forEach((s, i) => s.toggleAttribute("data-current", i === at));
89
+ const now = slides[at];
90
+ frags(now).forEach((f, i) => f.classList.toggle("is-on", i < step));
91
+ // a figure draws itself in once its slide has arrived, or once its own fragment lands
92
+ now.querySelectorAll("[data-draw], [data-lit]").forEach((el) => {
93
+ const own = el.closest(".frag");
94
+ el.classList.toggle("is-drawn", !own || own.classList.contains("is-on"));
95
+ });
96
+
97
+ if (entered) {
98
+ now.setAttribute("data-settling", "");
99
+ clearTimeout(settleTimer);
100
+ settleTimer = setTimeout(() => now.removeAttribute("data-settling"), SETTLE_MS);
101
+ }
102
+
103
+ dotsEl?.querySelectorAll(".presentation__dot").forEach((d, i) => {
104
+ d.toggleAttribute("data-seen", i <= at);
105
+ d.toggleAttribute("data-now", i === at);
106
+ });
107
+ gridEl?.querySelectorAll(".presentation__cell").forEach((c, i) => c.toggleAttribute("data-now", i === at));
108
+ if (posEl) posEl.textContent = `${at + 1} / ${total}`;
109
+ if (stripEl) stripEl.innerHTML = `<ul>${notesOf(now).map((n) => `<li>${n}</li>`).join("")}</ul>`;
110
+
111
+ syncControls();
112
+ deck.dispatchEvent(new CustomEvent("presentation:slide", {
113
+ detail: { index: at, step, slide: now, title: now.dataset.title || "", entered, total },
114
+ }));
115
+ channel?.postMessage({ type: "state", at, step });
116
+ if (deck.hasAttribute("data-hash")) history.replaceState(null, "", `#${at + 1}`);
117
+ }
118
+
119
+ function go(i, s = 0) {
120
+ const next = Math.max(0, Math.min(total - 1, i));
121
+ deck.dataset.dir = next < at ? "back" : "fwd";
122
+ const entered = next !== at || !slides[at].hasAttribute("data-current");
123
+ at = next; step = s;
124
+ paint(entered);
125
+ }
126
+ function next() {
127
+ if (step < steps(slides[at])) { step += 1; paint(false); return; }
128
+ if (at < total - 1) go(at + 1, 0);
129
+ }
130
+ function prev() {
131
+ if (step > 0) { step -= 1; paint(false); return; }
132
+ if (at > 0) go(at - 1, steps(slides[at - 1]));
133
+ }
134
+
135
+ /* ---- chrome ------------------------------------------------------------ */
136
+ if (dotsEl) {
137
+ dotsEl.innerHTML = slides.map((s, i) =>
138
+ `<button class="presentation__dot" type="button" data-to="${i}" title="${s.dataset.title || i + 1}">` +
139
+ `<span class="sr-only">${i + 1}</span></button>`).join("");
140
+ }
141
+ if (gridEl) {
142
+ gridEl.innerHTML = slides.map((s, i) =>
143
+ `<button class="presentation__cell" type="button" data-to="${i}">` +
144
+ `<span class="presentation__n">${String(i + 1).padStart(2, "0")}</span>` +
145
+ `<p class="presentation__t">${s.dataset.title || ""}</p></button>`).join("");
146
+ }
147
+ /* the control cluster: the same moves the keys make, for a presenter whose hands are on a
148
+ laptop, a reader at a desk, and a touch screen with no arrow keys at all */
149
+ deck.addEventListener("click", (e) => {
150
+ const to = e.target.closest("[data-to]");
151
+ if (to) { deck.dataset.view = "slides"; go(Number(to.dataset.to), 0); return; }
152
+ const ctl = e.target.closest("[data-ctl]");
153
+ if (ctl) {
154
+ switch (ctl.dataset.ctl) {
155
+ case "prev": prev(); break;
156
+ case "next": next(); break;
157
+ case "notes": deck.toggleAttribute("data-notes"); requestAnimationFrame(fitAll); break;
158
+ case "grid": deck.dataset.view = deck.dataset.view === "grid" ? "slides" : "grid"; break;
159
+ case "presenter": window.open(`${location.pathname}?presenter=1`, "grain-presenter", "width=1100,height=760"); break;
160
+ default: break;
161
+ }
162
+ syncControls();
163
+ return;
164
+ }
165
+ if (e.target.closest("[data-present-toggle]")) togglePresent();
166
+ });
167
+
168
+ function syncControls() {
169
+ const first = at === 0 && step === 0;
170
+ const last = at === total - 1 && step >= steps(slides[at]);
171
+ deck.querySelector('[data-ctl="prev"]')?.toggleAttribute("disabled", first);
172
+ deck.querySelector('[data-ctl="next"]')?.toggleAttribute("disabled", last);
173
+ deck.querySelector('[data-ctl="notes"]')
174
+ ?.setAttribute("aria-pressed", String(deck.hasAttribute("data-notes")));
175
+ deck.querySelector('[data-ctl="grid"]')
176
+ ?.setAttribute("aria-pressed", String(deck.dataset.view === "grid"));
177
+ }
178
+
179
+ /* ---- keys -------------------------------------------------------------- */
180
+ const typing = (e) => /^(INPUT|TEXTAREA|SELECT)$/.test(e.target.tagName) || e.target.isContentEditable;
181
+ addEventListener("keydown", (e) => {
182
+ if (e.metaKey || e.ctrlKey || e.altKey) return;
183
+ // a deck that is not presenting only claims the arrow keys; letters stay the app's
184
+ const owns = deck.hasAttribute("data-present") || deck.contains(document.activeElement) || deck.hasAttribute("data-solo");
185
+ switch (e.key) {
186
+ case "ArrowRight": case "PageDown": case " ":
187
+ if (typing(e)) return;
188
+ e.preventDefault(); next(); break;
189
+ case "ArrowLeft": case "PageUp":
190
+ if (typing(e)) return;
191
+ e.preventDefault(); prev(); break;
192
+ case "ArrowUp": if (typing(e) || cedes(deck, "ArrowUp")) return; e.preventDefault(); go(at - 1, 0); break;
193
+ case "ArrowDown": if (typing(e) || cedes(deck, "ArrowDown")) return; e.preventDefault(); go(at + 1, 0); break;
194
+ case "Home": if (typing(e)) return; e.preventDefault(); go(0, 0); break;
195
+ case "End": if (typing(e)) return; e.preventDefault(); go(total - 1, 0); break;
196
+ case "Escape": deck.dataset.view = "slides"; if (deck.hasAttribute("data-present")) togglePresent(); break;
197
+ default: break;
198
+ }
199
+ if (!owns || typing(e)) return;
200
+ switch (e.key.toLowerCase()) {
201
+ case "o": deck.dataset.view = deck.dataset.view === "grid" ? "slides" : "grid"; break;
202
+ case "n": deck.toggleAttribute("data-notes"); requestAnimationFrame(fitAll); break;
203
+ case "p": window.open(`${location.pathname}?presenter=1`, "grain-presenter", "width=1100,height=760"); break;
204
+ case "f": togglePresent(); break;
205
+ case "d": {
206
+ const root = document.documentElement;
207
+ const dark = root.dataset.colorScheme
208
+ ? root.dataset.colorScheme === "dark"
209
+ : matchMedia("(prefers-color-scheme: dark)").matches;
210
+ root.dataset.colorScheme = dark ? "light" : "dark";
211
+ break;
212
+ }
213
+ case ".": deck.style.visibility = deck.style.visibility === "hidden" ? "" : "hidden"; break;
214
+ default: break;
215
+ }
216
+ });
217
+
218
+ /* An app can claim a key for a slide's own control (the arrows on a guess marker, say) by
219
+ setting data-cede="ArrowUp ArrowDown" on the current slide. The deck yields, nothing more. */
220
+ function cedes(d, key) {
221
+ const s = d.querySelector(".presentation__slide[data-current]");
222
+ return !!s && (s.dataset.cede || "").split(/\s+/).includes(key);
223
+ }
224
+
225
+ function togglePresent() {
226
+ const on = deck.toggleAttribute("data-present");
227
+ if (on) document.documentElement.requestFullscreen?.().catch(() => { /* the deck is still full-bleed */ });
228
+ else if (document.fullscreenElement) document.exitFullscreen();
229
+ requestAnimationFrame(fitAll);
230
+ }
231
+ document.addEventListener("fullscreenchange", () => {
232
+ if (!document.fullscreenElement && deck.hasAttribute("data-present")) {
233
+ deck.removeAttribute("data-present");
234
+ requestAnimationFrame(fitAll);
235
+ }
236
+ });
237
+
238
+ // touch: a swipe is an arrow key
239
+ let x0 = null;
240
+ deck.addEventListener("touchstart", (e) => { x0 = e.changedTouches[0].clientX; }, { passive: true });
241
+ deck.addEventListener("touchend", (e) => {
242
+ if (x0 === null) return;
243
+ const dx = e.changedTouches[0].clientX - x0;
244
+ if (Math.abs(dx) > 45) (dx < 0 ? next : prev)();
245
+ x0 = null;
246
+ }, { passive: true });
247
+
248
+ /* ---- print: land everything first, so Cmd+P is the real deck ----------- */
249
+ addEventListener("beforeprint", () => {
250
+ // print swaps --u's source to viewport units (see the component's @media print block), so
251
+ // the fit pass has to run again under those rules or every slide prints at the screen's --fit
252
+ fitAll();
253
+ slides.forEach((s) => {
254
+ frags(s).forEach((f) => f.classList.add("is-on"));
255
+ s.querySelectorAll("[data-draw], [data-lit]").forEach((el) => el.classList.add("is-drawn"));
256
+ s.removeAttribute("data-settling");
257
+ });
258
+ deck.dispatchEvent(new CustomEvent("presentation:print"));
259
+ });
260
+
261
+ /* ---- the presenter window ---------------------------------------------- */
262
+ if (isPresenter) {
263
+ document.body.dataset.mode = "presenter";
264
+ const p = (sel) => document.querySelector(sel);
265
+ let t0 = Date.now();
266
+ const show = (i) => {
267
+ const s = slides[i];
268
+ if (p("[data-p-pos]")) p("[data-p-pos]").textContent = `${i + 1} / ${total}`;
269
+ if (p("[data-p-title]")) p("[data-p-title]").textContent = s.dataset.title || "";
270
+ if (p("[data-p-notes]")) p("[data-p-notes]").innerHTML = notesOf(s).map((n) => `<li>${n}</li>`).join("");
271
+ if (p("[data-p-next]")) p("[data-p-next]").textContent = slides[i + 1]?.dataset.title || "End of deck";
272
+ };
273
+ channel?.addEventListener("message", (e) => { if (e.data?.type === "state") show(e.data.at); });
274
+ addEventListener("keydown", (e) => {
275
+ if (e.key === "t" || e.key === "T") t0 = Date.now();
276
+ if (["ArrowRight", "ArrowLeft", "ArrowUp", "ArrowDown", " ", "Home", "End"].includes(e.key)) {
277
+ channel?.postMessage({ type: "key", key: e.key });
278
+ }
279
+ });
280
+ setInterval(() => {
281
+ const s = Math.floor((Date.now() - t0) / 1000);
282
+ const el = p("[data-p-clock]");
283
+ if (!el) return;
284
+ el.textContent = `${String(Math.floor(s / 60)).padStart(2, "0")}:${String(s % 60).padStart(2, "0")}`;
285
+ el.toggleAttribute("data-over", s > Number(deck.dataset.minutes || 20) * 60);
286
+ }, 500);
287
+ show(0);
288
+ } else {
289
+ channel?.addEventListener("message", (e) => {
290
+ if (e.data?.type !== "key") return;
291
+ const k = e.data.key;
292
+ if (k === "ArrowRight" || k === " ") next();
293
+ else if (k === "ArrowLeft") prev();
294
+ else if (k === "ArrowUp") go(at - 1, 0);
295
+ else if (k === "ArrowDown") go(at + 1, 0);
296
+ else if (k === "Home") go(0, 0);
297
+ else if (k === "End") go(total - 1, 0);
298
+ });
299
+ }
300
+
301
+ /* ---- boot -------------------------------------------------------------- */
302
+ addEventListener("resize", fitAll);
303
+ if (document.fonts) document.fonts.ready.then(fitAll);
304
+ // the deck may live in a pane that resizes without the window (a rail collapsing, say)
305
+ if ("ResizeObserver" in window) new ResizeObserver(() => fitAll()).observe(deck);
306
+ fitAll();
307
+ const fromHash = Number(location.hash.slice(1));
308
+ go(deck.hasAttribute("data-hash") && Number.isFinite(fromHash) && fromHash >= 1 ? fromHash - 1 : 0, 0);
309
+
310
+ (window.grain ||= {}).presentation = { go, next, prev, fitAll, deck };
311
+ }
312
+
313
+ if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", init);
314
+ else init();
315
+ })();
@@ -0,0 +1,77 @@
1
+ // grain/scripts/presentation.test.ts — DRIFT GUARD for the presentation island.
2
+ //
3
+ // Scope, stated honestly: this package has no DOM in test (zero runtime deps, and a hand-rolled
4
+ // fake would test the fake), so these are grep-style contract guards in the drawer.test.ts mould.
5
+ // They catch an obligation being DELETED, which is the realistic regression. Whether the fit pass
6
+ // actually stops a slide overflowing on a projector is measured in a browser against a consumer,
7
+ // and nothing in this file claims otherwise.
8
+ import { test, expect } from "bun:test";
9
+ import { readFileSync } from "node:fs";
10
+ import { join } from "node:path";
11
+
12
+ const here = new URL(".", import.meta.url).pathname;
13
+ const js = readFileSync(join(here, "presentation.js"), "utf8");
14
+
15
+ test("mounting is opt-in on [data-deck], so decks in a page don't get claimed by accident", () => {
16
+ expect(js).toMatch(/querySelectorAll\("\.presentation\[data-deck\]"\)/);
17
+ });
18
+
19
+ test("it is idempotent, like the other islands: a second load is a no-op", () => {
20
+ expect(js).toMatch(/if \(window\.grain && window\.grain\.presentation\) return/);
21
+ });
22
+
23
+ test("the seam is one event carrying enough for an app to drive its own figures", () => {
24
+ expect(js).toMatch(/new CustomEvent\("presentation:slide"/);
25
+ for (const key of ["index", "step", "slide", "title", "entered", "total"]) {
26
+ expect(js).toMatch(new RegExp(`\\b${key}\\b`));
27
+ }
28
+ });
29
+
30
+ test("the fit pass measures the WORST case: expandable cards open", () => {
31
+ // clicking a card on stage must never overflow a slide that fit a moment ago
32
+ expect(js).toMatch(/aria-expanded="false"/);
33
+ expect(js).toMatch(/setAttribute\("aria-expanded", "true"\)/);
34
+ });
35
+
36
+ test("the fit pass has a floor, so a runaway slide cannot loop forever", () => {
37
+ expect(js).toMatch(/while \(f > 0\.\d+ && body\.scrollHeight > room\(\)\)/);
38
+ });
39
+
40
+ test("print lands every fragment and figure first, then tells the app to do the same", () => {
41
+ const printBlock = js.slice(js.indexOf('addEventListener("beforeprint"'));
42
+ expect(printBlock).toMatch(/classList\.add\("is-on"\)/);
43
+ expect(printBlock).toMatch(/classList\.add\("is-drawn"\)/);
44
+ expect(printBlock).toMatch(/new CustomEvent\("presentation:print"\)/);
45
+ // print swaps the unit's source, so the fit pass has to run again under those rules
46
+ expect(printBlock).toMatch(/fitAll\(\)/);
47
+ });
48
+
49
+ test("a slide can claim a key back from the deck with data-cede", () => {
50
+ expect(js).toMatch(/dataset\.cede/);
51
+ expect(js).toMatch(/function cedes/);
52
+ });
53
+
54
+ test("the presenter window is the same page, synced, and never shown to the room", () => {
55
+ expect(js).toMatch(/presenter=1/);
56
+ expect(js).toMatch(/BroadcastChannel/);
57
+ expect(js).toMatch(/postMessage\(\{ type: "key"/);
58
+ });
59
+
60
+ test("typing in a field never moves the deck", () => {
61
+ expect(js).toMatch(/INPUT\|TEXTAREA\|SELECT/);
62
+ expect(js).toMatch(/isContentEditable/);
63
+ });
64
+
65
+ test("the fit pass re-runs on resize and on fonts loading, not once at boot", () => {
66
+ expect(js).toMatch(/addEventListener\("resize", fitAll\)/);
67
+ expect(js).toMatch(/document\.fonts.*fitAll/s);
68
+ expect(js).toMatch(/ResizeObserver/);
69
+ });
70
+
71
+ test("the public seam is small and named, and there is no back channel into the deck", () => {
72
+ expect(js).toMatch(/\(window\.grain \|\|= \{\}\)\.presentation = \{ go, next, prev, fitAll, deck \}/);
73
+ });
74
+
75
+ test("no em-dashes, per the standards this package follows", () => {
76
+ expect(js).not.toContain("—");
77
+ });