magic-pill 9.0.0 → 9.1.1
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/CHANGELOG.md +50 -0
- package/README.md +259 -15
- package/dist/MagicPill/MagicPill.d.mts +7 -1
- package/dist/MagicPill/MagicPill.d.ts +7 -1
- package/dist/MagicPill/MagicPill.js +40 -17
- package/dist/MagicPill/MagicPill.mjs +42 -19
- package/dist/MagicPill/choreography.js +1 -1
- package/dist/MagicPill/choreography.mjs +1 -1
- package/dist/Pill/Pill.js +53 -17
- package/dist/Pill/Pill.mjs +54 -18
- package/dist/Pill/PillContext.js +3 -1
- package/dist/Pill/PillContext.mjs +3 -1
- package/dist/Pill/PillPanel.d.mts +5 -5
- package/dist/Pill/PillPanel.d.ts +5 -5
- package/dist/Pill/PillPanel.js +28 -20
- package/dist/Pill/PillPanel.mjs +28 -20
- package/dist/Pill/morph.js +25 -7
- package/dist/Pill/morph.mjs +22 -8
- package/dist/Pill/scene.js +90 -0
- package/dist/Pill/scene.mjs +89 -0
- package/dist/Pill/themed.js +36 -0
- package/dist/Pill/themed.mjs +34 -0
- package/dist/Pill/types.d.mts +50 -0
- package/dist/Pill/types.d.ts +50 -0
- package/dist/Pill/zone.js +160 -0
- package/dist/Pill/zone.mjs +159 -0
- package/dist/PillForm/Calendar.d.mts +15 -0
- package/dist/PillForm/Calendar.d.ts +16 -0
- package/dist/PillForm/Calendar.js +371 -0
- package/dist/PillForm/Calendar.mjs +370 -0
- package/dist/PillForm/FormContext.d.mts +2 -0
- package/dist/PillForm/FormContext.d.ts +2 -0
- package/dist/PillForm/FormContext.js +8 -0
- package/dist/PillForm/FormContext.mjs +8 -0
- package/dist/PillForm/PillCheckbox.d.mts +8 -0
- package/dist/PillForm/PillCheckbox.d.ts +8 -0
- package/dist/PillForm/PillCheckbox.js +45 -0
- package/dist/PillForm/PillCheckbox.mjs +45 -0
- package/dist/PillForm/PillChoices.d.mts +39 -0
- package/dist/PillForm/PillChoices.d.ts +39 -0
- package/dist/PillForm/PillChoices.js +103 -0
- package/dist/PillForm/PillChoices.mjs +103 -0
- package/dist/PillForm/PillCode.d.mts +12 -0
- package/dist/PillForm/PillCode.d.ts +12 -0
- package/dist/PillForm/PillCode.js +60 -0
- package/dist/PillForm/PillCode.mjs +60 -0
- package/dist/PillForm/PillColor.d.mts +10 -0
- package/dist/PillForm/PillColor.d.ts +10 -0
- package/dist/PillForm/PillColor.js +56 -0
- package/dist/PillForm/PillColor.mjs +56 -0
- package/dist/PillForm/PillDate.d.mts +44 -0
- package/dist/PillForm/PillDate.d.ts +44 -0
- package/dist/PillForm/PillDate.js +193 -0
- package/dist/PillForm/PillDate.mjs +193 -0
- package/dist/PillForm/PillField.d.mts +38 -0
- package/dist/PillForm/PillField.d.ts +38 -0
- package/dist/PillForm/PillField.js +81 -0
- package/dist/PillForm/PillField.mjs +80 -0
- package/dist/PillForm/PillFields.d.mts +11 -0
- package/dist/PillForm/PillFields.d.ts +11 -0
- package/dist/PillForm/PillFields.js +21 -0
- package/dist/PillForm/PillFields.mjs +21 -0
- package/dist/PillForm/PillFile.d.mts +8 -0
- package/dist/PillForm/PillFile.d.ts +8 -0
- package/dist/PillForm/PillFile.js +55 -0
- package/dist/PillForm/PillFile.mjs +55 -0
- package/dist/PillForm/PillForm.d.mts +29 -0
- package/dist/PillForm/PillForm.d.ts +29 -0
- package/dist/PillForm/PillForm.js +66 -0
- package/dist/PillForm/PillForm.mjs +65 -0
- package/dist/PillForm/PillFormPanel.d.mts +24 -0
- package/dist/PillForm/PillFormPanel.d.ts +24 -0
- package/dist/PillForm/PillFormPanel.js +53 -0
- package/dist/PillForm/PillFormPanel.mjs +53 -0
- package/dist/PillForm/PillInput.d.mts +20 -0
- package/dist/PillForm/PillInput.d.ts +20 -0
- package/dist/PillForm/PillInput.js +84 -0
- package/dist/PillForm/PillInput.mjs +84 -0
- package/dist/PillForm/PillSelect.d.mts +23 -0
- package/dist/PillForm/PillSelect.d.ts +23 -0
- package/dist/PillForm/PillSelect.js +87 -0
- package/dist/PillForm/PillSelect.mjs +87 -0
- package/dist/PillForm/PillSlider.d.mts +17 -0
- package/dist/PillForm/PillSlider.d.ts +17 -0
- package/dist/PillForm/PillSlider.js +61 -0
- package/dist/PillForm/PillSlider.mjs +61 -0
- package/dist/PillForm/PillStepper.d.mts +13 -0
- package/dist/PillForm/PillStepper.d.ts +13 -0
- package/dist/PillForm/PillStepper.js +84 -0
- package/dist/PillForm/PillStepper.mjs +84 -0
- package/dist/PillForm/PillSubmit.d.mts +14 -0
- package/dist/PillForm/PillSubmit.d.ts +14 -0
- package/dist/PillForm/PillSubmit.js +90 -0
- package/dist/PillForm/PillSubmit.mjs +89 -0
- package/dist/PillForm/PillSwitch.d.mts +5 -0
- package/dist/PillForm/PillSwitch.d.ts +5 -0
- package/dist/PillForm/PillSwitch.js +38 -0
- package/dist/PillForm/PillSwitch.mjs +38 -0
- package/dist/PillForm/PillTextarea.d.mts +5 -0
- package/dist/PillForm/PillTextarea.d.ts +5 -0
- package/dist/PillForm/PillTextarea.js +49 -0
- package/dist/PillForm/PillTextarea.mjs +49 -0
- package/dist/PillForm/dates.d.mts +1 -0
- package/dist/PillForm/dates.d.ts +1 -0
- package/dist/PillForm/dates.js +155 -0
- package/dist/PillForm/dates.mjs +139 -0
- package/dist/PillForm/glyphs.js +65 -0
- package/dist/PillForm/glyphs.mjs +56 -0
- package/dist/PillForm/keyboard.js +34 -0
- package/dist/PillForm/keyboard.mjs +34 -0
- package/dist/PillForm/popover.js +84 -0
- package/dist/PillForm/popover.mjs +83 -0
- package/dist/PillForm/send.d.mts +6 -0
- package/dist/PillForm/send.d.ts +6 -0
- package/dist/PillForm/send.js +44 -0
- package/dist/PillForm/send.mjs +44 -0
- package/dist/PillForm/validity.js +74 -0
- package/dist/PillForm/validity.mjs +72 -0
- package/dist/PillNavbar/PillNavbar.d.mts +29 -6
- package/dist/PillNavbar/PillNavbar.d.ts +29 -6
- package/dist/PillNavbar/PillNavbar.js +35 -11
- package/dist/PillNavbar/PillNavbar.mjs +36 -12
- package/dist/PillNavbar/room.js +84 -0
- package/dist/PillNavbar/room.mjs +83 -0
- package/dist/PillNotification/PillNotification.d.mts +6 -0
- package/dist/PillNotification/PillNotification.d.ts +6 -0
- package/dist/PillNotification/PillNotification.js +3 -0
- package/dist/PillNotification/PillNotification.mjs +3 -0
- package/dist/PillOptics/PillOptics.d.mts +8 -0
- package/dist/PillOptics/PillOptics.d.ts +8 -0
- package/dist/PillOptics/PillOptics.js +195 -0
- package/dist/PillOptics/PillOptics.mjs +195 -0
- package/dist/PillOptics/adaptive.js +212 -0
- package/dist/PillOptics/adaptive.mjs +208 -0
- package/dist/PillOptics/lens.js +77 -0
- package/dist/PillOptics/lens.mjs +73 -0
- package/dist/PillOptics/light.js +37 -0
- package/dist/PillOptics/light.mjs +36 -0
- package/dist/calendar.css +499 -0
- package/dist/forms.css +1454 -0
- package/dist/index.d.mts +24 -3
- package/dist/index.d.ts +24 -3
- package/dist/index.js +36 -0
- package/dist/index.mjs +19 -1
- package/dist/styles.css +169 -19
- package/dist/themes.css +449 -0
- package/dist/utils/visuallyHidden.js +18 -0
- package/dist/utils/visuallyHidden.mjs +18 -0
- package/package.json +4 -1
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
const require_themed = require("../Pill/themed.js");
|
|
5
|
+
const require_adaptive = require("./adaptive.js");
|
|
6
|
+
const require_lens = require("./lens.js");
|
|
7
|
+
const require_light = require("./light.js");
|
|
8
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
9
|
+
|
|
10
|
+
// src/PillOptics/PillOptics.tsx
|
|
11
|
+
const LENSES = [
|
|
12
|
+
"--_mp-lens",
|
|
13
|
+
"--_mp-action-lens",
|
|
14
|
+
"--_mp-list-lens"
|
|
15
|
+
];
|
|
16
|
+
const WATCHED = /^(--_mp-(refraction|edge|fringe|follow|adaptive|action-layer|action-radius)|border-top-left-radius)$/;
|
|
17
|
+
/** Red, green and blue: how far each one bends apart, and the matrix that keeps it alone. */
|
|
18
|
+
const CHANNELS = [
|
|
19
|
+
[1, "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"],
|
|
20
|
+
[0, "0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0"],
|
|
21
|
+
[-1, "0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0"]
|
|
22
|
+
];
|
|
23
|
+
function LensFilter({ lens }) {
|
|
24
|
+
const bend = (apart, result) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("feDisplacementMap", {
|
|
25
|
+
in: "SourceGraphic",
|
|
26
|
+
in2: "map",
|
|
27
|
+
scale: lens.depth * 2 * (1 + apart * lens.fringe),
|
|
28
|
+
xChannelSelector: "R",
|
|
29
|
+
yChannelSelector: "G",
|
|
30
|
+
result
|
|
31
|
+
});
|
|
32
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("filter", {
|
|
33
|
+
id: require_lens.lensId(lens),
|
|
34
|
+
x: "0",
|
|
35
|
+
y: "0",
|
|
36
|
+
width: "100%",
|
|
37
|
+
height: "100%",
|
|
38
|
+
primitiveUnits: "userSpaceOnUse",
|
|
39
|
+
colorInterpolationFilters: "sRGB",
|
|
40
|
+
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("feImage", {
|
|
41
|
+
href: require_lens.lensMap(lens),
|
|
42
|
+
preserveAspectRatio: "none",
|
|
43
|
+
result: "map"
|
|
44
|
+
}), lens.fringe === 0 ? bend(0) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
|
|
45
|
+
CHANNELS.map(([apart, matrix], index) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react.Fragment, { children: [bend(apart, `bent${index}`), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("feColorMatrix", {
|
|
46
|
+
in: `bent${index}`,
|
|
47
|
+
type: "matrix",
|
|
48
|
+
values: matrix,
|
|
49
|
+
result: `channel${index}`
|
|
50
|
+
})] }, apart)),
|
|
51
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("feComposite", {
|
|
52
|
+
operator: "arithmetic",
|
|
53
|
+
k2: 1,
|
|
54
|
+
k3: 1,
|
|
55
|
+
in: "channel0",
|
|
56
|
+
in2: "channel1",
|
|
57
|
+
result: "two"
|
|
58
|
+
}),
|
|
59
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("feComposite", {
|
|
60
|
+
operator: "arithmetic",
|
|
61
|
+
k2: 1,
|
|
62
|
+
k3: 1,
|
|
63
|
+
in: "two",
|
|
64
|
+
in2: "channel2"
|
|
65
|
+
})
|
|
66
|
+
] })]
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const reader = (element) => {
|
|
70
|
+
const style = getComputedStyle(element);
|
|
71
|
+
return (property) => Number.parseFloat(style.getPropertyValue(property)) || 0;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* The lenses of a pill, of its actions, and of the lists and calendars it opens
|
|
75
|
+
* (null: none), from its tokens. In the order of LENSES.
|
|
76
|
+
*/
|
|
77
|
+
function readLenses(element, read, glassActions) {
|
|
78
|
+
const refraction = read("--_mp-refraction");
|
|
79
|
+
if (refraction <= 0) return [];
|
|
80
|
+
const edge = read("--_mp-edge");
|
|
81
|
+
const fringe = read("--_mp-fringe");
|
|
82
|
+
const corner = Math.min(read("border-top-left-radius"), element.offsetWidth / 2, element.offsetHeight / 2);
|
|
83
|
+
const pill = require_lens.makeLens(corner, edge, refraction, fringe);
|
|
84
|
+
const scaled = (radius) => {
|
|
85
|
+
const scale = corner > 0 ? Math.min(1, radius / corner) : 1;
|
|
86
|
+
return require_lens.makeLens(radius, edge * scale, refraction * scale, fringe);
|
|
87
|
+
};
|
|
88
|
+
return [
|
|
89
|
+
pill,
|
|
90
|
+
glassActions ? scaled(read("--_mp-action-radius")) : null,
|
|
91
|
+
scaled(read("--_mp-list-radius"))
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
const clear = (element) => LENSES.forEach((property) => element.style.removeProperty(property));
|
|
95
|
+
/** Runs an effect for the pills that ask for it, and only while some do. */
|
|
96
|
+
function makeGroup(start) {
|
|
97
|
+
let pills = [];
|
|
98
|
+
let stop;
|
|
99
|
+
return (next) => {
|
|
100
|
+
if (next.length === pills.length && next.every((pill) => pills.includes(pill))) return;
|
|
101
|
+
stop?.();
|
|
102
|
+
pills = next;
|
|
103
|
+
stop = next.length > 0 ? start(new Set(next)) : void 0;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* What themes can't do in CSS. Render it once, anywhere: the tokens of each
|
|
108
|
+
* themed pill decide. It draws the refraction of the glass (in the browsers
|
|
109
|
+
* that can: Chromium), makes the actions of the same glass
|
|
110
|
+
* (`--mp-action-glass`), has the light follow the pointer (`--mp-light-follow`),
|
|
111
|
+
* and tints the pill after what's behind it (`--mp-adaptive`).
|
|
112
|
+
*/
|
|
113
|
+
function PillOptics() {
|
|
114
|
+
const [lenses, setLenses] = (0, react.useState)([]);
|
|
115
|
+
const wanted = (0, react.useRef)(/* @__PURE__ */ new Map());
|
|
116
|
+
const apply = () => {
|
|
117
|
+
for (const [element, ids] of wanted.current) LENSES.forEach((property, index) => {
|
|
118
|
+
if (ids[index]) element.style.setProperty(property, `url(#${ids[index]})`);
|
|
119
|
+
else element.style.removeProperty(property);
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
(0, react.useLayoutEffect)(apply, [lenses]);
|
|
123
|
+
(0, react.useEffect)(() => {
|
|
124
|
+
let stopped = false;
|
|
125
|
+
let pending = false;
|
|
126
|
+
let refracts;
|
|
127
|
+
const following = makeGroup(require_light.followPointer);
|
|
128
|
+
const adaptive = makeGroup(require_adaptive.adapt);
|
|
129
|
+
const update = () => {
|
|
130
|
+
pending = false;
|
|
131
|
+
if (stopped) return;
|
|
132
|
+
const next = /* @__PURE__ */ new Map();
|
|
133
|
+
const before = wanted.current;
|
|
134
|
+
wanted.current = /* @__PURE__ */ new Map();
|
|
135
|
+
const follow = [];
|
|
136
|
+
const adapted = [];
|
|
137
|
+
for (const element of require_themed.themedPills) {
|
|
138
|
+
const read = reader(element);
|
|
139
|
+
if (read("--_mp-follow") > 0) follow.push(element);
|
|
140
|
+
if (read("--_mp-adaptive") > 0) adapted.push(element);
|
|
141
|
+
const glassActions = read("--_mp-action-layer") > 0;
|
|
142
|
+
if (glassActions) element.dataset.actions = "glass";
|
|
143
|
+
else delete element.dataset.actions;
|
|
144
|
+
const found = refracts === false ? [] : readLenses(element, read, glassActions);
|
|
145
|
+
if (!found.some((lens) => lens !== null)) continue;
|
|
146
|
+
refracts ?? (refracts = require_lens.canRefract());
|
|
147
|
+
if (!refracts) continue;
|
|
148
|
+
for (const lens of found) if (lens) next.set(require_lens.lensId(lens), lens);
|
|
149
|
+
wanted.current.set(element, found.map((lens) => lens && require_lens.lensId(lens)));
|
|
150
|
+
}
|
|
151
|
+
for (const element of before.keys()) if (!wanted.current.has(element)) clear(element);
|
|
152
|
+
following(follow);
|
|
153
|
+
adaptive(adapted);
|
|
154
|
+
setLenses((current) => {
|
|
155
|
+
return current.length === next.size && current.every((lens) => next.has(require_lens.lensId(lens))) ? current : [...next.values()];
|
|
156
|
+
});
|
|
157
|
+
apply();
|
|
158
|
+
};
|
|
159
|
+
const schedule = () => {
|
|
160
|
+
if (pending) return;
|
|
161
|
+
pending = true;
|
|
162
|
+
queueMicrotask(update);
|
|
163
|
+
};
|
|
164
|
+
const onTransitionEnd = ({ target, propertyName }) => {
|
|
165
|
+
if (WATCHED.test(propertyName) && require_themed.themedPills.has(target)) schedule();
|
|
166
|
+
};
|
|
167
|
+
schedule();
|
|
168
|
+
const unsubscribe = require_themed.onThemedPills(schedule);
|
|
169
|
+
document.addEventListener("transitionend", onTransitionEnd);
|
|
170
|
+
window.addEventListener("resize", schedule);
|
|
171
|
+
return () => {
|
|
172
|
+
stopped = true;
|
|
173
|
+
unsubscribe();
|
|
174
|
+
document.removeEventListener("transitionend", onTransitionEnd);
|
|
175
|
+
window.removeEventListener("resize", schedule);
|
|
176
|
+
for (const element of wanted.current.keys()) clear(element);
|
|
177
|
+
for (const element of require_themed.themedPills) delete element.dataset.actions;
|
|
178
|
+
wanted.current.clear();
|
|
179
|
+
following([]);
|
|
180
|
+
adaptive([]);
|
|
181
|
+
};
|
|
182
|
+
}, []);
|
|
183
|
+
if (lenses.length === 0) return null;
|
|
184
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("svg", {
|
|
185
|
+
"aria-hidden": "true",
|
|
186
|
+
focusable: "false",
|
|
187
|
+
width: "0",
|
|
188
|
+
height: "0",
|
|
189
|
+
style: { position: "absolute" },
|
|
190
|
+
"data-mp": "optics",
|
|
191
|
+
children: lenses.map((lens) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LensFilter, { lens }, require_lens.lensId(lens)))
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
exports.PillOptics = PillOptics;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Fragment, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
4
|
+
import { onThemedPills, themedPills } from "../Pill/themed.mjs";
|
|
5
|
+
import { adapt } from "./adaptive.mjs";
|
|
6
|
+
import { canRefract, lensId, lensMap, makeLens } from "./lens.mjs";
|
|
7
|
+
import { followPointer } from "./light.mjs";
|
|
8
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
|
|
10
|
+
// src/PillOptics/PillOptics.tsx
|
|
11
|
+
const LENSES = [
|
|
12
|
+
"--_mp-lens",
|
|
13
|
+
"--_mp-action-lens",
|
|
14
|
+
"--_mp-list-lens"
|
|
15
|
+
];
|
|
16
|
+
const WATCHED = /^(--_mp-(refraction|edge|fringe|follow|adaptive|action-layer|action-radius)|border-top-left-radius)$/;
|
|
17
|
+
/** Red, green and blue: how far each one bends apart, and the matrix that keeps it alone. */
|
|
18
|
+
const CHANNELS = [
|
|
19
|
+
[1, "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"],
|
|
20
|
+
[0, "0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0"],
|
|
21
|
+
[-1, "0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0"]
|
|
22
|
+
];
|
|
23
|
+
function LensFilter({ lens }) {
|
|
24
|
+
const bend = (apart, result) => /* @__PURE__ */ jsx("feDisplacementMap", {
|
|
25
|
+
in: "SourceGraphic",
|
|
26
|
+
in2: "map",
|
|
27
|
+
scale: lens.depth * 2 * (1 + apart * lens.fringe),
|
|
28
|
+
xChannelSelector: "R",
|
|
29
|
+
yChannelSelector: "G",
|
|
30
|
+
result
|
|
31
|
+
});
|
|
32
|
+
return /* @__PURE__ */ jsxs("filter", {
|
|
33
|
+
id: lensId(lens),
|
|
34
|
+
x: "0",
|
|
35
|
+
y: "0",
|
|
36
|
+
width: "100%",
|
|
37
|
+
height: "100%",
|
|
38
|
+
primitiveUnits: "userSpaceOnUse",
|
|
39
|
+
colorInterpolationFilters: "sRGB",
|
|
40
|
+
children: [/* @__PURE__ */ jsx("feImage", {
|
|
41
|
+
href: lensMap(lens),
|
|
42
|
+
preserveAspectRatio: "none",
|
|
43
|
+
result: "map"
|
|
44
|
+
}), lens.fringe === 0 ? bend(0) : /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
45
|
+
CHANNELS.map(([apart, matrix], index) => /* @__PURE__ */ jsxs(Fragment, { children: [bend(apart, `bent${index}`), /* @__PURE__ */ jsx("feColorMatrix", {
|
|
46
|
+
in: `bent${index}`,
|
|
47
|
+
type: "matrix",
|
|
48
|
+
values: matrix,
|
|
49
|
+
result: `channel${index}`
|
|
50
|
+
})] }, apart)),
|
|
51
|
+
/* @__PURE__ */ jsx("feComposite", {
|
|
52
|
+
operator: "arithmetic",
|
|
53
|
+
k2: 1,
|
|
54
|
+
k3: 1,
|
|
55
|
+
in: "channel0",
|
|
56
|
+
in2: "channel1",
|
|
57
|
+
result: "two"
|
|
58
|
+
}),
|
|
59
|
+
/* @__PURE__ */ jsx("feComposite", {
|
|
60
|
+
operator: "arithmetic",
|
|
61
|
+
k2: 1,
|
|
62
|
+
k3: 1,
|
|
63
|
+
in: "two",
|
|
64
|
+
in2: "channel2"
|
|
65
|
+
})
|
|
66
|
+
] })]
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const reader = (element) => {
|
|
70
|
+
const style = getComputedStyle(element);
|
|
71
|
+
return (property) => Number.parseFloat(style.getPropertyValue(property)) || 0;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* The lenses of a pill, of its actions, and of the lists and calendars it opens
|
|
75
|
+
* (null: none), from its tokens. In the order of LENSES.
|
|
76
|
+
*/
|
|
77
|
+
function readLenses(element, read, glassActions) {
|
|
78
|
+
const refraction = read("--_mp-refraction");
|
|
79
|
+
if (refraction <= 0) return [];
|
|
80
|
+
const edge = read("--_mp-edge");
|
|
81
|
+
const fringe = read("--_mp-fringe");
|
|
82
|
+
const corner = Math.min(read("border-top-left-radius"), element.offsetWidth / 2, element.offsetHeight / 2);
|
|
83
|
+
const pill = makeLens(corner, edge, refraction, fringe);
|
|
84
|
+
const scaled = (radius) => {
|
|
85
|
+
const scale = corner > 0 ? Math.min(1, radius / corner) : 1;
|
|
86
|
+
return makeLens(radius, edge * scale, refraction * scale, fringe);
|
|
87
|
+
};
|
|
88
|
+
return [
|
|
89
|
+
pill,
|
|
90
|
+
glassActions ? scaled(read("--_mp-action-radius")) : null,
|
|
91
|
+
scaled(read("--_mp-list-radius"))
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
const clear = (element) => LENSES.forEach((property) => element.style.removeProperty(property));
|
|
95
|
+
/** Runs an effect for the pills that ask for it, and only while some do. */
|
|
96
|
+
function makeGroup(start) {
|
|
97
|
+
let pills = [];
|
|
98
|
+
let stop;
|
|
99
|
+
return (next) => {
|
|
100
|
+
if (next.length === pills.length && next.every((pill) => pills.includes(pill))) return;
|
|
101
|
+
stop?.();
|
|
102
|
+
pills = next;
|
|
103
|
+
stop = next.length > 0 ? start(new Set(next)) : void 0;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* What themes can't do in CSS. Render it once, anywhere: the tokens of each
|
|
108
|
+
* themed pill decide. It draws the refraction of the glass (in the browsers
|
|
109
|
+
* that can: Chromium), makes the actions of the same glass
|
|
110
|
+
* (`--mp-action-glass`), has the light follow the pointer (`--mp-light-follow`),
|
|
111
|
+
* and tints the pill after what's behind it (`--mp-adaptive`).
|
|
112
|
+
*/
|
|
113
|
+
function PillOptics() {
|
|
114
|
+
const [lenses, setLenses] = useState([]);
|
|
115
|
+
const wanted = useRef(/* @__PURE__ */ new Map());
|
|
116
|
+
const apply = () => {
|
|
117
|
+
for (const [element, ids] of wanted.current) LENSES.forEach((property, index) => {
|
|
118
|
+
if (ids[index]) element.style.setProperty(property, `url(#${ids[index]})`);
|
|
119
|
+
else element.style.removeProperty(property);
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
useLayoutEffect(apply, [lenses]);
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
let stopped = false;
|
|
125
|
+
let pending = false;
|
|
126
|
+
let refracts;
|
|
127
|
+
const following = makeGroup(followPointer);
|
|
128
|
+
const adaptive = makeGroup(adapt);
|
|
129
|
+
const update = () => {
|
|
130
|
+
pending = false;
|
|
131
|
+
if (stopped) return;
|
|
132
|
+
const next = /* @__PURE__ */ new Map();
|
|
133
|
+
const before = wanted.current;
|
|
134
|
+
wanted.current = /* @__PURE__ */ new Map();
|
|
135
|
+
const follow = [];
|
|
136
|
+
const adapted = [];
|
|
137
|
+
for (const element of themedPills) {
|
|
138
|
+
const read = reader(element);
|
|
139
|
+
if (read("--_mp-follow") > 0) follow.push(element);
|
|
140
|
+
if (read("--_mp-adaptive") > 0) adapted.push(element);
|
|
141
|
+
const glassActions = read("--_mp-action-layer") > 0;
|
|
142
|
+
if (glassActions) element.dataset.actions = "glass";
|
|
143
|
+
else delete element.dataset.actions;
|
|
144
|
+
const found = refracts === false ? [] : readLenses(element, read, glassActions);
|
|
145
|
+
if (!found.some((lens) => lens !== null)) continue;
|
|
146
|
+
refracts ?? (refracts = canRefract());
|
|
147
|
+
if (!refracts) continue;
|
|
148
|
+
for (const lens of found) if (lens) next.set(lensId(lens), lens);
|
|
149
|
+
wanted.current.set(element, found.map((lens) => lens && lensId(lens)));
|
|
150
|
+
}
|
|
151
|
+
for (const element of before.keys()) if (!wanted.current.has(element)) clear(element);
|
|
152
|
+
following(follow);
|
|
153
|
+
adaptive(adapted);
|
|
154
|
+
setLenses((current) => {
|
|
155
|
+
return current.length === next.size && current.every((lens) => next.has(lensId(lens))) ? current : [...next.values()];
|
|
156
|
+
});
|
|
157
|
+
apply();
|
|
158
|
+
};
|
|
159
|
+
const schedule = () => {
|
|
160
|
+
if (pending) return;
|
|
161
|
+
pending = true;
|
|
162
|
+
queueMicrotask(update);
|
|
163
|
+
};
|
|
164
|
+
const onTransitionEnd = ({ target, propertyName }) => {
|
|
165
|
+
if (WATCHED.test(propertyName) && themedPills.has(target)) schedule();
|
|
166
|
+
};
|
|
167
|
+
schedule();
|
|
168
|
+
const unsubscribe = onThemedPills(schedule);
|
|
169
|
+
document.addEventListener("transitionend", onTransitionEnd);
|
|
170
|
+
window.addEventListener("resize", schedule);
|
|
171
|
+
return () => {
|
|
172
|
+
stopped = true;
|
|
173
|
+
unsubscribe();
|
|
174
|
+
document.removeEventListener("transitionend", onTransitionEnd);
|
|
175
|
+
window.removeEventListener("resize", schedule);
|
|
176
|
+
for (const element of wanted.current.keys()) clear(element);
|
|
177
|
+
for (const element of themedPills) delete element.dataset.actions;
|
|
178
|
+
wanted.current.clear();
|
|
179
|
+
following([]);
|
|
180
|
+
adaptive([]);
|
|
181
|
+
};
|
|
182
|
+
}, []);
|
|
183
|
+
if (lenses.length === 0) return null;
|
|
184
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
185
|
+
"aria-hidden": "true",
|
|
186
|
+
focusable: "false",
|
|
187
|
+
width: "0",
|
|
188
|
+
height: "0",
|
|
189
|
+
style: { position: "absolute" },
|
|
190
|
+
"data-mp": "optics",
|
|
191
|
+
children: lenses.map((lens) => /* @__PURE__ */ jsx(LensFilter, { lens }, lensId(lens)))
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export { PillOptics };
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// src/PillOptics/adaptive.ts
|
|
2
|
+
/** Past this, what's underneath no longer shows. */
|
|
3
|
+
const OPAQUE = .95;
|
|
4
|
+
/** A smaller change of colour than this (out of 255) isn't seen, and restyling the pill costs more than reading the page. */
|
|
5
|
+
const NOTICEABLE = 10;
|
|
6
|
+
/** Where the page is read, as parts of the pill's width and height. */
|
|
7
|
+
const ACROSS = [
|
|
8
|
+
.06,
|
|
9
|
+
.28,
|
|
10
|
+
.5,
|
|
11
|
+
.72,
|
|
12
|
+
.94
|
|
13
|
+
];
|
|
14
|
+
const DOWN = [.2, .8];
|
|
15
|
+
/** How wide a patch of a picture is read around each point, in px. */
|
|
16
|
+
const PATCH = 16;
|
|
17
|
+
/** Once per burst of scrolling events, and for what changes by itself (a new page, a video…). */
|
|
18
|
+
const SCROLL_DELAY = 100;
|
|
19
|
+
const INTERVAL = 500;
|
|
20
|
+
/** What layers look like on top of each other, front to back. */
|
|
21
|
+
function flatten(layers) {
|
|
22
|
+
const flat = [
|
|
23
|
+
0,
|
|
24
|
+
0,
|
|
25
|
+
0
|
|
26
|
+
];
|
|
27
|
+
let alpha = 0;
|
|
28
|
+
for (const layer of layers) {
|
|
29
|
+
const share = (1 - alpha) * layer[3];
|
|
30
|
+
flat.forEach((_, channel) => flat[channel] += layer[channel] * share);
|
|
31
|
+
alpha += share;
|
|
32
|
+
}
|
|
33
|
+
return alpha === 0 ? [
|
|
34
|
+
0,
|
|
35
|
+
0,
|
|
36
|
+
0,
|
|
37
|
+
0
|
|
38
|
+
] : [
|
|
39
|
+
flat[0] / alpha,
|
|
40
|
+
flat[1] / alpha,
|
|
41
|
+
flat[2] / alpha,
|
|
42
|
+
alpha
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
/** The mean of some colours, or null without any. */
|
|
46
|
+
function mean(colours) {
|
|
47
|
+
if (colours.length === 0) return null;
|
|
48
|
+
const average = (channel) => colours.reduce((sum, colour) => sum + colour[channel], 0) / colours.length;
|
|
49
|
+
return [
|
|
50
|
+
average(0),
|
|
51
|
+
average(1),
|
|
52
|
+
average(2),
|
|
53
|
+
average(3)
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
/** Whether two colours are far enough apart to be told apart on a pill. */
|
|
57
|
+
const differ = (one, other) => [
|
|
58
|
+
0,
|
|
59
|
+
1,
|
|
60
|
+
2
|
|
61
|
+
].some((channel) => Math.abs(one[channel] - other[channel]) >= NOTICEABLE);
|
|
62
|
+
const linear = (value) => {
|
|
63
|
+
const part = value / 255;
|
|
64
|
+
return part <= .04045 ? part / 12.92 : ((part + .055) / 1.055) ** 2.4;
|
|
65
|
+
};
|
|
66
|
+
/** Over this colour, a light pill or a dark one? With a margin around the limit, so it doesn't flicker. */
|
|
67
|
+
function pickScheme([red, green, blue], previous) {
|
|
68
|
+
return .2126 * linear(red) + .7152 * linear(green) + .0722 * linear(blue) > (previous === "light" ? .3 : .45) ? "light" : "dark";
|
|
69
|
+
}
|
|
70
|
+
let pixel = null;
|
|
71
|
+
function readPixel(draw) {
|
|
72
|
+
pixel ?? (pixel = document.createElement("canvas").getContext("2d", { willReadFrequently: true }));
|
|
73
|
+
if (!pixel) return null;
|
|
74
|
+
pixel.clearRect(0, 0, 1, 1);
|
|
75
|
+
try {
|
|
76
|
+
draw(pixel);
|
|
77
|
+
const [red, green, blue, alpha] = pixel.getImageData(0, 0, 1, 1).data;
|
|
78
|
+
return [
|
|
79
|
+
red,
|
|
80
|
+
green,
|
|
81
|
+
blue,
|
|
82
|
+
alpha / 255
|
|
83
|
+
];
|
|
84
|
+
} catch {
|
|
85
|
+
pixel = null;
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const KNOWN_COLOURS = 200;
|
|
90
|
+
const knownColours = /* @__PURE__ */ new Map();
|
|
91
|
+
function readColour(css) {
|
|
92
|
+
const known = knownColours.get(css);
|
|
93
|
+
if (known) return known;
|
|
94
|
+
const colour = readPixel((context) => {
|
|
95
|
+
context.fillStyle = css;
|
|
96
|
+
context.fillRect(0, 0, 1, 1);
|
|
97
|
+
});
|
|
98
|
+
if (knownColours.size >= KNOWN_COLOURS) knownColours.clear();
|
|
99
|
+
if (colour) knownColours.set(css, colour);
|
|
100
|
+
return colour;
|
|
101
|
+
}
|
|
102
|
+
const unreadable = /* @__PURE__ */ new WeakMap();
|
|
103
|
+
const sourceOf = (picture) => "currentSrc" in picture ? picture.currentSrc : "";
|
|
104
|
+
function sizeOf(picture) {
|
|
105
|
+
if (picture instanceof HTMLImageElement) return [picture.naturalWidth, picture.naturalHeight];
|
|
106
|
+
if (picture instanceof HTMLVideoElement) return [picture.videoWidth, picture.videoHeight];
|
|
107
|
+
return [picture.width, picture.height];
|
|
108
|
+
}
|
|
109
|
+
/** The mean colour of a picture around a point (as if it filled its box: `object-fit` isn't worth more). */
|
|
110
|
+
function readPicture(picture, x, y) {
|
|
111
|
+
if (unreadable.get(picture) === sourceOf(picture)) return null;
|
|
112
|
+
const [sourceWidth, sourceHeight] = sizeOf(picture);
|
|
113
|
+
const { left, top, width, height } = picture.getBoundingClientRect();
|
|
114
|
+
if (sourceWidth === 0 || sourceHeight === 0 || width === 0 || height === 0) return null;
|
|
115
|
+
const scaleX = sourceWidth / width;
|
|
116
|
+
const scaleY = sourceHeight / height;
|
|
117
|
+
const colour = readPixel((context) => context.drawImage(picture, (x - left - PATCH / 2) * scaleX, (y - top - PATCH / 2) * scaleY, PATCH * scaleX, PATCH * scaleY, 0, 0, 1, 1));
|
|
118
|
+
if (!colour) unreadable.set(picture, sourceOf(picture));
|
|
119
|
+
return colour;
|
|
120
|
+
}
|
|
121
|
+
const isPicture = (element) => element.matches("img, canvas, video");
|
|
122
|
+
/** The layers an element paints at a point, front to back. `url()` backgrounds can't be read. */
|
|
123
|
+
function layersOf(element, x, y) {
|
|
124
|
+
const { backgroundColor, backgroundImage } = getComputedStyle(element);
|
|
125
|
+
const stops = (backgroundImage.match(/(?:rgb|hsl|hwb|lab|lch|oklab|oklch|color)a?\([^()]*\)/g) ?? []).map(readColour);
|
|
126
|
+
return [
|
|
127
|
+
isPicture(element) ? readPicture(element, x, y) : null,
|
|
128
|
+
mean(stops.filter((stop) => stop !== null)),
|
|
129
|
+
readColour(backgroundColor)
|
|
130
|
+
].filter((layer) => layer !== null);
|
|
131
|
+
}
|
|
132
|
+
/** What the page shows where nothing else does. */
|
|
133
|
+
function canvasColour() {
|
|
134
|
+
for (const element of [document.documentElement, document.body]) {
|
|
135
|
+
const colour = readColour(getComputedStyle(element).backgroundColor);
|
|
136
|
+
if (colour && colour[3] > 0) return colour;
|
|
137
|
+
}
|
|
138
|
+
const scheme = getComputedStyle(document.documentElement).colorScheme ?? "";
|
|
139
|
+
return scheme.includes("dark") && (!scheme.includes("light") || matchMedia("(prefers-color-scheme: dark)").matches) ? [
|
|
140
|
+
18,
|
|
141
|
+
18,
|
|
142
|
+
18,
|
|
143
|
+
1
|
|
144
|
+
] : [
|
|
145
|
+
255,
|
|
146
|
+
255,
|
|
147
|
+
255,
|
|
148
|
+
1
|
|
149
|
+
];
|
|
150
|
+
}
|
|
151
|
+
/** The page's colour at a point, pills left out, or null out of the window. */
|
|
152
|
+
function colourAt(x, y) {
|
|
153
|
+
const elements = document.elementsFromPoint(x, y);
|
|
154
|
+
if (elements.length === 0) return null;
|
|
155
|
+
const layers = [];
|
|
156
|
+
for (const element of elements) {
|
|
157
|
+
if (element.closest("[data-mp=\"pill\"]")) continue;
|
|
158
|
+
layers.push(...layersOf(element, x, y));
|
|
159
|
+
if (flatten(layers)[3] > OPAQUE) return flatten(layers);
|
|
160
|
+
}
|
|
161
|
+
return flatten([...layers, canvasColour()]);
|
|
162
|
+
}
|
|
163
|
+
/** The mean colour of the page behind the pill, or null when none of it is in the window. */
|
|
164
|
+
function colourBehind(pill) {
|
|
165
|
+
const { left, top, width, height } = pill.getBoundingClientRect();
|
|
166
|
+
return mean(ACROSS.flatMap((across) => DOWN.map((down) => colourAt(left + width * across, top + height * down))).filter((colour) => colour !== null));
|
|
167
|
+
}
|
|
168
|
+
const isModalOpen = () => [...document.querySelectorAll("dialog[open]")].some((dialog) => dialog.matches(":modal"));
|
|
169
|
+
/** Keeps the pills' scheme and ambient colour up to date. Returns what stops it, and gives them their own colours back. */
|
|
170
|
+
function adapt(pills) {
|
|
171
|
+
let timer;
|
|
172
|
+
const shown = /* @__PURE__ */ new Map();
|
|
173
|
+
const update = () => {
|
|
174
|
+
if (document.hidden || isModalOpen()) return;
|
|
175
|
+
for (const pill of pills) {
|
|
176
|
+
if (pill.dataset.presence === "hidden") continue;
|
|
177
|
+
const colour = colourBehind(pill);
|
|
178
|
+
const last = shown.get(pill);
|
|
179
|
+
if (!colour || last && !differ(colour, last)) continue;
|
|
180
|
+
shown.set(pill, colour);
|
|
181
|
+
pill.dataset.scheme = pickScheme(colour, pill.dataset.scheme);
|
|
182
|
+
pill.style.setProperty("--_mp-ambient", `rgb(${colour.slice(0, 3).map(Math.round).join(" ")})`);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
const onScroll = () => {
|
|
186
|
+
timer ?? (timer = setTimeout(() => {
|
|
187
|
+
timer = void 0;
|
|
188
|
+
update();
|
|
189
|
+
}, SCROLL_DELAY));
|
|
190
|
+
};
|
|
191
|
+
update();
|
|
192
|
+
document.addEventListener("scroll", onScroll, {
|
|
193
|
+
passive: true,
|
|
194
|
+
capture: true
|
|
195
|
+
});
|
|
196
|
+
const interval = setInterval(update, INTERVAL);
|
|
197
|
+
return () => {
|
|
198
|
+
document.removeEventListener("scroll", onScroll, { capture: true });
|
|
199
|
+
clearInterval(interval);
|
|
200
|
+
clearTimeout(timer);
|
|
201
|
+
for (const pill of pills) {
|
|
202
|
+
delete pill.dataset.scheme;
|
|
203
|
+
pill.style.removeProperty("--_mp-ambient");
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
exports.adapt = adapt;
|
|
209
|
+
exports.differ = differ;
|
|
210
|
+
exports.flatten = flatten;
|
|
211
|
+
exports.mean = mean;
|
|
212
|
+
exports.pickScheme = pickScheme;
|