@n1kname/components 0.14.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n1kname/components",
3
- "version": "0.14.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.6.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>