@n1kname/components 0.12.0 → 0.14.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,12 +1,14 @@
1
1
  {
2
2
  "name": "@n1kname/components",
3
- "version": "0.12.0",
3
+ "version": "0.14.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",
11
+ "./highlight.css": "./src/highlight.css",
10
12
  "./icons": "./src/icons.ts",
11
13
  "./datatable.js": "./src/datatable.js",
12
14
  "./dotrows.js": "./src/dotrows.js",
@@ -0,0 +1,160 @@
1
+ ---
2
+ /**
3
+ * One pill, two actions: the link half opens it (a mail client, say), the half on the right
4
+ * copies the value, because a recruiter often wants the address rather than an app opening. Each
5
+ * half is its own control with its own focus ring; a hairline marks where one ends. The pill
6
+ * lifts and presses as one, so it never comes apart at the seam.
7
+ *
8
+ * The copy half is rendered hidden and shown from script only where the clipboard is there to
9
+ * use, so nobody sees a button that does nothing. The check arrives on the spring; "Copied" is
10
+ * read out from a separate live region, since a changing button label is not announced reliably.
11
+ */
12
+ import Tag from './Tag.astro';
13
+ import type { IconName } from './icons';
14
+
15
+ interface Props {
16
+ href: string;
17
+ /** What gets copied, such as the email address. */
18
+ value: string;
19
+ /** What is copied, for the accessible name: "Copy email address". */
20
+ what: string;
21
+ icon?: IconName;
22
+ }
23
+
24
+ const { href, value, what, icon } = Astro.props;
25
+ // Lucide's copy and check, drawn the way Icon draws at 15px.
26
+ const svg = { width: 15, height: 15, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', 'stroke-width': 1.75, 'stroke-linecap': 'round', 'stroke-linejoin': 'round', 'aria-hidden': 'true' } as const;
27
+ ---
28
+
29
+ <span class="copy-tag">
30
+ <Tag href={href} icon={icon} tone="accent" still><slot /></Tag>
31
+ <button class="copy" type="button" data-copy={value} aria-label={`Copy ${what}`} hidden>
32
+ <span class="ico">
33
+ <svg class="c1" {...svg}><rect width="14" height="14" x="8" y="8" rx="2" ry="2" /><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" /></svg>
34
+ <svg class="c2" {...svg}><path d="M20 6 9 17l-5-5" /></svg>
35
+ </span>
36
+ <span class="sr-only" role="status"></span>
37
+ </button>
38
+ </span>
39
+
40
+ <style>
41
+ .copy-tag {
42
+ display: inline-flex;
43
+ transition:
44
+ translate var(--motion-spring) var(--ease-spring),
45
+ scale var(--motion-base) var(--ease-out);
46
+ }
47
+
48
+ @media (hover: hover) {
49
+ .copy-tag:hover {
50
+ translate: 0 -2px;
51
+ }
52
+ }
53
+
54
+ .copy-tag:has(:active) {
55
+ scale: 0.97;
56
+ transition-duration: var(--motion-spring), 80ms;
57
+ }
58
+
59
+ @media (prefers-reduced-motion: reduce) {
60
+ .copy-tag {
61
+ translate: none !important;
62
+ scale: none !important;
63
+ }
64
+ }
65
+
66
+ /* Square inner corners only once the copy half is there to meet. */
67
+ .copy-tag:has(.copy:not([hidden])) :global(a.tag) {
68
+ border-start-end-radius: 0;
69
+ border-end-end-radius: 0;
70
+ }
71
+
72
+ .copy {
73
+ display: inline-flex;
74
+ align-items: center;
75
+ padding: 5px var(--sp-3);
76
+ border-radius: var(--radius-full);
77
+ border-start-start-radius: 0;
78
+ border-end-start-radius: 0;
79
+ border: 1px solid transparent;
80
+ background: var(--c-surface-tint);
81
+ color: var(--c-text-accent);
82
+ box-shadow: inset 1px 0 0 color-mix(in oklab, var(--c-text-accent) 22%, transparent);
83
+ cursor: pointer;
84
+ }
85
+
86
+ .copy[hidden] {
87
+ display: none;
88
+ }
89
+
90
+ .copy-tag :global(a.tag),
91
+ .copy {
92
+ transition: background-color var(--motion-hover) var(--ease-hover);
93
+ }
94
+
95
+ .copy-tag :global(a.tag:hover),
96
+ .copy:hover {
97
+ border-color: transparent;
98
+ background: color-mix(in oklab, var(--c-surface-tint), var(--c-accent) 14%);
99
+ }
100
+
101
+ .ico {
102
+ position: relative;
103
+ width: 15px;
104
+ height: 15px;
105
+ }
106
+
107
+ .ico svg {
108
+ position: absolute;
109
+ inset: 0;
110
+ transition:
111
+ opacity var(--motion-fast) var(--ease-out),
112
+ scale var(--motion-fast) var(--ease-out);
113
+ }
114
+
115
+ .c2,
116
+ .done .c1 {
117
+ opacity: 0;
118
+ scale: 0.6;
119
+ }
120
+
121
+ .done .c2 {
122
+ opacity: 1;
123
+ scale: 1;
124
+ transition:
125
+ opacity var(--motion-fast) var(--ease-out),
126
+ scale var(--motion-spring) var(--ease-spring);
127
+ }
128
+
129
+ .sr-only {
130
+ position: absolute;
131
+ width: 1px;
132
+ height: 1px;
133
+ overflow: hidden;
134
+ clip-path: inset(50%);
135
+ white-space: nowrap;
136
+ }
137
+ </style>
138
+
139
+ <script>
140
+ document.querySelectorAll<HTMLButtonElement>('.copy-tag button[data-copy]').forEach((button) => {
141
+ if (!navigator.clipboard) return;
142
+ button.hidden = false;
143
+ const status = button.querySelector('[role="status"]')!;
144
+ let timer = 0;
145
+ button.addEventListener('click', async () => {
146
+ try {
147
+ await navigator.clipboard.writeText(button.dataset.copy!);
148
+ } catch {
149
+ return;
150
+ }
151
+ button.classList.add('done');
152
+ status.textContent = 'Copied';
153
+ clearTimeout(timer);
154
+ timer = window.setTimeout(() => {
155
+ button.classList.remove('done');
156
+ status.textContent = '';
157
+ }, 2000);
158
+ });
159
+ });
160
+ </script>
@@ -0,0 +1,16 @@
1
+ ---
2
+ /**
3
+ * Highlights the words a heading or lede is about, with a band that sweeps in once. Give each
4
+ * mark its place with `n` when there are several, so they sweep one after the other. The look and
5
+ * the timing live in highlight.css, shared with PageHeader.
6
+ */
7
+ import './highlight.css';
8
+
9
+ interface Props {
10
+ /** This mark's place among the marks on the page, from 0. */
11
+ n?: number;
12
+ }
13
+ const { n = 0 } = Astro.props;
14
+ ---
15
+
16
+ <mark class="highlight" style={`--n: ${n}`}><slot /></mark>
@@ -0,0 +1,116 @@
1
+ ---
2
+ /**
3
+ * A card that is one link, such as a case on an index: a picture in the "media" slot and the
4
+ * argument beside it, or above it once the card is narrow. The whole card is the target.
5
+ *
6
+ * Under a real pointer the card lifts 2px and its picture leans in 2% inside its frame; a press
7
+ * gives a little; the spring brings both back. `transition` names the picture for a
8
+ * cross-document view transition, so it can grow into the same picture on the page it opens
9
+ * (give that one the same name, with view-transition-class: media; page-change.css animates it).
10
+ * Under reduced motion nothing moves, and only the edge and the fill answer the hover.
11
+ */
12
+ interface Props {
13
+ href: string;
14
+ /** A view-transition-name for the picture, unique on the page. */
15
+ transition?: string;
16
+ }
17
+
18
+ const { href, transition } = Astro.props;
19
+ const hasMedia = Astro.slots.has('media');
20
+ ---
21
+
22
+ <a class="link-card" href={href}>
23
+ <div class:list={['layout', { 'has-media': hasMedia }]}>
24
+ {
25
+ hasMedia && (
26
+ <div class="media" style={transition ? `view-transition-name: ${transition}; view-transition-class: media` : undefined}>
27
+ <slot name="media" />
28
+ </div>
29
+ )
30
+ }
31
+ <div class="body"><slot /></div>
32
+ </div>
33
+ </a>
34
+
35
+ <style>
36
+ .link-card {
37
+ container-type: inline-size;
38
+ display: block;
39
+ padding: var(--sp-6) var(--sp-7);
40
+ background: var(--c-surface);
41
+ border: 1px solid var(--c-border);
42
+ border-radius: var(--radius-lg);
43
+ color: inherit;
44
+ text-decoration: none;
45
+ transition:
46
+ border-color var(--motion-hover) var(--ease-hover),
47
+ background-color var(--motion-hover) var(--ease-hover),
48
+ translate var(--motion-spring) var(--ease-spring),
49
+ scale var(--motion-base) var(--ease-out);
50
+ }
51
+
52
+ /* Picture left, argument right. The picture's column is fixed rather than fractional, so the
53
+ * text measure does not shift between cards. Below 640px of card it stacks; the card is the
54
+ * container, so the layout inside it can ask. */
55
+ .has-media {
56
+ display: grid;
57
+ gap: var(--sp-5);
58
+ }
59
+
60
+ @container (min-width: 640px) {
61
+ .has-media {
62
+ grid-template-columns: 280px minmax(0, 1fr);
63
+ gap: var(--sp-6);
64
+ align-items: start;
65
+ }
66
+ }
67
+
68
+ .media {
69
+ overflow: hidden;
70
+ border-radius: var(--radius-sm);
71
+ }
72
+
73
+ .media > :global(*) {
74
+ display: block;
75
+ width: 100%;
76
+ transition: scale var(--motion-spring) var(--ease-spring);
77
+ }
78
+
79
+ .body {
80
+ min-width: 0;
81
+ }
82
+
83
+ .link-card:hover {
84
+ border-color: var(--c-accent-edge);
85
+ background: var(--c-surface-tint);
86
+ }
87
+
88
+ @media (hover: hover) {
89
+ .link-card:hover {
90
+ translate: 0 -2px;
91
+ }
92
+
93
+ .link-card:hover .media > :global(*) {
94
+ scale: 1.02;
95
+ }
96
+ }
97
+
98
+ .link-card:active {
99
+ scale: 0.99;
100
+ transition-duration: var(--motion-hover), var(--motion-hover), var(--motion-spring), 80ms;
101
+ }
102
+
103
+ @media (prefers-reduced-motion: reduce) {
104
+ .link-card,
105
+ .media > :global(*) {
106
+ translate: none !important;
107
+ scale: none !important;
108
+ }
109
+ }
110
+
111
+ @media (max-width: 560px) {
112
+ .link-card {
113
+ padding: var(--sp-5);
114
+ }
115
+ }
116
+ </style>
@@ -2,7 +2,15 @@
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
  */
12
+ import './highlight.css';
13
+
6
14
  interface Props {
7
15
  eyebrow?: string;
8
16
  title: string;
@@ -11,15 +19,28 @@ interface Props {
11
19
  meta?: string;
12
20
  /** Ids, for a page that fills these in once its data arrives. */
13
21
  ids?: { eyebrow?: string; title?: string; lede?: string; meta?: string };
22
+ /** The short accent bar under the title. */
23
+ rule?: boolean;
24
+ /** Words in the title or lede to highlight. */
25
+ mark?: string;
14
26
  }
15
- const { eyebrow, title, lede, meta, ids = {} } = Astro.props;
27
+ const { eyebrow, title, lede, meta, ids = {}, rule = false, mark } = Astro.props;
28
+
29
+ // The text around the marked words, or the text alone when they aren't in it.
30
+ const split = (text?: string) => {
31
+ const at = mark && text ? text.indexOf(mark) : -1;
32
+ return at < 0 ? null : [text!.slice(0, at), mark, text!.slice(at + mark!.length)];
33
+ };
34
+ const inTitle = split(title);
35
+ const inLede = inTitle ? null : split(lede);
16
36
  ---
17
37
 
18
38
  <header class="page-header">
19
39
  <div class="text">
20
40
  {(eyebrow || ids.eyebrow) && <p class="eyebrow" id={ids.eyebrow}>{eyebrow}</p>}
21
- <h1 id={ids.title}>{title}</h1>
22
- {(lede || ids.lede) && <p class="lede" id={ids.lede}>{lede}</p>}
41
+ <h1 id={ids.title}>{inTitle ? <>{inTitle[0]}<mark>{inTitle[1]}</mark>{inTitle[2]}</> : title}</h1>
42
+ {rule && <span class="rule" aria-hidden="true" />}
43
+ {(lede || ids.lede) && <p class="lede" id={ids.lede}>{inLede ? <>{inLede[0]}<mark>{inLede[1]}</mark>{inLede[2]}</> : lede}</p>}
23
44
  {(meta || ids.meta) && <p class="meta t-meta" id={ids.meta}>{meta}</p>}
24
45
  </div>
25
46
  {Astro.slots.has('actions') && <div class="actions"><slot name="actions" /></div>}
@@ -74,6 +95,31 @@ const { eyebrow, title, lede, meta, ids = {} } = Astro.props;
74
95
  min-height: 1lh;
75
96
  }
76
97
 
98
+ /* The house signature: a short accent bar under the title. */
99
+ .rule {
100
+ display: block;
101
+ width: var(--sp-7);
102
+ height: 5px;
103
+ margin: var(--sp-3) 0 var(--sp-4);
104
+ border-radius: var(--radius-rule);
105
+ background: var(--c-accent);
106
+ }
107
+
108
+ /* The highlighter itself is highlight.css. In the smaller lede the band is wider and stronger,
109
+ * and the figure takes the body colour, so it reads as clearly as it does in a headline. */
110
+ .lede :global(mark) {
111
+ --highlight: color-mix(in oklab, var(--c-accent) 42%, var(--c-bg));
112
+ --highlight-band: 0.6em;
113
+ color: var(--c-text);
114
+ }
115
+
116
+ @media (forced-colors: active) {
117
+ .rule {
118
+ background: CanvasText;
119
+ }
120
+
121
+ }
122
+
77
123
  .actions {
78
124
  display: flex;
79
125
  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,68 @@
1
+ ---
2
+ /**
3
+ * Lets something settle in once, as it comes a little way into view: it rises 24px and fades in.
4
+ * Only what starts below the fold is held back, so nothing on screen at load ever disappears.
5
+ * Items that arrive together go 90ms apart. A jump past items (an anchor, a fast fling) shows the
6
+ * ones it skipped at once, so nothing is left hidden above.
7
+ *
8
+ * Hidden only once the script has armed it, so without script, or with reduced motion, it is
9
+ * simply there. Each item fires `reveal` with its delay in `detail`, for a page that wants to
10
+ * start something of its own as it arrives.
11
+ */
12
+ interface Props {
13
+ /** The element to render, such as "li" for a row of a list. */
14
+ as?: string;
15
+ class?: string;
16
+ [attr: string]: unknown;
17
+ }
18
+
19
+ const { as: Element = 'div', class: className, ...rest } = Astro.props;
20
+ ---
21
+
22
+ <Element class:list={[className]} data-reveal {...rest}><slot /></Element>
23
+
24
+ <style is:global>
25
+ [data-reveal].reveal-armed {
26
+ opacity: 0;
27
+ translate: 0 24px;
28
+ transition:
29
+ opacity 600ms var(--ease-out),
30
+ translate 900ms var(--ease-soft);
31
+ transition-delay: var(--reveal-delay, 0ms);
32
+ }
33
+
34
+ [data-reveal].reveal-armed.reveal-in {
35
+ opacity: 1;
36
+ translate: 0 0;
37
+ }
38
+ </style>
39
+
40
+ <script>
41
+ if (matchMedia('(prefers-reduced-motion: no-preference)').matches) {
42
+ const items = [...document.querySelectorAll<HTMLElement>('[data-reveal]')].filter(
43
+ (el) => el.getBoundingClientRect().top > innerHeight,
44
+ );
45
+ const show = (el: HTMLElement, delay: number) => {
46
+ el.style.setProperty('--reveal-delay', `${delay}ms`);
47
+ el.classList.add('reveal-in');
48
+ el.dispatchEvent(new CustomEvent('reveal', { detail: { delay } }));
49
+ io.unobserve(el);
50
+ };
51
+ const io = new IntersectionObserver(
52
+ (entries) => {
53
+ entries
54
+ .filter((e) => e.isIntersecting)
55
+ .forEach((e, i) => {
56
+ const el = e.target as HTMLElement;
57
+ items.slice(0, items.indexOf(el)).filter((s) => !s.classList.contains('reveal-in')).forEach((s) => show(s, 0));
58
+ show(el, i * 90);
59
+ });
60
+ },
61
+ { rootMargin: '0px 0px -12% 0px' },
62
+ );
63
+ items.forEach((el) => {
64
+ el.classList.add('reveal-armed');
65
+ io.observe(el);
66
+ });
67
+ }
68
+ </script>
package/src/Tag.astro CHANGED
@@ -8,6 +8,9 @@
8
8
  * quiet → this is a descriptor, not something to click
9
9
  * warm → a warm tag: how something was done
10
10
  * cool → the cool tag: what something was about
11
+ *
12
+ * A linked tag lifts 2px under a real pointer and gives under a press, and the spring brings it
13
+ * back. `still` turns that off, for a tag inside something that moves as a whole (CopyTag).
11
14
  */
12
15
  import Icon from './Icon.astro';
13
16
  import type { IconName } from './icons';
@@ -17,13 +20,15 @@ interface Props {
17
20
  href?: string;
18
21
  tone?: 'accent' | 'quiet' | 'warm' | 'cool';
19
22
  small?: boolean;
23
+ /** No lift or press, for a linked tag inside something that moves as a whole. */
24
+ still?: boolean;
20
25
  }
21
26
 
22
- const { icon, href, tone = 'quiet', small = false } = Astro.props;
27
+ const { icon, href, tone = 'quiet', small = false, still = false } = Astro.props;
23
28
  const Tag = href ? 'a' : 'span';
24
29
  ---
25
30
 
26
- <Tag class:list={['tag', tone, { small }]} href={href}>
31
+ <Tag class:list={['tag', tone, { small, still }]} href={href}>
27
32
  {icon && <Icon name={icon} size={15} />}
28
33
  <slot />
29
34
  </Tag>
@@ -72,7 +77,27 @@ const Tag = href ? 'a' : 'span';
72
77
  a.tag {
73
78
  transition:
74
79
  border-color var(--motion-hover) var(--ease-hover),
75
- background-color var(--motion-hover) var(--ease-hover);
80
+ background-color var(--motion-hover) var(--ease-hover),
81
+ translate var(--motion-spring) var(--ease-spring),
82
+ scale var(--motion-base) var(--ease-out);
83
+ }
84
+
85
+ @media (hover: hover) {
86
+ a.tag:not(.still):hover {
87
+ translate: 0 -2px;
88
+ }
89
+ }
90
+
91
+ a.tag:not(.still):active {
92
+ scale: 0.97;
93
+ transition-duration: var(--motion-hover), var(--motion-hover), var(--motion-spring), 80ms;
94
+ }
95
+
96
+ @media (prefers-reduced-motion: reduce) {
97
+ a.tag {
98
+ translate: none !important;
99
+ scale: none !important;
100
+ }
76
101
  }
77
102
 
78
103
  a.quiet:hover {
@@ -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,44 @@
1
+ /*
2
+ * The highlighter: a band behind the words a page is about. It sits low, like a pen, wraps line
3
+ * by line, and is mixed into the page colour rather than see-through, so it holds up in dark. It
4
+ * sweeps in once, a moment after the words land; several marks go one after the other, by --n.
5
+ *
6
+ * Used by Highlight and PageHeader. A page that writes its words once its data arrives can wrap
7
+ * them in <mark class="highlight"> itself, and the sweep plays as they land.
8
+ *
9
+ * --highlight the band colour
10
+ * --highlight-band its height
11
+ * --highlight-delay before the first mark sweeps (300ms)
12
+ * --highlight-step between one mark and the next (450ms)
13
+ */
14
+ :where(mark.highlight, .page-header .text mark) {
15
+ --highlight: color-mix(in oklab, var(--c-accent) 32%, var(--c-bg));
16
+ --highlight-band: 0.42em;
17
+ padding-inline: 0.08em;
18
+ margin-inline: -0.08em;
19
+ background: linear-gradient(var(--highlight), var(--highlight)) no-repeat 0 88% / 100% var(--highlight-band);
20
+ color: inherit;
21
+ box-decoration-break: clone;
22
+ -webkit-box-decoration-break: clone;
23
+ }
24
+
25
+ @media (prefers-reduced-motion: no-preference) {
26
+ :where(mark.highlight, .page-header .text mark) {
27
+ animation: highlight 700ms var(--ease-out)
28
+ calc(var(--highlight-delay, 300ms) + var(--n, 0) * var(--highlight-step, 450ms)) both;
29
+ }
30
+ }
31
+
32
+ @keyframes highlight {
33
+ from {
34
+ background-size: 0% var(--highlight-band);
35
+ }
36
+ }
37
+
38
+ @media (forced-colors: active) {
39
+ :where(mark.highlight, .page-header .text mark) {
40
+ background: none;
41
+ text-decoration: underline 0.2em;
42
+ text-underline-offset: 0.1em;
43
+ }
44
+ }
@@ -0,0 +1,57 @@
1
+ /*
2
+ * Moving between pages: a short cross-fade of the whole page, PageNav's pill gliding from the
3
+ * link you left to the one you arrived at, and a picture with view-transition-class: media (a
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
+ * separate documents. Import once, per site, on every page: both pages must opt in.
6
+ *
7
+ * A browser without cross-document view transitions simply changes the page, and under
8
+ * reduced motion nothing moves.
9
+ */
10
+
11
+ @media (prefers-reduced-motion: no-preference) {
12
+ @view-transition {
13
+ navigation: auto;
14
+ }
15
+
16
+ ::view-transition-old(root),
17
+ ::view-transition-new(root) {
18
+ animation-duration: var(--motion-base);
19
+ animation-timing-function: var(--ease-out);
20
+ }
21
+
22
+ ::view-transition-group(page-nav-current) {
23
+ animation-duration: var(--motion-spring);
24
+ animation-timing-function: var(--ease-spring);
25
+ }
26
+
27
+ /* The pill is an empty shape, so it can stretch to its new width without a cross-fade. */
28
+ ::view-transition-old(page-nav-current) {
29
+ animation: none;
30
+ opacity: 0;
31
+ }
32
+
33
+ ::view-transition-new(page-nav-current) {
34
+ animation: none;
35
+ width: 100%;
36
+ height: 100%;
37
+ }
38
+
39
+ /* A picture that carries over, such as a case thumbnail becoming that case's header image. */
40
+ ::view-transition-group(*.media) {
41
+ animation-duration: var(--motion-spring);
42
+ animation-timing-function: var(--ease-spring);
43
+ }
44
+
45
+ /* The two pictures rarely share a shape (a 16:9 thumbnail, a 20:9 header), so each fills the
46
+ * moving frame and is cropped to it, rather than stretched. */
47
+ ::view-transition-image-pair(*.media) {
48
+ overflow: hidden;
49
+ border-radius: var(--radius-lg);
50
+ }
51
+
52
+ ::view-transition-old(*.media),
53
+ ::view-transition-new(*.media) {
54
+ height: 100%;
55
+ object-fit: cover;
56
+ }
57
+ }