@pencil-lang/figure 0.0.1 → 0.0.3

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/src/scheme.js ADDED
@@ -0,0 +1,62 @@
1
+ // A page that pins a theme, and a figure's sheets and poster that are picked by one.
2
+ //
3
+ // A figure's dark sheets and its dark poster are picked by `(prefers-color-scheme: dark)`, in a
4
+ // `<source>`'s `media`, which is the reader's system and nothing the page can answer. A page with
5
+ // a theme toggle pins a scheme over the system's, and says so in CSS, as it has to for its form
6
+ // controls and scrollbars to follow:
7
+ //
8
+ // :root[data-theme='dark'] { color-scheme: dark; }
9
+ //
10
+ // So that's what a figure reads: `color-scheme` where it sits. Listing one of `light` and `dark`
11
+ // pins it, and listing both or neither leaves it to the reader's system.
12
+
13
+ /**
14
+ * Answers `prefers-color-scheme` in the `media` of every `<source>` in a figure, its own and its
15
+ * poster's, with the scheme the page pins there, and gives that scheme, or `null` with none
16
+ * pinned. What was written is kept in `data-media` while it's answered, and put back when the
17
+ * pin goes. A page changing a `media` that's being answered changes `data-media`. The answer is
18
+ * a condition that always holds, or one that never does, in the query's place.
19
+ *
20
+ * It stands alone, without anything from this module, because `Figure.astro` writes it into the
21
+ * page as text: there it runs as the figure is read, before a poster in the other theme is
22
+ * fetched and painted, which the element's own script would be too late for.
23
+ */
24
+ export function settle(figure) {
25
+ const listed = getComputedStyle(figure).colorScheme.split(/\s+/);
26
+ const dark = listed.includes('dark');
27
+ const pinned = dark === listed.includes('light') ? null : dark ? 'dark' : 'light';
28
+ for (const source of figure.querySelectorAll('source[media], source[data-media]')) {
29
+ const written = source.dataset.media ?? source.getAttribute('media');
30
+ const media = pinned
31
+ ? written.replace(/\(\s*prefers-color-scheme\s*:\s*(light|dark)\s*\)/g, (_, scheme) =>
32
+ scheme === pinned ? '(min-width: 0px)' : '(not (min-width: 0px))',
33
+ )
34
+ : written;
35
+ if (media === written) delete source.dataset.media;
36
+ else source.dataset.media = written;
37
+ if (source.getAttribute('media') !== media) source.setAttribute('media', media);
38
+ }
39
+ return pinned;
40
+ }
41
+
42
+ // A toggle pins a scheme by changing an attribute on `<html>` or `<body>` that the page's CSS
43
+ // reads, so those are watched, once for every figure on the page.
44
+ const figures = new Set();
45
+ let watcher = null;
46
+
47
+ /** Settles a figure now and whenever the page may have pinned another scheme. */
48
+ export function follow(figure) {
49
+ settle(figure);
50
+ figures.add(figure);
51
+ watcher ??= new MutationObserver(() => {
52
+ for (const each of figures) settle(each);
53
+ });
54
+ watcher.observe(document.documentElement, { attributes: true });
55
+ if (document.body) watcher.observe(document.body, { attributes: true });
56
+ }
57
+
58
+ /** Stops following a figure that has left the page. */
59
+ export function unfollow(figure) {
60
+ figures.delete(figure);
61
+ if (!figures.size) watcher?.disconnect();
62
+ }