@n1kname/components 0.15.0 → 0.16.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 +4 -3
- package/src/AppShell.astro +412 -0
- package/src/PeriodPicker.astro +264 -0
- package/src/SideNav.astro +188 -0
- package/src/icons.ts +27 -0
- package/src/page-change.css +41 -2
- package/src/period.js +160 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n1kname/components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -13,10 +13,11 @@
|
|
|
13
13
|
"./datatable.js": "./src/datatable.js",
|
|
14
14
|
"./dotrows.js": "./src/dotrows.js",
|
|
15
15
|
"./rangebars.js": "./src/rangebars.js",
|
|
16
|
-
"./daystrips.js": "./src/daystrips.js"
|
|
16
|
+
"./daystrips.js": "./src/daystrips.js",
|
|
17
|
+
"./period.js": "./src/period.js"
|
|
17
18
|
},
|
|
18
19
|
"dependencies": {
|
|
19
|
-
"@n1kname/charts": "0.
|
|
20
|
+
"@n1kname/charts": "0.7.0"
|
|
20
21
|
},
|
|
21
22
|
"peerDependencies": {
|
|
22
23
|
"astro": "^7.4.4"
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* The frame of a tool with many pages, such as a dashboard: its pages stand in a column on the
|
|
4
|
+
* left, in groups (SideNav), and the page beside it. The column folds to its icons and back with
|
|
5
|
+
* a button at its top, and the choice is kept for the next page. Narrower than 1024px, a menu
|
|
6
|
+
* button in a bar across the top opens the pages in a drawer from the left. Detail that opens on
|
|
7
|
+
* the right, such as a SidePanel, keeps the right: where you are on the left, detail on the right.
|
|
8
|
+
*
|
|
9
|
+
* Moving between pages, with page-change.css, the column holds still while the page beside it
|
|
10
|
+
* cross-fades, and the current page's marker glides to the link you chose.
|
|
11
|
+
*
|
|
12
|
+
* The page's own <main> and footer go in the default slot; a ThemeToggle or the like goes in
|
|
13
|
+
* rail-foot (wide) and top-end (narrow), one in each, with different ids.
|
|
14
|
+
*/
|
|
15
|
+
import SideNav from './SideNav.astro';
|
|
16
|
+
import { ICONS, type IconName } from './icons';
|
|
17
|
+
|
|
18
|
+
interface Item {
|
|
19
|
+
href: string;
|
|
20
|
+
label: string;
|
|
21
|
+
icon?: IconName;
|
|
22
|
+
current?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface Props {
|
|
26
|
+
groups: { label: string; items: Item[] }[];
|
|
27
|
+
/** The site's name, its link home and, if it has one, its mark. */
|
|
28
|
+
brand: { label: string; href: string; logo?: string };
|
|
29
|
+
/** Names the navigation for a screen reader. */
|
|
30
|
+
label?: string;
|
|
31
|
+
/** The words on the buttons, for a site in another language. */
|
|
32
|
+
labels?: { collapse?: string; expand?: string; menu?: string; close?: string };
|
|
33
|
+
/** Where the folded choice is kept in localStorage. */
|
|
34
|
+
storageKey?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const { groups, brand, label = 'Pages', labels = {}, storageKey = 'nav-collapsed' } = Astro.props;
|
|
38
|
+
const words = { collapse: 'Collapse sidebar', expand: 'Expand sidebar', menu: label, close: 'Close', ...labels };
|
|
39
|
+
const icon = (name: IconName, size = 18) =>
|
|
40
|
+
`<svg width="${size}" height="${size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[name]}</svg>`;
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
<div class="app-shell" data-app-shell data-storage-key={storageKey}>
|
|
44
|
+
<script is:inline define:vars={{ storageKey }}>
|
|
45
|
+
// Folded or not, set before the column is drawn, so it doesn't move on arrival.
|
|
46
|
+
try {
|
|
47
|
+
if (localStorage.getItem(storageKey) === '1') document.currentScript.parentElement.dataset.navCollapsed = '';
|
|
48
|
+
} catch {}
|
|
49
|
+
</script>
|
|
50
|
+
<aside class="rail" id="app-shell-rail" data-nav-scroll>
|
|
51
|
+
<div class="rail-head">
|
|
52
|
+
<a class="home" href={brand.href}>
|
|
53
|
+
{brand.logo && <img src={brand.logo} alt="" width="20" height="20" />}
|
|
54
|
+
<span>{brand.label}</span>
|
|
55
|
+
</a>
|
|
56
|
+
<button
|
|
57
|
+
type="button"
|
|
58
|
+
class="icon-button fold"
|
|
59
|
+
aria-controls="app-shell-rail"
|
|
60
|
+
aria-label={words.collapse}
|
|
61
|
+
title={words.collapse}
|
|
62
|
+
data-nav-collapse
|
|
63
|
+
data-collapse={words.collapse}
|
|
64
|
+
data-expand={words.expand}
|
|
65
|
+
>
|
|
66
|
+
<Fragment set:html={icon('panel-left')} />
|
|
67
|
+
</button>
|
|
68
|
+
</div>
|
|
69
|
+
<SideNav groups={groups} label={label} id="app-shell-nav" />
|
|
70
|
+
<div class="rail-foot"><slot name="rail-foot" /></div>
|
|
71
|
+
</aside>
|
|
72
|
+
|
|
73
|
+
<header class="top">
|
|
74
|
+
<button type="button" class="icon-button menu" aria-label={words.menu} aria-haspopup="dialog" aria-controls="app-shell-drawer" data-drawer-open>
|
|
75
|
+
<Fragment set:html={icon('menu', 20)} />
|
|
76
|
+
</button>
|
|
77
|
+
<a class="home" href={brand.href}>
|
|
78
|
+
{brand.logo && <img src={brand.logo} alt="" width="20" height="20" />}
|
|
79
|
+
<span>{brand.label}</span>
|
|
80
|
+
</a>
|
|
81
|
+
<div class="top-end"><slot name="top-end" /></div>
|
|
82
|
+
</header>
|
|
83
|
+
|
|
84
|
+
<dialog class="drawer" id="app-shell-drawer" aria-label={label}>
|
|
85
|
+
<div class="drawer-head">
|
|
86
|
+
<a class="home" href={brand.href}>
|
|
87
|
+
{brand.logo && <img src={brand.logo} alt="" width="20" height="20" />}
|
|
88
|
+
<span>{brand.label}</span>
|
|
89
|
+
</a>
|
|
90
|
+
<button type="button" class="icon-button" aria-label={words.close} data-drawer-close>
|
|
91
|
+
<Fragment set:html={icon('x', 20)} />
|
|
92
|
+
</button>
|
|
93
|
+
</div>
|
|
94
|
+
<SideNav groups={groups} label={label} marker={false} id="app-shell-drawer-nav" />
|
|
95
|
+
</dialog>
|
|
96
|
+
|
|
97
|
+
<div class="column">
|
|
98
|
+
<slot />
|
|
99
|
+
</div>
|
|
100
|
+
</div>
|
|
101
|
+
|
|
102
|
+
<script>
|
|
103
|
+
// A motion token in milliseconds; the tokens are written in seconds.
|
|
104
|
+
const ms = (name: string, fallback: number) => {
|
|
105
|
+
const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
|
106
|
+
const n = parseFloat(v);
|
|
107
|
+
return Number.isNaN(n) ? fallback : v.endsWith('ms') ? n : n * 1000;
|
|
108
|
+
};
|
|
109
|
+
const reduced = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
110
|
+
|
|
111
|
+
document.querySelectorAll<HTMLElement>('[data-app-shell]').forEach((shell) => {
|
|
112
|
+
// The column folds to its icons and back, and the choice is kept for the next page.
|
|
113
|
+
const fold = shell.querySelector<HTMLButtonElement>('[data-nav-collapse]')!;
|
|
114
|
+
const name = () => {
|
|
115
|
+
const words = shell.hasAttribute('data-nav-collapsed') ? fold.dataset.expand! : fold.dataset.collapse!;
|
|
116
|
+
fold.setAttribute('aria-label', words);
|
|
117
|
+
fold.title = words;
|
|
118
|
+
};
|
|
119
|
+
fold.addEventListener('click', () => {
|
|
120
|
+
const folded = shell.toggleAttribute('data-nav-collapsed');
|
|
121
|
+
try {
|
|
122
|
+
localStorage.setItem(shell.dataset.storageKey!, folded ? '1' : '0');
|
|
123
|
+
} catch {}
|
|
124
|
+
name();
|
|
125
|
+
});
|
|
126
|
+
name();
|
|
127
|
+
|
|
128
|
+
// The drawer leaves the way it came before it closes. Safari can't animate a dialog's close
|
|
129
|
+
// in CSS alone, so the leaving plays first and the dialog closes after.
|
|
130
|
+
const drawer = shell.querySelector<HTMLDialogElement>('dialog.drawer')!;
|
|
131
|
+
const shut = () => {
|
|
132
|
+
if (!drawer.open || drawer.hasAttribute('data-closing')) return;
|
|
133
|
+
if (reduced()) return drawer.close();
|
|
134
|
+
drawer.setAttribute('data-closing', '');
|
|
135
|
+
setTimeout(() => {
|
|
136
|
+
drawer.close();
|
|
137
|
+
drawer.removeAttribute('data-closing');
|
|
138
|
+
}, ms('--motion-base', 180));
|
|
139
|
+
};
|
|
140
|
+
shell.querySelector('[data-drawer-open]')?.addEventListener('click', () => drawer.showModal());
|
|
141
|
+
shell.querySelector('[data-drawer-close]')?.addEventListener('click', shut);
|
|
142
|
+
drawer.addEventListener('cancel', (e) => {
|
|
143
|
+
e.preventDefault();
|
|
144
|
+
shut();
|
|
145
|
+
});
|
|
146
|
+
// A click on the backdrop lands on the dialog itself; the current page's link only closes it.
|
|
147
|
+
drawer.addEventListener('click', (e) => {
|
|
148
|
+
if (e.target === drawer || (e.target as Element).closest('a[aria-current]')) shut();
|
|
149
|
+
});
|
|
150
|
+
// Wide again with the drawer open: the column has the pages, so the drawer goes.
|
|
151
|
+
matchMedia('(min-width: 1024px)').addEventListener('change', (m) => m.matches && drawer.close());
|
|
152
|
+
});
|
|
153
|
+
</script>
|
|
154
|
+
|
|
155
|
+
<style>
|
|
156
|
+
/* The column folds in step with its words fading. The one width that moves in the system: the
|
|
157
|
+
column it frees has to be laid out anyway, and it moves only when you ask it to. */
|
|
158
|
+
.app-shell {
|
|
159
|
+
--rail: 240px;
|
|
160
|
+
display: grid;
|
|
161
|
+
grid-template-columns: var(--rail) minmax(0, 1fr);
|
|
162
|
+
min-height: 100dvh;
|
|
163
|
+
transition: grid-template-columns var(--motion-slow) var(--ease-standard);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/* Folded, the column is the icons' 40px squares, its padding and its rule. */
|
|
167
|
+
.app-shell[data-nav-collapsed] {
|
|
168
|
+
--rail: 65px;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
.rail {
|
|
172
|
+
position: sticky;
|
|
173
|
+
top: 0;
|
|
174
|
+
height: 100dvh;
|
|
175
|
+
display: flex;
|
|
176
|
+
flex-direction: column;
|
|
177
|
+
gap: var(--sp-5);
|
|
178
|
+
padding: var(--sp-4) var(--sp-3);
|
|
179
|
+
border-right: 1px solid var(--c-border);
|
|
180
|
+
background: var(--c-bg);
|
|
181
|
+
overflow: hidden auto;
|
|
182
|
+
overscroll-behavior: contain;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.rail :global(.side-nav) {
|
|
186
|
+
flex: 1;
|
|
187
|
+
align-content: start;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.rail-head {
|
|
191
|
+
display: flex;
|
|
192
|
+
align-items: center;
|
|
193
|
+
gap: var(--sp-1);
|
|
194
|
+
margin-block: calc(-1 * var(--sp-1));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
.rail-head .home {
|
|
198
|
+
flex: 1 1 auto;
|
|
199
|
+
min-width: 0;
|
|
200
|
+
overflow: hidden;
|
|
201
|
+
transition: opacity var(--motion-base) var(--ease-standard);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
.app-shell[data-nav-collapsed] .rail-head .home {
|
|
205
|
+
opacity: 0;
|
|
206
|
+
visibility: hidden;
|
|
207
|
+
transition:
|
|
208
|
+
opacity var(--motion-base) var(--ease-standard),
|
|
209
|
+
visibility 0s var(--motion-base);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
.rail-foot {
|
|
213
|
+
display: flex;
|
|
214
|
+
justify-content: flex-end;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
.home {
|
|
218
|
+
display: inline-flex;
|
|
219
|
+
align-items: center;
|
|
220
|
+
gap: var(--sp-2);
|
|
221
|
+
padding-inline: 10px;
|
|
222
|
+
font-weight: 600;
|
|
223
|
+
color: var(--c-text);
|
|
224
|
+
text-decoration: none;
|
|
225
|
+
white-space: nowrap;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
.home img {
|
|
229
|
+
flex: none;
|
|
230
|
+
border-radius: var(--radius-sm);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
.icon-button {
|
|
234
|
+
flex: none;
|
|
235
|
+
display: inline-grid;
|
|
236
|
+
place-items: center;
|
|
237
|
+
width: 40px;
|
|
238
|
+
height: 40px;
|
|
239
|
+
border: 0;
|
|
240
|
+
border-radius: var(--radius-md);
|
|
241
|
+
background: none;
|
|
242
|
+
color: var(--c-text);
|
|
243
|
+
cursor: pointer;
|
|
244
|
+
transition:
|
|
245
|
+
background-color var(--motion-hover) var(--ease-hover),
|
|
246
|
+
color var(--motion-hover) var(--ease-hover);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
.icon-button:hover {
|
|
250
|
+
background: var(--c-surface-hover);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
.icon-button:focus-visible {
|
|
254
|
+
outline: 2px solid var(--c-focus);
|
|
255
|
+
outline-offset: 2px;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
.fold {
|
|
259
|
+
color: var(--c-text-muted);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
.fold:hover {
|
|
263
|
+
color: var(--c-text);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
.column {
|
|
267
|
+
min-width: 0;
|
|
268
|
+
padding: 0 var(--layout-gutter);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
.top {
|
|
272
|
+
display: none;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
.drawer {
|
|
276
|
+
margin: 0 auto 0 0;
|
|
277
|
+
width: min(300px, 86vw);
|
|
278
|
+
max-width: 100vw;
|
|
279
|
+
height: 100dvh;
|
|
280
|
+
max-height: 100dvh;
|
|
281
|
+
padding: var(--sp-3) var(--sp-3) var(--sp-6);
|
|
282
|
+
border: 0;
|
|
283
|
+
border-inline-end: 1px solid var(--c-border);
|
|
284
|
+
background: var(--c-bg);
|
|
285
|
+
color: var(--c-text);
|
|
286
|
+
overflow-y: auto;
|
|
287
|
+
overscroll-behavior: contain;
|
|
288
|
+
/* From the edge it belongs to: it arrives on the spring and leaves quicker, the same way. */
|
|
289
|
+
opacity: 0;
|
|
290
|
+
transform: translateX(-100%);
|
|
291
|
+
transition:
|
|
292
|
+
opacity var(--motion-base) var(--ease-standard),
|
|
293
|
+
transform var(--motion-base) var(--ease-standard),
|
|
294
|
+
overlay var(--motion-base) allow-discrete,
|
|
295
|
+
display var(--motion-base) allow-discrete;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
.drawer[open] {
|
|
299
|
+
opacity: 1;
|
|
300
|
+
transform: none;
|
|
301
|
+
transition:
|
|
302
|
+
opacity var(--motion-slow) var(--ease-out),
|
|
303
|
+
transform var(--motion-spring) var(--ease-spring),
|
|
304
|
+
overlay var(--motion-spring) allow-discrete,
|
|
305
|
+
display var(--motion-spring) allow-discrete;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
@starting-style {
|
|
309
|
+
.drawer[open] {
|
|
310
|
+
opacity: 0;
|
|
311
|
+
transform: translateX(-100%);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
.drawer[open][data-closing] {
|
|
316
|
+
opacity: 0;
|
|
317
|
+
transform: translateX(-100%);
|
|
318
|
+
transition:
|
|
319
|
+
opacity var(--motion-base) var(--ease-standard),
|
|
320
|
+
transform var(--motion-base) var(--ease-standard);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
.drawer::backdrop {
|
|
324
|
+
background: rgb(0 0 0 / 0);
|
|
325
|
+
transition:
|
|
326
|
+
background-color var(--motion-base) var(--ease-standard),
|
|
327
|
+
overlay var(--motion-base) allow-discrete,
|
|
328
|
+
display var(--motion-base) allow-discrete;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
.drawer[open]::backdrop {
|
|
332
|
+
background: rgb(0 0 0 / 0.4);
|
|
333
|
+
transition-duration: var(--motion-slow);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
.drawer[open][data-closing]::backdrop {
|
|
337
|
+
background: rgb(0 0 0 / 0);
|
|
338
|
+
transition-duration: var(--motion-base);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
@starting-style {
|
|
342
|
+
.drawer[open]::backdrop {
|
|
343
|
+
background: rgb(0 0 0 / 0);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
.drawer-head {
|
|
348
|
+
display: flex;
|
|
349
|
+
align-items: center;
|
|
350
|
+
justify-content: space-between;
|
|
351
|
+
margin-bottom: var(--sp-4);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
@media (max-width: 1023px) {
|
|
355
|
+
.app-shell {
|
|
356
|
+
grid-template-columns: minmax(0, 1fr);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
.rail {
|
|
360
|
+
display: none;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
.top {
|
|
364
|
+
position: sticky;
|
|
365
|
+
top: 0;
|
|
366
|
+
z-index: var(--z-sticky);
|
|
367
|
+
display: flex;
|
|
368
|
+
align-items: center;
|
|
369
|
+
gap: var(--sp-2);
|
|
370
|
+
padding: var(--sp-2) var(--layout-gutter);
|
|
371
|
+
border-bottom: 1px solid var(--c-border);
|
|
372
|
+
background: var(--c-bg);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
.top .home {
|
|
376
|
+
padding-inline: var(--sp-1);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
.top .menu {
|
|
380
|
+
margin-left: calc(-1 * var(--sp-2));
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
.top-end {
|
|
384
|
+
margin-left: auto;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/* With page-change.css, the column holds still while the page beside it changes. */
|
|
389
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
390
|
+
.rail {
|
|
391
|
+
view-transition-name: side-rail;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
@media (prefers-reduced-motion: reduce) {
|
|
396
|
+
.app-shell {
|
|
397
|
+
transition: none;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
.drawer,
|
|
401
|
+
.drawer[open],
|
|
402
|
+
.drawer[open][data-closing] {
|
|
403
|
+
transform: none;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
@starting-style {
|
|
407
|
+
.drawer[open] {
|
|
408
|
+
transform: none;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
</style>
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* A period stepper: ‹ 2025 ›. Time is mostly read a step at a time, so the arrows do the common
|
|
4
|
+
* thing, and the period itself opens every period at once, in a grid. With `all`, the line ends in
|
|
5
|
+
* a choice covering every period ("All years"): forward from the newest period is all of them,
|
|
6
|
+
* and back from there is the newest again.
|
|
7
|
+
*
|
|
8
|
+
* With `param`, the choice lives in the address (?year=2025), so a link or a reload keeps it and
|
|
9
|
+
* Back steps back through it; "all" is no parameter at all. The periods are known at build time
|
|
10
|
+
* (`periods`) or set later from script with setPeriods() from period.js, such as years read from
|
|
11
|
+
* an API. Each change fires `period-change` on the element, with the period as `detail.value`
|
|
12
|
+
* ("all" for all of them).
|
|
13
|
+
*
|
|
14
|
+
* Keyboard: Tab reaches each part; in the list, the arrow keys move through the periods and
|
|
15
|
+
* Escape closes it.
|
|
16
|
+
*/
|
|
17
|
+
interface Props {
|
|
18
|
+
/** The address parameter that holds the choice. Leave it out to keep the choice in the page. */
|
|
19
|
+
param?: string;
|
|
20
|
+
/** Periods known at build time, oldest first. */
|
|
21
|
+
periods?: string[];
|
|
22
|
+
/** The words for a choice covering every period, such as "All years". Leave it out for none. */
|
|
23
|
+
all?: string;
|
|
24
|
+
/** The period chosen when the address says nothing: "all", or one of the periods. Default: all if there is one, otherwise the newest. */
|
|
25
|
+
initial?: string;
|
|
26
|
+
/** The words a screen reader hears. */
|
|
27
|
+
labels?: { name?: string; previous?: string; next?: string; list?: string };
|
|
28
|
+
/** Unique on the page. */
|
|
29
|
+
id?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const { param, periods = [], all, initial, labels = {}, id = 'period' } = Astro.props;
|
|
33
|
+
const words = { name: 'Period', previous: 'Previous', next: 'Next', list: 'Choose a period', ...labels };
|
|
34
|
+
const chevron = (d: string) =>
|
|
35
|
+
`<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="${d}"/></svg>`;
|
|
36
|
+
const start = initial ?? (all ? 'all' : periods.at(-1));
|
|
37
|
+
const shown = start === 'all' ? all : start;
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
<div
|
|
41
|
+
class="period"
|
|
42
|
+
id={id}
|
|
43
|
+
data-period
|
|
44
|
+
data-param={param}
|
|
45
|
+
data-all={all}
|
|
46
|
+
data-value={start}
|
|
47
|
+
hidden={!periods.length}
|
|
48
|
+
>
|
|
49
|
+
<button type="button" class="step" data-step="-1" aria-label={words.previous}><Fragment set:html={chevron('m15 18-6-6 6-6')} /></button>
|
|
50
|
+
<button type="button" class="current" popovertarget={`${id}-list`} aria-haspopup="true" aria-expanded="false">
|
|
51
|
+
<span class="sr">{words.name}: </span><span class="label"><span class="sizer" aria-hidden="true">{shown}</span><span class="now">{shown}</span></span>
|
|
52
|
+
<svg class="chev" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 9 6 6 6-6"></path></svg>
|
|
53
|
+
</button>
|
|
54
|
+
<button type="button" class="step" data-step="1" aria-label={words.next}><Fragment set:html={chevron('m9 18 6-6-6-6')} /></button>
|
|
55
|
+
<div id={`${id}-list`} class="period-list" popover role="group" aria-label={words.list}>
|
|
56
|
+
{all && <button type="button" class="option all" data-value="all">{all}</button>}
|
|
57
|
+
<div class="periods">
|
|
58
|
+
{
|
|
59
|
+
[...periods].reverse().map((p) => (
|
|
60
|
+
<button type="button" class="option" data-value={p}>
|
|
61
|
+
{p}
|
|
62
|
+
</button>
|
|
63
|
+
))
|
|
64
|
+
}
|
|
65
|
+
</div>
|
|
66
|
+
</div>
|
|
67
|
+
</div>
|
|
68
|
+
|
|
69
|
+
<script>
|
|
70
|
+
import { wirePeriodPicker } from './period.js';
|
|
71
|
+
document.querySelectorAll<HTMLElement>('[data-period]').forEach(wirePeriodPicker);
|
|
72
|
+
</script>
|
|
73
|
+
|
|
74
|
+
<style>
|
|
75
|
+
.period {
|
|
76
|
+
display: inline-flex;
|
|
77
|
+
align-items: stretch;
|
|
78
|
+
min-height: 40px;
|
|
79
|
+
border: 1px solid var(--c-border-strong);
|
|
80
|
+
border-radius: var(--radius-sm);
|
|
81
|
+
background: var(--c-surface);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.period[hidden] {
|
|
85
|
+
display: none;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.period > button {
|
|
89
|
+
display: inline-flex;
|
|
90
|
+
align-items: center;
|
|
91
|
+
gap: var(--sp-2);
|
|
92
|
+
border: 0;
|
|
93
|
+
background: none;
|
|
94
|
+
color: var(--c-text);
|
|
95
|
+
font: inherit;
|
|
96
|
+
font-size: var(--text-body-sm);
|
|
97
|
+
font-weight: 600;
|
|
98
|
+
cursor: pointer;
|
|
99
|
+
transition:
|
|
100
|
+
background-color var(--motion-hover) var(--ease-hover),
|
|
101
|
+
color var(--motion-hover) var(--ease-hover);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
.period > button:hover:not(:disabled) {
|
|
105
|
+
background: var(--c-surface-hover);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
.period > button:active:not(:disabled) {
|
|
109
|
+
transform: scale(0.97);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
.period > button:disabled {
|
|
113
|
+
color: var(--c-text-muted);
|
|
114
|
+
cursor: default;
|
|
115
|
+
opacity: 0.5;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.step {
|
|
119
|
+
padding: 0 var(--sp-2);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
.step:first-child {
|
|
123
|
+
border-radius: var(--radius-sm) 0 0 var(--radius-sm);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.step:last-of-type {
|
|
127
|
+
border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.current {
|
|
131
|
+
padding: 0 var(--sp-3);
|
|
132
|
+
border-inline: 1px solid var(--c-border) !important;
|
|
133
|
+
font-variant-numeric: tabular-nums;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/* The period rolls: the old one leaves the way time went and the new one arrives from the
|
|
137
|
+
other side. The sizer, the longest words it may show, keeps the control's width still. */
|
|
138
|
+
.label {
|
|
139
|
+
display: inline-grid;
|
|
140
|
+
overflow: hidden;
|
|
141
|
+
justify-items: center;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.label > :global(span) {
|
|
145
|
+
grid-area: 1 / 1;
|
|
146
|
+
white-space: nowrap;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
.sizer {
|
|
150
|
+
visibility: hidden;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.chev {
|
|
154
|
+
color: var(--c-text-muted);
|
|
155
|
+
transition: transform var(--motion-base) var(--ease-out);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
.current[aria-expanded='true'] .chev {
|
|
159
|
+
transform: rotate(180deg);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.period-list {
|
|
163
|
+
position: fixed;
|
|
164
|
+
inset: auto;
|
|
165
|
+
margin: 0;
|
|
166
|
+
width: 17rem;
|
|
167
|
+
max-width: calc(100vw - 2 * var(--sp-4));
|
|
168
|
+
padding: var(--sp-2);
|
|
169
|
+
border: 1px solid var(--c-border-strong);
|
|
170
|
+
border-radius: var(--radius-md);
|
|
171
|
+
background: var(--c-surface);
|
|
172
|
+
color: var(--c-text);
|
|
173
|
+
box-shadow: 0 12px 32px rgb(0 0 0 / 0.18);
|
|
174
|
+
/* It opens on the spring from the control it belongs to, and leaves quicker. */
|
|
175
|
+
opacity: 0;
|
|
176
|
+
transform: translateY(-4px) scale(0.98);
|
|
177
|
+
transform-origin: top center;
|
|
178
|
+
transition:
|
|
179
|
+
opacity var(--motion-fast) var(--ease-standard),
|
|
180
|
+
transform var(--motion-fast) var(--ease-standard),
|
|
181
|
+
overlay var(--motion-fast) allow-discrete,
|
|
182
|
+
display var(--motion-fast) allow-discrete;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.period-list:popover-open {
|
|
186
|
+
opacity: 1;
|
|
187
|
+
transform: none;
|
|
188
|
+
transition:
|
|
189
|
+
opacity var(--motion-base) var(--ease-out),
|
|
190
|
+
transform var(--motion-spring) var(--ease-spring),
|
|
191
|
+
overlay var(--motion-spring) allow-discrete,
|
|
192
|
+
display var(--motion-spring) allow-discrete;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
@starting-style {
|
|
196
|
+
.period-list:popover-open {
|
|
197
|
+
opacity: 0;
|
|
198
|
+
transform: translateY(-6px) scale(0.97);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
.periods {
|
|
203
|
+
display: grid;
|
|
204
|
+
grid-template-columns: repeat(4, 1fr);
|
|
205
|
+
gap: 2px;
|
|
206
|
+
margin-top: 2px;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/* Periods set from script are made in the page, so :global reaches them. */
|
|
210
|
+
.period-list :global(.option) {
|
|
211
|
+
min-height: 36px;
|
|
212
|
+
border: 0;
|
|
213
|
+
border-radius: var(--radius-sm);
|
|
214
|
+
background: none;
|
|
215
|
+
color: var(--c-text-secondary);
|
|
216
|
+
font: inherit;
|
|
217
|
+
font-size: var(--text-body-sm);
|
|
218
|
+
font-variant-numeric: tabular-nums;
|
|
219
|
+
cursor: pointer;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
.period-list :global(.option.all) {
|
|
223
|
+
width: 100%;
|
|
224
|
+
text-align: start;
|
|
225
|
+
padding-inline: var(--sp-3);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
.period-list :global(.option:hover) {
|
|
229
|
+
background: var(--c-surface-hover);
|
|
230
|
+
color: var(--c-text);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
.period-list :global(.option[aria-pressed='true']) {
|
|
234
|
+
background: var(--c-text);
|
|
235
|
+
color: var(--c-bg);
|
|
236
|
+
font-weight: 600;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
.period-list :global(.option:focus-visible),
|
|
240
|
+
.period > button:focus-visible {
|
|
241
|
+
outline: 2px solid var(--c-focus);
|
|
242
|
+
outline-offset: -2px;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
.sr {
|
|
246
|
+
position: absolute;
|
|
247
|
+
width: 1px;
|
|
248
|
+
height: 1px;
|
|
249
|
+
overflow: hidden;
|
|
250
|
+
clip-path: inset(50%);
|
|
251
|
+
white-space: nowrap;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
@media (prefers-reduced-motion: reduce) {
|
|
255
|
+
.period > button:active:not(:disabled) {
|
|
256
|
+
transform: none;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
.period-list,
|
|
260
|
+
.period-list:popover-open {
|
|
261
|
+
transform: none;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
</style>
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Navigation down the side, in groups, for a site with more pages than a row holds, such as a
|
|
4
|
+
* dashboard's ten. With page-change.css, the current page's marker glides to the next page's
|
|
5
|
+
* link while the words and icons stay put above it. Usually inside AppShell, which gives it a
|
|
6
|
+
* column, a phone drawer and the button that folds it.
|
|
7
|
+
*
|
|
8
|
+
* Inside an element with data-nav-collapsed it shows its icons only: the words stay for a screen
|
|
9
|
+
* reader, and a pointer or keyboard on a link shows its name beside it.
|
|
10
|
+
*
|
|
11
|
+
* One with a marker per page: the marker's transition name must be unique, so a second copy of
|
|
12
|
+
* the same navigation (AppShell's drawer) leaves it out.
|
|
13
|
+
*/
|
|
14
|
+
import { ICONS, type IconName } from './icons';
|
|
15
|
+
|
|
16
|
+
interface Item {
|
|
17
|
+
href: string;
|
|
18
|
+
label: string;
|
|
19
|
+
icon?: IconName;
|
|
20
|
+
current?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface Group {
|
|
24
|
+
label: string;
|
|
25
|
+
items: Item[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface Props {
|
|
29
|
+
groups: Group[];
|
|
30
|
+
/** Names the navigation for a screen reader. */
|
|
31
|
+
label?: string;
|
|
32
|
+
/** Leave the gliding marker out, for a second copy of the same navigation. */
|
|
33
|
+
marker?: boolean;
|
|
34
|
+
/** The nav's id, and the start of its group labels' ids. */
|
|
35
|
+
id?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const { groups, label = 'Pages', marker = true, id = 'side-nav' } = Astro.props;
|
|
39
|
+
const icon = (name?: IconName) =>
|
|
40
|
+
name
|
|
41
|
+
? `<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[name]}</svg>`
|
|
42
|
+
: '';
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
<nav id={id} class:list={['side-nav', { glides: marker }]} aria-label={label} data-side-nav>
|
|
46
|
+
{
|
|
47
|
+
groups.map((g, i) => (
|
|
48
|
+
<div class="group">
|
|
49
|
+
<p class="group-label" id={`${id}-g${i}`}>
|
|
50
|
+
<span>{g.label}</span>
|
|
51
|
+
</p>
|
|
52
|
+
<ul aria-labelledby={`${id}-g${i}`}>
|
|
53
|
+
{g.items.map((item) => (
|
|
54
|
+
<li>
|
|
55
|
+
<a href={item.href} aria-current={item.current ? 'page' : undefined}>
|
|
56
|
+
{item.current && <span class="marker" aria-hidden="true" />}
|
|
57
|
+
<Fragment set:html={icon(item.icon)} />
|
|
58
|
+
<span class="label">{item.label}</span>
|
|
59
|
+
</a>
|
|
60
|
+
</li>
|
|
61
|
+
))}
|
|
62
|
+
</ul>
|
|
63
|
+
</div>
|
|
64
|
+
))
|
|
65
|
+
}
|
|
66
|
+
<span class="tip" aria-hidden="true" hidden></span>
|
|
67
|
+
</nav>
|
|
68
|
+
|
|
69
|
+
<script>
|
|
70
|
+
// Collapsed, a link's name appears beside it on hover and on keyboard focus. Measured, since
|
|
71
|
+
// the column it sits in scrolls and would clip anything that stuck out of it.
|
|
72
|
+
document.querySelectorAll<HTMLElement>('[data-side-nav]').forEach((nav) => {
|
|
73
|
+
const tip = nav.querySelector<HTMLElement>('.tip')!;
|
|
74
|
+
const show = (a: HTMLElement) => {
|
|
75
|
+
if (!nav.closest('[data-nav-collapsed]')) return;
|
|
76
|
+
const r = a.getBoundingClientRect();
|
|
77
|
+
tip.textContent = a.querySelector('.label')?.textContent ?? '';
|
|
78
|
+
tip.style.top = `${r.top + r.height / 2}px`;
|
|
79
|
+
tip.style.left = `${r.right + 8}px`;
|
|
80
|
+
tip.hidden = false;
|
|
81
|
+
};
|
|
82
|
+
const hide = () => (tip.hidden = true);
|
|
83
|
+
nav.addEventListener('pointerover', (e) => {
|
|
84
|
+
const a = (e.target as Element).closest<HTMLElement>('a');
|
|
85
|
+
if (a) show(a);
|
|
86
|
+
});
|
|
87
|
+
nav.addEventListener('pointerleave', hide);
|
|
88
|
+
nav.addEventListener('focusin', (e) => {
|
|
89
|
+
const a = (e.target as Element).closest<HTMLElement>('a');
|
|
90
|
+
if (a?.matches(':focus-visible')) show(a);
|
|
91
|
+
});
|
|
92
|
+
nav.addEventListener('focusout', hide);
|
|
93
|
+
nav.closest('[data-nav-scroll]')?.addEventListener('scroll', hide, { passive: true });
|
|
94
|
+
addEventListener('keydown', (e) => e.key === 'Escape' && hide());
|
|
95
|
+
});
|
|
96
|
+
</script>
|
|
97
|
+
|
|
98
|
+
<style>
|
|
99
|
+
/* The tracks may be narrower than the words, so that collapsed, a link is as wide as its column
|
|
100
|
+
and the words are cut off inside it, not past it. */
|
|
101
|
+
.side-nav { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--sp-5); }
|
|
102
|
+
.group-label {
|
|
103
|
+
position: relative;
|
|
104
|
+
margin: 0 0 var(--sp-1);
|
|
105
|
+
padding-inline: 11px;
|
|
106
|
+
font-size: var(--text-label);
|
|
107
|
+
font-weight: 600;
|
|
108
|
+
letter-spacing: 0.04em;
|
|
109
|
+
text-transform: uppercase;
|
|
110
|
+
white-space: nowrap;
|
|
111
|
+
color: var(--c-text-muted);
|
|
112
|
+
}
|
|
113
|
+
ul { display: grid; grid-template-columns: minmax(0, 1fr); gap: 2px; margin: 0; padding: 0; list-style: none; }
|
|
114
|
+
a {
|
|
115
|
+
position: relative;
|
|
116
|
+
isolation: isolate;
|
|
117
|
+
display: flex;
|
|
118
|
+
align-items: center;
|
|
119
|
+
gap: var(--sp-3);
|
|
120
|
+
min-height: 36px;
|
|
121
|
+
/* 11px each side, so an icon sits in the middle of a 40px square, collapsed or not. */
|
|
122
|
+
padding: var(--sp-1) 11px;
|
|
123
|
+
border-radius: var(--radius-md);
|
|
124
|
+
font-size: var(--text-body-sm);
|
|
125
|
+
color: var(--c-text-secondary);
|
|
126
|
+
text-decoration: none;
|
|
127
|
+
white-space: nowrap;
|
|
128
|
+
transition:
|
|
129
|
+
color var(--motion-hover) var(--ease-hover),
|
|
130
|
+
background-color var(--motion-hover) var(--ease-hover);
|
|
131
|
+
}
|
|
132
|
+
a :global(svg) { flex: none; color: var(--c-text-muted); transition: color var(--motion-hover) var(--ease-hover); }
|
|
133
|
+
a:hover { color: var(--c-text); }
|
|
134
|
+
a:hover :global(svg) { color: var(--c-text-secondary); }
|
|
135
|
+
a:not([aria-current]):hover { background: var(--c-surface-hover); }
|
|
136
|
+
a:focus-visible { outline: 2px solid var(--c-focus); outline-offset: 2px; }
|
|
137
|
+
a[aria-current='page'] { color: var(--c-text); font-weight: 600; }
|
|
138
|
+
a[aria-current="page"] :global(svg) { color: var(--c-accent); }
|
|
139
|
+
.marker {
|
|
140
|
+
position: absolute;
|
|
141
|
+
inset: 0;
|
|
142
|
+
z-index: -1;
|
|
143
|
+
border: 1px solid var(--c-border);
|
|
144
|
+
border-radius: inherit;
|
|
145
|
+
background: var(--c-surface);
|
|
146
|
+
}
|
|
147
|
+
/* The words and icons are a layer of their own while the page changes, above the marker, so the
|
|
148
|
+
marker passes under them instead of over them (page-change.css). */
|
|
149
|
+
.glides { view-transition-name: side-nav; }
|
|
150
|
+
.glides .marker { view-transition-name: side-nav-current; }
|
|
151
|
+
|
|
152
|
+
/* Collapsed: icons only. The words fade, and the group names give way to a short rule, keeping
|
|
153
|
+
their height so no icon moves up or down. */
|
|
154
|
+
.label, .group-label > span { transition: opacity var(--motion-base) var(--ease-standard); }
|
|
155
|
+
.group-label::before {
|
|
156
|
+
content: '';
|
|
157
|
+
position: absolute;
|
|
158
|
+
top: 50%;
|
|
159
|
+
left: 11px;
|
|
160
|
+
width: 18px;
|
|
161
|
+
border-top: 1px solid var(--c-border-strong);
|
|
162
|
+
opacity: 0;
|
|
163
|
+
transition: opacity var(--motion-base) var(--ease-standard);
|
|
164
|
+
}
|
|
165
|
+
:global([data-nav-collapsed]) .label,
|
|
166
|
+
:global([data-nav-collapsed]) .group-label > span { opacity: 0; }
|
|
167
|
+
:global([data-nav-collapsed]) .group-label::before { opacity: 1; }
|
|
168
|
+
:global([data-nav-collapsed]) a { overflow: hidden; }
|
|
169
|
+
|
|
170
|
+
.tip {
|
|
171
|
+
position: fixed;
|
|
172
|
+
z-index: var(--z-panel);
|
|
173
|
+
translate: 0 -50%;
|
|
174
|
+
padding: var(--sp-1) var(--sp-2);
|
|
175
|
+
border-radius: var(--radius-sm);
|
|
176
|
+
background: var(--c-text);
|
|
177
|
+
color: var(--c-bg);
|
|
178
|
+
font-size: var(--text-caption);
|
|
179
|
+
font-weight: 600;
|
|
180
|
+
white-space: nowrap;
|
|
181
|
+
pointer-events: none;
|
|
182
|
+
}
|
|
183
|
+
/* No motion: a name you look for many times a day should just be there. */
|
|
184
|
+
.tip[hidden] { display: none; }
|
|
185
|
+
|
|
186
|
+
@media (pointer: coarse) { a { min-height: 44px; } }
|
|
187
|
+
@media (forced-colors: active) { .marker { border-color: CanvasText; } }
|
|
188
|
+
</style>
|
package/src/icons.ts
CHANGED
|
@@ -71,6 +71,33 @@ export const ICONS = {
|
|
|
71
71
|
'building-2':
|
|
72
72
|
'<path d="M10 12h4"/><path d="M10 8h4"/><path d="M14 21v-3a2 2 0 0 0-4 0v3"/><path d="M6 10H4a2 2 0 0 0-2 2v7a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-2"/><path d="M6 21V5a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v16"/>',
|
|
73
73
|
car: '<path d="M19 17h2c.6 0 1-.4 1-1v-3c0-.9-.7-1.7-1.5-1.9C18.7 10.6 16 10 16 10s-1.3-1.4-2.2-2.3c-.5-.4-1.1-.7-1.8-.7H5c-.6 0-1.1.4-1.4.9l-1.4 2.9A3.7 3.7 0 0 0 2 12v4c0 .6.4 1 1 1h2"/><circle cx="7" cy="17" r="2"/><path d="M9 17h6"/><circle cx="17" cy="17" r="2"/>',
|
|
74
|
+
// Pages in a side nav (a dashboard's), and the controls of AppShell and PeriodPicker.
|
|
75
|
+
'layout-dashboard':
|
|
76
|
+
'<rect width="7" height="9" x="3" y="3" rx="1"/><rect width="7" height="5" x="14" y="3" rx="1"/><rect width="7" height="9" x="14" y="12" rx="1"/><rect width="7" height="5" x="3" y="16" rx="1"/>',
|
|
77
|
+
calendar:
|
|
78
|
+
'<path d="M8 2v4"/><path d="M16 2v4"/><rect width="18" height="18" x="3" y="4" rx="2"/><path d="M3 10h18"/>',
|
|
79
|
+
map:
|
|
80
|
+
'<path d="M14.106 5.553a2 2 0 0 0 1.788 0l3.659-1.83A1 1 0 0 1 21 4.619v12.764a1 1 0 0 1-.553.894l-4.553 2.277a2 2 0 0 1-1.788 0l-4.212-2.106a2 2 0 0 0-1.788 0l-3.659 1.83A1 1 0 0 1 3 19.381V6.618a1 1 0 0 1 .553-.894l4.553-2.277a2 2 0 0 1 1.788 0z"/><path d="M15 5.764v15"/><path d="M9 3.236v15"/>',
|
|
81
|
+
activity:
|
|
82
|
+
'<path d="M22 12h-2.48a2 2 0 0 0-1.93 1.46l-2.35 8.36a.25.25 0 0 1-.48 0L9.24 2.18a.25.25 0 0 0-.48 0l-2.35 8.36A2 2 0 0 1 4.49 12H2"/>',
|
|
83
|
+
heart:
|
|
84
|
+
'<path d="M2 9.5a5.5 5.5 0 0 1 9.591-3.676.56.56 0 0 0 .818 0A5.49 5.49 0 0 1 22 9.5c0 2.29-1.5 4-3 5.5l-5.492 5.313a2 2 0 0 1-3 .019L5 15c-1.5-1.5-3-3.2-3-5.5"/>',
|
|
85
|
+
moon:
|
|
86
|
+
'<path d="M20.985 12.486a9 9 0 1 1-9.473-9.472c.405-.022.617.46.402.803a6 6 0 0 0 8.268 8.268c.344-.215.825-.004.803.401"/>',
|
|
87
|
+
dumbbell:
|
|
88
|
+
'<path d="M17.596 12.768a2 2 0 1 0 2.829-2.829l-1.768-1.767a2 2 0 0 0 2.828-2.829l-2.828-2.828a2 2 0 0 0-2.829 2.828l-1.767-1.768a2 2 0 1 0-2.829 2.829z"/><path d="m2.5 21.5 1.4-1.4"/><path d="m20.1 3.9 1.4-1.4"/><path d="M5.343 21.485a2 2 0 1 0 2.829-2.828l1.767 1.768a2 2 0 1 0 2.829-2.829l-6.364-6.364a2 2 0 1 0-2.829 2.829l1.768 1.767a2 2 0 0 0-2.828 2.829z"/><path d="m9.6 14.4 4.8-4.8"/>',
|
|
89
|
+
scale:
|
|
90
|
+
'<path d="M12 3v18"/><path d="m19 8 3 8a5 5 0 0 1-6 0zV7"/><path d="M3 7h1a17 17 0 0 0 8-2 17 17 0 0 0 8 2h1"/><path d="m5 8 3 8a5 5 0 0 1-6 0zV7"/><path d="M7 21h10"/>',
|
|
91
|
+
menu:
|
|
92
|
+
'<path d="M4 5h16"/><path d="M4 12h16"/><path d="M4 19h16"/>',
|
|
93
|
+
'chevron-left':
|
|
94
|
+
'<path d="m15 18-6-6 6-6"/>',
|
|
95
|
+
'chevron-right':
|
|
96
|
+
'<path d="m9 18 6-6-6-6"/>',
|
|
97
|
+
'panel-left':
|
|
98
|
+
'<rect width="18" height="18" x="3" y="3" rx="2"/><path d="M9 3v18"/>',
|
|
99
|
+
x:
|
|
100
|
+
'<path d="M18 6 6 18"/><path d="m6 6 12 12"/>',
|
|
74
101
|
} as const;
|
|
75
102
|
|
|
76
103
|
export type IconName = keyof typeof ICONS;
|
package/src/page-change.css
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Moving between pages: a short cross-fade of the whole page, PageNav's pill
|
|
3
|
-
* link you left to the one you arrived at, and a picture with view-transition-class: media (a
|
|
2
|
+
* Moving between pages: a short cross-fade of the whole page, PageNav's pill (or SideNav's
|
|
3
|
+
* marker) gliding from the link you left to the one you arrived at, and a picture with view-transition-class: media (a
|
|
4
4
|
* LinkCard's, say) growing into the picture of the same name on the next page, all on the spring. For a site whose pages are
|
|
5
5
|
* separate documents. Import once, per site, on every page: both pages must opt in.
|
|
6
6
|
*
|
|
@@ -36,6 +36,45 @@
|
|
|
36
36
|
height: 100%;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
/* SideNav's marker, the same way. The column it sits in (AppShell's side-rail) holds still: it
|
|
40
|
+
* shows the new page's column at once, the marker glides over its background, and the words
|
|
41
|
+
* and icons (side-nav) lie on top, so the marker never covers them on its way. */
|
|
42
|
+
::view-transition-group(side-nav-current) {
|
|
43
|
+
z-index: 1;
|
|
44
|
+
animation-duration: var(--motion-spring);
|
|
45
|
+
animation-timing-function: var(--ease-spring);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
::view-transition-old(side-nav-current) {
|
|
49
|
+
animation: none;
|
|
50
|
+
opacity: 0;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
::view-transition-new(side-nav-current) {
|
|
54
|
+
animation: none;
|
|
55
|
+
width: 100%;
|
|
56
|
+
height: 100%;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
::view-transition-group(side-rail),
|
|
60
|
+
::view-transition-group(side-nav) {
|
|
61
|
+
animation: none;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
::view-transition-group(side-nav) {
|
|
65
|
+
z-index: 2;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
::view-transition-old(side-rail),
|
|
69
|
+
::view-transition-old(side-nav) {
|
|
70
|
+
display: none;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
::view-transition-new(side-rail),
|
|
74
|
+
::view-transition-new(side-nav) {
|
|
75
|
+
animation: none;
|
|
76
|
+
}
|
|
77
|
+
|
|
39
78
|
/* A picture that carries over, such as a case thumbnail becoming that case's header image. */
|
|
40
79
|
::view-transition-group(*.media) {
|
|
41
80
|
animation-duration: var(--motion-spring);
|
package/src/period.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// Drives PeriodPicker. Its state is in the page: the periods are the list's buttons (newest
|
|
2
|
+
// first), the choice is data-value on the picker and, with data-param, the address too. So a
|
|
3
|
+
// picker needs no object kept anywhere, and setPeriods() can fill one at any time.
|
|
4
|
+
//
|
|
5
|
+
// import { setPeriods } from '@n1kname/components/period.js';
|
|
6
|
+
// picker.addEventListener('period-change', (e) => show(e.detail.value));
|
|
7
|
+
// setPeriods(picker, ['2011', '2012', ..., '2026']); // oldest first
|
|
8
|
+
|
|
9
|
+
const reduced = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
10
|
+
const token = (name) => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
|
11
|
+
// A motion token in milliseconds; the tokens are written in seconds.
|
|
12
|
+
const ms = (name, fallback) => {
|
|
13
|
+
const v = token(name);
|
|
14
|
+
const n = parseFloat(v);
|
|
15
|
+
return Number.isNaN(n) ? fallback : v.endsWith('ms') ? n : n * 1000;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// Oldest first.
|
|
19
|
+
const periodsOf = (root) => [...root.querySelectorAll('.periods .option')].map((o) => o.dataset.value).reverse();
|
|
20
|
+
const nameOf = (root, v) => (v === 'all' ? root.dataset.all : v);
|
|
21
|
+
|
|
22
|
+
// The choice: the address's, when the picker keeps it there and it names a period or all.
|
|
23
|
+
export const periodOf = (root) => {
|
|
24
|
+
const periods = periodsOf(root);
|
|
25
|
+
const all = root.dataset.all !== undefined;
|
|
26
|
+
const fallback = root.dataset.value ?? (all ? 'all' : periods.at(-1));
|
|
27
|
+
if (!root.dataset.param) return fallback;
|
|
28
|
+
const asked = new URL(location.href).searchParams.get(root.dataset.param);
|
|
29
|
+
if (asked === null) return all ? 'all' : fallback;
|
|
30
|
+
return !periods.length || periods.includes(asked) ? asked : fallback;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// The period rolls by a line: up when time goes forward, down when it goes back.
|
|
34
|
+
const roll = (root, to, dir) => {
|
|
35
|
+
const label = root.querySelector('.label');
|
|
36
|
+
const old = label.querySelector('.now');
|
|
37
|
+
if (old.textContent === nameOf(root, to)) return;
|
|
38
|
+
const now = document.createElement('span');
|
|
39
|
+
now.className = 'now';
|
|
40
|
+
now.textContent = nameOf(root, to);
|
|
41
|
+
label.append(now);
|
|
42
|
+
if (!dir || reduced()) return old.remove();
|
|
43
|
+
const timing = { duration: ms('--motion-spring', 340), easing: token('--ease-spring') || 'ease-out' };
|
|
44
|
+
old.classList.remove('now');
|
|
45
|
+
old.animate([{ transform: 'none', opacity: 1 }, { transform: `translateY(${-dir * 100}%)`, opacity: 0 }], { ...timing, fill: 'forwards' }).finished.then(() => old.remove());
|
|
46
|
+
now.animate([{ transform: `translateY(${dir * 100}%)`, opacity: 0 }, { transform: 'none', opacity: 1 }], timing);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Shows the choice: the label, the steps that lead somewhere, the pressed period in the list.
|
|
50
|
+
const sync = (root, dir = 0) => {
|
|
51
|
+
const v = periodOf(root);
|
|
52
|
+
const periods = periodsOf(root);
|
|
53
|
+
const all = root.dataset.all !== undefined;
|
|
54
|
+
root.dataset.value = v;
|
|
55
|
+
roll(root, v, dir);
|
|
56
|
+
const [prev, next] = root.querySelectorAll('.step');
|
|
57
|
+
const at = periods.indexOf(v);
|
|
58
|
+
prev.disabled = !periods.length || at === 0;
|
|
59
|
+
next.disabled = !periods.length || v === 'all' || (!all && at === periods.length - 1);
|
|
60
|
+
root.querySelectorAll('.option').forEach((o) => o.setAttribute('aria-pressed', String(o.dataset.value === v)));
|
|
61
|
+
// The sizer holds the longest words the label may show, so the control keeps its width.
|
|
62
|
+
const longest = [root.dataset.all ?? '', ...periods].reduce((a, b) => (b.length > a.length ? b : a), '');
|
|
63
|
+
root.querySelector('.sizer').textContent = longest || v;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const go = (root, v, dir) => {
|
|
67
|
+
if (root.dataset.param) {
|
|
68
|
+
const url = new URL(location.href);
|
|
69
|
+
if (v === 'all') url.searchParams.delete(root.dataset.param);
|
|
70
|
+
else url.searchParams.set(root.dataset.param, v);
|
|
71
|
+
history.pushState(null, '', url);
|
|
72
|
+
}
|
|
73
|
+
root.dataset.value = v;
|
|
74
|
+
sync(root, dir);
|
|
75
|
+
root.dispatchEvent(new CustomEvent('period-change', { bubbles: true, detail: { value: v } }));
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// Fills a picker with its periods, oldest first, and shows it.
|
|
79
|
+
export const setPeriods = (root, periods) => {
|
|
80
|
+
root.querySelector('.periods').replaceChildren(
|
|
81
|
+
...[...periods].reverse().map((p) => {
|
|
82
|
+
const b = document.createElement('button');
|
|
83
|
+
b.type = 'button';
|
|
84
|
+
b.className = 'option';
|
|
85
|
+
b.dataset.value = String(p);
|
|
86
|
+
b.textContent = String(p);
|
|
87
|
+
return b;
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
root.hidden = false;
|
|
91
|
+
sync(root);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// Run once per picker, by PeriodPicker itself.
|
|
95
|
+
export const wirePeriodPicker = (root) => {
|
|
96
|
+
const list = root.querySelector('.period-list');
|
|
97
|
+
const button = root.querySelector('.current');
|
|
98
|
+
|
|
99
|
+
root.querySelectorAll('.step').forEach((step) =>
|
|
100
|
+
step.addEventListener('click', () => {
|
|
101
|
+
const dir = Number(step.dataset.step);
|
|
102
|
+
const periods = periodsOf(root);
|
|
103
|
+
const v = periodOf(root);
|
|
104
|
+
// The line runs oldest to newest, then all: back from all is the newest.
|
|
105
|
+
const to = v === 'all' ? periods.at(-1) : (periods[periods.indexOf(v) + dir] ?? 'all');
|
|
106
|
+
go(root, to, dir);
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
list.addEventListener('click', (e) => {
|
|
111
|
+
const o = e.target.closest('.option');
|
|
112
|
+
if (!o) return;
|
|
113
|
+
const periods = periodsOf(root);
|
|
114
|
+
const was = periodOf(root);
|
|
115
|
+
const to = o.dataset.value;
|
|
116
|
+
list.hidePopover();
|
|
117
|
+
button.focus();
|
|
118
|
+
if (to === was) return;
|
|
119
|
+
const later = to === 'all' || (was !== 'all' && periods.indexOf(to) > periods.indexOf(was));
|
|
120
|
+
go(root, to, later ? 1 : -1);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// The list opens under the control, kept inside the window, with focus on the chosen period.
|
|
124
|
+
list.addEventListener('toggle', (e) => {
|
|
125
|
+
const open = e.newState === 'open';
|
|
126
|
+
button.setAttribute('aria-expanded', String(open));
|
|
127
|
+
if (!open) {
|
|
128
|
+
// Closed from inside, by Escape or a choice: focus goes back to the control (Safari
|
|
129
|
+
// leaves it on the page).
|
|
130
|
+
if (list.contains(document.activeElement) || document.activeElement === document.body) button.focus();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const r = root.getBoundingClientRect();
|
|
134
|
+
const w = list.offsetWidth;
|
|
135
|
+
list.style.top = `${r.bottom + 6}px`;
|
|
136
|
+
list.style.left = `${Math.max(16, Math.min(r.right - w, innerWidth - w - 16))}px`;
|
|
137
|
+
// After the browser has finished opening it, which would otherwise keep focus on the button.
|
|
138
|
+
requestAnimationFrame(() => (list.querySelector('[aria-pressed="true"]') ?? list.querySelector('.option'))?.focus());
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Arrow keys move through the list in reading order, all first.
|
|
142
|
+
list.addEventListener('keydown', (e) => {
|
|
143
|
+
const options = [...list.querySelectorAll('.option')];
|
|
144
|
+
const at = options.indexOf(document.activeElement);
|
|
145
|
+
const cols = getComputedStyle(list.querySelector('.periods')).gridTemplateColumns.split(' ').length;
|
|
146
|
+
const step = { ArrowRight: 1, ArrowDown: e.target.classList.contains('all') ? 1 : cols, ArrowLeft: -1, ArrowUp: -cols }[e.key];
|
|
147
|
+
if (step === undefined || at < 0) return;
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
options[Math.max(0, Math.min(options.length - 1, at + step))].focus();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (root.dataset.param) {
|
|
153
|
+
addEventListener('popstate', () => {
|
|
154
|
+
const was = root.dataset.value;
|
|
155
|
+
sync(root);
|
|
156
|
+
if (root.dataset.value !== was) root.dispatchEvent(new CustomEvent('period-change', { bubbles: true, detail: { value: root.dataset.value } }));
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
sync(root);
|
|
160
|
+
};
|