@n1kname/components 0.27.0 → 0.29.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.
@@ -5,9 +5,10 @@
5
5
  * margin beside the reading column.
6
6
  *
7
7
  * It finds where you are from the headings its links point to: the last one above the top third of
8
- * the window is current, and its link gets `aria-current="location"`. That changes as you scroll,
9
- * many times a minute, so it changes at once rather than gliding. Before the first heading nothing
10
- * is current. Without script it is a plain list of links.
8
+ * the window is current, and its link gets `aria-current="location"`. A marker on the rule glides to
9
+ * it over --motion-base, short enough to keep up with scrolling, and the text brightens as it lands;
10
+ * under reduced motion it moves at once. Before the first heading nothing is current. Without
11
+ * script it is a plain list of links.
11
12
  *
12
13
  * The page places it: it is sticky inside whatever holds it, so give it a column or a margin that
13
14
  * runs the length of the text. It never hides itself, so it is always in the tab order.
@@ -34,6 +35,7 @@ const hash = (h: string) => (h.startsWith('#') ? h : `#${h}`);
34
35
 
35
36
  <nav id={id} class:list={['section-nav', className]} aria-label={label} data-section-nav>
36
37
  {title && <p class="title" aria-hidden="true">{title}</p>}
38
+ <span class="marker" aria-hidden="true"></span>
37
39
  <ol>
38
40
  {
39
41
  items.map((item) => (
@@ -72,6 +74,31 @@ const hash = (h: string) => (h.startsWith('#') ? h : `#${h}`);
72
74
  border-left: 1px solid var(--c-border);
73
75
  }
74
76
 
77
+ /* Where you are: one bar on the rule that glides from section to section. */
78
+ .marker {
79
+ position: absolute;
80
+ top: 0;
81
+ left: 0;
82
+ width: 2px;
83
+ height: var(--h, 0);
84
+ background: var(--c-accent);
85
+ border-radius: 1px;
86
+ opacity: 0;
87
+ translate: 0 var(--y, 0);
88
+ transition:
89
+ translate var(--motion-base) var(--ease-out),
90
+ height var(--motion-base) var(--ease-out),
91
+ opacity var(--motion-fast) var(--ease-out);
92
+ }
93
+
94
+ .marker.on {
95
+ opacity: 1;
96
+ }
97
+
98
+ .marker.still {
99
+ transition: none;
100
+ }
101
+
75
102
  a {
76
103
  display: block;
77
104
  margin-left: -1px;
@@ -81,18 +108,17 @@ const hash = (h: string) => (h.startsWith('#') ? h : `#${h}`);
81
108
  color: var(--c-text-muted);
82
109
  text-decoration: none;
83
110
  text-wrap: pretty;
84
- transition: color var(--motion-hover) var(--ease-hover);
111
+ transition: color var(--motion-base) var(--ease-out);
85
112
  }
86
113
 
87
114
  a:hover {
88
115
  color: var(--c-text);
89
116
  }
90
117
 
91
- /* Where you are: the text at full strength and the accent bar on the rule. */
118
+ /* Where you are: the text at full strength; the marker carries the bar. Its weight stays the
119
+ * same, so the list never reflows as the marker moves. */
92
120
  a[aria-current] {
93
121
  color: var(--c-text);
94
- font-weight: 600;
95
- border-left-color: var(--c-accent);
96
122
  }
97
123
 
98
124
  a:focus-visible {
@@ -102,8 +128,8 @@ const hash = (h: string) => (h.startsWith('#') ? h : `#${h}`);
102
128
  }
103
129
 
104
130
  @media (forced-colors: active) {
105
- a[aria-current] {
106
- border-left-color: CanvasText;
131
+ .marker {
132
+ background: CanvasText;
107
133
  }
108
134
  }
109
135
  </style>
@@ -111,6 +137,19 @@ const hash = (h: string) => (h.startsWith('#') ? h : `#${h}`);
111
137
  <script>
112
138
  document.querySelectorAll<HTMLElement>('[data-section-nav]').forEach((nav) => {
113
139
  const links = [...nav.querySelectorAll<HTMLAnchorElement>('a[href^="#"]')];
140
+ const marker = nav.querySelector<HTMLElement>('.marker');
141
+ let placed = false;
142
+ const mark = (a?: HTMLAnchorElement) => {
143
+ if (!marker) return;
144
+ marker.classList.toggle('on', !!a);
145
+ if (!a) return;
146
+ // The first time in place; after that it glides.
147
+ marker.classList.toggle('still', !placed);
148
+ marker.style.setProperty('--y', `${a.parentElement!.offsetTop}px`);
149
+ marker.style.setProperty('--h', `${a.parentElement!.offsetHeight}px`);
150
+ if (!placed) requestAnimationFrame(() => marker.classList.remove('still'));
151
+ placed = true;
152
+ };
114
153
  const heads = links.map((a) => document.getElementById(decodeURIComponent(a.hash.slice(1))));
115
154
  let current = -2;
116
155
  const update = () => {
@@ -121,6 +160,7 @@ const hash = (h: string) => (h.startsWith('#') ? h : `#${h}`);
121
160
  if (now === current) return;
122
161
  current = now;
123
162
  links.forEach((a, i) => (i === now ? a.setAttribute('aria-current', 'location') : a.removeAttribute('aria-current')));
163
+ mark(links[now]);
124
164
  // Keep the current link in view when the list is taller than the window. The list scrolls
125
165
  // itself; the page never moves for it.
126
166
  const a = links[now];
@@ -143,7 +183,10 @@ const hash = (h: string) => (h.startsWith('#') ? h : `#${h}`);
143
183
  },
144
184
  { passive: true },
145
185
  );
146
- addEventListener('resize', update, { passive: true });
186
+ addEventListener('resize', () => {
187
+ update();
188
+ mark(links[current]);
189
+ }, { passive: true });
147
190
  update();
148
191
  });
149
192
  </script>
@@ -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>