magic-pill 9.0.0 → 9.1.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 +44 -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 +1452 -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
package/dist/Pill/types.d.ts
CHANGED
|
@@ -22,6 +22,12 @@ export type PillOrientation = "horizontal" | "vertical";
|
|
|
22
22
|
* define your own: `[data-mp="pill"][data-variant="brand"] { --mp-surface: … }`.
|
|
23
23
|
*/
|
|
24
24
|
export type PillVariant = "neutral" | "success" | "danger" | (string & {});
|
|
25
|
+
/**
|
|
26
|
+
* The pill's material. `glass` and `liquid` need `magic-pill/themes.css`. Any
|
|
27
|
+
* other string is written to `data-theme` so you can define your own:
|
|
28
|
+
* `[data-mp="pill"][data-theme="paper"] { --mp-surface: … }`.
|
|
29
|
+
*/
|
|
30
|
+
export type PillTheme = "plain" | "glass" | "liquid" | (string & {});
|
|
25
31
|
/**
|
|
26
32
|
* A component that renders a link from an `href`, such as Next's `Link`.
|
|
27
33
|
* React Router's `Link` takes `to`: wrap it.
|
|
@@ -30,6 +36,8 @@ export type LinkComponent = ComponentType<AnchorHTMLAttributes<HTMLAnchorElement
|
|
|
30
36
|
href: string;
|
|
31
37
|
ref?: Ref<HTMLAnchorElement>;
|
|
32
38
|
}>;
|
|
39
|
+
/** How a pill goes from one scene to the next: through the circle, sliding, or at once. */
|
|
40
|
+
export type SceneChange = "circle" | "slide" | "none";
|
|
33
41
|
export interface PillProps extends Omit<ComponentPropsWithoutRef<"div">, "children"> {
|
|
34
42
|
children?: ReactNode;
|
|
35
43
|
/** Controlled presence. */
|
|
@@ -40,14 +48,37 @@ export interface PillProps extends Omit<ComponentPropsWithoutRef<"div">, "childr
|
|
|
40
48
|
onPresenceChange?: (presence: PillPresence) => void;
|
|
41
49
|
/** Default `inline`. */
|
|
42
50
|
placement?: PillPlacement;
|
|
51
|
+
/**
|
|
52
|
+
* Fixed placements only: pills with the same placement line up one after
|
|
53
|
+
* the other, the first one set up keeping its spot. `false` leaves this
|
|
54
|
+
* pill out: it stays in its spot and the others ignore it. Default `true`.
|
|
55
|
+
*/
|
|
56
|
+
ladder?: boolean;
|
|
43
57
|
/**
|
|
44
58
|
* `overlay` only: a click outside the modal does what Escape does (closes
|
|
45
59
|
* the panel if it's open, the modal otherwise). Default `false`.
|
|
46
60
|
*/
|
|
47
61
|
closeOnBackdropClick?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* `overlay` only. `glow` is an animated backdrop, and needs
|
|
64
|
+
* `magic-pill/themes.css`. Any other string is written to `data-backdrop` so
|
|
65
|
+
* you can define your own:
|
|
66
|
+
* `[data-mp="pill"][data-backdrop="dots"]::backdrop { … }`. Default `plain`.
|
|
67
|
+
*/
|
|
68
|
+
backdrop?: "plain" | "glow" | (string & {});
|
|
48
69
|
orientation?: PillOrientation;
|
|
49
70
|
/** Default `neutral`. */
|
|
50
71
|
variant?: PillVariant;
|
|
72
|
+
/** The variant picks the colour, the theme the material. Default `plain`. */
|
|
73
|
+
theme?: PillTheme;
|
|
74
|
+
/**
|
|
75
|
+
* A name for what the pill shows. When it changes, the pill doesn't jump to
|
|
76
|
+
* the new content: it closes on the old one and opens on the new one, as
|
|
77
|
+
* between a host's navbar and its notifications.
|
|
78
|
+
*/
|
|
79
|
+
scene?: string | number;
|
|
80
|
+
/** How: through the circle (default), sliding in the open pill, or at once. */
|
|
81
|
+
sceneChange?: SceneChange;
|
|
51
82
|
/** Keeps the behaviour and the data attributes, drops the visual styles. */
|
|
52
83
|
unstyled?: boolean;
|
|
53
84
|
}
|
|
@@ -78,14 +109,33 @@ export interface PillLinkActionProps extends PillActionBase, Omit<ComponentProps
|
|
|
78
109
|
export type PillActionProps = PillButtonActionProps | PillLinkActionProps;
|
|
79
110
|
export interface PillPanelProps extends Omit<ComponentPropsWithoutRef<"div">, "title"> {
|
|
80
111
|
title?: ReactNode;
|
|
112
|
+
/**
|
|
113
|
+
* For several panels in one pill: the trigger whose `panel` is the same
|
|
114
|
+
* opens this one. Default: the pill's only panel, with no name.
|
|
115
|
+
*/
|
|
116
|
+
name?: string;
|
|
117
|
+
/** At the end of the title's row, e.g. `<PillAction label="Default" … />`. */
|
|
118
|
+
titleAction?: ReactNode;
|
|
119
|
+
/** In the footer, before the close button, e.g. `<PillAction label="Save" … />`. */
|
|
120
|
+
footer?: ReactNode;
|
|
81
121
|
/** Default "Close". */
|
|
82
122
|
closeLabel?: string;
|
|
83
123
|
/** Default <CloseIcon />. */
|
|
84
124
|
closeIcon?: ReactNode;
|
|
125
|
+
/** Always show the close button's label, not only on hover and focus. Default `false`. */
|
|
126
|
+
closeExpanded?: boolean;
|
|
127
|
+
}
|
|
128
|
+
export interface PillPanelTriggerProps extends Omit<PillButtonActionProps, "onClick"> {
|
|
129
|
+
/** The `name` of the panel it opens, when the pill has several. */
|
|
130
|
+
panel?: string;
|
|
85
131
|
}
|
|
86
132
|
export interface PillContextValue {
|
|
87
133
|
presence: PillPresence;
|
|
88
134
|
setPresence: (presence: PillPresence) => void;
|
|
135
|
+
/** Opens the pill's panel, or the one with that name. */
|
|
136
|
+
openPanel: (name?: string) => void;
|
|
137
|
+
/** Back to the level the pill had before its panel opened. */
|
|
138
|
+
closePanel: () => void;
|
|
89
139
|
placement: PillPlacement;
|
|
90
140
|
orientation: PillOrientation;
|
|
91
141
|
variant: PillVariant;
|
|
@@ -0,0 +1,160 @@
|
|
|
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/zone.ts
|
|
8
|
+
/** How far each pill moves from its own spot. Pure, so it can be tested. */
|
|
9
|
+
function planLadder(boxes) {
|
|
10
|
+
let edge;
|
|
11
|
+
return boxes.map(({ offset, size, gap, visible }) => {
|
|
12
|
+
const shift = edge === void 0 ? 0 : Math.max(0, edge + gap - offset);
|
|
13
|
+
if (visible && size > 0) edge = Math.max(edge ?? 0, offset + shift + size);
|
|
14
|
+
return shift;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/** Tags the ladder's animations, which run alongside the size and placement ones. */
|
|
18
|
+
const SHIFT = "mp-shift";
|
|
19
|
+
/** Read by the stylesheet, which adds them to the placement's inset. */
|
|
20
|
+
const SHIFTS = {
|
|
21
|
+
x: "--_mp-shift-x",
|
|
22
|
+
y: "--_mp-shift-y"
|
|
23
|
+
};
|
|
24
|
+
/** The pills of each zone, in the order they were set up. */
|
|
25
|
+
const zones = /* @__PURE__ */ new Map();
|
|
26
|
+
/** Where a zone's pills are measured from, and along which axis. */
|
|
27
|
+
function axisOf(zone) {
|
|
28
|
+
if (zone === "left" || zone === "right") return {
|
|
29
|
+
shift: SHIFTS.x,
|
|
30
|
+
inset: zone,
|
|
31
|
+
size: "width"
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
shift: SHIFTS.y,
|
|
35
|
+
inset: zone.startsWith("top") ? "insetBlockStart" : "insetBlockEnd",
|
|
36
|
+
size: "blockSize"
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const number = (value) => Number.parseFloat(value) || 0;
|
|
40
|
+
function measure(rung, element, zone) {
|
|
41
|
+
if (!rung.frozen) rung.size = element.offsetWidth ? number(getComputedStyle(element)[axisOf(zone).size]) : 0;
|
|
42
|
+
}
|
|
43
|
+
/** Puts a zone's pills in their spots, moving there if a motion is given. */
|
|
44
|
+
function arrange(zone, planMotion) {
|
|
45
|
+
const { shift, inset } = axisOf(zone);
|
|
46
|
+
const rungs = zones.get(zone) ?? [];
|
|
47
|
+
const elements = rungs.map((rung) => rung.element);
|
|
48
|
+
if (rungs.length < 2 && !elements[0]?.style.getPropertyValue(shift)) return;
|
|
49
|
+
const motion = require_timing.prefersReducedMotion() ? void 0 : planMotion?.();
|
|
50
|
+
const before = motion ? elements.map((element) => element.getBoundingClientRect()) : [];
|
|
51
|
+
for (const element of elements) element.style.removeProperty(shift);
|
|
52
|
+
planLadder(rungs.map((rung, index) => {
|
|
53
|
+
measure(rung, elements[index], zone);
|
|
54
|
+
const style = getComputedStyle(elements[index]);
|
|
55
|
+
return {
|
|
56
|
+
offset: number(style[inset]),
|
|
57
|
+
size: rung.size,
|
|
58
|
+
gap: number(style.rowGap),
|
|
59
|
+
visible: rung.presence !== "hidden" && style.position === "fixed"
|
|
60
|
+
};
|
|
61
|
+
})).forEach((value, index) => {
|
|
62
|
+
if (value) elements[index].style.setProperty(shift, `${value}px`);
|
|
63
|
+
});
|
|
64
|
+
if (!motion) return;
|
|
65
|
+
elements.forEach((element, index) => {
|
|
66
|
+
if (!element.animate) return;
|
|
67
|
+
require_morph.cancel(element, SHIFT);
|
|
68
|
+
if (rungs[index] === motion.except || getComputedStyle(element).visibility === "hidden") return;
|
|
69
|
+
const from = before[index];
|
|
70
|
+
const to = element.getBoundingClientRect();
|
|
71
|
+
const x = from.left - to.left;
|
|
72
|
+
const y = from.top - to.top;
|
|
73
|
+
if (Math.abs(x) < .5 && Math.abs(y) < .5) return;
|
|
74
|
+
const { delay = 0, duration, easing = require_morph.EASING } = motion;
|
|
75
|
+
element.animate([{ translate: `${x}px ${y}px` }, { translate: "0px 0px" }], {
|
|
76
|
+
id: SHIFT,
|
|
77
|
+
delay,
|
|
78
|
+
duration,
|
|
79
|
+
easing,
|
|
80
|
+
fill: "backwards",
|
|
81
|
+
composite: "add"
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const travel = (element) => {
|
|
86
|
+
const { fade, change } = require_morph.readMorphTimings(element);
|
|
87
|
+
return { duration: fade + change };
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Gives the pill its place among the ones that share its placement.
|
|
91
|
+
* `zone`: its placement, or nothing for a pill that keeps to itself.
|
|
92
|
+
*/
|
|
93
|
+
function useZone(rootRef, zone, presence) {
|
|
94
|
+
const rungRef = (0, react.useRef)({
|
|
95
|
+
element: null,
|
|
96
|
+
presence,
|
|
97
|
+
size: 0,
|
|
98
|
+
frozen: presence === "panel"
|
|
99
|
+
});
|
|
100
|
+
(0, react.useLayoutEffect)(() => {
|
|
101
|
+
const rung = rungRef.current;
|
|
102
|
+
const element = rootRef.current;
|
|
103
|
+
if (!zone || !element) return;
|
|
104
|
+
const rungs = zones.get(zone) ?? [];
|
|
105
|
+
zones.set(zone, rungs);
|
|
106
|
+
rungs.push(rung);
|
|
107
|
+
rung.element = element;
|
|
108
|
+
const moved = rung.zone !== void 0 && rung.zone !== zone;
|
|
109
|
+
rung.zone = zone;
|
|
110
|
+
arrange(zone, () => ({
|
|
111
|
+
...travel(element),
|
|
112
|
+
except: moved ? void 0 : rung
|
|
113
|
+
}));
|
|
114
|
+
const observer = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(() => {
|
|
115
|
+
measure(rung, element, zone);
|
|
116
|
+
if (rungs.length > 1) arrange(zone);
|
|
117
|
+
});
|
|
118
|
+
observer?.observe(element);
|
|
119
|
+
return () => {
|
|
120
|
+
observer?.disconnect();
|
|
121
|
+
rungs.splice(rungs.indexOf(rung), 1);
|
|
122
|
+
element.style.removeProperty(SHIFTS.x);
|
|
123
|
+
element.style.removeProperty(SHIFTS.y);
|
|
124
|
+
arrange(zone, () => travel(element));
|
|
125
|
+
};
|
|
126
|
+
}, [rootRef, zone]);
|
|
127
|
+
(0, react.useLayoutEffect)(() => {
|
|
128
|
+
const rung = rungRef.current;
|
|
129
|
+
const previous = rung.presence;
|
|
130
|
+
const { element } = rung;
|
|
131
|
+
rung.presence = presence;
|
|
132
|
+
if (presence === "panel") rung.frozen = true;
|
|
133
|
+
if (previous === presence || !element || !rung.zone) return;
|
|
134
|
+
if (previous === "panel") {
|
|
135
|
+
const closing = (element.getAnimations?.() ?? []).filter(({ id }) => id === require_morph.MORPH);
|
|
136
|
+
Promise.allSettled(closing.map(({ finished }) => finished)).then(() => {
|
|
137
|
+
if (rung.presence === "panel") return;
|
|
138
|
+
rung.frozen = false;
|
|
139
|
+
if (rung.zone) arrange(rung.zone, () => travel(element));
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if (presence === "hidden") arrange(rung.zone, () => {
|
|
143
|
+
const { fade, change } = require_morph.readMorphTimings(element);
|
|
144
|
+
const exit = require_timing.parseDuration(getComputedStyle(element).getPropertyValue("--_mp-exit"), 300);
|
|
145
|
+
return {
|
|
146
|
+
delay: fade + change + exit / 3,
|
|
147
|
+
duration: fade + change,
|
|
148
|
+
except: rung
|
|
149
|
+
};
|
|
150
|
+
});
|
|
151
|
+
else if (previous === "hidden") arrange(rung.zone, () => ({
|
|
152
|
+
duration: require_morph.readMorphTimings(element).change,
|
|
153
|
+
easing: "ease-out",
|
|
154
|
+
except: rung
|
|
155
|
+
}));
|
|
156
|
+
}, [presence]);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
exports.planLadder = planLadder;
|
|
160
|
+
exports.useZone = useZone;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useLayoutEffect, useRef } from "react";
|
|
4
|
+
import { EASING, MORPH, cancel, readMorphTimings } from "./morph.mjs";
|
|
5
|
+
import { parseDuration, prefersReducedMotion } from "../utils/timing.mjs";
|
|
6
|
+
|
|
7
|
+
// src/Pill/zone.ts
|
|
8
|
+
/** How far each pill moves from its own spot. Pure, so it can be tested. */
|
|
9
|
+
function planLadder(boxes) {
|
|
10
|
+
let edge;
|
|
11
|
+
return boxes.map(({ offset, size, gap, visible }) => {
|
|
12
|
+
const shift = edge === void 0 ? 0 : Math.max(0, edge + gap - offset);
|
|
13
|
+
if (visible && size > 0) edge = Math.max(edge ?? 0, offset + shift + size);
|
|
14
|
+
return shift;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/** Tags the ladder's animations, which run alongside the size and placement ones. */
|
|
18
|
+
const SHIFT = "mp-shift";
|
|
19
|
+
/** Read by the stylesheet, which adds them to the placement's inset. */
|
|
20
|
+
const SHIFTS = {
|
|
21
|
+
x: "--_mp-shift-x",
|
|
22
|
+
y: "--_mp-shift-y"
|
|
23
|
+
};
|
|
24
|
+
/** The pills of each zone, in the order they were set up. */
|
|
25
|
+
const zones = /* @__PURE__ */ new Map();
|
|
26
|
+
/** Where a zone's pills are measured from, and along which axis. */
|
|
27
|
+
function axisOf(zone) {
|
|
28
|
+
if (zone === "left" || zone === "right") return {
|
|
29
|
+
shift: SHIFTS.x,
|
|
30
|
+
inset: zone,
|
|
31
|
+
size: "width"
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
shift: SHIFTS.y,
|
|
35
|
+
inset: zone.startsWith("top") ? "insetBlockStart" : "insetBlockEnd",
|
|
36
|
+
size: "blockSize"
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const number = (value) => Number.parseFloat(value) || 0;
|
|
40
|
+
function measure(rung, element, zone) {
|
|
41
|
+
if (!rung.frozen) rung.size = element.offsetWidth ? number(getComputedStyle(element)[axisOf(zone).size]) : 0;
|
|
42
|
+
}
|
|
43
|
+
/** Puts a zone's pills in their spots, moving there if a motion is given. */
|
|
44
|
+
function arrange(zone, planMotion) {
|
|
45
|
+
const { shift, inset } = axisOf(zone);
|
|
46
|
+
const rungs = zones.get(zone) ?? [];
|
|
47
|
+
const elements = rungs.map((rung) => rung.element);
|
|
48
|
+
if (rungs.length < 2 && !elements[0]?.style.getPropertyValue(shift)) return;
|
|
49
|
+
const motion = prefersReducedMotion() ? void 0 : planMotion?.();
|
|
50
|
+
const before = motion ? elements.map((element) => element.getBoundingClientRect()) : [];
|
|
51
|
+
for (const element of elements) element.style.removeProperty(shift);
|
|
52
|
+
planLadder(rungs.map((rung, index) => {
|
|
53
|
+
measure(rung, elements[index], zone);
|
|
54
|
+
const style = getComputedStyle(elements[index]);
|
|
55
|
+
return {
|
|
56
|
+
offset: number(style[inset]),
|
|
57
|
+
size: rung.size,
|
|
58
|
+
gap: number(style.rowGap),
|
|
59
|
+
visible: rung.presence !== "hidden" && style.position === "fixed"
|
|
60
|
+
};
|
|
61
|
+
})).forEach((value, index) => {
|
|
62
|
+
if (value) elements[index].style.setProperty(shift, `${value}px`);
|
|
63
|
+
});
|
|
64
|
+
if (!motion) return;
|
|
65
|
+
elements.forEach((element, index) => {
|
|
66
|
+
if (!element.animate) return;
|
|
67
|
+
cancel(element, SHIFT);
|
|
68
|
+
if (rungs[index] === motion.except || getComputedStyle(element).visibility === "hidden") return;
|
|
69
|
+
const from = before[index];
|
|
70
|
+
const to = element.getBoundingClientRect();
|
|
71
|
+
const x = from.left - to.left;
|
|
72
|
+
const y = from.top - to.top;
|
|
73
|
+
if (Math.abs(x) < .5 && Math.abs(y) < .5) return;
|
|
74
|
+
const { delay = 0, duration, easing = EASING } = motion;
|
|
75
|
+
element.animate([{ translate: `${x}px ${y}px` }, { translate: "0px 0px" }], {
|
|
76
|
+
id: SHIFT,
|
|
77
|
+
delay,
|
|
78
|
+
duration,
|
|
79
|
+
easing,
|
|
80
|
+
fill: "backwards",
|
|
81
|
+
composite: "add"
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const travel = (element) => {
|
|
86
|
+
const { fade, change } = readMorphTimings(element);
|
|
87
|
+
return { duration: fade + change };
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Gives the pill its place among the ones that share its placement.
|
|
91
|
+
* `zone`: its placement, or nothing for a pill that keeps to itself.
|
|
92
|
+
*/
|
|
93
|
+
function useZone(rootRef, zone, presence) {
|
|
94
|
+
const rungRef = useRef({
|
|
95
|
+
element: null,
|
|
96
|
+
presence,
|
|
97
|
+
size: 0,
|
|
98
|
+
frozen: presence === "panel"
|
|
99
|
+
});
|
|
100
|
+
useLayoutEffect(() => {
|
|
101
|
+
const rung = rungRef.current;
|
|
102
|
+
const element = rootRef.current;
|
|
103
|
+
if (!zone || !element) return;
|
|
104
|
+
const rungs = zones.get(zone) ?? [];
|
|
105
|
+
zones.set(zone, rungs);
|
|
106
|
+
rungs.push(rung);
|
|
107
|
+
rung.element = element;
|
|
108
|
+
const moved = rung.zone !== void 0 && rung.zone !== zone;
|
|
109
|
+
rung.zone = zone;
|
|
110
|
+
arrange(zone, () => ({
|
|
111
|
+
...travel(element),
|
|
112
|
+
except: moved ? void 0 : rung
|
|
113
|
+
}));
|
|
114
|
+
const observer = typeof ResizeObserver === "undefined" ? void 0 : new ResizeObserver(() => {
|
|
115
|
+
measure(rung, element, zone);
|
|
116
|
+
if (rungs.length > 1) arrange(zone);
|
|
117
|
+
});
|
|
118
|
+
observer?.observe(element);
|
|
119
|
+
return () => {
|
|
120
|
+
observer?.disconnect();
|
|
121
|
+
rungs.splice(rungs.indexOf(rung), 1);
|
|
122
|
+
element.style.removeProperty(SHIFTS.x);
|
|
123
|
+
element.style.removeProperty(SHIFTS.y);
|
|
124
|
+
arrange(zone, () => travel(element));
|
|
125
|
+
};
|
|
126
|
+
}, [rootRef, zone]);
|
|
127
|
+
useLayoutEffect(() => {
|
|
128
|
+
const rung = rungRef.current;
|
|
129
|
+
const previous = rung.presence;
|
|
130
|
+
const { element } = rung;
|
|
131
|
+
rung.presence = presence;
|
|
132
|
+
if (presence === "panel") rung.frozen = true;
|
|
133
|
+
if (previous === presence || !element || !rung.zone) return;
|
|
134
|
+
if (previous === "panel") {
|
|
135
|
+
const closing = (element.getAnimations?.() ?? []).filter(({ id }) => id === MORPH);
|
|
136
|
+
Promise.allSettled(closing.map(({ finished }) => finished)).then(() => {
|
|
137
|
+
if (rung.presence === "panel") return;
|
|
138
|
+
rung.frozen = false;
|
|
139
|
+
if (rung.zone) arrange(rung.zone, () => travel(element));
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if (presence === "hidden") arrange(rung.zone, () => {
|
|
143
|
+
const { fade, change } = readMorphTimings(element);
|
|
144
|
+
const exit = parseDuration(getComputedStyle(element).getPropertyValue("--_mp-exit"), 300);
|
|
145
|
+
return {
|
|
146
|
+
delay: fade + change + exit / 3,
|
|
147
|
+
duration: fade + change,
|
|
148
|
+
except: rung
|
|
149
|
+
};
|
|
150
|
+
});
|
|
151
|
+
else if (previous === "hidden") arrange(rung.zone, () => ({
|
|
152
|
+
duration: readMorphTimings(element).change,
|
|
153
|
+
easing: "ease-out",
|
|
154
|
+
except: rung
|
|
155
|
+
}));
|
|
156
|
+
}, [presence]);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export { planLadder, useZone };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** The calendar's own words. Days and months come from the browser, in the locale's language. */
|
|
2
|
+
export interface CalendarLabels {
|
|
3
|
+
previous: string;
|
|
4
|
+
next: string;
|
|
5
|
+
/** Read with the month: what clicking it does. */
|
|
6
|
+
zoomOut: string;
|
|
7
|
+
today: string;
|
|
8
|
+
now: string;
|
|
9
|
+
clear: string;
|
|
10
|
+
done: string;
|
|
11
|
+
hours: string;
|
|
12
|
+
minutes: string;
|
|
13
|
+
period: string;
|
|
14
|
+
week: string;
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import "./dates.js";
|
|
2
|
+
/** The calendar's own words. Days and months come from the browser, in the locale's language. */
|
|
3
|
+
export interface CalendarLabels {
|
|
4
|
+
previous: string;
|
|
5
|
+
next: string;
|
|
6
|
+
/** Read with the month: what clicking it does. */
|
|
7
|
+
zoomOut: string;
|
|
8
|
+
today: string;
|
|
9
|
+
now: string;
|
|
10
|
+
clear: string;
|
|
11
|
+
done: string;
|
|
12
|
+
hours: string;
|
|
13
|
+
minutes: string;
|
|
14
|
+
period: string;
|
|
15
|
+
week: string;
|
|
16
|
+
}
|