@n1kname/components 0.26.0 → 0.28.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n1kname/components",
3
- "version": "0.26.0",
3
+ "version": "0.28.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
package/src/Clip.astro ADDED
@@ -0,0 +1,198 @@
1
+ ---
2
+ /**
3
+ * A short looping product clip, for where movement carries the point better than a still: a flow
4
+ * running, a theme swapping, a filter applied. On a sunken plate, with a caption.
5
+ *
6
+ * Three things it gets right, which is why it is a component rather than a bare <video>:
7
+ * 1. It never starts on its own for someone who asked for reduced motion. CSS can't cancel
8
+ * autoplay, so it is started from script, and only when motion is welcome and the clip is
9
+ * on screen.
10
+ * 2. Anything that moves for more than five seconds needs a way to stop it (WCAG 2.2.2). The
11
+ * Pause button is always there, never a hover reveal, so touch and keyboard have it too.
12
+ * 3. The still frame (`poster`) is required with a clip: it is what shows before the video
13
+ * loads, and all there is for anyone who never gets the video.
14
+ *
15
+ * Leave `src` off and a labelled placeholder holds the place at the same size, so a page can be
16
+ * written and reviewed before the clip exists.
17
+ */
18
+ interface Props {
19
+ /** The clip's address. Leave off for a placeholder. */
20
+ src?: string;
21
+ /** The still frame's address. Required with `src`. */
22
+ poster?: string;
23
+ /** What the clip shows, for a screen reader, and on the placeholder. */
24
+ alt: string;
25
+ caption?: string;
26
+ width: number;
27
+ height: number;
28
+ /** Widest the clip is drawn, in px. */
29
+ maxWidth?: number;
30
+ type?: string;
31
+ /** The placeholder's label. */
32
+ placeholder?: string;
33
+ class?: string;
34
+ }
35
+
36
+ const { src, poster, alt, caption, width, height, maxWidth = 1240, type = 'video/mp4', placeholder = 'Clip to add', class: className } = Astro.props;
37
+
38
+ if (src && !poster) {
39
+ throw new Error(
40
+ `Clip "${src}" has no poster. A clip without a still frame is a blank rectangle until it downloads, and stays blank for anyone who blocks video.`,
41
+ );
42
+ }
43
+ ---
44
+
45
+ <figure class:list={['clip', className]}>
46
+ <div class="plate">
47
+ {
48
+ src ? (
49
+ <div class="frame" style={`max-width: ${maxWidth}px; aspect-ratio: ${width} / ${height}`}>
50
+ <video data-clip muted loop playsinline preload="metadata" poster={poster} aria-label={alt}>
51
+ <source src={src} type={type} />
52
+ </video>
53
+ <button type="button" class="toggle" data-clip-toggle aria-pressed="false">
54
+ <span data-label>Play</span>
55
+ </button>
56
+ </div>
57
+ ) : (
58
+ <div class="placeholder" role="img" aria-label={`Placeholder. ${alt}`} style={`max-width: ${maxWidth}px; aspect-ratio: ${width} / ${height}`}>
59
+ <span class="pl-label">{placeholder}</span>
60
+ <span class="spec">
61
+ {width} × {height}
62
+ </span>
63
+ <span class="brief">{alt}</span>
64
+ </div>
65
+ )
66
+ }
67
+ </div>
68
+ {caption && <figcaption>{caption}</figcaption>}
69
+ </figure>
70
+
71
+ <script>
72
+ const reduce = matchMedia('(prefers-reduced-motion: reduce)');
73
+ document.querySelectorAll<HTMLVideoElement>('video[data-clip]').forEach((video) => {
74
+ const frame = video.closest('.frame');
75
+ const button = frame?.querySelector<HTMLButtonElement>('[data-clip-toggle]');
76
+ const label = button?.querySelector('[data-label]');
77
+ const sync = () => {
78
+ const playing = !video.paused && !video.ended;
79
+ button?.setAttribute('aria-pressed', String(playing));
80
+ if (label) label.textContent = playing ? 'Pause' : 'Play';
81
+ };
82
+ button?.addEventListener('click', () => (video.paused ? video.play() : video.pause()));
83
+ video.addEventListener('play', sync);
84
+ video.addEventListener('pause', sync);
85
+ // Starts on its own only when motion is welcome, and only once it is on screen: a clip already
86
+ // scrolled past shouldn't spend bandwidth.
87
+ if (!reduce.matches) {
88
+ new IntersectionObserver(
89
+ (entries, obs) => {
90
+ if (!entries.some((e) => e.isIntersecting)) return;
91
+ video.play().catch(() => {});
92
+ obs.disconnect();
93
+ },
94
+ { rootMargin: '100px' },
95
+ ).observe(video);
96
+ }
97
+ sync();
98
+ });
99
+ </script>
100
+
101
+ <style>
102
+ .clip {
103
+ margin-block: var(--sp-6) var(--sp-7);
104
+ }
105
+
106
+ .plate {
107
+ display: flex;
108
+ justify-content: center;
109
+ padding: var(--sp-6);
110
+ background: var(--c-surface-sunken);
111
+ border: 1px solid var(--c-border);
112
+ border-radius: var(--radius-lg);
113
+ }
114
+
115
+ .frame {
116
+ position: relative;
117
+ width: 100%;
118
+ }
119
+
120
+ video {
121
+ display: block;
122
+ width: 100%;
123
+ height: 100%;
124
+ object-fit: cover;
125
+ border: 1px solid var(--c-border);
126
+ border-radius: var(--radius-sm);
127
+ background: var(--c-surface);
128
+ }
129
+
130
+ .toggle {
131
+ position: absolute;
132
+ right: var(--sp-3);
133
+ bottom: var(--sp-3);
134
+ display: inline-flex;
135
+ align-items: center;
136
+ gap: var(--sp-2);
137
+ padding: 5px var(--sp-3);
138
+ border: 1px solid var(--c-border);
139
+ border-radius: var(--radius-full);
140
+ background: var(--c-surface);
141
+ color: var(--c-text);
142
+ font-family: var(--font-body);
143
+ font-size: var(--text-body-sm);
144
+ line-height: 1.4;
145
+ cursor: pointer;
146
+ transition: border-color var(--motion-hover) var(--ease-hover);
147
+ }
148
+
149
+ .toggle:hover {
150
+ border-color: var(--c-border-strong);
151
+ }
152
+
153
+ .toggle:focus-visible {
154
+ outline: 2px solid var(--c-focus);
155
+ outline-offset: 2px;
156
+ }
157
+
158
+ .placeholder {
159
+ display: grid;
160
+ place-content: center;
161
+ justify-items: center;
162
+ gap: var(--sp-2);
163
+ width: 100%;
164
+ padding: var(--sp-6);
165
+ text-align: center;
166
+ background: var(--c-surface);
167
+ border: 1px dashed var(--c-border-strong);
168
+ border-radius: var(--radius-sm);
169
+ }
170
+
171
+ .pl-label {
172
+ font-family: var(--font-body);
173
+ font-weight: 600;
174
+ font-size: var(--text-body-sm);
175
+ color: var(--c-text);
176
+ }
177
+
178
+ .spec {
179
+ font-family: var(--font-mono);
180
+ font-size: var(--text-caption);
181
+ color: var(--c-text-muted);
182
+ }
183
+
184
+ .brief {
185
+ max-width: 46ch;
186
+ margin-top: var(--sp-2);
187
+ font-size: var(--text-caption);
188
+ line-height: 1.5;
189
+ color: var(--c-text-muted);
190
+ }
191
+
192
+ figcaption {
193
+ max-width: var(--layout-measure);
194
+ margin-top: var(--sp-4);
195
+ font-size: var(--text-body-sm);
196
+ color: var(--c-text-muted);
197
+ }
198
+ </style>
@@ -67,7 +67,7 @@ const rounded = (pts: [number, number][], radii: number[]) =>
67
67
  return `${i ? 'L' : 'M'}${a[0]} ${a[1]} Q${p[0]} ${p[1]} ${b[0]} ${b[1]}`;
68
68
  })
69
69
  .join(' ') + ' Z';
70
- const big = rounded([[1, 60], [100, 1], [199, 60], [100, 119]], [5, 16, 5, 16]);
70
+ const big = rounded([[1, 70], [100, 1], [199, 70], [100, 139]], [5, 16, 5, 16]);
71
71
  const small = (x: number) => rounded([[x + 4, 50], [x + 50, 18], [x + 96, 50], [x + 50, 82]], [3, 9, 3, 9]);
72
72
  let hash = 0;
73
73
  for (const c of title) hash = (hash * 31 + c.charCodeAt(0)) | 0;
@@ -108,22 +108,22 @@ const columns = items.map((it) => (it.kind === 'moment' ? 'minmax(4.5rem, auto)'
108
108
  </svg>
109
109
  ) : (
110
110
  <>
111
- <svg viewBox="0 0 200 120" aria-hidden="true">
111
+ <svg viewBox="0 0 200 140" aria-hidden="true">
112
112
  <clipPath id={`${uid}-${i}`}>
113
113
  <path d={big} />
114
114
  </clipPath>
115
115
  <g clip-path={`url(#${uid}-${i})`}>
116
- <rect class="open" x="0" y="0" width="100" height="120" />
117
- <rect class="close" x="100" y="0" width="100" height="120" />
118
- <line class="split" x1="100" y1="0" x2="100" y2="120" vector-effect="non-scaling-stroke" />
116
+ <rect class="open" x="0" y="0" width="100" height="140" />
117
+ <rect class="close" x="100" y="0" width="100" height="140" />
118
+ <line class="split" x1="100" y1="0" x2="100" y2="140" vector-effect="non-scaling-stroke" />
119
119
  </g>
120
120
  <path class="edge" d={big} vector-effect="non-scaling-stroke" />
121
121
  </svg>
122
- <p class="halves">
122
+ <div class="halves">
123
123
  {it.phase.halves?.map((h) => (
124
124
  <span>{h.label}</span>
125
125
  ))}
126
- </p>
126
+ </div>
127
127
  </>
128
128
  )}
129
129
  </div>
@@ -277,7 +277,7 @@ const columns = items.map((it) => (it.kind === 'moment' ? 'minmax(4.5rem, auto)'
277
277
  align-self: center;
278
278
  position: relative;
279
279
  width: 100%;
280
- aspect-ratio: 5 / 3;
280
+ aspect-ratio: 10 / 7;
281
281
  }
282
282
 
283
283
  .repeat .shape {
@@ -324,8 +324,9 @@ const columns = items.map((it) => (it.kind === 'moment' ? 'minmax(4.5rem, auto)'
324
324
  stroke-width: 1.5;
325
325
  }
326
326
 
327
- /* Each label centred on its half's own middle, a third of the way in from its point, at one
328
- * size wherever the diamond is drawn. */
327
+ /* Each label centred on the middle of its half along the line through the points, where the
328
+ * half is widest at the label's height, at one size wherever the diamond is drawn. A label too
329
+ * long for its half wraps. */
329
330
  .halves {
330
331
  position: absolute;
331
332
  z-index: 2;
@@ -341,17 +342,17 @@ const columns = items.map((it) => (it.kind === 'moment' ? 'minmax(4.5rem, auto)'
341
342
  .halves span {
342
343
  position: absolute;
343
344
  top: 50%;
344
- width: 30%;
345
+ width: 40%;
345
346
  translate: -50% -50%;
346
347
  text-wrap: balance;
347
348
  }
348
349
 
349
350
  .halves span:first-child {
350
- left: 34%;
351
+ left: 27%;
351
352
  }
352
353
 
353
354
  .halves span:last-child {
354
- left: 66%;
355
+ left: 73%;
355
356
  }
356
357
 
357
358
  .name {
@@ -389,6 +390,13 @@ const columns = items.map((it) => (it.kind === 'moment' ? 'minmax(4.5rem, auto)'
389
390
  text-align: center;
390
391
  }
391
392
 
393
+ /* Where a phase is too narrow for its two notes side by side, they stack. */
394
+ @container (max-width: 960px) {
395
+ .notes {
396
+ grid-template-columns: 1fr;
397
+ }
398
+ }
399
+
392
400
  .note {
393
401
  margin: 0;
394
402
  font-size: var(--text-caption);
@@ -10,6 +10,7 @@
10
10
  * reduced motion, the diagram is simply there and nothing moves.
11
11
  */
12
12
  import { ICONS, type IconName } from './icons';
13
+ import DiagramZoom from './parts/DiagramZoom.astro';
13
14
 
14
15
  type Side = 'top' | 'right' | 'bottom' | 'left';
15
16
  type Tone = 'accent' | 'cool' | 'quiet' | 'solid';
@@ -66,7 +67,10 @@ interface Props {
66
67
  /** A short legend in the corner: lines of text, the first one bold. */
67
68
  note?: { x: number; y: number; lines: { text: string; tone?: Tone }[] };
68
69
  caption?: string;
69
- /** Narrowest the drawing may get before it scrolls instead. */
70
+ /**
71
+ * Narrowest the drawing reads well at. Narrower, it still fits the width, smaller, and an
72
+ * Enlarge button opens it full size in a dialog.
73
+ */
70
74
  minWidth?: number;
71
75
  /**
72
76
  * A second drawing of the same diagram for a narrow screen, below 560px wide: its own size, where
@@ -213,9 +217,9 @@ const lines = (label: string) => {
213
217
  ---
214
218
 
215
219
  <figure class:list={['flow', { 'has-narrow': narrow }]} data-flow>
216
- {views.map(({ kind, width, height, nodes, drawn, rings, note, minWidth }) => (
217
- <div class:list={['scroll', kind]} tabindex={kind === 'wide' ? '0' : undefined} role={kind === 'wide' ? 'region' : undefined} aria-label={kind === 'wide' ? title : undefined}>
218
- <svg viewBox={`0 0 ${width} ${height}`} role="img" aria-labelledby={`${uid}-${kind}-t ${uid}-${kind}-d`} style={minWidth ? `min-width: ${minWidth}px` : undefined}>
220
+ {views.map(({ kind, width, height, nodes, drawn, rings, note }) => (
221
+ <div class:list={['scroll', kind]}>
222
+ <svg class="drawing" viewBox={`0 0 ${width} ${height}`} role="img" aria-labelledby={`${uid}-${kind}-t ${uid}-${kind}-d`} data-zoom-source={kind === 'wide' ? '' : undefined}>
219
223
  <title id={`${uid}-${kind}-t`}>{title}</title>
220
224
  <desc id={`${uid}-${kind}-d`}>{desc}</desc>
221
225
 
@@ -294,6 +298,7 @@ const lines = (label: string) => {
294
298
  )
295
299
  }
296
300
  </svg>
301
+ {kind === 'wide' && <DiagramZoom title={title} min={minWidth} id={uid} />}
297
302
  </div>
298
303
  ))}
299
304
  {caption && <figcaption>{caption}</figcaption>}
@@ -351,19 +356,10 @@ const lines = (label: string) => {
351
356
 
352
357
  .has-narrow .scroll.narrow {
353
358
  display: block;
354
- overflow: visible;
355
359
  padding: var(--sp-5) var(--sp-3);
356
360
  }
357
361
  }
358
362
 
359
- .scroll {
360
- overflow-x: auto;
361
- background: var(--c-surface-sunken);
362
- border: 1px solid var(--c-border);
363
- border-radius: var(--radius-lg);
364
- padding: var(--sp-6) var(--sp-5);
365
- }
366
-
367
363
  svg {
368
364
  display: block;
369
365
  width: 100%;
@@ -0,0 +1,165 @@
1
+ ---
2
+ /**
3
+ * A sequence of stages, each with its number, its name, a line on what it was, and a picture of it:
4
+ * wireframes, prototype, beta, shipped. The words are text and only the screens are pictures, so it
5
+ * reads at any size, is found by search, and reflows: four across, then two, then one, by the width
6
+ * it has.
7
+ *
8
+ * The numbers carry the order. An arrow between columns would say the same and then break as soon
9
+ * as the row reflows.
10
+ *
11
+ * A picture is an imported image (it goes through astro:assets, sized and converted to webp) or an
12
+ * address. Leave it off and a labelled placeholder holds the slot.
13
+ */
14
+ import type { ImageMetadata } from 'astro';
15
+ import { Image } from 'astro:assets';
16
+
17
+ interface Step {
18
+ label: string;
19
+ body?: string;
20
+ image?: ImageMetadata | string;
21
+ /** What the picture shows. */
22
+ alt?: string;
23
+ }
24
+
25
+ interface Props {
26
+ steps: Step[];
27
+ caption?: string;
28
+ class?: string;
29
+ }
30
+
31
+ const { steps, caption, class: className } = Astro.props;
32
+ ---
33
+
34
+ <figure class:list={['steps', className]}>
35
+ <ol>
36
+ {
37
+ steps.map((s, i) => (
38
+ <li>
39
+ <div class="number">{String(i + 1).padStart(2, '0')}</div>
40
+ <div class="label">{s.label}</div>
41
+ {s.body && <div class="body">{s.body}</div>}
42
+ <div class="shot">
43
+ {typeof s.image === 'object' ? (
44
+ <Image
45
+ src={s.image}
46
+ alt={s.alt ?? ''}
47
+ widths={[...new Set([320, 640, 918].filter((w) => w <= (s.image as ImageMetadata).width))]}
48
+ sizes="(max-width: 620px) 100vw, (max-width: 1000px) 45vw, 310px"
49
+ format="webp"
50
+ quality={80}
51
+ loading="lazy"
52
+ />
53
+ ) : s.image ? (
54
+ <img src={s.image} alt={s.alt ?? ''} loading="lazy" />
55
+ ) : (
56
+ <div class="placeholder" role="img" aria-label={`Placeholder. ${s.alt ?? s.label}`}>
57
+ <span>{s.label}</span>
58
+ </div>
59
+ )}
60
+ </div>
61
+ </li>
62
+ ))
63
+ }
64
+ </ol>
65
+ {caption && <figcaption>{caption}</figcaption>}
66
+ </figure>
67
+
68
+ <style>
69
+ .steps {
70
+ margin-block: var(--sp-6) var(--sp-7);
71
+ container-type: inline-size;
72
+ }
73
+
74
+ ol {
75
+ display: grid;
76
+ grid-template-columns: repeat(4, minmax(0, 1fr));
77
+ align-items: start;
78
+ gap: var(--sp-5);
79
+ margin: 0;
80
+ padding: 0;
81
+ list-style: none;
82
+ }
83
+
84
+ @container (max-width: 860px) {
85
+ ol {
86
+ grid-template-columns: repeat(2, minmax(0, 1fr));
87
+ gap: var(--sp-6) var(--sp-5);
88
+ }
89
+ }
90
+
91
+ @container (max-width: 480px) {
92
+ ol {
93
+ grid-template-columns: minmax(0, 1fr);
94
+ }
95
+ }
96
+
97
+ /* Divs, not paragraphs, so a page's paragraph spacing can't reach in. */
98
+ .number {
99
+ margin-bottom: var(--sp-2);
100
+ font-family: var(--font-mono);
101
+ font-size: var(--text-caption);
102
+ line-height: var(--leading-caption);
103
+ font-variant-numeric: tabular-nums;
104
+ color: var(--c-text-accent);
105
+ }
106
+
107
+ .label {
108
+ margin-bottom: var(--sp-2);
109
+ font-family: var(--font-body);
110
+ font-weight: 600;
111
+ font-size: var(--text-sub);
112
+ line-height: var(--leading-sub);
113
+ letter-spacing: var(--tracking-sub);
114
+ color: var(--c-text);
115
+ text-wrap: pretty;
116
+ }
117
+
118
+ .body {
119
+ margin-bottom: var(--sp-4);
120
+ font-size: var(--text-body-sm);
121
+ line-height: 1.5;
122
+ color: var(--c-text-muted);
123
+ }
124
+
125
+ /* Each picture runs at its own height rather than cropped to a shared one: a fixed window left a
126
+ * sliver of the next screen showing, which reads as a mistake. The columns are top-aligned, so a
127
+ * ragged bottom edge costs nothing. */
128
+ .shot {
129
+ padding: var(--sp-3);
130
+ background: var(--c-surface-sunken);
131
+ border: 1px solid var(--c-border);
132
+ border-radius: var(--radius-sm);
133
+ }
134
+
135
+ .shot :global(img) {
136
+ display: block;
137
+ width: 100%;
138
+ height: auto;
139
+ border-radius: 2px;
140
+ }
141
+
142
+ .placeholder {
143
+ display: grid;
144
+ place-items: center;
145
+ aspect-ratio: 2 / 5;
146
+ background: var(--c-surface);
147
+ border: 1px dashed var(--c-border-strong);
148
+ border-radius: 2px;
149
+ }
150
+
151
+ .placeholder span {
152
+ padding: var(--sp-3);
153
+ font-family: var(--font-mono);
154
+ font-size: var(--text-caption);
155
+ color: var(--c-text-muted);
156
+ text-align: center;
157
+ }
158
+
159
+ figcaption {
160
+ max-width: var(--layout-measure);
161
+ margin-top: var(--sp-4);
162
+ font-size: var(--text-body-sm);
163
+ color: var(--c-text-muted);
164
+ }
165
+ </style>
@@ -0,0 +1,329 @@
1
+ ---
2
+ /**
3
+ * A hierarchy in tiers: who sits under whom, one row per tier, laid out for you. An organisation
4
+ * over its legal entities over their locations, say, with a band under the last tier for who uses
5
+ * all of it. Give it the tree; it places every node over the middle of what's under it and draws
6
+ * the lines down.
7
+ *
8
+ * Tiers read through tint and border, not a saturated fill: the top tier deepest, with a heavier
9
+ * edge, then lighter, then plain. Every colour is a diagram token, so it retints in dark mode. The
10
+ * tier names run down the left.
11
+ *
12
+ * It builds itself once, top down, as it comes into view: each tier settles in, then the lines to
13
+ * the next draw down. Without script, or with reduced motion, it is simply there. Narrower than
14
+ * `minWidth`, it fits the width, smaller, with an Enlarge button that opens it full size.
15
+ */
16
+ import DiagramZoom from './parts/DiagramZoom.astro';
17
+
18
+ interface TierNode {
19
+ label: string;
20
+ /** A second, quieter line. */
21
+ sub?: string;
22
+ children?: TierNode[];
23
+ }
24
+
25
+ interface Props {
26
+ /** What the diagram is, and what it shows in words, for a screen reader. */
27
+ title: string;
28
+ desc: string;
29
+ /** One or more at the top. */
30
+ root: TierNode | TierNode[];
31
+ /** The tiers' names, top first, down the left. */
32
+ tiers?: string[];
33
+ /** A band under the last tier, across all of it, with one line of text. Its name, if any, is
34
+ * the entry in `tiers` after the tree's own. */
35
+ base?: string;
36
+ caption?: string;
37
+ /** The width it reads well at. */
38
+ minWidth?: number;
39
+ class?: string;
40
+ }
41
+
42
+ const { title, desc, root, tiers = [], base, caption, minWidth = 760, class: className } = Astro.props;
43
+
44
+ let hash = 0;
45
+ for (const c of title) hash = (hash * 31 + c.charCodeAt(0)) | 0;
46
+ const uid = `tiers-${(hash >>> 0).toString(36)}`;
47
+
48
+ // Geometry, in one place.
49
+ const LEFT = tiers.length ? 140 : 20; // where the tree starts, right of the tier names
50
+ const LEAF = 190; // a leaf's width
51
+ const GAP = 20; // between leaves under one parent
52
+ const GROUP = 75; // between leaves under different parents
53
+ const ROW = 110; // from one tier to the next
54
+ const NODE = 60; // a node's height
55
+ const BAND = 50; // the base band's height
56
+ const TOP = 12;
57
+
58
+ interface Placed {
59
+ label: string;
60
+ sub?: string;
61
+ depth: number;
62
+ cx: number;
63
+ w: number;
64
+ y: number;
65
+ kids: Placed[];
66
+ parent?: Placed;
67
+ }
68
+
69
+ // Leaves are placed left to right; a parent sits over the middle of its children.
70
+ let cursor = LEFT;
71
+ let lastParent: Placed | undefined | null = null;
72
+ const place = (n: TierNode, depth: number, parent?: Placed): Placed => {
73
+ const p: Placed = { label: n.label, sub: n.sub, depth, cx: 0, w: LEAF, y: TOP + depth * ROW, kids: [], parent };
74
+ if (n.children?.length) {
75
+ p.kids = n.children.map((c) => place(c, depth + 1, p));
76
+ const first = p.kids[0];
77
+ const last = p.kids[p.kids.length - 1];
78
+ p.cx = (first.cx + last.cx) / 2;
79
+ const span = last.cx + last.w / 2 - (first.cx - first.w / 2);
80
+ p.w = Math.min(Math.max(LEAF + 90, span * 0.6), 340);
81
+ } else {
82
+ if (lastParent !== null) cursor += lastParent === parent ? GAP : GROUP;
83
+ p.cx = cursor + LEAF / 2;
84
+ cursor += LEAF;
85
+ lastParent = parent;
86
+ }
87
+ return p;
88
+ };
89
+ const roots = (Array.isArray(root) ? root : [root]).map((r) => place(r, 0));
90
+
91
+ const all: Placed[] = [];
92
+ const walk = (p: Placed) => {
93
+ all.push(p);
94
+ p.kids.forEach(walk);
95
+ };
96
+ roots.forEach(walk);
97
+ const leaves = all.filter((p) => !p.kids.length);
98
+ const depth = Math.max(...all.map((p) => p.depth));
99
+ const tone = (d: number) => (d === 0 ? 'primary' : d === 1 && depth > 1 ? 'second' : 'leaf');
100
+
101
+ const left = Math.min(...all.map((p) => p.cx - p.w / 2));
102
+ const right = Math.max(...all.map((p) => p.cx + p.w / 2));
103
+ const bandY = TOP + (depth + 1) * ROW - 7;
104
+ const width = Math.ceil(right + 20);
105
+ const height = Math.ceil((base ? bandY + BAND : TOP + depth * ROW + NODE) + 12);
106
+
107
+ // The lines from each parent down to its children: down to halfway, across, down to each.
108
+ const lines = all
109
+ .filter((p) => p.kids.length)
110
+ .map((p) => {
111
+ const bottom = p.y + NODE;
112
+ const mid = bottom + (ROW - NODE) / 2;
113
+ const xs = p.kids.map((k) => k.cx);
114
+ let d = `M${p.cx} ${bottom} V${mid}`;
115
+ const lo = Math.min(p.cx, ...xs);
116
+ const hi = Math.max(p.cx, ...xs);
117
+ if (hi > lo) d += ` M${lo} ${mid} H${hi}`;
118
+ for (const k of p.kids) d += ` M${k.cx} ${mid} V${k.y}`;
119
+ return { d, step: p.depth * 2 + 1 };
120
+ });
121
+ const bandLines = base ? leaves.map((l) => `M${l.cx} ${l.y + NODE} V${bandY}`).join(' ') : '';
122
+ const labelX = LEFT - 16;
123
+ ---
124
+
125
+ <figure class:list={['tiers', className]} data-tiers>
126
+ <div class="plate">
127
+ <svg viewBox={`0 0 ${width} ${height}`} role="img" aria-labelledby={`${uid}-t ${uid}-d`} data-zoom-source>
128
+ <title id={`${uid}-t`}>{title}</title>
129
+ <desc id={`${uid}-d`}>{desc}</desc>
130
+
131
+ {
132
+ tiers.length > 0 && (
133
+ <g class="tier-names">
134
+ {tiers.map((t, i) =>
135
+ i <= depth ? (
136
+ <text data-step={i * 2} x={labelX} y={TOP + i * ROW + 34}>
137
+ {t}
138
+ </text>
139
+ ) : (
140
+ base &&
141
+ i === depth + 1 && (
142
+ <text data-step={depth * 2 + 2} x={labelX} y={bandY + 30}>
143
+ {t}
144
+ </text>
145
+ )
146
+ ),
147
+ )}
148
+ </g>
149
+ )
150
+ }
151
+
152
+ <g class="lines">
153
+ {lines.map((l) => <path data-step={l.step} d={l.d} />)}
154
+ {bandLines && <path data-step={depth * 2 + 1} d={bandLines} />}
155
+ </g>
156
+
157
+ {
158
+ all.map((p) => (
159
+ <g class:list={['node', tone(p.depth)]} data-step={p.depth * 2}>
160
+ <rect x={p.cx - p.w / 2} y={p.y} width={p.w} height={NODE} rx="10" />
161
+ <text x={p.cx} y={p.y + (p.sub ? 26 : 35)} class="name">
162
+ {p.label}
163
+ </text>
164
+ {p.sub && (
165
+ <text x={p.cx} y={p.y + 46} class="sub">
166
+ {p.sub}
167
+ </text>
168
+ )}
169
+ </g>
170
+ ))
171
+ }
172
+
173
+ {
174
+ base && (
175
+ <g class="node passive" data-step={depth * 2 + 2}>
176
+ <rect x={left} y={bandY} width={right - left} height={BAND} rx="10" />
177
+ <text x={(left + right) / 2} y={bandY + 30} class="sub">
178
+ {base}
179
+ </text>
180
+ </g>
181
+ )
182
+ }
183
+ </svg>
184
+ <DiagramZoom title={title} min={minWidth} id={uid} />
185
+ </div>
186
+ {caption && <figcaption>{caption}</figcaption>}
187
+ </figure>
188
+
189
+ <script>
190
+ // Even steps are a tier settling in, odd steps the lines drawing down to the next.
191
+ const STEP = 170;
192
+ if (matchMedia('(prefers-reduced-motion: no-preference)').matches) {
193
+ document.querySelectorAll<SVGSVGElement>('[data-tiers] svg[data-zoom-source]').forEach((svg) => {
194
+ const root = getComputedStyle(document.documentElement);
195
+ const spring = root.getPropertyValue('--ease-spring').trim() || 'cubic-bezier(0.22, 1, 0.36, 1)';
196
+ // A tier settles in three quarters of the system's grow.
197
+ const g = root.getPropertyValue('--motion-grow').trim();
198
+ const settle = (parseFloat(g) * (g.endsWith('ms') ? 1 : 1000) || 700) * 0.75;
199
+ const parts = [...svg.querySelectorAll<SVGElement>('[data-step]')].map((el) => {
200
+ const delay = Number(el.dataset.step) * STEP;
201
+ const a =
202
+ el instanceof SVGPathElement
203
+ ? (el.setAttribute('pathLength', '1'),
204
+ el.animate([{ strokeDasharray: '1 1', strokeDashoffset: 1 }, { strokeDasharray: '1 1', strokeDashoffset: 0 }], {
205
+ duration: STEP * 1.6,
206
+ delay,
207
+ easing: 'cubic-bezier(0.3, 0, 0.2, 1)',
208
+ fill: 'backwards',
209
+ }))
210
+ : el.animate([{ opacity: 0, transform: 'translateY(-8px)' }, { opacity: 1, transform: 'none' }], {
211
+ duration: settle,
212
+ delay,
213
+ easing: spring,
214
+ fill: 'backwards',
215
+ });
216
+ a.pause();
217
+ return a;
218
+ });
219
+ // Already on screen when the page opens: it is simply there.
220
+ if (svg.getBoundingClientRect().top < innerHeight * 0.85) {
221
+ parts.forEach((a) => a.finish());
222
+ return;
223
+ }
224
+ const io = new IntersectionObserver(
225
+ (entries) => {
226
+ if (!entries.some((e) => e.isIntersecting)) return;
227
+ io.disconnect();
228
+ parts.forEach((a) => a.play());
229
+ },
230
+ { rootMargin: '0px 0px -15% 0px' },
231
+ );
232
+ io.observe(svg);
233
+ });
234
+ }
235
+ </script>
236
+
237
+ <style>
238
+ .tiers {
239
+ margin-block: var(--sp-7);
240
+ }
241
+
242
+ .plate {
243
+ background: var(--c-surface-sunken);
244
+ border: 1px solid var(--c-border);
245
+ border-radius: var(--radius-lg);
246
+ padding: var(--sp-6) var(--sp-5);
247
+ }
248
+
249
+ svg {
250
+ display: block;
251
+ width: 100%;
252
+ height: auto;
253
+ }
254
+
255
+ rect {
256
+ stroke-width: 1.5;
257
+ }
258
+
259
+ .leaf rect {
260
+ fill: var(--c-dg-leaf-bg);
261
+ stroke: var(--c-dg-edge);
262
+ }
263
+
264
+ .leaf text {
265
+ fill: var(--c-dg-leaf-text);
266
+ }
267
+
268
+ .second rect {
269
+ fill: var(--c-dg-second-bg);
270
+ stroke: var(--c-dg-second-border);
271
+ }
272
+
273
+ .second text {
274
+ fill: var(--c-dg-second-text);
275
+ }
276
+
277
+ /* The top tier reads as first through a heavier edge and a deeper tint, not a saturated fill. */
278
+ .primary rect {
279
+ fill: var(--c-dg-primary-bg);
280
+ stroke: var(--c-dg-primary-border);
281
+ stroke-width: 2.5;
282
+ }
283
+
284
+ .primary text {
285
+ fill: var(--c-dg-primary-text);
286
+ }
287
+
288
+ .passive rect {
289
+ fill: var(--c-dg-passive-bg);
290
+ stroke: var(--c-dg-edge);
291
+ }
292
+
293
+ .passive text {
294
+ fill: var(--c-dg-passive-text);
295
+ }
296
+
297
+ .name {
298
+ font-family: var(--font-body);
299
+ font-weight: 600;
300
+ font-size: 15px;
301
+ text-anchor: middle;
302
+ }
303
+
304
+ .sub {
305
+ font-family: var(--font-body);
306
+ font-size: 12.5px;
307
+ text-anchor: middle;
308
+ }
309
+
310
+ .tier-names text {
311
+ font-family: var(--font-mono);
312
+ font-size: 12px;
313
+ text-anchor: end;
314
+ fill: var(--c-text-muted);
315
+ }
316
+
317
+ .lines path {
318
+ fill: none;
319
+ stroke: var(--c-dg-line);
320
+ stroke-width: 1.5;
321
+ }
322
+
323
+ figcaption {
324
+ max-width: var(--layout-measure);
325
+ margin-top: var(--sp-4);
326
+ font-size: var(--text-body-sm);
327
+ color: var(--c-text-muted);
328
+ }
329
+ </style>
@@ -0,0 +1,190 @@
1
+ ---
2
+ /**
3
+ * The Enlarge button and dialog a drawn diagram (Flow diagram, Tiers) carries. Put it inside the
4
+ * diagram's plate, after the drawing, and mark the drawing `data-zoom-source`. When the plate is
5
+ * narrower than `min`, the drawing fits it, smaller, and the button shows; it opens a copy of the
6
+ * drawing full size in a modal dialog, centred, to pan. Without script the button never shows.
7
+ */
8
+ interface Props {
9
+ title: string;
10
+ /** The width the drawing reads well at, in px. */
11
+ min: number;
12
+ id: string;
13
+ }
14
+
15
+ const { title, min, id } = Astro.props;
16
+ ---
17
+
18
+ <div class="zoom-host" data-diagram-zoom data-min={min}>
19
+ <button type="button" class="enlarge" data-enlarge hidden>
20
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
21
+ <path d="M15 3h6v6M9 21H3v-6M21 3l-7 7M3 21l7-7"></path>
22
+ </svg>
23
+ Enlarge<span class="visually-hidden">: {title}</span>
24
+ </button>
25
+ <dialog class="zoom" aria-labelledby={`${id}-zoom`}>
26
+ <div class="zoom-bar">
27
+ <p class="zoom-title" id={`${id}-zoom`}>{title}</p>
28
+ <button type="button" class="close" data-close>Close</button>
29
+ </div>
30
+ <div class="zoom-scroll" tabindex="0" role="region" aria-label={`${title}, full size`} data-zoom-body></div>
31
+ </dialog>
32
+ </div>
33
+
34
+ <style>
35
+ .zoom-host {
36
+ display: flex;
37
+ justify-content: flex-end;
38
+ }
39
+
40
+ .enlarge {
41
+ display: inline-flex;
42
+ align-items: center;
43
+ gap: var(--sp-1);
44
+ margin-top: var(--sp-3);
45
+ padding: var(--sp-1) var(--sp-3);
46
+ border: 1px solid var(--c-border);
47
+ border-radius: var(--radius-full);
48
+ background: var(--c-surface);
49
+ color: var(--c-text);
50
+ font: inherit;
51
+ font-size: var(--text-caption);
52
+ font-weight: 600;
53
+ cursor: pointer;
54
+ transition: border-color var(--motion-hover) var(--ease-hover);
55
+ }
56
+
57
+ .enlarge[hidden] {
58
+ display: none;
59
+ }
60
+
61
+ .enlarge svg {
62
+ display: inline-block;
63
+ width: 16px;
64
+ height: 16px;
65
+ }
66
+
67
+ .enlarge:hover {
68
+ border-color: var(--c-border-strong);
69
+ }
70
+
71
+ .enlarge:focus-visible,
72
+ .close:focus-visible,
73
+ .zoom-scroll:focus-visible {
74
+ outline: 2px solid var(--c-focus);
75
+ outline-offset: 2px;
76
+ }
77
+
78
+ .visually-hidden {
79
+ position: absolute;
80
+ width: 1px;
81
+ height: 1px;
82
+ overflow: hidden;
83
+ clip-path: inset(50%);
84
+ white-space: nowrap;
85
+ }
86
+
87
+ .zoom {
88
+ width: calc(100vw - var(--sp-4));
89
+ max-width: none;
90
+ height: calc(100dvh - var(--sp-4));
91
+ max-height: none;
92
+ padding: 0;
93
+ border: 1px solid var(--c-border);
94
+ border-radius: var(--radius-lg);
95
+ background: var(--c-bg);
96
+ color: var(--c-text);
97
+ }
98
+
99
+ .zoom[open] {
100
+ display: flex;
101
+ flex-direction: column;
102
+ }
103
+
104
+ .zoom::backdrop {
105
+ background: color-mix(in oklab, var(--c-bg) 70%, transparent);
106
+ }
107
+
108
+ .zoom-bar {
109
+ display: flex;
110
+ align-items: center;
111
+ justify-content: space-between;
112
+ gap: var(--sp-3);
113
+ padding: var(--sp-3) var(--sp-4);
114
+ border-bottom: 1px solid var(--c-border);
115
+ }
116
+
117
+ .zoom-title {
118
+ margin: 0;
119
+ font-weight: 600;
120
+ font-size: var(--text-body-sm);
121
+ }
122
+
123
+ .close {
124
+ padding: var(--sp-1) var(--sp-3);
125
+ border: 1px solid var(--c-border);
126
+ border-radius: var(--radius-full);
127
+ background: var(--c-surface);
128
+ color: var(--c-text);
129
+ font: inherit;
130
+ font-size: var(--text-caption);
131
+ font-weight: 600;
132
+ cursor: pointer;
133
+ }
134
+
135
+ /* Full size, so the words read; it pans where the screen is smaller. */
136
+ .zoom-scroll {
137
+ flex: 1;
138
+ overflow: auto;
139
+ padding: var(--sp-5);
140
+ background: var(--c-surface-sunken);
141
+ }
142
+
143
+ .zoom-scroll > :global(svg) {
144
+ display: block;
145
+ width: var(--full);
146
+ min-width: var(--full);
147
+ max-width: none;
148
+ height: auto;
149
+ overflow: visible;
150
+ }
151
+ </style>
152
+
153
+ <script>
154
+ document.querySelectorAll<HTMLElement>('[data-diagram-zoom]').forEach((host) => {
155
+ const min = Number(host.dataset.min) || 0;
156
+ const plate = host.parentElement!;
157
+ const fig = host.closest('figure');
158
+ const source = plate.querySelector<SVGSVGElement>('[data-zoom-source]');
159
+ const button = host.querySelector<HTMLButtonElement>('[data-enlarge]')!;
160
+ const dialog = host.querySelector<HTMLDialogElement>('dialog')!;
161
+ const body = host.querySelector<HTMLElement>('[data-zoom-body]')!;
162
+ if (!source) return;
163
+ const fit = () => {
164
+ const small = plate.offsetParent !== null && plate.clientWidth < min;
165
+ button.hidden = !small;
166
+ fig?.classList.toggle('is-small', small);
167
+ };
168
+ new ResizeObserver(fit).observe(plate);
169
+ fit();
170
+ button.addEventListener('click', () => {
171
+ const copy = source.cloneNode(true) as SVGSVGElement;
172
+ copy.removeAttribute('aria-labelledby');
173
+ copy.removeAttribute('data-zoom-source');
174
+ copy.setAttribute('aria-hidden', 'true');
175
+ copy.querySelectorAll('[id]').forEach((el) => el.removeAttribute('id'));
176
+ copy.style.setProperty('--full', `${Math.max(min, 640)}px`);
177
+ body.replaceChildren(copy);
178
+ dialog.showModal();
179
+ // Open on the middle of the drawing, where its point usually is.
180
+ body.scrollLeft = (body.scrollWidth - body.clientWidth) / 2;
181
+ });
182
+ dialog.querySelector('[data-close]')?.addEventListener('click', () => dialog.close());
183
+ // Back to Enlarge on close, however it closed: Safari doesn't focus a button that's clicked,
184
+ // so it has nothing to go back to on its own.
185
+ dialog.addEventListener('close', () => button.focus());
186
+ dialog.addEventListener('click', (e) => {
187
+ if (e.target === dialog) dialog.close();
188
+ });
189
+ });
190
+ </script>