@n1kname/components 0.12.0 → 0.13.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 +2 -1
- package/src/PageHeader.astro +78 -3
- package/src/PageNav.astro +105 -0
- package/src/ThemeToggle.astro +180 -0
- package/src/page-change.css +37 -0
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n1kname/components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./*.astro": "./src/*.astro",
|
|
8
8
|
"./type.css": "./src/type.css",
|
|
9
9
|
"./marks.css": "./src/marks.css",
|
|
10
|
+
"./page-change.css": "./src/page-change.css",
|
|
10
11
|
"./icons": "./src/icons.ts",
|
|
11
12
|
"./datatable.js": "./src/datatable.js",
|
|
12
13
|
"./dotrows.js": "./src/dotrows.js",
|
package/src/PageHeader.astro
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* The top of a page: what it is, what it says, and how fresh that is, with the page's
|
|
4
4
|
* actions beside it. Actions go in the "actions" slot.
|
|
5
|
+
*
|
|
6
|
+
* `rule` puts the house signature, a short accent bar, under the title. `mark` names the
|
|
7
|
+
* words that answer the question, such as "6,5% lower": the first place they appear, in the
|
|
8
|
+
* title or else the lede, gets a highlighter that sweeps in once. A page that writes its title
|
|
9
|
+
* or lede once its data arrives wraps those words in <mark> itself; the sweep plays as they
|
|
10
|
+
* land.
|
|
5
11
|
*/
|
|
6
12
|
interface Props {
|
|
7
13
|
eyebrow?: string;
|
|
@@ -11,15 +17,28 @@ interface Props {
|
|
|
11
17
|
meta?: string;
|
|
12
18
|
/** Ids, for a page that fills these in once its data arrives. */
|
|
13
19
|
ids?: { eyebrow?: string; title?: string; lede?: string; meta?: string };
|
|
20
|
+
/** The short accent bar under the title. */
|
|
21
|
+
rule?: boolean;
|
|
22
|
+
/** Words in the title or lede to highlight. */
|
|
23
|
+
mark?: string;
|
|
14
24
|
}
|
|
15
|
-
const { eyebrow, title, lede, meta, ids = {} } = Astro.props;
|
|
25
|
+
const { eyebrow, title, lede, meta, ids = {}, rule = false, mark } = Astro.props;
|
|
26
|
+
|
|
27
|
+
// The text around the marked words, or the text alone when they aren't in it.
|
|
28
|
+
const split = (text?: string) => {
|
|
29
|
+
const at = mark && text ? text.indexOf(mark) : -1;
|
|
30
|
+
return at < 0 ? null : [text!.slice(0, at), mark, text!.slice(at + mark!.length)];
|
|
31
|
+
};
|
|
32
|
+
const inTitle = split(title);
|
|
33
|
+
const inLede = inTitle ? null : split(lede);
|
|
16
34
|
---
|
|
17
35
|
|
|
18
36
|
<header class="page-header">
|
|
19
37
|
<div class="text">
|
|
20
38
|
{(eyebrow || ids.eyebrow) && <p class="eyebrow" id={ids.eyebrow}>{eyebrow}</p>}
|
|
21
|
-
<h1 id={ids.title}>{title}</h1>
|
|
22
|
-
{
|
|
39
|
+
<h1 id={ids.title}>{inTitle ? <>{inTitle[0]}<mark>{inTitle[1]}</mark>{inTitle[2]}</> : title}</h1>
|
|
40
|
+
{rule && <span class="rule" aria-hidden="true" />}
|
|
41
|
+
{(lede || ids.lede) && <p class="lede" id={ids.lede}>{inLede ? <>{inLede[0]}<mark>{inLede[1]}</mark>{inLede[2]}</> : lede}</p>}
|
|
23
42
|
{(meta || ids.meta) && <p class="meta t-meta" id={ids.meta}>{meta}</p>}
|
|
24
43
|
</div>
|
|
25
44
|
{Astro.slots.has('actions') && <div class="actions"><slot name="actions" /></div>}
|
|
@@ -74,6 +93,62 @@ const { eyebrow, title, lede, meta, ids = {} } = Astro.props;
|
|
|
74
93
|
min-height: 1lh;
|
|
75
94
|
}
|
|
76
95
|
|
|
96
|
+
/* The house signature: a short accent bar under the title. */
|
|
97
|
+
.rule {
|
|
98
|
+
display: block;
|
|
99
|
+
width: var(--sp-7);
|
|
100
|
+
height: 5px;
|
|
101
|
+
margin: var(--sp-3) 0 var(--sp-4);
|
|
102
|
+
border-radius: var(--radius-rule);
|
|
103
|
+
background: var(--c-accent);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/* A highlighter behind the words that answer the question. It sits low, like a pen, wraps
|
|
107
|
+
* line by line, and is mixed into the page colour rather than see-through, so it holds up in
|
|
108
|
+
* dark. It sweeps in once, a moment after the words land. */
|
|
109
|
+
.text :global(mark) {
|
|
110
|
+
--sweep: color-mix(in oklab, var(--c-accent) 32%, var(--c-bg));
|
|
111
|
+
--band: 0.42em;
|
|
112
|
+
padding-inline: 0.08em;
|
|
113
|
+
margin-inline: -0.08em;
|
|
114
|
+
background: linear-gradient(var(--sweep), var(--sweep)) no-repeat 0 88% / 100% var(--band);
|
|
115
|
+
color: inherit;
|
|
116
|
+
box-decoration-break: clone;
|
|
117
|
+
-webkit-box-decoration-break: clone;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/* In the smaller lede the band is wider and stronger, and the figure takes the body colour,
|
|
121
|
+
* so it reads as clearly as it does in a headline. */
|
|
122
|
+
.lede :global(mark) {
|
|
123
|
+
--sweep: color-mix(in oklab, var(--c-accent) 42%, var(--c-bg));
|
|
124
|
+
--band: 0.6em;
|
|
125
|
+
color: var(--c-text);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
129
|
+
.text :global(mark) {
|
|
130
|
+
animation: sweep 700ms var(--ease-out) 300ms both;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
@keyframes sweep {
|
|
135
|
+
from {
|
|
136
|
+
background-size: 0% var(--band);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@media (forced-colors: active) {
|
|
141
|
+
.rule {
|
|
142
|
+
background: CanvasText;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
.text :global(mark) {
|
|
146
|
+
background: none;
|
|
147
|
+
text-decoration: underline 0.2em;
|
|
148
|
+
text-underline-offset: 0.1em;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
77
152
|
.actions {
|
|
78
153
|
display: flex;
|
|
79
154
|
flex-wrap: wrap;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* The row of links across a site's pages, with the current page marked by a pill. The pill is
|
|
4
|
+
* an empty shape behind the link's text, so that with page-change.css it can glide to the next
|
|
5
|
+
* page's link while the words cross-fade, without stretching them. Without page-change.css, or
|
|
6
|
+
* in a browser without cross-document view transitions, the page simply changes.
|
|
7
|
+
*
|
|
8
|
+
* One per page: the pill's transition name must be unique.
|
|
9
|
+
*/
|
|
10
|
+
interface Item {
|
|
11
|
+
href: string;
|
|
12
|
+
label: string;
|
|
13
|
+
current?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface Props {
|
|
17
|
+
items: Item[];
|
|
18
|
+
/** Names the navigation for a screen reader. */
|
|
19
|
+
label?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const { items, label = 'Pages' } = Astro.props;
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
<nav class="page-nav" aria-label={label}>
|
|
26
|
+
<ul>
|
|
27
|
+
{
|
|
28
|
+
items.map((item) => (
|
|
29
|
+
<li>
|
|
30
|
+
<a href={item.href} aria-current={item.current ? 'page' : undefined}>
|
|
31
|
+
{item.current && <span class="pill" aria-hidden="true" />}
|
|
32
|
+
{item.label}
|
|
33
|
+
</a>
|
|
34
|
+
</li>
|
|
35
|
+
))
|
|
36
|
+
}
|
|
37
|
+
</ul>
|
|
38
|
+
</nav>
|
|
39
|
+
|
|
40
|
+
<style>
|
|
41
|
+
ul {
|
|
42
|
+
display: flex;
|
|
43
|
+
flex-wrap: wrap;
|
|
44
|
+
gap: var(--sp-1);
|
|
45
|
+
margin: 0;
|
|
46
|
+
padding: 0;
|
|
47
|
+
list-style: none;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
a {
|
|
51
|
+
position: relative;
|
|
52
|
+
isolation: isolate;
|
|
53
|
+
display: block;
|
|
54
|
+
padding: var(--sp-1) var(--sp-3);
|
|
55
|
+
border-radius: var(--radius-md);
|
|
56
|
+
font-size: var(--text-body-sm);
|
|
57
|
+
line-height: var(--leading-body-sm);
|
|
58
|
+
color: var(--c-text-secondary);
|
|
59
|
+
text-decoration: none;
|
|
60
|
+
transition:
|
|
61
|
+
color var(--motion-hover) var(--ease-hover),
|
|
62
|
+
background-color var(--motion-hover) var(--ease-hover);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
a:hover {
|
|
66
|
+
color: var(--c-text);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
a:not([aria-current]):hover {
|
|
70
|
+
background: var(--c-surface-hover);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
a:focus-visible {
|
|
74
|
+
outline: 2px solid var(--c-focus);
|
|
75
|
+
outline-offset: 2px;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
a[aria-current='page'] {
|
|
79
|
+
color: var(--c-text);
|
|
80
|
+
font-weight: 600;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.pill {
|
|
84
|
+
position: absolute;
|
|
85
|
+
inset: 0;
|
|
86
|
+
z-index: -1;
|
|
87
|
+
border: 1px solid var(--c-border);
|
|
88
|
+
border-radius: inherit;
|
|
89
|
+
background: var(--c-surface);
|
|
90
|
+
view-transition-name: page-nav-current;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* A fingertip needs more room than a pointer. */
|
|
94
|
+
@media (pointer: coarse) {
|
|
95
|
+
a {
|
|
96
|
+
padding-block: var(--sp-2);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
@media (forced-colors: active) {
|
|
101
|
+
.pill {
|
|
102
|
+
border-color: CanvasText;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
</style>
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* The light and dark switch: a round button whose icon shows the theme it goes to, the moon
|
|
4
|
+
* in light and the sun in dark. The two icons share a cell and cross-fade. The choice is kept
|
|
5
|
+
* under `theme` in localStorage, and the page's own head script applies it before paint (see
|
|
6
|
+
* the docs page), so nothing flashes. Without that, it follows the system, as tokens.css does.
|
|
7
|
+
*
|
|
8
|
+
* Where the browser has view transitions, the whole page eases into the new colours over
|
|
9
|
+
* --motion-base instead of snapping. Not under reduced motion.
|
|
10
|
+
*
|
|
11
|
+
* The label names the theme it goes to, without waiting for a script.
|
|
12
|
+
*/
|
|
13
|
+
interface Props {
|
|
14
|
+
/** What the button says to a screen reader, in each direction. */
|
|
15
|
+
labels?: { toDark: string; toLight: string };
|
|
16
|
+
id?: string;
|
|
17
|
+
class?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const { labels = { toDark: 'Switch to dark', toLight: 'Switch to light' }, id, class: className } = Astro.props;
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
<button type="button" class:list={['theme-toggle', className]} id={id} data-theme-toggle>
|
|
24
|
+
<span class="sr to-dark">{labels.toDark}</span><span class="sr to-light">{labels.toLight}</span>
|
|
25
|
+
<svg class="moon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
|
26
|
+
<path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"></path>
|
|
27
|
+
</svg>
|
|
28
|
+
<svg class="sun" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
|
29
|
+
<circle cx="12" cy="12" r="4"></circle>
|
|
30
|
+
<path d="M12 2v2m0 16v2M4.93 4.93l1.41 1.41m11.32 11.32 1.41 1.41M2 12h2m16 0h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41"></path>
|
|
31
|
+
</svg>
|
|
32
|
+
</button>
|
|
33
|
+
|
|
34
|
+
<script>
|
|
35
|
+
const apply = (next: string) => {
|
|
36
|
+
document.documentElement.dataset.theme = next;
|
|
37
|
+
// The browser bar follows the choice too, where the page marks its two theme-colors.
|
|
38
|
+
document.querySelectorAll<HTMLMetaElement>('meta[name="theme-color"][data-for]').forEach((m) => {
|
|
39
|
+
m.media = m.dataset.for === next ? 'all' : 'not all';
|
|
40
|
+
});
|
|
41
|
+
try {
|
|
42
|
+
localStorage.setItem('theme', next);
|
|
43
|
+
} catch {}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
document.querySelectorAll('[data-theme-toggle]').forEach((button) =>
|
|
47
|
+
button.addEventListener('click', () => {
|
|
48
|
+
const root = document.documentElement;
|
|
49
|
+
const dark = root.dataset.theme ? root.dataset.theme === 'dark' : matchMedia('(prefers-color-scheme: dark)').matches;
|
|
50
|
+
const next = dark ? 'light' : 'dark';
|
|
51
|
+
// The page eases into its new colours: one cross-fade of the whole page, which costs far
|
|
52
|
+
// less than a transition on every element.
|
|
53
|
+
if (!document.startViewTransition || matchMedia('(prefers-reduced-motion: reduce)').matches) return apply(next);
|
|
54
|
+
root.dataset.themeChange = '';
|
|
55
|
+
document.startViewTransition(() => apply(next)).finished.finally(() => delete root.dataset.themeChange);
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<style>
|
|
61
|
+
.theme-toggle {
|
|
62
|
+
display: grid;
|
|
63
|
+
place-items: center;
|
|
64
|
+
flex: none;
|
|
65
|
+
width: 40px;
|
|
66
|
+
height: 40px;
|
|
67
|
+
padding: 0;
|
|
68
|
+
border: 1px solid var(--c-border);
|
|
69
|
+
border-radius: var(--radius-full);
|
|
70
|
+
background: var(--c-surface);
|
|
71
|
+
color: var(--c-text);
|
|
72
|
+
cursor: pointer;
|
|
73
|
+
transition:
|
|
74
|
+
border-color var(--motion-hover) var(--ease-hover),
|
|
75
|
+
transform var(--motion-fast) var(--ease-out);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@media (pointer: coarse) {
|
|
79
|
+
.theme-toggle {
|
|
80
|
+
width: 44px;
|
|
81
|
+
height: 44px;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.theme-toggle:hover {
|
|
86
|
+
border-color: var(--c-border-strong);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.theme-toggle:focus-visible {
|
|
90
|
+
outline: 2px solid var(--c-focus);
|
|
91
|
+
outline-offset: 2px;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.theme-toggle:active {
|
|
95
|
+
transform: scale(0.94);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.sr {
|
|
99
|
+
position: absolute;
|
|
100
|
+
width: 1px;
|
|
101
|
+
height: 1px;
|
|
102
|
+
overflow: hidden;
|
|
103
|
+
clip-path: inset(50%);
|
|
104
|
+
white-space: nowrap;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
svg {
|
|
108
|
+
grid-area: 1 / 1;
|
|
109
|
+
transition:
|
|
110
|
+
opacity var(--motion-base) var(--ease-out),
|
|
111
|
+
transform var(--motion-gentle) var(--ease-soft);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/* In light: the moon, and the label for dark. */
|
|
115
|
+
.sun {
|
|
116
|
+
opacity: 0;
|
|
117
|
+
transform: rotate(-45deg) scale(0.8);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.to-light {
|
|
121
|
+
display: none;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
:global(:root[data-theme='dark']) .sun {
|
|
125
|
+
opacity: 1;
|
|
126
|
+
transform: none;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
:global(:root[data-theme='dark']) .moon {
|
|
130
|
+
opacity: 0;
|
|
131
|
+
transform: rotate(45deg) scale(0.8);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
:global(:root[data-theme='dark']) .to-light {
|
|
135
|
+
display: inline;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
:global(:root[data-theme='dark']) .to-dark {
|
|
139
|
+
display: none;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
@media (prefers-color-scheme: dark) {
|
|
143
|
+
:global(:root:not([data-theme='light'])) .sun {
|
|
144
|
+
opacity: 1;
|
|
145
|
+
transform: none;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
:global(:root:not([data-theme='light'])) .moon {
|
|
149
|
+
opacity: 0;
|
|
150
|
+
transform: rotate(45deg) scale(0.8);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
:global(:root:not([data-theme='light'])) .to-light {
|
|
154
|
+
display: inline;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
:global(:root:not([data-theme='light'])) .to-dark {
|
|
158
|
+
display: none;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
@media (prefers-reduced-motion: reduce) {
|
|
163
|
+
.theme-toggle:active {
|
|
164
|
+
transform: none;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@media (forced-colors: active) {
|
|
169
|
+
.theme-toggle {
|
|
170
|
+
border-color: ButtonText;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/* The page's cross-fade into the new theme. The icons turn inside it. */
|
|
175
|
+
:global(:root[data-theme-change]::view-transition-old(root)),
|
|
176
|
+
:global(:root[data-theme-change]::view-transition-new(root)) {
|
|
177
|
+
animation-duration: var(--motion-base);
|
|
178
|
+
animation-timing-function: var(--ease-out);
|
|
179
|
+
}
|
|
180
|
+
</style>
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Moving between pages: a short cross-fade of the whole page, and PageNav's pill gliding from
|
|
3
|
+
* the link you left to the one you arrived at, on the spring. For a site whose pages are
|
|
4
|
+
* separate documents. Import once, per site, on every page: both pages must opt in.
|
|
5
|
+
*
|
|
6
|
+
* A browser without cross-document view transitions simply changes the page, and under
|
|
7
|
+
* reduced motion nothing moves.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
11
|
+
@view-transition {
|
|
12
|
+
navigation: auto;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
::view-transition-old(root),
|
|
16
|
+
::view-transition-new(root) {
|
|
17
|
+
animation-duration: var(--motion-base);
|
|
18
|
+
animation-timing-function: var(--ease-out);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
::view-transition-group(page-nav-current) {
|
|
22
|
+
animation-duration: var(--motion-spring);
|
|
23
|
+
animation-timing-function: var(--ease-spring);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* The pill is an empty shape, so it can stretch to its new width without a cross-fade. */
|
|
27
|
+
::view-transition-old(page-nav-current) {
|
|
28
|
+
animation: none;
|
|
29
|
+
opacity: 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
::view-transition-new(page-nav-current) {
|
|
33
|
+
animation: none;
|
|
34
|
+
width: 100%;
|
|
35
|
+
height: 100%;
|
|
36
|
+
}
|
|
37
|
+
}
|