@n1kname/components 0.65.1 → 0.66.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 +1 -1
- package/src/Phases.astro +2 -1
- package/src/ThemeToggle.astro +24 -8
package/package.json
CHANGED
package/src/Phases.astro
CHANGED
|
@@ -123,7 +123,8 @@ const last = phases.length - 1;
|
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
[data-tone='accent'] {
|
|
126
|
-
|
|
126
|
+
/* 24%, as in Tracks: at 16% the band read pale next to the cool one. */
|
|
127
|
+
--fill: color-mix(in oklab, var(--c-accent) 24%, var(--c-surface));
|
|
127
128
|
--mark: var(--c-accent-edge);
|
|
128
129
|
}
|
|
129
130
|
|
package/src/ThemeToggle.astro
CHANGED
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* The light and dark switch: a round button whose icon shows the theme it goes to, the moon
|
|
4
4
|
* in light and the sun in dark. The two icons share a cell and cross-fade. The choice is kept
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* in a `theme` cookie, so sites on one host share it (a cookie ignores the port) and, with
|
|
6
|
+
* `cookieDomain`, so do the subdomains of one domain. It is also kept under `theme` in
|
|
7
|
+
* localStorage, for pages whose head script predates the cookie. The page's own head script
|
|
8
|
+
* applies it before paint (see the docs page), so nothing flashes. Without that, it follows
|
|
9
|
+
* the system, as tokens.css does.
|
|
7
10
|
*
|
|
8
11
|
* Where the browser has view transitions, the whole page eases into the new colours over
|
|
9
12
|
* --motion-base instead of snapping. Not under reduced motion.
|
|
@@ -13,14 +16,17 @@
|
|
|
13
16
|
interface Props {
|
|
14
17
|
/** What the button says to a screen reader, in each direction. */
|
|
15
18
|
labels?: { toDark: string; toLight: string };
|
|
19
|
+
/** Share the choice with every subdomain of this domain, e.g. `example.com`. Ignored on a
|
|
20
|
+
* host outside it (a LAN address, localhost), where the cookie stays with the host. */
|
|
21
|
+
cookieDomain?: string;
|
|
16
22
|
id?: string;
|
|
17
23
|
class?: string;
|
|
18
24
|
}
|
|
19
25
|
|
|
20
|
-
const { labels = { toDark: 'Switch to dark', toLight: 'Switch to light' }, id, class: className } = Astro.props;
|
|
26
|
+
const { labels = { toDark: 'Switch to dark', toLight: 'Switch to light' }, cookieDomain, id, class: className } = Astro.props;
|
|
21
27
|
---
|
|
22
28
|
|
|
23
|
-
<button type="button" class:list={['theme-toggle', className]} id={id} data-theme-toggle>
|
|
29
|
+
<button type="button" class:list={['theme-toggle', className]} id={id} data-theme-toggle data-cookie-domain={cookieDomain}>
|
|
24
30
|
<span class="sr to-dark">{labels.toDark}</span><span class="sr to-light">{labels.toLight}</span>
|
|
25
31
|
<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
32
|
<path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"></path>
|
|
@@ -32,27 +38,37 @@ const { labels = { toDark: 'Switch to dark', toLight: 'Switch to light' }, id, c
|
|
|
32
38
|
</button>
|
|
33
39
|
|
|
34
40
|
<script>
|
|
35
|
-
|
|
41
|
+
// A year, on the whole host, or the whole domain where the page asks and is on it.
|
|
42
|
+
const keep = (next: string, domain?: string) => {
|
|
43
|
+
const host = location.hostname;
|
|
44
|
+
const shared = domain && (host === domain || host.endsWith('.' + domain)) ? `; domain=${domain}` : '';
|
|
45
|
+
const secure = location.protocol === 'https:' ? '; secure' : '';
|
|
46
|
+
document.cookie = `theme=${next}; path=/; max-age=31536000; samesite=lax${shared}${secure}`;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const apply = (next: string, domain?: string) => {
|
|
36
50
|
document.documentElement.dataset.theme = next;
|
|
37
51
|
// The browser bar follows the choice too, where the page marks its two theme-colors.
|
|
38
52
|
document.querySelectorAll<HTMLMetaElement>('meta[name="theme-color"][data-for]').forEach((m) => {
|
|
39
53
|
m.media = m.dataset.for === next ? 'all' : 'not all';
|
|
40
54
|
});
|
|
55
|
+
keep(next, domain);
|
|
41
56
|
try {
|
|
42
57
|
localStorage.setItem('theme', next);
|
|
43
58
|
} catch {}
|
|
44
59
|
};
|
|
45
60
|
|
|
46
|
-
document.querySelectorAll('[data-theme-toggle]').forEach((button) =>
|
|
61
|
+
document.querySelectorAll<HTMLElement>('[data-theme-toggle]').forEach((button) =>
|
|
47
62
|
button.addEventListener('click', () => {
|
|
63
|
+
const domain = button.dataset.cookieDomain;
|
|
48
64
|
const root = document.documentElement;
|
|
49
65
|
const dark = root.dataset.theme ? root.dataset.theme === 'dark' : matchMedia('(prefers-color-scheme: dark)').matches;
|
|
50
66
|
const next = dark ? 'light' : 'dark';
|
|
51
67
|
// The page eases into its new colours: one cross-fade of the whole page, which costs far
|
|
52
68
|
// less than a transition on every element.
|
|
53
|
-
if (!document.startViewTransition || matchMedia('(prefers-reduced-motion: reduce)').matches) return apply(next);
|
|
69
|
+
if (!document.startViewTransition || matchMedia('(prefers-reduced-motion: reduce)').matches) return apply(next, domain);
|
|
54
70
|
root.dataset.themeChange = '';
|
|
55
|
-
document.startViewTransition(() => apply(next)).finished.finally(() => delete root.dataset.themeChange);
|
|
71
|
+
document.startViewTransition(() => apply(next, domain)).finished.finally(() => delete root.dataset.themeChange);
|
|
56
72
|
}),
|
|
57
73
|
);
|
|
58
74
|
</script>
|