magic-pill 9.0.0-next.0 → 9.0.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 (112) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +6 -2
  3. package/dist/MagicPill/MagicPill.js +194 -2
  4. package/dist/MagicPill/MagicPill.mjs +194 -2
  5. package/dist/MagicPill/choreography.js +132 -2
  6. package/dist/MagicPill/choreography.mjs +125 -2
  7. package/dist/Pill/Pill.js +106 -2
  8. package/dist/Pill/Pill.mjs +106 -2
  9. package/dist/Pill/PillAction.js +67 -2
  10. package/dist/Pill/PillAction.mjs +67 -2
  11. package/dist/Pill/PillContext.js +26 -2
  12. package/dist/Pill/PillContext.mjs +24 -2
  13. package/dist/Pill/PillPanel.js +90 -2
  14. package/dist/Pill/PillPanel.mjs +89 -2
  15. package/dist/Pill/parts.js +46 -2
  16. package/dist/Pill/parts.mjs +43 -2
  17. package/dist/PillNavbar/PillNavbar.js +44 -2
  18. package/dist/PillNavbar/PillNavbar.mjs +44 -2
  19. package/dist/PillNavbar/matchPath.js +34 -2
  20. package/dist/PillNavbar/matchPath.mjs +34 -2
  21. package/dist/PillNotification/PillNotification.js +64 -2
  22. package/dist/PillNotification/PillNotification.mjs +63 -2
  23. package/dist/icons/ArrowIcon.js +21 -2
  24. package/dist/icons/ArrowIcon.mjs +21 -2
  25. package/dist/icons/BoxIcon.js +21 -2
  26. package/dist/icons/BoxIcon.mjs +21 -2
  27. package/dist/icons/CheckIcon.js +17 -2
  28. package/dist/icons/CheckIcon.mjs +17 -2
  29. package/dist/icons/CloseIcon.js +17 -2
  30. package/dist/icons/CloseIcon.mjs +17 -2
  31. package/dist/icons/CouponIcon.js +17 -2
  32. package/dist/icons/CouponIcon.mjs +17 -2
  33. package/dist/icons/DocumentIcon.js +17 -2
  34. package/dist/icons/DocumentIcon.mjs +17 -2
  35. package/dist/icons/HelpIcon.js +17 -2
  36. package/dist/icons/HelpIcon.mjs +17 -2
  37. package/dist/icons/InfoIcon.js +17 -2
  38. package/dist/icons/InfoIcon.mjs +17 -2
  39. package/dist/icons/MailIcon.js +17 -2
  40. package/dist/icons/MailIcon.mjs +17 -2
  41. package/dist/icons/PhoneIcon.js +21 -2
  42. package/dist/icons/PhoneIcon.mjs +21 -2
  43. package/dist/icons/PlusIcon.js +17 -2
  44. package/dist/icons/PlusIcon.mjs +17 -2
  45. package/dist/icons/UndoIcon.js +21 -2
  46. package/dist/icons/UndoIcon.mjs +21 -2
  47. package/dist/index.js +50 -1
  48. package/dist/index.mjs +24 -1
  49. package/dist/store/store.js +104 -2
  50. package/dist/store/store.mjs +102 -2
  51. package/dist/store/usePillQueue.js +17 -2
  52. package/dist/store/usePillQueue.mjs +17 -2
  53. package/dist/styles.css +125 -35
  54. package/dist/utils/inputModality.js +23 -0
  55. package/dist/utils/inputModality.mjs +22 -0
  56. package/dist/utils/react.js +23 -2
  57. package/dist/utils/react.mjs +21 -2
  58. package/dist/utils/url.js +22 -2
  59. package/dist/utils/url.mjs +22 -2
  60. package/package.json +3 -2
  61. package/dist/MagicPill/MagicPill.js.map +0 -1
  62. package/dist/MagicPill/MagicPill.mjs.map +0 -1
  63. package/dist/MagicPill/choreography.js.map +0 -1
  64. package/dist/MagicPill/choreography.mjs.map +0 -1
  65. package/dist/Pill/Pill.js.map +0 -1
  66. package/dist/Pill/Pill.mjs.map +0 -1
  67. package/dist/Pill/PillAction.js.map +0 -1
  68. package/dist/Pill/PillAction.mjs.map +0 -1
  69. package/dist/Pill/PillContext.js.map +0 -1
  70. package/dist/Pill/PillContext.mjs.map +0 -1
  71. package/dist/Pill/PillPanel.js.map +0 -1
  72. package/dist/Pill/PillPanel.mjs.map +0 -1
  73. package/dist/Pill/parts.js.map +0 -1
  74. package/dist/Pill/parts.mjs.map +0 -1
  75. package/dist/PillNavbar/PillNavbar.js.map +0 -1
  76. package/dist/PillNavbar/PillNavbar.mjs.map +0 -1
  77. package/dist/PillNavbar/matchPath.js.map +0 -1
  78. package/dist/PillNavbar/matchPath.mjs.map +0 -1
  79. package/dist/PillNotification/PillNotification.js.map +0 -1
  80. package/dist/PillNotification/PillNotification.mjs.map +0 -1
  81. package/dist/icons/ArrowIcon.js.map +0 -1
  82. package/dist/icons/ArrowIcon.mjs.map +0 -1
  83. package/dist/icons/BoxIcon.js.map +0 -1
  84. package/dist/icons/BoxIcon.mjs.map +0 -1
  85. package/dist/icons/CheckIcon.js.map +0 -1
  86. package/dist/icons/CheckIcon.mjs.map +0 -1
  87. package/dist/icons/CloseIcon.js.map +0 -1
  88. package/dist/icons/CloseIcon.mjs.map +0 -1
  89. package/dist/icons/CouponIcon.js.map +0 -1
  90. package/dist/icons/CouponIcon.mjs.map +0 -1
  91. package/dist/icons/DocumentIcon.js.map +0 -1
  92. package/dist/icons/DocumentIcon.mjs.map +0 -1
  93. package/dist/icons/HelpIcon.js.map +0 -1
  94. package/dist/icons/HelpIcon.mjs.map +0 -1
  95. package/dist/icons/InfoIcon.js.map +0 -1
  96. package/dist/icons/InfoIcon.mjs.map +0 -1
  97. package/dist/icons/MailIcon.js.map +0 -1
  98. package/dist/icons/MailIcon.mjs.map +0 -1
  99. package/dist/icons/PhoneIcon.js.map +0 -1
  100. package/dist/icons/PhoneIcon.mjs.map +0 -1
  101. package/dist/icons/PlusIcon.js.map +0 -1
  102. package/dist/icons/PlusIcon.mjs.map +0 -1
  103. package/dist/icons/UndoIcon.js.map +0 -1
  104. package/dist/icons/UndoIcon.mjs.map +0 -1
  105. package/dist/store/store.js.map +0 -1
  106. package/dist/store/store.mjs.map +0 -1
  107. package/dist/store/usePillQueue.js.map +0 -1
  108. package/dist/store/usePillQueue.mjs.map +0 -1
  109. package/dist/utils/react.js.map +0 -1
  110. package/dist/utils/react.mjs.map +0 -1
  111. package/dist/utils/url.js.map +0 -1
  112. package/dist/utils/url.mjs.map +0 -1
@@ -1,2 +1,125 @@
1
- const e=Object.freeze({kind:`none`}),t=Object.freeze({kind:`base`}),n=Object.freeze({kind:`idle`}),r=/* @__PURE__ */ new Set([`compact`,`expanded`,`panel`]),i=e=>r.has(e),a=e=>e.kind===`notification`?`notification:${e.item.id}`:e.kind,o=e=>e.kind===`none`?`hidden`:e.kind===`idle`?`rest`:`compact`;function s(e,t,n){let r=[],{scene:s,presence:c}=e,l=(e,t,n=s)=>{r.push({delay:e,presence:t,scene:n}),c=t,s=n};if(a(s)!==a(t)){if(t.kind===`none`)return c!==`hidden`&&l(0,`hidden`),l(n.reduced?n.exit:n.change+n.exit,`hidden`,t),r;i(c)?n.reduced?(l(0,`hidden`),l(n.exit,`hidden`,t)):(l(0,`rest`),l(n.change,`rest`,t)):l(0,c,t)}else s!==t&&l(0,c,t);let u=o(t);return u===`hidden`||u===`rest`?c!==u&&l(0,u):i(c)||(c===`hidden`&&!n.reduced?(l(0,`rest`),l(n.enter,`compact`)):l(0,`compact`)),r}function c(e,t){let n=/^\s*([\d.]+)(ms|s)\s*$/.exec(e);if(!n)return t;let r=Number.parseFloat(n[1]);return n[2]===`s`?r*1e3:r}function l(e){let t=e?getComputedStyle(e):null,n=(e,n)=>c(t?.getPropertyValue(e)??``,n),r=typeof window<`u`&&typeof window.matchMedia==`function`&&window.matchMedia(`(prefers-reduced-motion: reduce)`).matches;return r?{enter:0,change:0,exit:150,reduced:r}:{enter:n(`--_mp-enter`,500),change:n(`--_mp-change`,250),exit:n(`--_mp-exit`,300),reduced:r}}export{t as BASE,n as IDLE,e as NONE,i as isOpen,c as parseDuration,s as planSteps,l as readTimings};
2
- //# sourceMappingURL=choreography.mjs.map
1
+ // src/MagicPill/choreography.ts
2
+ const NONE = Object.freeze({ kind: "none" });
3
+ const BASE = Object.freeze({ kind: "base" });
4
+ const IDLE = Object.freeze({ kind: "idle" });
5
+ const OPEN_LEVELS = /* @__PURE__ */ new Set([
6
+ "compact",
7
+ "expanded",
8
+ "panel"
9
+ ]);
10
+ const isOpen = (presence) => OPEN_LEVELS.has(presence);
11
+ const sceneKey = (scene) => scene.kind === "notification" ? `notification:${scene.item.id}` : scene.kind;
12
+ /** Where each scene settles: the base and notifications are pills, idle is a circle. */
13
+ const settledPresence = (scene) => scene.kind === "none" ? "hidden" : scene.kind === "idle" ? "rest" : "compact";
14
+ /**
15
+ * The steps from what's displayed to the target scene:
16
+ * - new content: fade it out, collapse to the circle, swap the content there, grow back;
17
+ * - nothing left to show: hide (the stylesheet shrinks, then scales out), then drop the content;
18
+ * - from hidden: pop in as a circle, then grow into the pill.
19
+ * Open levels the user chose (expanded, panel) are kept while the scene stays the same.
20
+ */
21
+ function planSteps(current, target, timings) {
22
+ const steps = [];
23
+ let { scene, presence } = current;
24
+ const go = (delay, nextPresence, nextScene = scene) => {
25
+ steps.push({
26
+ delay,
27
+ presence: nextPresence,
28
+ scene: nextScene
29
+ });
30
+ presence = nextPresence;
31
+ scene = nextScene;
32
+ };
33
+ if (sceneKey(scene) !== sceneKey(target)) {
34
+ if (target.kind === "none") {
35
+ if (presence !== "hidden") go(0, "hidden");
36
+ go(timings.reduced ? timings.exit : timings.fade + timings.change + timings.exit, "hidden", target);
37
+ return steps;
38
+ }
39
+ if (isOpen(presence)) {
40
+ if (timings.reduced) {
41
+ go(0, "hidden");
42
+ go(timings.exit, "hidden", target);
43
+ } else {
44
+ go(0, "rest");
45
+ go(timings.fade + timings.change, "rest", target);
46
+ }
47
+ } else go(0, presence, target);
48
+ } else if (scene !== target) go(0, presence, target);
49
+ const settled = settledPresence(target);
50
+ if (settled === "hidden" || settled === "rest") {
51
+ if (presence !== settled) go(0, settled);
52
+ } else if (!isOpen(presence)) {
53
+ if (presence === "hidden" && !timings.reduced) {
54
+ go(0, "rest");
55
+ go(timings.enter, "compact");
56
+ } else go(0, "compact");
57
+ }
58
+ return steps;
59
+ }
60
+ /**
61
+ * Groups the steps by the time they're due. Steps due at the same time
62
+ * collapse into the last one, except new content swapped in while the pill
63
+ * is closed: it's shown closed first, so the stylesheet can transition it
64
+ * open (otherwise it appears at the circle's width and wraps word by word).
65
+ */
66
+ function batchSteps(current, steps) {
67
+ const batches = [];
68
+ let at = 0;
69
+ let scene = current.scene;
70
+ let unshown = false;
71
+ for (const { delay, scene: nextScene, presence } of steps) {
72
+ at += delay;
73
+ const state = {
74
+ scene: nextScene,
75
+ presence
76
+ };
77
+ const swapped = nextScene !== scene;
78
+ scene = nextScene;
79
+ const batch = batches[batches.length - 1];
80
+ if (batch && batch.at === at) {
81
+ if (unshown && isOpen(presence)) {
82
+ batch.states.push(state);
83
+ unshown = false;
84
+ } else {
85
+ batch.states[batch.states.length - 1] = state;
86
+ unshown = (unshown || swapped) && !isOpen(presence);
87
+ }
88
+ } else {
89
+ batches.push({
90
+ at,
91
+ states: [state]
92
+ });
93
+ unshown = swapped && !isOpen(presence);
94
+ }
95
+ }
96
+ return batches;
97
+ }
98
+ /** "500ms" or "0.5s" → 500; anything else → the fallback. */
99
+ function parseDuration(value, fallback) {
100
+ const match = /^\s*([\d.]+)(ms|s)\s*$/.exec(value);
101
+ if (!match) return fallback;
102
+ const amount = Number.parseFloat(match[1]);
103
+ return match[2] === "s" ? amount * 1e3 : amount;
104
+ }
105
+ /** The timings the stylesheet uses for this pill, or its defaults (e.g. when unstyled). */
106
+ function readTimings(element) {
107
+ const style = element ? getComputedStyle(element) : null;
108
+ const read = (name, fallback) => parseDuration(style?.getPropertyValue(name) ?? "", fallback);
109
+ const reduced = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
110
+ return reduced ? {
111
+ enter: 0,
112
+ fade: 0,
113
+ change: 0,
114
+ exit: 150,
115
+ reduced
116
+ } : {
117
+ enter: read("--_mp-enter", 500),
118
+ fade: read("--_mp-fade", 150),
119
+ change: read("--_mp-change", 250),
120
+ exit: read("--_mp-exit", 300),
121
+ reduced
122
+ };
123
+ }
124
+
125
+ export { BASE, IDLE, NONE, batchSteps, isOpen, parseDuration, planSteps, readTimings };
package/dist/Pill/Pill.js CHANGED
@@ -1,2 +1,106 @@
1
- "use client";let e=require("react");const t=require("./PillContext.js"),n=require("../utils/react.js");let r=require("react/jsx-runtime");const i=e=>e===`left`||e===`right`?`vertical`:`horizontal`,a=(0,e.forwardRef)(function({presence:a,defaultPresence:o=`compact`,onPresenceChange:s,placement:c=`inline`,orientation:l,variant:u=`neutral`,unstyled:d=!1,children:f,onKeyDown:p,...m},h){let[g,_]=(0,e.useState)(o),v=a!==void 0,y=v?a:g,b=l??i(c),x=c===`overlay`,S=y!==`hidden`,C=(0,e.useRef)(null),w=(0,e.useMemo)(()=>n.mergeRefs(C,h),[h]),T=(0,e.useRef)(null),E=(0,e.useId)(),D=(0,e.useRef)(`compact`);(0,e.useEffect)(()=>{y!==`panel`&&y!==`hidden`&&(D.current=y)},[y]);let O=(0,e.useCallback)(e=>{e!==y&&(v||_(e),s?.(e))},[y,v,s]),k=(0,e.useCallback)(()=>{y===`panel`&&O(D.current)},[y,O]);(0,e.useEffect)(()=>{let e=C.current;x&&e&&typeof e.showModal==`function`&&(S&&!e.open?e.showModal():!S&&e.open&&e.close())},[x,S]);let A=e=>{p?.(e),e.defaultPrevented||x||e.key===`Escape`&&y===`panel`&&(e.preventDefault(),e.stopPropagation(),k())},j=e=>{e.preventDefault(),y===`panel`?k():O(`hidden`)},M=()=>{y!==`hidden`&&O(`hidden`)},N=(0,e.useMemo)(()=>({presence:y,setPresence:O,closePanel:k,placement:c,orientation:b,variant:u,panelId:E,panelTriggerRef:T,rootRef:C}),[y,O,k,c,b,u,E]),P=x?`dialog`:`div`;return/* @__PURE__ */ (0,r.jsx)(t.PillContext.Provider,{value:N,children:/* @__PURE__ */ (0,r.jsx)(P,{...m,ref:w,"data-mp":`pill`,"data-presence":y,"data-placement":c,"data-orientation":b,"data-variant":u,"data-unstyled":d?``:void 0,...n.inertProps(!S&&!x),onKeyDown:A,...x?{onCancel:j,onClose:M}:{},children:f})})});module.exports=a;
2
- //# sourceMappingURL=Pill.js.map
1
+ "use client";
2
+
3
+ let react = require("react");
4
+ const require_PillContext = require("./PillContext.js");
5
+ const require_react = require("../utils/react.js");
6
+ let react_jsx_runtime = require("react/jsx-runtime");
7
+
8
+ // src/Pill/Pill.tsx
9
+ const defaultOrientation = (placement) => placement === "left" || placement === "right" ? "vertical" : "horizontal";
10
+ /**
11
+ * The pill: a container with the Magic Pill design language. Put any parts
12
+ * (or anything else) inside. Its look and animations follow its data attributes.
13
+ */
14
+ const Pill = (0, react.forwardRef)(function Pill({ presence: presenceProp, defaultPresence = "compact", onPresenceChange, placement = "inline", orientation, variant = "neutral", unstyled = false, children, onKeyDown, ...rest }, forwardedRef) {
15
+ const [uncontrolledPresence, setUncontrolledPresence] = (0, react.useState)(defaultPresence);
16
+ const isControlled = presenceProp !== void 0;
17
+ const presence = isControlled ? presenceProp : uncontrolledPresence;
18
+ const resolvedOrientation = orientation ?? defaultOrientation(placement);
19
+ const isOverlay = placement === "overlay";
20
+ const isVisible = presence !== "hidden";
21
+ const rootRef = (0, react.useRef)(null);
22
+ const ref = (0, react.useMemo)(() => require_react.mergeRefs(rootRef, forwardedRef), [forwardedRef]);
23
+ const panelTriggerRef = (0, react.useRef)(null);
24
+ const panelId = (0, react.useId)();
25
+ const returnPresenceRef = (0, react.useRef)("compact");
26
+ (0, react.useEffect)(() => {
27
+ if (presence !== "panel" && presence !== "hidden") returnPresenceRef.current = presence;
28
+ }, [presence]);
29
+ const setPresence = (0, react.useCallback)((next) => {
30
+ if (next === presence) return;
31
+ if (!isControlled) setUncontrolledPresence(next);
32
+ onPresenceChange?.(next);
33
+ }, [
34
+ presence,
35
+ isControlled,
36
+ onPresenceChange
37
+ ]);
38
+ const closePanel = (0, react.useCallback)(() => {
39
+ if (presence === "panel") setPresence(returnPresenceRef.current);
40
+ }, [presence, setPresence]);
41
+ (0, react.useEffect)(() => {
42
+ const dialog = rootRef.current;
43
+ if (!isOverlay || !dialog || typeof dialog.showModal !== "function") return;
44
+ if (isVisible && !dialog.open) dialog.showModal();
45
+ else if (!isVisible && dialog.open) dialog.close();
46
+ }, [isOverlay, isVisible]);
47
+ const handleKeyDown = (event) => {
48
+ onKeyDown?.(event);
49
+ if (event.defaultPrevented || isOverlay) return;
50
+ if (event.key === "Escape" && presence === "panel") {
51
+ event.preventDefault();
52
+ event.stopPropagation();
53
+ closePanel();
54
+ }
55
+ };
56
+ const handleCancel = (event) => {
57
+ event.preventDefault();
58
+ if (presence === "panel") closePanel();
59
+ else setPresence("hidden");
60
+ };
61
+ const handleClose = () => {
62
+ if (presence !== "hidden") setPresence("hidden");
63
+ };
64
+ const context = (0, react.useMemo)(() => ({
65
+ presence,
66
+ setPresence,
67
+ closePanel,
68
+ placement,
69
+ orientation: resolvedOrientation,
70
+ variant,
71
+ panelId,
72
+ panelTriggerRef,
73
+ rootRef
74
+ }), [
75
+ presence,
76
+ setPresence,
77
+ closePanel,
78
+ placement,
79
+ resolvedOrientation,
80
+ variant,
81
+ panelId
82
+ ]);
83
+ const Element = isOverlay ? "dialog" : "div";
84
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_PillContext.PillContext.Provider, {
85
+ value: context,
86
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Element, {
87
+ ...rest,
88
+ ref,
89
+ "data-mp": "pill",
90
+ "data-presence": presence,
91
+ "data-placement": placement,
92
+ "data-orientation": resolvedOrientation,
93
+ "data-variant": variant,
94
+ "data-unstyled": unstyled ? "" : void 0,
95
+ ...require_react.inertProps(!isVisible && !isOverlay),
96
+ onKeyDown: handleKeyDown,
97
+ ...isOverlay ? {
98
+ onCancel: handleCancel,
99
+ onClose: handleClose
100
+ } : {},
101
+ children
102
+ })
103
+ });
104
+ });
105
+
106
+ module.exports = Pill;
@@ -1,2 +1,106 @@
1
- "use client";import{forwardRef as e,useCallback as t,useEffect as n,useId as r,useMemo as i,useRef as a,useState as o}from"react";import{PillContext as s}from"./PillContext.mjs";import{inertProps as c,mergeRefs as l}from"../utils/react.mjs";import{jsx as u}from"react/jsx-runtime";const d=e=>e===`left`||e===`right`?`vertical`:`horizontal`,f=e(function({presence:e,defaultPresence:f=`compact`,onPresenceChange:p,placement:m=`inline`,orientation:h,variant:g=`neutral`,unstyled:_=!1,children:v,onKeyDown:y,...b},x){let[S,C]=o(f),w=e!==void 0,T=w?e:S,E=h??d(m),D=m===`overlay`,O=T!==`hidden`,k=a(null),A=i(()=>l(k,x),[x]),j=a(null),M=r(),N=a(`compact`);n(()=>{T!==`panel`&&T!==`hidden`&&(N.current=T)},[T]);let P=t(e=>{e!==T&&(w||C(e),p?.(e))},[T,w,p]),F=t(()=>{T===`panel`&&P(N.current)},[T,P]);n(()=>{let e=k.current;D&&e&&typeof e.showModal==`function`&&(O&&!e.open?e.showModal():!O&&e.open&&e.close())},[D,O]);let I=e=>{y?.(e),e.defaultPrevented||D||e.key===`Escape`&&T===`panel`&&(e.preventDefault(),e.stopPropagation(),F())},L=e=>{e.preventDefault(),T===`panel`?F():P(`hidden`)},R=()=>{T!==`hidden`&&P(`hidden`)},z=i(()=>({presence:T,setPresence:P,closePanel:F,placement:m,orientation:E,variant:g,panelId:M,panelTriggerRef:j,rootRef:k}),[T,P,F,m,E,g,M]),B=D?`dialog`:`div`;return/* @__PURE__ */ u(s.Provider,{value:z,children:/* @__PURE__ */ u(B,{...b,ref:A,"data-mp":`pill`,"data-presence":T,"data-placement":m,"data-orientation":E,"data-variant":g,"data-unstyled":_?``:void 0,...c(!O&&!D),onKeyDown:I,...D?{onCancel:L,onClose:R}:{},children:v})})});export{f as default};
2
- //# sourceMappingURL=Pill.mjs.map
1
+ "use client";
2
+
3
+ import { forwardRef, useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
4
+ import { PillContext } from "./PillContext.mjs";
5
+ import { inertProps, mergeRefs } from "../utils/react.mjs";
6
+ import { jsx } from "react/jsx-runtime";
7
+
8
+ // src/Pill/Pill.tsx
9
+ const defaultOrientation = (placement) => placement === "left" || placement === "right" ? "vertical" : "horizontal";
10
+ /**
11
+ * The pill: a container with the Magic Pill design language. Put any parts
12
+ * (or anything else) inside. Its look and animations follow its data attributes.
13
+ */
14
+ const Pill = forwardRef(function Pill({ presence: presenceProp, defaultPresence = "compact", onPresenceChange, placement = "inline", orientation, variant = "neutral", unstyled = false, children, onKeyDown, ...rest }, forwardedRef) {
15
+ const [uncontrolledPresence, setUncontrolledPresence] = useState(defaultPresence);
16
+ const isControlled = presenceProp !== void 0;
17
+ const presence = isControlled ? presenceProp : uncontrolledPresence;
18
+ const resolvedOrientation = orientation ?? defaultOrientation(placement);
19
+ const isOverlay = placement === "overlay";
20
+ const isVisible = presence !== "hidden";
21
+ const rootRef = useRef(null);
22
+ const ref = useMemo(() => mergeRefs(rootRef, forwardedRef), [forwardedRef]);
23
+ const panelTriggerRef = useRef(null);
24
+ const panelId = useId();
25
+ const returnPresenceRef = useRef("compact");
26
+ useEffect(() => {
27
+ if (presence !== "panel" && presence !== "hidden") returnPresenceRef.current = presence;
28
+ }, [presence]);
29
+ const setPresence = useCallback((next) => {
30
+ if (next === presence) return;
31
+ if (!isControlled) setUncontrolledPresence(next);
32
+ onPresenceChange?.(next);
33
+ }, [
34
+ presence,
35
+ isControlled,
36
+ onPresenceChange
37
+ ]);
38
+ const closePanel = useCallback(() => {
39
+ if (presence === "panel") setPresence(returnPresenceRef.current);
40
+ }, [presence, setPresence]);
41
+ useEffect(() => {
42
+ const dialog = rootRef.current;
43
+ if (!isOverlay || !dialog || typeof dialog.showModal !== "function") return;
44
+ if (isVisible && !dialog.open) dialog.showModal();
45
+ else if (!isVisible && dialog.open) dialog.close();
46
+ }, [isOverlay, isVisible]);
47
+ const handleKeyDown = (event) => {
48
+ onKeyDown?.(event);
49
+ if (event.defaultPrevented || isOverlay) return;
50
+ if (event.key === "Escape" && presence === "panel") {
51
+ event.preventDefault();
52
+ event.stopPropagation();
53
+ closePanel();
54
+ }
55
+ };
56
+ const handleCancel = (event) => {
57
+ event.preventDefault();
58
+ if (presence === "panel") closePanel();
59
+ else setPresence("hidden");
60
+ };
61
+ const handleClose = () => {
62
+ if (presence !== "hidden") setPresence("hidden");
63
+ };
64
+ const context = useMemo(() => ({
65
+ presence,
66
+ setPresence,
67
+ closePanel,
68
+ placement,
69
+ orientation: resolvedOrientation,
70
+ variant,
71
+ panelId,
72
+ panelTriggerRef,
73
+ rootRef
74
+ }), [
75
+ presence,
76
+ setPresence,
77
+ closePanel,
78
+ placement,
79
+ resolvedOrientation,
80
+ variant,
81
+ panelId
82
+ ]);
83
+ const Element = isOverlay ? "dialog" : "div";
84
+ return /* @__PURE__ */ jsx(PillContext.Provider, {
85
+ value: context,
86
+ children: /* @__PURE__ */ jsx(Element, {
87
+ ...rest,
88
+ ref,
89
+ "data-mp": "pill",
90
+ "data-presence": presence,
91
+ "data-placement": placement,
92
+ "data-orientation": resolvedOrientation,
93
+ "data-variant": variant,
94
+ "data-unstyled": unstyled ? "" : void 0,
95
+ ...inertProps(!isVisible && !isOverlay),
96
+ onKeyDown: handleKeyDown,
97
+ ...isOverlay ? {
98
+ onCancel: handleCancel,
99
+ onClose: handleClose
100
+ } : {},
101
+ children
102
+ })
103
+ });
104
+ });
105
+
106
+ export { Pill as default };
@@ -1,2 +1,67 @@
1
- let e=require("react");const t=require("../utils/url.js"),n=require("../utils/react.js");let r=require("react/jsx-runtime");const i=(0,e.forwardRef)(function(e,i){let{icon:a,label:o,expanded:s}=e,c=a!=null,l={"data-mp":`action`,"data-expanded":s||!c?``:void 0},u=/* @__PURE__ */ (0,r.jsxs)(r.Fragment,{children:[c&&/* @__PURE__ */ (0,r.jsx)(`span`,{"data-mp":`action-icon`,"aria-hidden":`true`,children:a}),/* @__PURE__ */ (0,r.jsx)(`span`,{"data-mp":`label`,children:o})]});if(e.href!==void 0){let{icon:a,label:s,expanded:c,href:d,linkComponent:f,...p}=e,m=i;return t.isSafeHref(d)?f?/* @__PURE__ */ (0,r.jsx)(f,{...p,...l,ref:m,href:d,children:u}):/* @__PURE__ */ (0,r.jsx)(`a`,{...p,...l,ref:m,href:d,children:u}):(n.devWarn(`<PillAction label="${o}"> got an unsafe href and renders without a link. Only relative, http(s):, mailto: and tel: URLs are allowed.`),/* @__PURE__ */ (0,r.jsx)(`a`,{...p,...l,ref:m,"aria-disabled":`true`,children:u}))}let{icon:d,label:f,expanded:p,href:m,...h}=e;return/* @__PURE__ */ (0,r.jsx)(`button`,{type:`button`,...h,...l,ref:i,children:u})});module.exports=i;
2
- //# sourceMappingURL=PillAction.js.map
1
+ let react = require("react");
2
+ const require_url = require("../utils/url.js");
3
+ const require_react = require("../utils/react.js");
4
+ let react_jsx_runtime = require("react/jsx-runtime");
5
+
6
+ // src/Pill/PillAction.tsx
7
+ /**
8
+ * The light round button: its icon at rest, icon + bold label when expanded.
9
+ * A <button> with `onClick`, a link with `href`.
10
+ */
11
+ const PillAction = (0, react.forwardRef)(function PillAction(props, ref) {
12
+ const { icon, label, expanded } = props;
13
+ const hasIcon = icon !== void 0 && icon !== null;
14
+ const shared = {
15
+ "data-mp": "action",
16
+ "data-expanded": expanded || !hasIcon ? "" : void 0
17
+ };
18
+ const content = /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
19
+ "data-mp": "action-body",
20
+ children: [hasIcon && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
21
+ "data-mp": "action-icon",
22
+ "aria-hidden": "true",
23
+ children: icon
24
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
25
+ "data-mp": "label",
26
+ children: label
27
+ })]
28
+ });
29
+ if (props.href !== void 0) {
30
+ const { icon: _icon, label: _label, expanded: _expanded, href, linkComponent: Link, ...rest } = props;
31
+ const anchorRef = ref;
32
+ if (!require_url.isSafeHref(href)) {
33
+ require_react.devWarn(`<PillAction label="${label}"> got an unsafe href and renders without a link. Only relative, http(s):, mailto: and tel: URLs are allowed.`);
34
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("a", {
35
+ ...rest,
36
+ ...shared,
37
+ ref: anchorRef,
38
+ "aria-disabled": "true",
39
+ children: content
40
+ });
41
+ }
42
+ if (Link) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Link, {
43
+ ...rest,
44
+ ...shared,
45
+ ref: anchorRef,
46
+ href,
47
+ children: content
48
+ });
49
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("a", {
50
+ ...rest,
51
+ ...shared,
52
+ ref: anchorRef,
53
+ href,
54
+ children: content
55
+ });
56
+ }
57
+ const { icon: _icon, label: _label, expanded: _expanded, href: _href, ...rest } = props;
58
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
59
+ type: "button",
60
+ ...rest,
61
+ ...shared,
62
+ ref,
63
+ children: content
64
+ });
65
+ });
66
+
67
+ module.exports = PillAction;
@@ -1,2 +1,67 @@
1
- import{forwardRef as e}from"react";import{isSafeHref as t}from"../utils/url.mjs";import{devWarn as n}from"../utils/react.mjs";import{Fragment as r,jsx as i,jsxs as a}from"react/jsx-runtime";const o=e(function(e,o){let{icon:s,label:c,expanded:l}=e,u=s!=null,d={"data-mp":`action`,"data-expanded":l||!u?``:void 0},f=/* @__PURE__ */ a(r,{children:[u&&/* @__PURE__ */ i(`span`,{"data-mp":`action-icon`,"aria-hidden":`true`,children:s}),/* @__PURE__ */ i(`span`,{"data-mp":`label`,children:c})]});if(e.href!==void 0){let{icon:r,label:a,expanded:s,href:l,linkComponent:u,...p}=e,m=o;return t(l)?i(u||`a`,{...p,...d,ref:m,href:l,children:f}):(n(`<PillAction label="${c}"> got an unsafe href and renders without a link. Only relative, http(s):, mailto: and tel: URLs are allowed.`),/* @__PURE__ */ i(`a`,{...p,...d,ref:m,"aria-disabled":`true`,children:f}))}let{icon:p,label:m,expanded:h,href:g,..._}=e;return/* @__PURE__ */ i(`button`,{type:`button`,..._,...d,ref:o,children:f})});export{o as default};
2
- //# sourceMappingURL=PillAction.mjs.map
1
+ import { forwardRef } from "react";
2
+ import { isSafeHref } from "../utils/url.mjs";
3
+ import { devWarn } from "../utils/react.mjs";
4
+ import { jsx, jsxs } from "react/jsx-runtime";
5
+
6
+ // src/Pill/PillAction.tsx
7
+ /**
8
+ * The light round button: its icon at rest, icon + bold label when expanded.
9
+ * A <button> with `onClick`, a link with `href`.
10
+ */
11
+ const PillAction = forwardRef(function PillAction(props, ref) {
12
+ const { icon, label, expanded } = props;
13
+ const hasIcon = icon !== void 0 && icon !== null;
14
+ const shared = {
15
+ "data-mp": "action",
16
+ "data-expanded": expanded || !hasIcon ? "" : void 0
17
+ };
18
+ const content = /* @__PURE__ */ jsxs("span", {
19
+ "data-mp": "action-body",
20
+ children: [hasIcon && /* @__PURE__ */ jsx("span", {
21
+ "data-mp": "action-icon",
22
+ "aria-hidden": "true",
23
+ children: icon
24
+ }), /* @__PURE__ */ jsx("span", {
25
+ "data-mp": "label",
26
+ children: label
27
+ })]
28
+ });
29
+ if (props.href !== void 0) {
30
+ const { icon: _icon, label: _label, expanded: _expanded, href, linkComponent: Link, ...rest } = props;
31
+ const anchorRef = ref;
32
+ if (!isSafeHref(href)) {
33
+ devWarn(`<PillAction label="${label}"> got an unsafe href and renders without a link. Only relative, http(s):, mailto: and tel: URLs are allowed.`);
34
+ return /* @__PURE__ */ jsx("a", {
35
+ ...rest,
36
+ ...shared,
37
+ ref: anchorRef,
38
+ "aria-disabled": "true",
39
+ children: content
40
+ });
41
+ }
42
+ if (Link) return /* @__PURE__ */ jsx(Link, {
43
+ ...rest,
44
+ ...shared,
45
+ ref: anchorRef,
46
+ href,
47
+ children: content
48
+ });
49
+ return /* @__PURE__ */ jsx("a", {
50
+ ...rest,
51
+ ...shared,
52
+ ref: anchorRef,
53
+ href,
54
+ children: content
55
+ });
56
+ }
57
+ const { icon: _icon, label: _label, expanded: _expanded, href: _href, ...rest } = props;
58
+ return /* @__PURE__ */ jsx("button", {
59
+ type: "button",
60
+ ...rest,
61
+ ...shared,
62
+ ref,
63
+ children: content
64
+ });
65
+ });
66
+
67
+ export { PillAction as default };
@@ -1,2 +1,26 @@
1
- "use client";let e=require("react");const t=(0,e.createContext)(null);function n(n){let r=(0,e.useContext)(t);if(!r)throw Error(`[magic-pill] ${n} must be rendered inside a <Pill>.`);return r}exports.PillContext=t,exports.useInternalPill=n,exports.usePill=function(){let{presence:e,setPresence:t,placement:r,orientation:i,variant:a}=n(`usePill()`);return{presence:e,setPresence:t,placement:r,orientation:i,variant:a}};
2
- //# sourceMappingURL=PillContext.js.map
1
+ "use client";
2
+
3
+ let react = require("react");
4
+
5
+ // src/Pill/PillContext.ts
6
+ const PillContext = (0, react.createContext)(null);
7
+ function useInternalPill(component) {
8
+ const context = (0, react.useContext)(PillContext);
9
+ if (!context) throw new Error(`[magic-pill] ${component} must be rendered inside a <Pill>.`);
10
+ return context;
11
+ }
12
+ /** Read and drive the enclosing <Pill>: what you need to write your own template. */
13
+ function usePill() {
14
+ const { presence, setPresence, placement, orientation, variant } = useInternalPill("usePill()");
15
+ return {
16
+ presence,
17
+ setPresence,
18
+ placement,
19
+ orientation,
20
+ variant
21
+ };
22
+ }
23
+
24
+ exports.PillContext = PillContext;
25
+ exports.useInternalPill = useInternalPill;
26
+ exports.usePill = usePill;
@@ -1,2 +1,24 @@
1
- "use client";import{createContext as e,useContext as t}from"react";const n=e(null);function r(e){let r=t(n);if(!r)throw Error(`[magic-pill] ${e} must be rendered inside a <Pill>.`);return r}function i(){let{presence:e,setPresence:t,placement:n,orientation:i,variant:a}=r(`usePill()`);return{presence:e,setPresence:t,placement:n,orientation:i,variant:a}}export{n as PillContext,r as useInternalPill,i as usePill};
2
- //# sourceMappingURL=PillContext.mjs.map
1
+ "use client";
2
+
3
+ import { createContext, useContext } from "react";
4
+
5
+ // src/Pill/PillContext.ts
6
+ const PillContext = createContext(null);
7
+ function useInternalPill(component) {
8
+ const context = useContext(PillContext);
9
+ if (!context) throw new Error(`[magic-pill] ${component} must be rendered inside a <Pill>.`);
10
+ return context;
11
+ }
12
+ /** Read and drive the enclosing <Pill>: what you need to write your own template. */
13
+ function usePill() {
14
+ const { presence, setPresence, placement, orientation, variant } = useInternalPill("usePill()");
15
+ return {
16
+ presence,
17
+ setPresence,
18
+ placement,
19
+ orientation,
20
+ variant
21
+ };
22
+ }
23
+
24
+ export { PillContext, useInternalPill, usePill };
@@ -1,2 +1,90 @@
1
- "use client";let e=require("react");const t=require("./PillAction.js"),n=require("./PillContext.js"),r=require("../icons/CloseIcon.js"),i=require("../utils/react.js");let a=require("react/jsx-runtime");const o=(0,e.forwardRef)(function({title:o,closeLabel:s=`Close`,closeIcon:c,children:l,...u},d){let{presence:f,closePanel:p,panelId:m,panelTriggerRef:h,rootRef:g}=n.useInternalPill(`<PillPanel>`),_=f===`panel`,v=(0,e.useId)(),y=(0,e.useRef)(null),b=(0,e.useMemo)(()=>i.mergeRefs(y,d),[d]),x=(0,e.useRef)(_);(0,e.useEffect)(()=>{let e=x.current;if(x.current=_,_&&!e)y.current?.focus({preventScroll:!0});else if(!_&&e){let e=document.activeElement;(!e||e===document.body||g.current?.contains(e))&&h.current?.focus()}},[_,h,g]);let S=o!=null;return/* @__PURE__ */ (0,a.jsxs)(`div`,{...u,ref:b,id:m,"data-mp":`panel`,"data-open":_?``:void 0,role:`region`,"aria-labelledby":S?v:void 0,tabIndex:-1,children:[S&&/* @__PURE__ */ (0,a.jsx)(`div`,{id:v,"data-mp":`panel-title`,children:o}),/* @__PURE__ */ (0,a.jsx)(`div`,{"data-mp":`panel-body`,children:l}),/* @__PURE__ */ (0,a.jsx)(`div`,{"data-mp":`panel-footer`,children:/* @__PURE__ */ (0,a.jsx)(t,{icon:c??/* @__PURE__ */ (0,a.jsx)(r,{}),label:s,onClick:p})})]})}),s=(0,e.forwardRef)(function(r,o){let{presence:s,setPresence:c,closePanel:l,panelId:u,panelTriggerRef:d}=n.useInternalPill(`<PillPanelTrigger>`),f=s===`panel`,p=(0,e.useRef)(null),m=(0,e.useMemo)(()=>i.mergeRefs(p,o),[o]),h=()=>{d.current=p.current,f?l():c(`panel`)};return/* @__PURE__ */ (0,a.jsx)(t,{...r,ref:m,"aria-expanded":f,"aria-controls":u,onClick:h})});exports.PillPanel=o,exports.PillPanelTrigger=s;
2
- //# sourceMappingURL=PillPanel.js.map
1
+ "use client";
2
+
3
+ let react = require("react");
4
+ const require_PillAction = require("./PillAction.js");
5
+ const require_PillContext = require("./PillContext.js");
6
+ const require_CloseIcon = require("../icons/CloseIcon.js");
7
+ const require_inputModality = require("../utils/inputModality.js");
8
+ const require_react = require("../utils/react.js");
9
+ let react_jsx_runtime = require("react/jsx-runtime");
10
+
11
+ // src/Pill/PillPanel.tsx
12
+ /**
13
+ * Content shown when the pill's presence is `panel` (one panel per pill).
14
+ * Labelled by its title, focused when it opens, gives focus back to its
15
+ * trigger when it closes.
16
+ */
17
+ const PillPanel = (0, react.forwardRef)(function PillPanel({ title, closeLabel = "Close", closeIcon, children, ...rest }, forwardedRef) {
18
+ const { presence, closePanel, panelId, panelTriggerRef, rootRef } = require_PillContext.useInternalPill("<PillPanel>");
19
+ const isOpen = presence === "panel";
20
+ const titleId = (0, react.useId)();
21
+ const panelRef = (0, react.useRef)(null);
22
+ const ref = (0, react.useMemo)(() => require_react.mergeRefs(panelRef, forwardedRef), [forwardedRef]);
23
+ const wasOpenRef = (0, react.useRef)(isOpen);
24
+ (0, react.useEffect)(require_inputModality.trackInputModality, []);
25
+ (0, react.useEffect)(() => {
26
+ const wasOpen = wasOpenRef.current;
27
+ wasOpenRef.current = isOpen;
28
+ if (isOpen && !wasOpen) panelRef.current?.focus({ preventScroll: true });
29
+ else if (!isOpen && wasOpen) {
30
+ const active = document.activeElement;
31
+ if (!active || active === document.body || rootRef.current?.contains(active)) panelTriggerRef.current?.focus(require_inputModality.getInputModality() === "pointer" ? { focusVisible: false } : void 0);
32
+ }
33
+ }, [
34
+ isOpen,
35
+ panelTriggerRef,
36
+ rootRef
37
+ ]);
38
+ const hasTitle = title !== void 0 && title !== null;
39
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
40
+ ...rest,
41
+ ref,
42
+ id: panelId,
43
+ "data-mp": "panel",
44
+ "data-open": isOpen ? "" : void 0,
45
+ role: "region",
46
+ "aria-labelledby": hasTitle ? titleId : void 0,
47
+ tabIndex: -1,
48
+ children: [
49
+ hasTitle && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
50
+ id: titleId,
51
+ "data-mp": "panel-title",
52
+ children: title
53
+ }),
54
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
55
+ "data-mp": "panel-body",
56
+ children
57
+ }),
58
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
59
+ "data-mp": "panel-footer",
60
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_PillAction, {
61
+ icon: closeIcon ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_CloseIcon, {}),
62
+ label: closeLabel,
63
+ onClick: closePanel
64
+ })
65
+ })
66
+ ]
67
+ });
68
+ });
69
+ /** An action that opens (and closes) the pill's panel. */
70
+ const PillPanelTrigger = (0, react.forwardRef)(function PillPanelTrigger(props, forwardedRef) {
71
+ const { presence, setPresence, closePanel, panelId, panelTriggerRef } = require_PillContext.useInternalPill("<PillPanelTrigger>");
72
+ const isOpen = presence === "panel";
73
+ const triggerRef = (0, react.useRef)(null);
74
+ const ref = (0, react.useMemo)(() => require_react.mergeRefs(triggerRef, forwardedRef), [forwardedRef]);
75
+ const toggle = () => {
76
+ panelTriggerRef.current = triggerRef.current;
77
+ if (isOpen) closePanel();
78
+ else setPresence("panel");
79
+ };
80
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_PillAction, {
81
+ ...props,
82
+ ref,
83
+ "aria-expanded": isOpen,
84
+ "aria-controls": panelId,
85
+ onClick: toggle
86
+ });
87
+ });
88
+
89
+ exports.PillPanel = PillPanel;
90
+ exports.PillPanelTrigger = PillPanelTrigger;