magic-pill 9.1.1 → 9.2.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/CHANGELOG.md +19 -0
- package/README.md +10 -2
- package/dist/MagicPill/MagicPill.d.mts +9 -1
- package/dist/MagicPill/MagicPill.d.ts +9 -1
- package/dist/MagicPill/MagicPill.js +207 -106
- package/dist/MagicPill/MagicPill.mjs +208 -107
- package/dist/MagicPill/flag.js +93 -0
- package/dist/MagicPill/flag.mjs +91 -0
- package/dist/MagicPill/useChoreography.js +65 -0
- package/dist/MagicPill/useChoreography.mjs +65 -0
- package/dist/Pill/Pill.js +20 -9
- package/dist/Pill/Pill.mjs +21 -10
- package/dist/Pill/morph.js +175 -24
- package/dist/Pill/morph.mjs +174 -25
- package/dist/Pill/travel.js +76 -0
- package/dist/Pill/travel.mjs +75 -0
- package/dist/Pill/types.d.mts +8 -0
- package/dist/Pill/types.d.ts +8 -0
- package/dist/PillForm/PillField.js +16 -1
- package/dist/PillForm/PillField.mjs +17 -2
- package/dist/PillNavbar/PillNavbar.js +2 -1
- package/dist/PillNavbar/PillNavbar.mjs +2 -1
- package/dist/PillNotification/PillNotification.d.mts +16 -1
- package/dist/PillNotification/PillNotification.d.ts +16 -1
- package/dist/PillNotification/PillNotification.js +41 -30
- package/dist/PillNotification/PillNotification.mjs +41 -30
- package/dist/forms.css +38 -26
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/store/store.d.mts +9 -1
- package/dist/store/store.d.ts +9 -1
- package/dist/styles.css +85 -0
- package/package.json +1 -1
package/dist/Pill/morph.mjs
CHANGED
|
@@ -7,6 +7,8 @@ import { parseDuration, prefersReducedMotion } from "../utils/timing.mjs";
|
|
|
7
7
|
const CLOSED = /* @__PURE__ */ new Set(["rest", "hidden"]);
|
|
8
8
|
/** Tags this module's animations, so a size change can stop the one before it. */
|
|
9
9
|
const MORPH = "mp-morph";
|
|
10
|
+
/** Tags the placement animation, which runs alongside the size ones. */
|
|
11
|
+
const MOVE = "mp-move";
|
|
10
12
|
/** The length of the fading edge over the panel's clipped content. */
|
|
11
13
|
const EDGE = 48;
|
|
12
14
|
const EASING = "ease-in-out";
|
|
@@ -38,7 +40,7 @@ const held = (width) => ({
|
|
|
38
40
|
* own animation starts), to hold them there during an animation.
|
|
39
41
|
*/
|
|
40
42
|
function holdMessages(element) {
|
|
41
|
-
const messages = [...element.querySelectorAll(":scope > [data-mp=\"message\"]")].map((message) => [message, held(message.getBoundingClientRect().width)]);
|
|
43
|
+
const messages = [...element.querySelectorAll(":scope > :is([data-mp=\"message\"], [data-mp=\"body\"], [data-mp=\"card\"])")].map((message) => [message, held(message.getBoundingClientRect().width)]);
|
|
42
44
|
return (options) => messages.map(([message, frame]) => message.animate([frame, frame], options));
|
|
43
45
|
}
|
|
44
46
|
const sameSize = (a, b) => Math.abs(a.width - b.width) < 1 && Math.abs(a.height - b.height) < 1;
|
|
@@ -103,6 +105,78 @@ function planMorph(from, to, previous, next, timings, panel) {
|
|
|
103
105
|
}
|
|
104
106
|
return plan;
|
|
105
107
|
}
|
|
108
|
+
/** Tags the exit played by script (see `planExit`). */
|
|
109
|
+
const EXIT = "mp-exit";
|
|
110
|
+
/**
|
|
111
|
+
* The pill leaving, as the stylesheet times it: its content fades out, it
|
|
112
|
+
* shrinks to the circle (the size morph), scales out, then stops being
|
|
113
|
+
* visible. The stylesheet does it with delayed transitions, which a restyle
|
|
114
|
+
* of the whole page can cut short: on iOS, the toolbar folding away as the
|
|
115
|
+
* page scrolls resizes the viewport, and the pill vanished at once instead.
|
|
116
|
+
* Played by script on top, the exit keeps its course. Pure, so it can be tested.
|
|
117
|
+
*/
|
|
118
|
+
function planExit(start, timings, exit) {
|
|
119
|
+
const wait = timings.fade + timings.change;
|
|
120
|
+
const total = wait + exit;
|
|
121
|
+
if (exit <= 0 || total <= 0) return null;
|
|
122
|
+
const plan = { pill: {
|
|
123
|
+
keyframes: [
|
|
124
|
+
{
|
|
125
|
+
scale: start.scale,
|
|
126
|
+
visibility: "visible"
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
scale: start.scale,
|
|
130
|
+
visibility: "visible",
|
|
131
|
+
offset: wait / total,
|
|
132
|
+
easing: "ease-in"
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
scale: "0",
|
|
136
|
+
visibility: "visible"
|
|
137
|
+
}
|
|
138
|
+
],
|
|
139
|
+
options: {
|
|
140
|
+
id: EXIT,
|
|
141
|
+
duration: total
|
|
142
|
+
}
|
|
143
|
+
} };
|
|
144
|
+
if (start.content !== void 0 && timings.fade > 0) plan.content = {
|
|
145
|
+
keyframes: [{ opacity: start.content }, { opacity: "0" }],
|
|
146
|
+
options: {
|
|
147
|
+
id: EXIT,
|
|
148
|
+
duration: timings.fade,
|
|
149
|
+
easing: "ease-in"
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
return plan;
|
|
153
|
+
}
|
|
154
|
+
/** The pill's own content: what fades out as it closes (not its icon, panels or a swap's copy). */
|
|
155
|
+
const fadingContent = (element) => [...element.children].filter((child) => child instanceof HTMLElement && ![
|
|
156
|
+
"icon",
|
|
157
|
+
"panel",
|
|
158
|
+
"outgoing"
|
|
159
|
+
].includes(child.getAttribute("data-mp") ?? ""));
|
|
160
|
+
/** Plays the exit on the pill that has just been hidden. */
|
|
161
|
+
function playExit(element, timings) {
|
|
162
|
+
const style = getComputedStyle(element);
|
|
163
|
+
const scale = style.scale && style.scale !== "none" ? style.scale : "1";
|
|
164
|
+
cancel(element, EXIT, true);
|
|
165
|
+
const content = fadingContent(element);
|
|
166
|
+
const opacity = content.length > 0 ? getComputedStyle(content[0]).opacity : "0";
|
|
167
|
+
const exit = parseDuration(style.getPropertyValue("--_mp-exit"), 300);
|
|
168
|
+
const plan = planExit({
|
|
169
|
+
scale,
|
|
170
|
+
content: Number(opacity) > 0 ? opacity : void 0
|
|
171
|
+
}, timings, exit);
|
|
172
|
+
if (!plan) return [];
|
|
173
|
+
const animations = [element.animate(plan.pill.keyframes, plan.pill.options)];
|
|
174
|
+
if (plan.content) {
|
|
175
|
+
const { keyframes, options } = plan.content;
|
|
176
|
+
for (const child of content) animations.push(child.animate(keyframes, options));
|
|
177
|
+
}
|
|
178
|
+
return animations;
|
|
179
|
+
}
|
|
106
180
|
const readSize = (element) => ({
|
|
107
181
|
width: element.offsetWidth,
|
|
108
182
|
height: element.offsetHeight
|
|
@@ -202,6 +276,39 @@ function planSwap(from, to, timings, anchor, down = false) {
|
|
|
202
276
|
};
|
|
203
277
|
}
|
|
204
278
|
const CONTROLS = "input, textarea, select";
|
|
279
|
+
/** What lays a copy out the way the original is on screen, whatever the pill turns into. */
|
|
280
|
+
const FROZEN = [
|
|
281
|
+
"flex-direction",
|
|
282
|
+
"flex-wrap",
|
|
283
|
+
"align-items",
|
|
284
|
+
"justify-content",
|
|
285
|
+
"align-self",
|
|
286
|
+
"gap",
|
|
287
|
+
"direction"
|
|
288
|
+
];
|
|
289
|
+
/**
|
|
290
|
+
* The copy laid out as the original is now, element by element: its sizes
|
|
291
|
+
* and the directions its content runs in. For a pill that turns as the new
|
|
292
|
+
* content comes in (a sidebar's notification, from a column to a row): the
|
|
293
|
+
* stylesheet would lay the copy out again for the new orientation.
|
|
294
|
+
*/
|
|
295
|
+
function freezeLayout(from, to) {
|
|
296
|
+
if (!(from instanceof HTMLElement || from instanceof SVGElement) || !("style" in to)) return;
|
|
297
|
+
const computed = getComputedStyle(from);
|
|
298
|
+
const style = to.style;
|
|
299
|
+
for (const property of FROZEN) style.setProperty(property, computed.getPropertyValue(property));
|
|
300
|
+
const { width, height } = from.getBoundingClientRect();
|
|
301
|
+
if (computed.display !== "inline") {
|
|
302
|
+
style.boxSizing = "border-box";
|
|
303
|
+
style.width = px(width);
|
|
304
|
+
style.height = px(height);
|
|
305
|
+
style.flexShrink = "0";
|
|
306
|
+
}
|
|
307
|
+
[...from.children].forEach((child, index) => {
|
|
308
|
+
const copy = to.children[index];
|
|
309
|
+
if (copy) freezeLayout(child, copy);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
205
312
|
/** What was typed or chosen: a copied control starts from its default value. */
|
|
206
313
|
function copyValues(from, to) {
|
|
207
314
|
const copies = to.querySelectorAll(CONTROLS);
|
|
@@ -217,11 +324,16 @@ function copyValues(from, to) {
|
|
|
217
324
|
* laid over it, to slide out once the new content is in. Null when there's
|
|
218
325
|
* nothing to animate (unstyled, reduced motion, no Web Animations API).
|
|
219
326
|
*/
|
|
220
|
-
function captureSwap(element) {
|
|
327
|
+
function captureSwap(element, { freeze = false } = {}) {
|
|
221
328
|
if (!element || element.hasAttribute("data-unstyled") || !element.animate || prefersReducedMotion()) return null;
|
|
222
329
|
const from = readExactSize(element);
|
|
223
330
|
const outgoing = document.createElement("div");
|
|
224
|
-
for (const child of element.children)
|
|
331
|
+
for (const child of element.children) {
|
|
332
|
+
if (child.getAttribute("data-mp") === "outgoing") continue;
|
|
333
|
+
const copy = child.cloneNode(true);
|
|
334
|
+
if (freeze) freezeLayout(child, copy);
|
|
335
|
+
outgoing.append(copy);
|
|
336
|
+
}
|
|
225
337
|
stopMorphs(element);
|
|
226
338
|
copyValues(element, outgoing);
|
|
227
339
|
for (const node of outgoing.querySelectorAll("[id], [name]")) {
|
|
@@ -248,6 +360,7 @@ function captureSwap(element) {
|
|
|
248
360
|
/** Once the new content is in: the swap prepared by `captureSwap`, played. */
|
|
249
361
|
function playSwap(element, { from, outgoing, anchor, down }) {
|
|
250
362
|
stopMorphs(element, outgoing);
|
|
363
|
+
cancel(element, MOVE);
|
|
251
364
|
const plan = planSwap(from, readExactSize(element), readMorphTimings(element), anchor, down);
|
|
252
365
|
const done = () => outgoing.remove();
|
|
253
366
|
if (!plan) return done();
|
|
@@ -329,27 +442,32 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
|
|
|
329
442
|
const panel = readPanel(element);
|
|
330
443
|
if (presence === "panel") openPanel.current = panel?.box;
|
|
331
444
|
const panelBox = previous === "panel" ? openPanel.current : panel?.box;
|
|
332
|
-
const
|
|
333
|
-
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
const animations = [element.animate(plan.pill, options)];
|
|
340
|
-
if (plan.panel && panel) {
|
|
341
|
-
animations.push(panel.element.animate(plan.panel, options));
|
|
342
|
-
const footer = panel.element.querySelector("[data-mp=\"panel-footer\"]");
|
|
343
|
-
if (footer) animations.push(footer.animate([{ position: "relative" }, { position: "relative" }], options));
|
|
344
|
-
}
|
|
345
|
-
if (!CLOSED.has(presence) && presence !== "panel") animations.push(...hold(options));
|
|
346
|
-
if (plan.content) {
|
|
347
|
-
const { keyframes, options } = plan.content;
|
|
348
|
-
for (const child of element.children) if (child !== panel?.element) animations.push(child.animate(keyframes, {
|
|
349
|
-
...options,
|
|
445
|
+
const timings = readMorphTimings(element);
|
|
446
|
+
const plan = planMorph(from, to, previous, presence, timings, panelBox);
|
|
447
|
+
const animations = [];
|
|
448
|
+
if (plan) {
|
|
449
|
+
const hold = holdMessages(element);
|
|
450
|
+
const options = {
|
|
451
|
+
...plan.options,
|
|
350
452
|
id: MORPH
|
|
351
|
-
}
|
|
453
|
+
};
|
|
454
|
+
animations.push(element.animate(plan.pill, options));
|
|
455
|
+
if (plan.panel && panel) {
|
|
456
|
+
animations.push(panel.element.animate(plan.panel, options));
|
|
457
|
+
const footer = panel.element.querySelector("[data-mp=\"panel-footer\"]");
|
|
458
|
+
if (footer) animations.push(footer.animate([{ position: "relative" }, { position: "relative" }], options));
|
|
459
|
+
}
|
|
460
|
+
if (!CLOSED.has(presence) && presence !== "panel") animations.push(...hold(options));
|
|
461
|
+
if (plan.content) {
|
|
462
|
+
const { keyframes, options } = plan.content;
|
|
463
|
+
for (const child of element.children) if (child !== panel?.element) animations.push(child.animate(keyframes, {
|
|
464
|
+
...options,
|
|
465
|
+
id: MORPH
|
|
466
|
+
}));
|
|
467
|
+
}
|
|
352
468
|
}
|
|
469
|
+
if (presence === "hidden") animations.push(...playExit(element, timings));
|
|
470
|
+
if (animations.length === 0) return;
|
|
353
471
|
running.current = animations;
|
|
354
472
|
animations[0].finished.then(() => {
|
|
355
473
|
if (running.current === animations) running.current = [];
|
|
@@ -361,8 +479,39 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
|
|
|
361
479
|
lastSize
|
|
362
480
|
]);
|
|
363
481
|
}
|
|
364
|
-
/**
|
|
365
|
-
|
|
482
|
+
/**
|
|
483
|
+
* When the pill is asked back before its exit is over: the exit played by
|
|
484
|
+
* script stops, and the pill grows back from the scale it had got to, as
|
|
485
|
+
* the stylesheet's entry does. The stylesheet can't do it here: browsers
|
|
486
|
+
* don't turn a transition back once a script has animated the same property,
|
|
487
|
+
* so it would carry on scaling out first. Its leftover transition is dropped.
|
|
488
|
+
*/
|
|
489
|
+
var ExitWatch = class extends Component {
|
|
490
|
+
getSnapshotBeforeUpdate(previous) {
|
|
491
|
+
const element = this.props.target.current;
|
|
492
|
+
if (!element || previous.presence !== "hidden" || this.props.presence === "hidden") return null;
|
|
493
|
+
if ((element.getAnimations?.() ?? []).filter(({ id }) => id === EXIT).length === 0) return null;
|
|
494
|
+
const { scale } = getComputedStyle(element);
|
|
495
|
+
cancel(element, EXIT, true);
|
|
496
|
+
return scale && scale !== "none" ? scale : "1";
|
|
497
|
+
}
|
|
498
|
+
componentDidUpdate(_props, _state, scale) {
|
|
499
|
+
const element = this.props.target.current;
|
|
500
|
+
if (scale === null || !element) return;
|
|
501
|
+
for (const animation of element.getAnimations()) if (animation.transitionProperty === "scale") animation.cancel();
|
|
502
|
+
if (Number(scale) >= 1) return;
|
|
503
|
+
const style = getComputedStyle(element);
|
|
504
|
+
const enter = parseDuration(style.getPropertyValue("--_mp-enter"), 500);
|
|
505
|
+
element.animate([{ scale }, { scale: "1" }], {
|
|
506
|
+
id: EXIT,
|
|
507
|
+
duration: enter * Math.min(1, 1 - Number(scale)),
|
|
508
|
+
easing: style.getPropertyValue("--_mp-ease-enter").trim() || "ease-out"
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
render() {
|
|
512
|
+
return null;
|
|
513
|
+
}
|
|
514
|
+
};
|
|
366
515
|
/**
|
|
367
516
|
* The pill moved to another placement (or orientation): it travels there
|
|
368
517
|
* from where it was (`from`), resizing on the way if its size changed.
|
|
@@ -410,4 +559,4 @@ var PlacementWatch = class extends Component {
|
|
|
410
559
|
}
|
|
411
560
|
};
|
|
412
561
|
|
|
413
|
-
export { EASING, MORPH, PlacementWatch, animateMove, cancel, captureSwap, planMorph, planSwap, playSwap, readMorphTimings, useSizeMorph };
|
|
562
|
+
export { EASING, ExitWatch, MORPH, PlacementWatch, animateMove, cancel, captureSwap, planExit, planMorph, planSwap, playSwap, readMorphTimings, useSizeMorph };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
const require_morph = require("./morph.js");
|
|
5
|
+
const require_timing = require("../utils/timing.js");
|
|
6
|
+
|
|
7
|
+
// src/Pill/travel.ts
|
|
8
|
+
const CLOSED = /* @__PURE__ */ new Set(["rest", "hidden"]);
|
|
9
|
+
const sameSpot = (a, b) => a.placement === b.placement && a.orientation === b.orientation;
|
|
10
|
+
/**
|
|
11
|
+
* Whether the pill goes from one spot to the other through the circle. Not
|
|
12
|
+
* into or out of the page's flow, nor into or out of the modal (another
|
|
13
|
+
* element): those change at once, as before. Pure, so it can be tested.
|
|
14
|
+
*/
|
|
15
|
+
function travelsThroughCircle(from, to) {
|
|
16
|
+
const fixed = (placement) => placement !== "inline" && placement !== "overlay";
|
|
17
|
+
return !sameSpot(from, to) && fixed(from.placement) && fixed(to.placement);
|
|
18
|
+
}
|
|
19
|
+
/** The spot to show, and the level to show it at. */
|
|
20
|
+
function useTravel(rootRef, { placement, orientation, presence, enabled }) {
|
|
21
|
+
const [shown, setShown] = (0, react.useState)({
|
|
22
|
+
placement,
|
|
23
|
+
orientation
|
|
24
|
+
});
|
|
25
|
+
const [phase, setPhase] = (0, react.useState)("still");
|
|
26
|
+
const target = {
|
|
27
|
+
placement,
|
|
28
|
+
orientation
|
|
29
|
+
};
|
|
30
|
+
const targetRef = (0, react.useRef)(target);
|
|
31
|
+
targetRef.current = target;
|
|
32
|
+
const focused = (0, react.useRef)(null);
|
|
33
|
+
const moving = !sameSpot(shown, target);
|
|
34
|
+
if (moving && phase === "still") {
|
|
35
|
+
if (enabled && travelsThroughCircle(shown, target) && !CLOSED.has(presence) && !require_timing.prefersReducedMotion()) setPhase("closing");
|
|
36
|
+
else setShown(target);
|
|
37
|
+
}
|
|
38
|
+
(0, react.useLayoutEffect)(() => {
|
|
39
|
+
if (phase !== "closing") return;
|
|
40
|
+
const root = rootRef.current;
|
|
41
|
+
const active = document.activeElement;
|
|
42
|
+
focused.current = root && active instanceof HTMLElement && root.contains(active) ? active : null;
|
|
43
|
+
}, [rootRef, phase]);
|
|
44
|
+
(0, react.useEffect)(() => {
|
|
45
|
+
const root = rootRef.current;
|
|
46
|
+
if (phase === "still" || !root) return;
|
|
47
|
+
const { fade, change } = require_morph.readMorphTimings(root);
|
|
48
|
+
const timer = setTimeout(() => {
|
|
49
|
+
if (phase === "closing") {
|
|
50
|
+
setShown(targetRef.current);
|
|
51
|
+
setPhase("travelling");
|
|
52
|
+
} else setPhase("still");
|
|
53
|
+
}, fade + change);
|
|
54
|
+
return () => clearTimeout(timer);
|
|
55
|
+
}, [
|
|
56
|
+
rootRef,
|
|
57
|
+
phase,
|
|
58
|
+
shown
|
|
59
|
+
]);
|
|
60
|
+
(0, react.useEffect)(() => {
|
|
61
|
+
if (phase === "travelling" && moving) setShown(targetRef.current);
|
|
62
|
+
}, [phase, moving]);
|
|
63
|
+
(0, react.useEffect)(() => {
|
|
64
|
+
const element = focused.current;
|
|
65
|
+
if (phase !== "still" || !element) return;
|
|
66
|
+
focused.current = null;
|
|
67
|
+
if (document.activeElement === document.body && element.isConnected) element.focus({ preventScroll: true });
|
|
68
|
+
}, [phase]);
|
|
69
|
+
return {
|
|
70
|
+
...shown,
|
|
71
|
+
presence: phase !== "still" && !CLOSED.has(presence) ? "rest" : presence
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
exports.travelsThroughCircle = travelsThroughCircle;
|
|
76
|
+
exports.useTravel = useTravel;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
4
|
+
import { readMorphTimings } from "./morph.mjs";
|
|
5
|
+
import { prefersReducedMotion } from "../utils/timing.mjs";
|
|
6
|
+
|
|
7
|
+
// src/Pill/travel.ts
|
|
8
|
+
const CLOSED = /* @__PURE__ */ new Set(["rest", "hidden"]);
|
|
9
|
+
const sameSpot = (a, b) => a.placement === b.placement && a.orientation === b.orientation;
|
|
10
|
+
/**
|
|
11
|
+
* Whether the pill goes from one spot to the other through the circle. Not
|
|
12
|
+
* into or out of the page's flow, nor into or out of the modal (another
|
|
13
|
+
* element): those change at once, as before. Pure, so it can be tested.
|
|
14
|
+
*/
|
|
15
|
+
function travelsThroughCircle(from, to) {
|
|
16
|
+
const fixed = (placement) => placement !== "inline" && placement !== "overlay";
|
|
17
|
+
return !sameSpot(from, to) && fixed(from.placement) && fixed(to.placement);
|
|
18
|
+
}
|
|
19
|
+
/** The spot to show, and the level to show it at. */
|
|
20
|
+
function useTravel(rootRef, { placement, orientation, presence, enabled }) {
|
|
21
|
+
const [shown, setShown] = useState({
|
|
22
|
+
placement,
|
|
23
|
+
orientation
|
|
24
|
+
});
|
|
25
|
+
const [phase, setPhase] = useState("still");
|
|
26
|
+
const target = {
|
|
27
|
+
placement,
|
|
28
|
+
orientation
|
|
29
|
+
};
|
|
30
|
+
const targetRef = useRef(target);
|
|
31
|
+
targetRef.current = target;
|
|
32
|
+
const focused = useRef(null);
|
|
33
|
+
const moving = !sameSpot(shown, target);
|
|
34
|
+
if (moving && phase === "still") {
|
|
35
|
+
if (enabled && travelsThroughCircle(shown, target) && !CLOSED.has(presence) && !prefersReducedMotion()) setPhase("closing");
|
|
36
|
+
else setShown(target);
|
|
37
|
+
}
|
|
38
|
+
useLayoutEffect(() => {
|
|
39
|
+
if (phase !== "closing") return;
|
|
40
|
+
const root = rootRef.current;
|
|
41
|
+
const active = document.activeElement;
|
|
42
|
+
focused.current = root && active instanceof HTMLElement && root.contains(active) ? active : null;
|
|
43
|
+
}, [rootRef, phase]);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
const root = rootRef.current;
|
|
46
|
+
if (phase === "still" || !root) return;
|
|
47
|
+
const { fade, change } = readMorphTimings(root);
|
|
48
|
+
const timer = setTimeout(() => {
|
|
49
|
+
if (phase === "closing") {
|
|
50
|
+
setShown(targetRef.current);
|
|
51
|
+
setPhase("travelling");
|
|
52
|
+
} else setPhase("still");
|
|
53
|
+
}, fade + change);
|
|
54
|
+
return () => clearTimeout(timer);
|
|
55
|
+
}, [
|
|
56
|
+
rootRef,
|
|
57
|
+
phase,
|
|
58
|
+
shown
|
|
59
|
+
]);
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (phase === "travelling" && moving) setShown(targetRef.current);
|
|
62
|
+
}, [phase, moving]);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
const element = focused.current;
|
|
65
|
+
if (phase !== "still" || !element) return;
|
|
66
|
+
focused.current = null;
|
|
67
|
+
if (document.activeElement === document.body && element.isConnected) element.focus({ preventScroll: true });
|
|
68
|
+
}, [phase]);
|
|
69
|
+
return {
|
|
70
|
+
...shown,
|
|
71
|
+
presence: phase !== "still" && !CLOSED.has(presence) ? "rest" : presence
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { travelsThroughCircle, useTravel };
|
package/dist/Pill/types.d.mts
CHANGED
|
@@ -54,6 +54,14 @@ export interface PillProps extends Omit<ComponentPropsWithoutRef<"div">, "childr
|
|
|
54
54
|
* pill out: it stays in its spot and the others ignore it. Default `true`.
|
|
55
55
|
*/
|
|
56
56
|
ladder?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Fixed placements only: when the placement (or the orientation) changes,
|
|
59
|
+
* the pill closes to a circle, the circle travels, and the pill opens again
|
|
60
|
+
* at its new place, so its content doesn't jump on the way (from the bottom
|
|
61
|
+
* to a side, it turns vertical inside the circle). `false` travels at full
|
|
62
|
+
* size instead. With reduced motion, it changes place at once. Default `true`.
|
|
63
|
+
*/
|
|
64
|
+
moveThroughCircle?: boolean;
|
|
57
65
|
/**
|
|
58
66
|
* `overlay` only: a click outside the modal does what Escape does (closes
|
|
59
67
|
* the panel if it's open, the modal otherwise). Default `false`.
|
package/dist/Pill/types.d.ts
CHANGED
|
@@ -54,6 +54,14 @@ export interface PillProps extends Omit<ComponentPropsWithoutRef<"div">, "childr
|
|
|
54
54
|
* pill out: it stays in its spot and the others ignore it. Default `true`.
|
|
55
55
|
*/
|
|
56
56
|
ladder?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Fixed placements only: when the placement (or the orientation) changes,
|
|
59
|
+
* the pill closes to a circle, the circle travels, and the pill opens again
|
|
60
|
+
* at its new place, so its content doesn't jump on the way (from the bottom
|
|
61
|
+
* to a side, it turns vertical inside the circle). `false` travels at full
|
|
62
|
+
* size instead. With reduced motion, it changes place at once. Default `true`.
|
|
63
|
+
*/
|
|
64
|
+
moveThroughCircle?: boolean;
|
|
57
65
|
/**
|
|
58
66
|
* `overlay` only: a click outside the modal does what Escape does (closes
|
|
59
67
|
* the panel if it's open, the modal otherwise). Default `false`.
|
|
@@ -3,12 +3,23 @@
|
|
|
3
3
|
let react = require("react");
|
|
4
4
|
const require_FormContext = require("./FormContext.js");
|
|
5
5
|
const require_glyphs = require("./glyphs.js");
|
|
6
|
+
const require_inputModality = require("../utils/inputModality.js");
|
|
6
7
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
7
8
|
|
|
8
9
|
// src/PillForm/PillField.tsx
|
|
10
|
+
/**
|
|
11
|
+
* Browsers draw `:focus-visible` on a text field even when it was clicked or
|
|
12
|
+
* tapped. The field remembers where its focus came from, so forms.css draws
|
|
13
|
+
* the ring for the keyboard only. Written on the element, not in a state: a
|
|
14
|
+
* focus shouldn't re-render the field.
|
|
15
|
+
*/
|
|
16
|
+
function noteFocusOrigin(event) {
|
|
17
|
+
event.currentTarget.toggleAttribute("data-pointer-focus", require_inputModality.getInputModality() === "pointer");
|
|
18
|
+
}
|
|
9
19
|
/** The shell, with the options only our own fields use. */
|
|
10
|
-
const FieldShell = (0, react.forwardRef)(function FieldShell({ label, hideLabel, hint, error, optional, layout, aside, group, isText, children, ...rest }, ref) {
|
|
20
|
+
const FieldShell = (0, react.forwardRef)(function FieldShell({ label, hideLabel, hint, error, optional, layout, aside, group, isText, children, onFocus, ...rest }, ref) {
|
|
11
21
|
const form = (0, react.useContext)(require_FormContext.FormContext);
|
|
22
|
+
(0, react.useEffect)(require_inputModality.trackInputModality, []);
|
|
12
23
|
const id = (0, react.useId)();
|
|
13
24
|
const hintId = `${id}hint`;
|
|
14
25
|
const errorId = `${id}error`;
|
|
@@ -27,6 +38,10 @@ const FieldShell = (0, react.forwardRef)(function FieldShell({ label, hideLabel,
|
|
|
27
38
|
"aria-invalid": group === "radiogroup" ? control["aria-invalid"] : void 0
|
|
28
39
|
},
|
|
29
40
|
ref,
|
|
41
|
+
onFocus: (event) => {
|
|
42
|
+
noteFocusOrigin(event);
|
|
43
|
+
onFocus?.(event);
|
|
44
|
+
},
|
|
30
45
|
"data-mp": "field",
|
|
31
46
|
"data-layout": layout ?? (isText && form.layout || "stacked"),
|
|
32
47
|
"data-invalid": error ? "" : void 0,
|
|
@@ -1,14 +1,25 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { forwardRef, useContext, useId } from "react";
|
|
3
|
+
import { forwardRef, useContext, useEffect, useId } from "react";
|
|
4
4
|
import { FormContext } from "./FormContext.mjs";
|
|
5
5
|
import { AlertGlyph } from "./glyphs.mjs";
|
|
6
|
+
import { getInputModality, trackInputModality } from "../utils/inputModality.mjs";
|
|
6
7
|
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
7
8
|
|
|
8
9
|
// src/PillForm/PillField.tsx
|
|
10
|
+
/**
|
|
11
|
+
* Browsers draw `:focus-visible` on a text field even when it was clicked or
|
|
12
|
+
* tapped. The field remembers where its focus came from, so forms.css draws
|
|
13
|
+
* the ring for the keyboard only. Written on the element, not in a state: a
|
|
14
|
+
* focus shouldn't re-render the field.
|
|
15
|
+
*/
|
|
16
|
+
function noteFocusOrigin(event) {
|
|
17
|
+
event.currentTarget.toggleAttribute("data-pointer-focus", getInputModality() === "pointer");
|
|
18
|
+
}
|
|
9
19
|
/** The shell, with the options only our own fields use. */
|
|
10
|
-
const FieldShell = forwardRef(function FieldShell({ label, hideLabel, hint, error, optional, layout, aside, group, isText, children, ...rest }, ref) {
|
|
20
|
+
const FieldShell = forwardRef(function FieldShell({ label, hideLabel, hint, error, optional, layout, aside, group, isText, children, onFocus, ...rest }, ref) {
|
|
11
21
|
const form = useContext(FormContext);
|
|
22
|
+
useEffect(trackInputModality, []);
|
|
12
23
|
const id = useId();
|
|
13
24
|
const hintId = `${id}hint`;
|
|
14
25
|
const errorId = `${id}error`;
|
|
@@ -27,6 +38,10 @@ const FieldShell = forwardRef(function FieldShell({ label, hideLabel, hint, erro
|
|
|
27
38
|
"aria-invalid": group === "radiogroup" ? control["aria-invalid"] : void 0
|
|
28
39
|
},
|
|
29
40
|
ref,
|
|
41
|
+
onFocus: (event) => {
|
|
42
|
+
noteFocusOrigin(event);
|
|
43
|
+
onFocus?.(event);
|
|
44
|
+
},
|
|
30
45
|
"data-mp": "field",
|
|
31
46
|
"data-layout": layout ?? (isText && form.layout || "stacked"),
|
|
32
47
|
"data-invalid": error ? "" : void 0,
|
|
@@ -53,7 +53,8 @@ const PillNavbar = (0, react.forwardRef)(function PillNavbar({ items, pathname,
|
|
|
53
53
|
label,
|
|
54
54
|
href: item.href,
|
|
55
55
|
linkComponent,
|
|
56
|
-
"aria-current": item === active ? "page" : void 0
|
|
56
|
+
"aria-current": item === active ? "page" : void 0,
|
|
57
|
+
"data-href": item.href
|
|
57
58
|
}, item.href);
|
|
58
59
|
};
|
|
59
60
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("nav", {
|
|
@@ -53,7 +53,8 @@ const PillNavbar = forwardRef(function PillNavbar({ items, pathname, linkCompone
|
|
|
53
53
|
label,
|
|
54
54
|
href: item.href,
|
|
55
55
|
linkComponent,
|
|
56
|
-
"aria-current": item === active ? "page" : void 0
|
|
56
|
+
"aria-current": item === active ? "page" : void 0,
|
|
57
|
+
"data-href": item.href
|
|
57
58
|
}, item.href);
|
|
58
59
|
};
|
|
59
60
|
return /* @__PURE__ */ jsx("nav", {
|
|
@@ -37,6 +37,15 @@ export interface NotificationPanel {
|
|
|
37
37
|
/** Always show the close button's label, not only on hover and focus. */
|
|
38
38
|
closeExpanded?: boolean;
|
|
39
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* How a notification's parts are arranged: in one row (`pill`, the default),
|
|
42
|
+
* with the actions on a second row under the message when they need the
|
|
43
|
+
* room (`rows`: a wrapping message, or an action and a panel's trigger), or
|
|
44
|
+
* in a column, as a sidebar is (`card`), or in a flag out of a sidebar's
|
|
45
|
+
* edge (`flag`, which the template lays out in one row). Card and flag are
|
|
46
|
+
* a sidebar host's.
|
|
47
|
+
*/
|
|
48
|
+
export type NotificationLayout = "pill" | "rows" | "card" | "flag";
|
|
40
49
|
export interface PillNotificationClassNames {
|
|
41
50
|
icon?: string;
|
|
42
51
|
divider?: string;
|
|
@@ -51,11 +60,17 @@ export interface PillNotificationProps {
|
|
|
51
60
|
message: ReactNode;
|
|
52
61
|
action?: NotificationAction;
|
|
53
62
|
panel?: NotificationPanel;
|
|
63
|
+
/**
|
|
64
|
+
* `rows` puts the actions under the message when they need the room;
|
|
65
|
+
* `card` stacks everything in a column, actions full width. Default `pill`.
|
|
66
|
+
*/
|
|
67
|
+
layout?: NotificationLayout;
|
|
54
68
|
linkComponent?: LinkComponent;
|
|
55
69
|
classNames?: PillNotificationClassNames;
|
|
56
70
|
}
|
|
57
71
|
/**
|
|
58
|
-
* Icon · divider · message · action, with an optional expandable panel.
|
|
72
|
+
* Icon · divider · message · action, with an optional expandable panel. With
|
|
73
|
+
* `layout="rows"`, the actions go under the message when they need the room.
|
|
59
74
|
* Render it inside a <Pill>, or let <MagicPill> render it for pill().notify().
|
|
60
75
|
*/
|
|
61
76
|
export declare const PillNotification: (props: PillNotificationProps) => ReactNode;
|
|
@@ -37,6 +37,15 @@ export interface NotificationPanel {
|
|
|
37
37
|
/** Always show the close button's label, not only on hover and focus. */
|
|
38
38
|
closeExpanded?: boolean;
|
|
39
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* How a notification's parts are arranged: in one row (`pill`, the default),
|
|
42
|
+
* with the actions on a second row under the message when they need the
|
|
43
|
+
* room (`rows`: a wrapping message, or an action and a panel's trigger), or
|
|
44
|
+
* in a column, as a sidebar is (`card`), or in a flag out of a sidebar's
|
|
45
|
+
* edge (`flag`, which the template lays out in one row). Card and flag are
|
|
46
|
+
* a sidebar host's.
|
|
47
|
+
*/
|
|
48
|
+
export type NotificationLayout = "pill" | "rows" | "card" | "flag";
|
|
40
49
|
export interface PillNotificationClassNames {
|
|
41
50
|
icon?: string;
|
|
42
51
|
divider?: string;
|
|
@@ -51,11 +60,17 @@ export interface PillNotificationProps {
|
|
|
51
60
|
message: ReactNode;
|
|
52
61
|
action?: NotificationAction;
|
|
53
62
|
panel?: NotificationPanel;
|
|
63
|
+
/**
|
|
64
|
+
* `rows` puts the actions under the message when they need the room;
|
|
65
|
+
* `card` stacks everything in a column, actions full width. Default `pill`.
|
|
66
|
+
*/
|
|
67
|
+
layout?: NotificationLayout;
|
|
54
68
|
linkComponent?: LinkComponent;
|
|
55
69
|
classNames?: PillNotificationClassNames;
|
|
56
70
|
}
|
|
57
71
|
/**
|
|
58
|
-
* Icon · divider · message · action, with an optional expandable panel.
|
|
72
|
+
* Icon · divider · message · action, with an optional expandable panel. With
|
|
73
|
+
* `layout="rows"`, the actions go under the message when they need the room.
|
|
59
74
|
* Render it inside a <Pill>, or let <MagicPill> render it for pill().notify().
|
|
60
75
|
*/
|
|
61
76
|
export declare const PillNotification: (props: PillNotificationProps) => ReactNode;
|