magic-pill 9.1.1 → 9.3.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 +32 -0
- package/MIGRATING.md +2 -0
- package/README.md +55 -17
- package/dist/MagicPill/MagicPill.d.mts +9 -1
- package/dist/MagicPill/MagicPill.d.ts +9 -1
- package/dist/MagicPill/MagicPill.js +208 -107
- package/dist/MagicPill/MagicPill.mjs +211 -110
- package/dist/MagicPill/flag.js +93 -0
- package/dist/MagicPill/flag.mjs +91 -0
- package/dist/MagicPill/useChoreography.js +66 -0
- package/dist/MagicPill/useChoreography.mjs +66 -0
- package/dist/Pill/Pill.js +20 -9
- package/dist/Pill/Pill.mjs +21 -10
- package/dist/Pill/modal.js +2 -1
- package/dist/Pill/modal.mjs +3 -2
- package/dist/Pill/morph.js +178 -26
- package/dist/Pill/morph.mjs +178 -28
- package/dist/Pill/travel.js +77 -0
- package/dist/Pill/travel.mjs +76 -0
- package/dist/Pill/types.d.mts +8 -0
- package/dist/Pill/types.d.ts +8 -0
- package/dist/Pill/zone.js +3 -2
- package/dist/Pill/zone.mjs +4 -3
- package/dist/PillForm/PillField.js +16 -1
- package/dist/PillForm/PillField.mjs +17 -2
- package/dist/PillForm/PillSubmit.js +1 -1
- package/dist/PillForm/PillSubmit.mjs +3 -3
- package/dist/PillForm/popover.js +2 -1
- package/dist/PillForm/popover.mjs +3 -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/PillOptics/PillOptics.js +2 -1
- package/dist/PillOptics/PillOptics.mjs +3 -2
- package/dist/forms.css +40 -27
- package/dist/index.d.mts +29 -15
- package/dist/index.d.ts +29 -15
- package/dist/index.js +38 -12
- package/dist/index.mjs +38 -12
- package/dist/store/store.d.mts +9 -1
- package/dist/store/store.d.ts +9 -1
- package/dist/styles.css +91 -3
- package/dist/utils/react.js +8 -1
- package/dist/utils/react.mjs +8 -2
- package/package.json +2 -2
- package/dist/icons/ArrowIcon.d.mts +0 -4
- package/dist/icons/ArrowIcon.d.ts +0 -4
- package/dist/icons/BoxIcon.d.mts +0 -4
- package/dist/icons/BoxIcon.d.ts +0 -4
- package/dist/icons/CheckIcon.d.mts +0 -4
- package/dist/icons/CheckIcon.d.ts +0 -4
- package/dist/icons/CloseIcon.d.mts +0 -4
- package/dist/icons/CloseIcon.d.ts +0 -4
- package/dist/icons/CouponIcon.d.mts +0 -4
- package/dist/icons/CouponIcon.d.ts +0 -4
- package/dist/icons/DocumentIcon.d.mts +0 -4
- package/dist/icons/DocumentIcon.d.ts +0 -4
- package/dist/icons/HelpIcon.d.mts +0 -4
- package/dist/icons/HelpIcon.d.ts +0 -4
- package/dist/icons/InfoIcon.d.mts +0 -4
- package/dist/icons/InfoIcon.d.ts +0 -4
- package/dist/icons/MailIcon.d.mts +0 -4
- package/dist/icons/MailIcon.d.ts +0 -4
- package/dist/icons/PhoneIcon.d.mts +0 -4
- package/dist/icons/PhoneIcon.d.ts +0 -4
- package/dist/icons/PlusIcon.d.mts +0 -4
- package/dist/icons/PlusIcon.d.ts +0 -4
- package/dist/icons/UndoIcon.d.mts +0 -4
- package/dist/icons/UndoIcon.d.ts +0 -4
package/dist/Pill/morph.mjs
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { Component,
|
|
3
|
+
import { Component, useRef } from "react";
|
|
4
|
+
import { useIsomorphicLayoutEffect } from "../utils/react.mjs";
|
|
4
5
|
import { parseDuration, prefersReducedMotion } from "../utils/timing.mjs";
|
|
5
6
|
|
|
6
7
|
// src/Pill/morph.ts
|
|
7
8
|
const CLOSED = /* @__PURE__ */ new Set(["rest", "hidden"]);
|
|
8
9
|
/** Tags this module's animations, so a size change can stop the one before it. */
|
|
9
10
|
const MORPH = "mp-morph";
|
|
11
|
+
/** Tags the placement animation, which runs alongside the size ones. */
|
|
12
|
+
const MOVE = "mp-move";
|
|
10
13
|
/** The length of the fading edge over the panel's clipped content. */
|
|
11
14
|
const EDGE = 48;
|
|
12
15
|
const EASING = "ease-in-out";
|
|
@@ -38,7 +41,7 @@ const held = (width) => ({
|
|
|
38
41
|
* own animation starts), to hold them there during an animation.
|
|
39
42
|
*/
|
|
40
43
|
function holdMessages(element) {
|
|
41
|
-
const messages = [...element.querySelectorAll(":scope > [data-mp=\"message\"]")].map((message) => [message, held(message.getBoundingClientRect().width)]);
|
|
44
|
+
const messages = [...element.querySelectorAll(":scope > :is([data-mp=\"message\"], [data-mp=\"body\"], [data-mp=\"card\"])")].map((message) => [message, held(message.getBoundingClientRect().width)]);
|
|
42
45
|
return (options) => messages.map(([message, frame]) => message.animate([frame, frame], options));
|
|
43
46
|
}
|
|
44
47
|
const sameSize = (a, b) => Math.abs(a.width - b.width) < 1 && Math.abs(a.height - b.height) < 1;
|
|
@@ -103,6 +106,78 @@ function planMorph(from, to, previous, next, timings, panel) {
|
|
|
103
106
|
}
|
|
104
107
|
return plan;
|
|
105
108
|
}
|
|
109
|
+
/** Tags the exit played by script (see `planExit`). */
|
|
110
|
+
const EXIT = "mp-exit";
|
|
111
|
+
/**
|
|
112
|
+
* The pill leaving, as the stylesheet times it: its content fades out, it
|
|
113
|
+
* shrinks to the circle (the size morph), scales out, then stops being
|
|
114
|
+
* visible. The stylesheet does it with delayed transitions, which a restyle
|
|
115
|
+
* of the whole page can cut short: on iOS, the toolbar folding away as the
|
|
116
|
+
* page scrolls resizes the viewport, and the pill vanished at once instead.
|
|
117
|
+
* Played by script on top, the exit keeps its course. Pure, so it can be tested.
|
|
118
|
+
*/
|
|
119
|
+
function planExit(start, timings, exit) {
|
|
120
|
+
const wait = timings.fade + timings.change;
|
|
121
|
+
const total = wait + exit;
|
|
122
|
+
if (exit <= 0 || total <= 0) return null;
|
|
123
|
+
const plan = { pill: {
|
|
124
|
+
keyframes: [
|
|
125
|
+
{
|
|
126
|
+
scale: start.scale,
|
|
127
|
+
visibility: "visible"
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
scale: start.scale,
|
|
131
|
+
visibility: "visible",
|
|
132
|
+
offset: wait / total,
|
|
133
|
+
easing: "ease-in"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
scale: "0",
|
|
137
|
+
visibility: "visible"
|
|
138
|
+
}
|
|
139
|
+
],
|
|
140
|
+
options: {
|
|
141
|
+
id: EXIT,
|
|
142
|
+
duration: total
|
|
143
|
+
}
|
|
144
|
+
} };
|
|
145
|
+
if (start.content !== void 0 && timings.fade > 0) plan.content = {
|
|
146
|
+
keyframes: [{ opacity: start.content }, { opacity: "0" }],
|
|
147
|
+
options: {
|
|
148
|
+
id: EXIT,
|
|
149
|
+
duration: timings.fade,
|
|
150
|
+
easing: "ease-in"
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
return plan;
|
|
154
|
+
}
|
|
155
|
+
/** The pill's own content: what fades out as it closes (not its icon, panels or a swap's copy). */
|
|
156
|
+
const fadingContent = (element) => [...element.children].filter((child) => child instanceof HTMLElement && ![
|
|
157
|
+
"icon",
|
|
158
|
+
"panel",
|
|
159
|
+
"outgoing"
|
|
160
|
+
].includes(child.getAttribute("data-mp") ?? ""));
|
|
161
|
+
/** Plays the exit on the pill that has just been hidden. */
|
|
162
|
+
function playExit(element, timings) {
|
|
163
|
+
const style = getComputedStyle(element);
|
|
164
|
+
const scale = style.scale && style.scale !== "none" ? style.scale : "1";
|
|
165
|
+
cancel(element, EXIT, true);
|
|
166
|
+
const content = fadingContent(element);
|
|
167
|
+
const opacity = content.length > 0 ? getComputedStyle(content[0]).opacity : "0";
|
|
168
|
+
const exit = parseDuration(style.getPropertyValue("--_mp-exit"), 300);
|
|
169
|
+
const plan = planExit({
|
|
170
|
+
scale,
|
|
171
|
+
content: Number(opacity) > 0 ? opacity : void 0
|
|
172
|
+
}, timings, exit);
|
|
173
|
+
if (!plan) return [];
|
|
174
|
+
const animations = [element.animate(plan.pill.keyframes, plan.pill.options)];
|
|
175
|
+
if (plan.content) {
|
|
176
|
+
const { keyframes, options } = plan.content;
|
|
177
|
+
for (const child of content) animations.push(child.animate(keyframes, options));
|
|
178
|
+
}
|
|
179
|
+
return animations;
|
|
180
|
+
}
|
|
106
181
|
const readSize = (element) => ({
|
|
107
182
|
width: element.offsetWidth,
|
|
108
183
|
height: element.offsetHeight
|
|
@@ -202,6 +277,39 @@ function planSwap(from, to, timings, anchor, down = false) {
|
|
|
202
277
|
};
|
|
203
278
|
}
|
|
204
279
|
const CONTROLS = "input, textarea, select";
|
|
280
|
+
/** What lays a copy out the way the original is on screen, whatever the pill turns into. */
|
|
281
|
+
const FROZEN = [
|
|
282
|
+
"flex-direction",
|
|
283
|
+
"flex-wrap",
|
|
284
|
+
"align-items",
|
|
285
|
+
"justify-content",
|
|
286
|
+
"align-self",
|
|
287
|
+
"gap",
|
|
288
|
+
"direction"
|
|
289
|
+
];
|
|
290
|
+
/**
|
|
291
|
+
* The copy laid out as the original is now, element by element: its sizes
|
|
292
|
+
* and the directions its content runs in. For a pill that turns as the new
|
|
293
|
+
* content comes in (a sidebar's notification, from a column to a row): the
|
|
294
|
+
* stylesheet would lay the copy out again for the new orientation.
|
|
295
|
+
*/
|
|
296
|
+
function freezeLayout(from, to) {
|
|
297
|
+
if (!(from instanceof HTMLElement || from instanceof SVGElement) || !("style" in to)) return;
|
|
298
|
+
const computed = getComputedStyle(from);
|
|
299
|
+
const style = to.style;
|
|
300
|
+
for (const property of FROZEN) style.setProperty(property, computed.getPropertyValue(property));
|
|
301
|
+
const { width, height } = from.getBoundingClientRect();
|
|
302
|
+
if (computed.display !== "inline") {
|
|
303
|
+
style.boxSizing = "border-box";
|
|
304
|
+
style.width = px(width);
|
|
305
|
+
style.height = px(height);
|
|
306
|
+
style.flexShrink = "0";
|
|
307
|
+
}
|
|
308
|
+
[...from.children].forEach((child, index) => {
|
|
309
|
+
const copy = to.children[index];
|
|
310
|
+
if (copy) freezeLayout(child, copy);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
205
313
|
/** What was typed or chosen: a copied control starts from its default value. */
|
|
206
314
|
function copyValues(from, to) {
|
|
207
315
|
const copies = to.querySelectorAll(CONTROLS);
|
|
@@ -217,11 +325,16 @@ function copyValues(from, to) {
|
|
|
217
325
|
* laid over it, to slide out once the new content is in. Null when there's
|
|
218
326
|
* nothing to animate (unstyled, reduced motion, no Web Animations API).
|
|
219
327
|
*/
|
|
220
|
-
function captureSwap(element) {
|
|
328
|
+
function captureSwap(element, { freeze = false } = {}) {
|
|
221
329
|
if (!element || element.hasAttribute("data-unstyled") || !element.animate || prefersReducedMotion()) return null;
|
|
222
330
|
const from = readExactSize(element);
|
|
223
331
|
const outgoing = document.createElement("div");
|
|
224
|
-
for (const child of element.children)
|
|
332
|
+
for (const child of element.children) {
|
|
333
|
+
if (child.getAttribute("data-mp") === "outgoing") continue;
|
|
334
|
+
const copy = child.cloneNode(true);
|
|
335
|
+
if (freeze) freezeLayout(child, copy);
|
|
336
|
+
outgoing.append(copy);
|
|
337
|
+
}
|
|
225
338
|
stopMorphs(element);
|
|
226
339
|
copyValues(element, outgoing);
|
|
227
340
|
for (const node of outgoing.querySelectorAll("[id], [name]")) {
|
|
@@ -248,6 +361,7 @@ function captureSwap(element) {
|
|
|
248
361
|
/** Once the new content is in: the swap prepared by `captureSwap`, played. */
|
|
249
362
|
function playSwap(element, { from, outgoing, anchor, down }) {
|
|
250
363
|
stopMorphs(element, outgoing);
|
|
364
|
+
cancel(element, MOVE);
|
|
251
365
|
const plan = planSwap(from, readExactSize(element), readMorphTimings(element), anchor, down);
|
|
252
366
|
const done = () => outgoing.remove();
|
|
253
367
|
if (!plan) return done();
|
|
@@ -290,7 +404,7 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
|
|
|
290
404
|
const lastPresence = useRef(presence);
|
|
291
405
|
const running = useRef([]);
|
|
292
406
|
const openPanel = useRef(void 0);
|
|
293
|
-
|
|
407
|
+
useIsomorphicLayoutEffect(() => {
|
|
294
408
|
const element = rootRef.current;
|
|
295
409
|
if (!element || typeof ResizeObserver === "undefined") return;
|
|
296
410
|
const observer = new ResizeObserver((entries) => {
|
|
@@ -309,7 +423,7 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
|
|
|
309
423
|
running.current.forEach((animation) => animation.cancel());
|
|
310
424
|
};
|
|
311
425
|
}, [rootRef, lastSize]);
|
|
312
|
-
|
|
426
|
+
useIsomorphicLayoutEffect(() => {
|
|
313
427
|
const element = rootRef.current;
|
|
314
428
|
const previous = lastPresence.current;
|
|
315
429
|
lastPresence.current = presence;
|
|
@@ -329,27 +443,32 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
|
|
|
329
443
|
const panel = readPanel(element);
|
|
330
444
|
if (presence === "panel") openPanel.current = panel?.box;
|
|
331
445
|
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,
|
|
446
|
+
const timings = readMorphTimings(element);
|
|
447
|
+
const plan = planMorph(from, to, previous, presence, timings, panelBox);
|
|
448
|
+
const animations = [];
|
|
449
|
+
if (plan) {
|
|
450
|
+
const hold = holdMessages(element);
|
|
451
|
+
const options = {
|
|
452
|
+
...plan.options,
|
|
350
453
|
id: MORPH
|
|
351
|
-
}
|
|
454
|
+
};
|
|
455
|
+
animations.push(element.animate(plan.pill, options));
|
|
456
|
+
if (plan.panel && panel) {
|
|
457
|
+
animations.push(panel.element.animate(plan.panel, options));
|
|
458
|
+
const footer = panel.element.querySelector("[data-mp=\"panel-footer\"]");
|
|
459
|
+
if (footer) animations.push(footer.animate([{ position: "relative" }, { position: "relative" }], options));
|
|
460
|
+
}
|
|
461
|
+
if (!CLOSED.has(presence) && presence !== "panel") animations.push(...hold(options));
|
|
462
|
+
if (plan.content) {
|
|
463
|
+
const { keyframes, options } = plan.content;
|
|
464
|
+
for (const child of element.children) if (child !== panel?.element) animations.push(child.animate(keyframes, {
|
|
465
|
+
...options,
|
|
466
|
+
id: MORPH
|
|
467
|
+
}));
|
|
468
|
+
}
|
|
352
469
|
}
|
|
470
|
+
if (presence === "hidden") animations.push(...playExit(element, timings));
|
|
471
|
+
if (animations.length === 0) return;
|
|
353
472
|
running.current = animations;
|
|
354
473
|
animations[0].finished.then(() => {
|
|
355
474
|
if (running.current === animations) running.current = [];
|
|
@@ -361,8 +480,39 @@ function useSizeMorph(rootRef, presence, enabled, lastSize) {
|
|
|
361
480
|
lastSize
|
|
362
481
|
]);
|
|
363
482
|
}
|
|
364
|
-
/**
|
|
365
|
-
|
|
483
|
+
/**
|
|
484
|
+
* When the pill is asked back before its exit is over: the exit played by
|
|
485
|
+
* script stops, and the pill grows back from the scale it had got to, as
|
|
486
|
+
* the stylesheet's entry does. The stylesheet can't do it here: browsers
|
|
487
|
+
* don't turn a transition back once a script has animated the same property,
|
|
488
|
+
* so it would carry on scaling out first. Its leftover transition is dropped.
|
|
489
|
+
*/
|
|
490
|
+
var ExitWatch = class extends Component {
|
|
491
|
+
getSnapshotBeforeUpdate(previous) {
|
|
492
|
+
const element = this.props.target.current;
|
|
493
|
+
if (!element || previous.presence !== "hidden" || this.props.presence === "hidden") return null;
|
|
494
|
+
if ((element.getAnimations?.() ?? []).filter(({ id }) => id === EXIT).length === 0) return null;
|
|
495
|
+
const { scale } = getComputedStyle(element);
|
|
496
|
+
cancel(element, EXIT, true);
|
|
497
|
+
return scale && scale !== "none" ? scale : "1";
|
|
498
|
+
}
|
|
499
|
+
componentDidUpdate(_props, _state, scale) {
|
|
500
|
+
const element = this.props.target.current;
|
|
501
|
+
if (scale === null || !element) return;
|
|
502
|
+
for (const animation of element.getAnimations()) if (animation.transitionProperty === "scale") animation.cancel();
|
|
503
|
+
if (Number(scale) >= 1) return;
|
|
504
|
+
const style = getComputedStyle(element);
|
|
505
|
+
const enter = parseDuration(style.getPropertyValue("--_mp-enter"), 500);
|
|
506
|
+
element.animate([{ scale }, { scale: "1" }], {
|
|
507
|
+
id: EXIT,
|
|
508
|
+
duration: enter * Math.min(1, 1 - Number(scale)),
|
|
509
|
+
easing: style.getPropertyValue("--_mp-ease-enter").trim() || "ease-out"
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
render() {
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
};
|
|
366
516
|
/**
|
|
367
517
|
* The pill moved to another placement (or orientation): it travels there
|
|
368
518
|
* from where it was (`from`), resizing on the way if its size changed.
|
|
@@ -410,4 +560,4 @@ var PlacementWatch = class extends Component {
|
|
|
410
560
|
}
|
|
411
561
|
};
|
|
412
562
|
|
|
413
|
-
export { EASING, MORPH, PlacementWatch, animateMove, cancel, captureSwap, planMorph, planSwap, playSwap, readMorphTimings, useSizeMorph };
|
|
563
|
+
export { EASING, ExitWatch, MORPH, PlacementWatch, animateMove, cancel, captureSwap, planExit, planMorph, planSwap, playSwap, readMorphTimings, useSizeMorph };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
const require_react = require("../utils/react.js");
|
|
5
|
+
const require_morph = require("./morph.js");
|
|
6
|
+
const require_timing = require("../utils/timing.js");
|
|
7
|
+
|
|
8
|
+
// src/Pill/travel.ts
|
|
9
|
+
const CLOSED = /* @__PURE__ */ new Set(["rest", "hidden"]);
|
|
10
|
+
const sameSpot = (a, b) => a.placement === b.placement && a.orientation === b.orientation;
|
|
11
|
+
/**
|
|
12
|
+
* Whether the pill goes from one spot to the other through the circle. Not
|
|
13
|
+
* into or out of the page's flow, nor into or out of the modal (another
|
|
14
|
+
* element): those change at once, as before. Pure, so it can be tested.
|
|
15
|
+
*/
|
|
16
|
+
function travelsThroughCircle(from, to) {
|
|
17
|
+
const fixed = (placement) => placement !== "inline" && placement !== "overlay";
|
|
18
|
+
return !sameSpot(from, to) && fixed(from.placement) && fixed(to.placement);
|
|
19
|
+
}
|
|
20
|
+
/** The spot to show, and the level to show it at. */
|
|
21
|
+
function useTravel(rootRef, { placement, orientation, presence, enabled }) {
|
|
22
|
+
const [shown, setShown] = (0, react.useState)({
|
|
23
|
+
placement,
|
|
24
|
+
orientation
|
|
25
|
+
});
|
|
26
|
+
const [phase, setPhase] = (0, react.useState)("still");
|
|
27
|
+
const target = {
|
|
28
|
+
placement,
|
|
29
|
+
orientation
|
|
30
|
+
};
|
|
31
|
+
const targetRef = (0, react.useRef)(target);
|
|
32
|
+
targetRef.current = target;
|
|
33
|
+
const focused = (0, react.useRef)(null);
|
|
34
|
+
const moving = !sameSpot(shown, target);
|
|
35
|
+
if (moving && phase === "still") {
|
|
36
|
+
if (enabled && travelsThroughCircle(shown, target) && !CLOSED.has(presence) && !require_timing.prefersReducedMotion()) setPhase("closing");
|
|
37
|
+
else setShown(target);
|
|
38
|
+
}
|
|
39
|
+
require_react.useIsomorphicLayoutEffect(() => {
|
|
40
|
+
if (phase !== "closing") return;
|
|
41
|
+
const root = rootRef.current;
|
|
42
|
+
const active = document.activeElement;
|
|
43
|
+
focused.current = root && active instanceof HTMLElement && root.contains(active) ? active : null;
|
|
44
|
+
}, [rootRef, phase]);
|
|
45
|
+
(0, react.useEffect)(() => {
|
|
46
|
+
const root = rootRef.current;
|
|
47
|
+
if (phase === "still" || !root) return;
|
|
48
|
+
const { fade, change } = require_morph.readMorphTimings(root);
|
|
49
|
+
const timer = setTimeout(() => {
|
|
50
|
+
if (phase === "closing") {
|
|
51
|
+
setShown(targetRef.current);
|
|
52
|
+
setPhase("travelling");
|
|
53
|
+
} else setPhase("still");
|
|
54
|
+
}, fade + change);
|
|
55
|
+
return () => clearTimeout(timer);
|
|
56
|
+
}, [
|
|
57
|
+
rootRef,
|
|
58
|
+
phase,
|
|
59
|
+
shown
|
|
60
|
+
]);
|
|
61
|
+
(0, react.useEffect)(() => {
|
|
62
|
+
if (phase === "travelling" && moving) setShown(targetRef.current);
|
|
63
|
+
}, [phase, moving]);
|
|
64
|
+
(0, react.useEffect)(() => {
|
|
65
|
+
const element = focused.current;
|
|
66
|
+
if (phase !== "still" || !element) return;
|
|
67
|
+
focused.current = null;
|
|
68
|
+
if (document.activeElement === document.body && element.isConnected) element.focus({ preventScroll: true });
|
|
69
|
+
}, [phase]);
|
|
70
|
+
return {
|
|
71
|
+
...shown,
|
|
72
|
+
presence: phase !== "still" && !CLOSED.has(presence) ? "rest" : presence
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
exports.travelsThroughCircle = travelsThroughCircle;
|
|
77
|
+
exports.useTravel = useTravel;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef, useState } from "react";
|
|
4
|
+
import { useIsomorphicLayoutEffect } from "../utils/react.mjs";
|
|
5
|
+
import { readMorphTimings } from "./morph.mjs";
|
|
6
|
+
import { prefersReducedMotion } from "../utils/timing.mjs";
|
|
7
|
+
|
|
8
|
+
// src/Pill/travel.ts
|
|
9
|
+
const CLOSED = /* @__PURE__ */ new Set(["rest", "hidden"]);
|
|
10
|
+
const sameSpot = (a, b) => a.placement === b.placement && a.orientation === b.orientation;
|
|
11
|
+
/**
|
|
12
|
+
* Whether the pill goes from one spot to the other through the circle. Not
|
|
13
|
+
* into or out of the page's flow, nor into or out of the modal (another
|
|
14
|
+
* element): those change at once, as before. Pure, so it can be tested.
|
|
15
|
+
*/
|
|
16
|
+
function travelsThroughCircle(from, to) {
|
|
17
|
+
const fixed = (placement) => placement !== "inline" && placement !== "overlay";
|
|
18
|
+
return !sameSpot(from, to) && fixed(from.placement) && fixed(to.placement);
|
|
19
|
+
}
|
|
20
|
+
/** The spot to show, and the level to show it at. */
|
|
21
|
+
function useTravel(rootRef, { placement, orientation, presence, enabled }) {
|
|
22
|
+
const [shown, setShown] = useState({
|
|
23
|
+
placement,
|
|
24
|
+
orientation
|
|
25
|
+
});
|
|
26
|
+
const [phase, setPhase] = useState("still");
|
|
27
|
+
const target = {
|
|
28
|
+
placement,
|
|
29
|
+
orientation
|
|
30
|
+
};
|
|
31
|
+
const targetRef = useRef(target);
|
|
32
|
+
targetRef.current = target;
|
|
33
|
+
const focused = useRef(null);
|
|
34
|
+
const moving = !sameSpot(shown, target);
|
|
35
|
+
if (moving && phase === "still") {
|
|
36
|
+
if (enabled && travelsThroughCircle(shown, target) && !CLOSED.has(presence) && !prefersReducedMotion()) setPhase("closing");
|
|
37
|
+
else setShown(target);
|
|
38
|
+
}
|
|
39
|
+
useIsomorphicLayoutEffect(() => {
|
|
40
|
+
if (phase !== "closing") return;
|
|
41
|
+
const root = rootRef.current;
|
|
42
|
+
const active = document.activeElement;
|
|
43
|
+
focused.current = root && active instanceof HTMLElement && root.contains(active) ? active : null;
|
|
44
|
+
}, [rootRef, phase]);
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
const root = rootRef.current;
|
|
47
|
+
if (phase === "still" || !root) return;
|
|
48
|
+
const { fade, change } = readMorphTimings(root);
|
|
49
|
+
const timer = setTimeout(() => {
|
|
50
|
+
if (phase === "closing") {
|
|
51
|
+
setShown(targetRef.current);
|
|
52
|
+
setPhase("travelling");
|
|
53
|
+
} else setPhase("still");
|
|
54
|
+
}, fade + change);
|
|
55
|
+
return () => clearTimeout(timer);
|
|
56
|
+
}, [
|
|
57
|
+
rootRef,
|
|
58
|
+
phase,
|
|
59
|
+
shown
|
|
60
|
+
]);
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (phase === "travelling" && moving) setShown(targetRef.current);
|
|
63
|
+
}, [phase, moving]);
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
const element = focused.current;
|
|
66
|
+
if (phase !== "still" || !element) return;
|
|
67
|
+
focused.current = null;
|
|
68
|
+
if (document.activeElement === document.body && element.isConnected) element.focus({ preventScroll: true });
|
|
69
|
+
}, [phase]);
|
|
70
|
+
return {
|
|
71
|
+
...shown,
|
|
72
|
+
presence: phase !== "still" && !CLOSED.has(presence) ? "rest" : presence
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
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`.
|
package/dist/Pill/zone.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
let react = require("react");
|
|
4
|
+
const require_react = require("../utils/react.js");
|
|
4
5
|
const require_morph = require("./morph.js");
|
|
5
6
|
const require_timing = require("../utils/timing.js");
|
|
6
7
|
|
|
@@ -97,7 +98,7 @@ function useZone(rootRef, zone, presence) {
|
|
|
97
98
|
size: 0,
|
|
98
99
|
frozen: presence === "panel"
|
|
99
100
|
});
|
|
100
|
-
|
|
101
|
+
require_react.useIsomorphicLayoutEffect(() => {
|
|
101
102
|
const rung = rungRef.current;
|
|
102
103
|
const element = rootRef.current;
|
|
103
104
|
if (!zone || !element) return;
|
|
@@ -124,7 +125,7 @@ function useZone(rootRef, zone, presence) {
|
|
|
124
125
|
arrange(zone, () => travel(element));
|
|
125
126
|
};
|
|
126
127
|
}, [rootRef, zone]);
|
|
127
|
-
|
|
128
|
+
require_react.useIsomorphicLayoutEffect(() => {
|
|
128
129
|
const rung = rungRef.current;
|
|
129
130
|
const previous = rung.presence;
|
|
130
131
|
const { element } = rung;
|
package/dist/Pill/zone.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { useRef } from "react";
|
|
4
|
+
import { useIsomorphicLayoutEffect } from "../utils/react.mjs";
|
|
4
5
|
import { EASING, MORPH, cancel, readMorphTimings } from "./morph.mjs";
|
|
5
6
|
import { parseDuration, prefersReducedMotion } from "../utils/timing.mjs";
|
|
6
7
|
|
|
@@ -97,7 +98,7 @@ function useZone(rootRef, zone, presence) {
|
|
|
97
98
|
size: 0,
|
|
98
99
|
frozen: presence === "panel"
|
|
99
100
|
});
|
|
100
|
-
|
|
101
|
+
useIsomorphicLayoutEffect(() => {
|
|
101
102
|
const rung = rungRef.current;
|
|
102
103
|
const element = rootRef.current;
|
|
103
104
|
if (!zone || !element) return;
|
|
@@ -124,7 +125,7 @@ function useZone(rootRef, zone, presence) {
|
|
|
124
125
|
arrange(zone, () => travel(element));
|
|
125
126
|
};
|
|
126
127
|
}, [rootRef, zone]);
|
|
127
|
-
|
|
128
|
+
useIsomorphicLayoutEffect(() => {
|
|
128
129
|
const rung = rungRef.current;
|
|
129
130
|
const previous = rung.presence;
|
|
130
131
|
const { element } = rung;
|
|
@@ -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,
|
|
@@ -33,7 +33,7 @@ function useBusyLabel(busyLabel, interval, isBusy) {
|
|
|
33
33
|
/** One label replacing another: the action widens or narrows with it. */
|
|
34
34
|
function useLabelMorph(buttonRef, label) {
|
|
35
35
|
const previous = (0, react.useRef)(null);
|
|
36
|
-
|
|
36
|
+
require_react.useIsomorphicLayoutEffect(() => {
|
|
37
37
|
const text = buttonRef.current?.querySelector("[data-mp=\"label\"] > span");
|
|
38
38
|
if (!text) return;
|
|
39
39
|
text.getAnimations?.().forEach((animation) => animation.cancel());
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { forwardRef, useContext, useEffect,
|
|
3
|
+
import { forwardRef, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
4
4
|
import { FormContext } from "./FormContext.mjs";
|
|
5
5
|
import PillAction from "../Pill/PillAction.mjs";
|
|
6
6
|
import CheckIcon from "../icons/CheckIcon.mjs";
|
|
7
|
-
import { mergeRefs } from "../utils/react.mjs";
|
|
7
|
+
import { mergeRefs, useIsomorphicLayoutEffect } from "../utils/react.mjs";
|
|
8
8
|
import { prefersReducedMotion } from "../utils/timing.mjs";
|
|
9
9
|
import { VISUALLY_HIDDEN } from "../utils/visuallyHidden.mjs";
|
|
10
10
|
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -33,7 +33,7 @@ function useBusyLabel(busyLabel, interval, isBusy) {
|
|
|
33
33
|
/** One label replacing another: the action widens or narrows with it. */
|
|
34
34
|
function useLabelMorph(buttonRef, label) {
|
|
35
35
|
const previous = useRef(null);
|
|
36
|
-
|
|
36
|
+
useIsomorphicLayoutEffect(() => {
|
|
37
37
|
const text = buttonRef.current?.querySelector("[data-mp=\"label\"] > span");
|
|
38
38
|
if (!text) return;
|
|
39
39
|
text.getAnimations?.().forEach((animation) => animation.cancel());
|
package/dist/PillForm/popover.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
let react = require("react");
|
|
4
|
+
const require_react = require("../utils/react.js");
|
|
4
5
|
|
|
5
6
|
// src/PillForm/popover.ts
|
|
6
7
|
/** From the field, and from the screen's edges */
|
|
@@ -27,7 +28,7 @@ function placePopover(field, popover, viewport, isRtl) {
|
|
|
27
28
|
* elsewhere closes it. Without popovers, it's shown in the page, under the field.
|
|
28
29
|
*/
|
|
29
30
|
function usePopover(popoverRef, fieldRef, isOpen, onClose) {
|
|
30
|
-
|
|
31
|
+
require_react.useIsomorphicLayoutEffect(() => {
|
|
31
32
|
const popover = popoverRef.current;
|
|
32
33
|
const field = fieldRef.current;
|
|
33
34
|
if (!isOpen || !popover || !field || !supportsPopover()) return;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { useEffect
|
|
3
|
+
import { useEffect } from "react";
|
|
4
|
+
import { useIsomorphicLayoutEffect } from "../utils/react.mjs";
|
|
4
5
|
|
|
5
6
|
// src/PillForm/popover.ts
|
|
6
7
|
/** From the field, and from the screen's edges */
|
|
@@ -27,7 +28,7 @@ function placePopover(field, popover, viewport, isRtl) {
|
|
|
27
28
|
* elsewhere closes it. Without popovers, it's shown in the page, under the field.
|
|
28
29
|
*/
|
|
29
30
|
function usePopover(popoverRef, fieldRef, isOpen, onClose) {
|
|
30
|
-
|
|
31
|
+
useIsomorphicLayoutEffect(() => {
|
|
31
32
|
const popover = popoverRef.current;
|
|
32
33
|
const field = fieldRef.current;
|
|
33
34
|
if (!isOpen || !popover || !field || !supportsPopover()) return;
|