magic-pill 9.3.0 → 9.4.0-next.2

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.
Files changed (54) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +114 -3
  3. package/dist/MagicPill/MagicPill.d.mts +51 -1
  4. package/dist/MagicPill/MagicPill.d.ts +51 -1
  5. package/dist/MagicPill/MagicPill.js +53 -4
  6. package/dist/MagicPill/MagicPill.mjs +55 -6
  7. package/dist/MagicPill/flag.js +1 -59
  8. package/dist/MagicPill/flag.mjs +1 -58
  9. package/dist/Pill/PillProgress.d.mts +12 -0
  10. package/dist/Pill/PillProgress.d.ts +12 -0
  11. package/dist/Pill/PillProgress.js +42 -0
  12. package/dist/Pill/PillProgress.mjs +42 -0
  13. package/dist/Pill/goo.js +576 -0
  14. package/dist/Pill/goo.mjs +566 -0
  15. package/dist/Pill/morph.js +11 -4
  16. package/dist/Pill/morph.mjs +10 -5
  17. package/dist/PillNavbar/PillNavbar.d.mts +8 -0
  18. package/dist/PillNavbar/PillNavbar.d.ts +8 -0
  19. package/dist/PillNavbar/PillNavbar.js +3 -2
  20. package/dist/PillNavbar/PillNavbar.mjs +3 -2
  21. package/dist/PillNavbar/room.d.mts +3 -0
  22. package/dist/PillNavbar/room.d.ts +2 -0
  23. package/dist/PillNavbar/room.js +89 -17
  24. package/dist/PillNavbar/room.mjs +89 -18
  25. package/dist/index.d.mts +3 -2
  26. package/dist/index.d.ts +3 -2
  27. package/dist/index.js +2 -0
  28. package/dist/index.mjs +2 -1
  29. package/dist/satellites/Actions.d.mts +12 -0
  30. package/dist/satellites/Actions.d.ts +11 -0
  31. package/dist/satellites/Actions.js +181 -0
  32. package/dist/satellites/Actions.mjs +177 -0
  33. package/dist/satellites/Orbitals.js +585 -0
  34. package/dist/satellites/Orbitals.mjs +585 -0
  35. package/dist/satellites/Satellites.js +634 -0
  36. package/dist/satellites/Satellites.mjs +634 -0
  37. package/dist/satellites/layout.js +209 -0
  38. package/dist/satellites/layout.mjs +209 -0
  39. package/dist/satellites/measure.js +25 -0
  40. package/dist/satellites/measure.mjs +24 -0
  41. package/dist/satellites/orbits.js +139 -0
  42. package/dist/satellites/orbits.mjs +136 -0
  43. package/dist/satellites/types.d.mts +58 -0
  44. package/dist/satellites/types.d.ts +58 -0
  45. package/dist/satellites.d.mts +5 -0
  46. package/dist/satellites.d.ts +5 -0
  47. package/dist/satellites.js +14 -0
  48. package/dist/satellites.mjs +12 -0
  49. package/dist/store/store.d.mts +103 -0
  50. package/dist/store/store.d.ts +103 -0
  51. package/dist/store/store.js +52 -1
  52. package/dist/store/store.mjs +52 -2
  53. package/dist/styles.css +322 -8
  54. package/package.json +11 -1
@@ -0,0 +1,177 @@
1
+ "use client";
2
+
3
+ import { useEffect, useRef, useSyncExternalStore } from "react";
4
+ import PillAction from "../Pill/PillAction.mjs";
5
+ import { PillActions, PillDivider } from "../Pill/parts.mjs";
6
+ import { getStore } from "../store/store.mjs";
7
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
8
+
9
+ // src/satellites/Actions.tsx
10
+ /**
11
+ * Sets the current page's actions while the calling component is mounted:
12
+ * call it in a page, and they show next to the pill there, and go when the
13
+ * page goes. `null` or none: no actions. Give a stable array (a constant or
14
+ * `useMemo`), or they're set again at every render.
15
+ */
16
+ function usePillActions(items, options = {}) {
17
+ const store = getStore(options.pill ?? "main");
18
+ useEffect(() => {
19
+ store.setActions(items);
20
+ return () => {
21
+ if (store.getActions() === items) store.setActions(null);
22
+ };
23
+ }, [store, items]);
24
+ }
25
+ /** The host's page actions, as they change. */
26
+ function useHostActions(host) {
27
+ return useSyncExternalStore(host.store.subscribe, host.store.getActions, () => null);
28
+ }
29
+ /**
30
+ * A row of actions cut short (`data-wheel`) turns like a wheel: a mouse
31
+ * wheel, a drag, a swipe or the arrow keys bring the others in, each one
32
+ * snapping into place, and the ends that hide some fade out.
33
+ */
34
+ function useActionsWheel(track) {
35
+ useEffect(() => {
36
+ const element = track.current;
37
+ if (!element) return;
38
+ const vertical = () => getComputedStyle(element).flexDirection.startsWith("column");
39
+ const cut = () => element.hasAttribute("data-wheel");
40
+ const ends = () => {
41
+ const along = vertical();
42
+ const position = Math.abs(along ? element.scrollTop : element.scrollLeft);
43
+ const room = along ? element.scrollHeight - element.clientHeight : element.scrollWidth - element.clientWidth;
44
+ element.toggleAttribute("data-more-start", cut() && position > 1);
45
+ element.toggleAttribute("data-more-end", cut() && position < room - 1);
46
+ };
47
+ const onWheel = (event) => {
48
+ if (!cut() || vertical()) return;
49
+ if (Math.abs(event.deltaY) <= Math.abs(event.deltaX)) return;
50
+ event.preventDefault();
51
+ element.scrollBy({ left: event.deltaY });
52
+ };
53
+ let drag = null;
54
+ let swallowClick = false;
55
+ const onPointerDown = (event) => {
56
+ if (!cut() || event.pointerType !== "mouse" || event.button !== 0) return;
57
+ drag = {
58
+ x: event.clientX,
59
+ y: event.clientY,
60
+ start: vertical() ? element.scrollTop : element.scrollLeft,
61
+ moved: false
62
+ };
63
+ };
64
+ const onPointerMove = (event) => {
65
+ if (!drag) return;
66
+ const along = vertical();
67
+ const delta = along ? event.clientY - drag.y : event.clientX - drag.x;
68
+ if (!drag.moved && Math.abs(delta) < 6) return;
69
+ if (!drag.moved) {
70
+ drag.moved = true;
71
+ element.style.scrollSnapType = "none";
72
+ }
73
+ if (along) element.scrollTop = drag.start - delta;
74
+ else element.scrollLeft = drag.start - delta;
75
+ };
76
+ const onPointerUp = () => {
77
+ if (!drag) return;
78
+ if (drag.moved) {
79
+ swallowClick = true;
80
+ element.style.removeProperty("scroll-snap-type");
81
+ setTimeout(() => swallowClick = false, 0);
82
+ }
83
+ drag = null;
84
+ };
85
+ const onClick = (event) => {
86
+ if (!swallowClick) return;
87
+ event.preventDefault();
88
+ event.stopPropagation();
89
+ swallowClick = false;
90
+ };
91
+ const onKeyDown = (event) => {
92
+ if (!cut()) return;
93
+ const along = vertical();
94
+ const rtl = getComputedStyle(element).direction === "rtl";
95
+ const next = along ? "ArrowDown" : rtl ? "ArrowLeft" : "ArrowRight";
96
+ const previous = along ? "ArrowUp" : rtl ? "ArrowRight" : "ArrowLeft";
97
+ if (event.key !== next && event.key !== previous) return;
98
+ const actions = [...element.children];
99
+ const index = actions.findIndex((action) => action.contains(document.activeElement));
100
+ if (index < 0) return;
101
+ const target = actions[index + (event.key === next ? 1 : -1)];
102
+ const focusable = target?.matches("a, button") ? target : target?.querySelector("a, button");
103
+ if (!focusable) return;
104
+ event.preventDefault();
105
+ focusable.focus({ preventScroll: true });
106
+ target.scrollIntoView({
107
+ block: "nearest",
108
+ inline: "nearest",
109
+ behavior: "smooth"
110
+ });
111
+ };
112
+ const observer = typeof MutationObserver === "undefined" ? void 0 : new MutationObserver(ends);
113
+ observer?.observe(element, {
114
+ attributes: true,
115
+ attributeFilter: ["data-wheel", "style"]
116
+ });
117
+ element.addEventListener("scroll", ends, { passive: true });
118
+ element.addEventListener("wheel", onWheel, { passive: false });
119
+ element.addEventListener("pointerdown", onPointerDown);
120
+ window.addEventListener("pointermove", onPointerMove);
121
+ window.addEventListener("pointerup", onPointerUp);
122
+ element.addEventListener("click", onClick, true);
123
+ element.addEventListener("keydown", onKeyDown);
124
+ ends();
125
+ return () => {
126
+ observer?.disconnect();
127
+ element.removeEventListener("scroll", ends);
128
+ element.removeEventListener("wheel", onWheel);
129
+ element.removeEventListener("pointerdown", onPointerDown);
130
+ window.removeEventListener("pointermove", onPointerMove);
131
+ window.removeEventListener("pointerup", onPointerUp);
132
+ element.removeEventListener("click", onClick, true);
133
+ element.removeEventListener("keydown", onKeyDown);
134
+ };
135
+ }, [track]);
136
+ }
137
+ /**
138
+ * The actions themselves, as the pill's own: their label on hover and focus,
139
+ * a link or a button. `wheel`: cut short to that length, in px, turning to
140
+ * show the others.
141
+ */
142
+ function ActionList({ items, host, wheel, inside }) {
143
+ const track = useRef(null);
144
+ useActionsWheel(track);
145
+ return /* @__PURE__ */ jsx(PillActions, {
146
+ ref: track,
147
+ "data-inside": inside ? "" : void 0,
148
+ "data-wheel": wheel !== void 0 ? "" : void 0,
149
+ style: wheel !== void 0 ? { "--_mp-wheel-length": `${wheel}px` } : void 0,
150
+ children: items.map((item) => item.href !== void 0 ? /* @__PURE__ */ jsx(PillAction, {
151
+ icon: item.icon,
152
+ label: item.label,
153
+ expanded: item.expanded,
154
+ href: item.href,
155
+ linkComponent: host.linkComponent,
156
+ onClick: item.onClick
157
+ }, item.id ?? item.label) : /* @__PURE__ */ jsx(PillAction, {
158
+ icon: item.icon,
159
+ label: item.label,
160
+ expanded: item.expanded,
161
+ onClick: item.onClick
162
+ }, item.id ?? item.label))
163
+ });
164
+ }
165
+ /** In the pill itself (`actionsStyle="inside"`): the actions, then a divider (the other way round on the end side). */
166
+ function InsideActions({ host }) {
167
+ const items = useHostActions(host);
168
+ if (!items) return null;
169
+ const list = /* @__PURE__ */ jsx(ActionList, {
170
+ items,
171
+ host,
172
+ inside: true
173
+ });
174
+ return host.actionsSide === "end" ? /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(PillDivider, {}), list] }) : /* @__PURE__ */ jsxs(Fragment$1, { children: [list, /* @__PURE__ */ jsx(PillDivider, {})] });
175
+ }
176
+
177
+ export { ActionList, InsideActions, useActionsWheel, useHostActions, usePillActions };