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.
- package/CHANGELOG.md +15 -0
- package/README.md +6 -2
- package/dist/MagicPill/MagicPill.js +194 -2
- package/dist/MagicPill/MagicPill.mjs +194 -2
- package/dist/MagicPill/choreography.js +132 -2
- package/dist/MagicPill/choreography.mjs +125 -2
- package/dist/Pill/Pill.js +106 -2
- package/dist/Pill/Pill.mjs +106 -2
- package/dist/Pill/PillAction.js +67 -2
- package/dist/Pill/PillAction.mjs +67 -2
- package/dist/Pill/PillContext.js +26 -2
- package/dist/Pill/PillContext.mjs +24 -2
- package/dist/Pill/PillPanel.js +90 -2
- package/dist/Pill/PillPanel.mjs +89 -2
- package/dist/Pill/parts.js +46 -2
- package/dist/Pill/parts.mjs +43 -2
- package/dist/PillNavbar/PillNavbar.js +44 -2
- package/dist/PillNavbar/PillNavbar.mjs +44 -2
- package/dist/PillNavbar/matchPath.js +34 -2
- package/dist/PillNavbar/matchPath.mjs +34 -2
- package/dist/PillNotification/PillNotification.js +64 -2
- package/dist/PillNotification/PillNotification.mjs +63 -2
- package/dist/icons/ArrowIcon.js +21 -2
- package/dist/icons/ArrowIcon.mjs +21 -2
- package/dist/icons/BoxIcon.js +21 -2
- package/dist/icons/BoxIcon.mjs +21 -2
- package/dist/icons/CheckIcon.js +17 -2
- package/dist/icons/CheckIcon.mjs +17 -2
- package/dist/icons/CloseIcon.js +17 -2
- package/dist/icons/CloseIcon.mjs +17 -2
- package/dist/icons/CouponIcon.js +17 -2
- package/dist/icons/CouponIcon.mjs +17 -2
- package/dist/icons/DocumentIcon.js +17 -2
- package/dist/icons/DocumentIcon.mjs +17 -2
- package/dist/icons/HelpIcon.js +17 -2
- package/dist/icons/HelpIcon.mjs +17 -2
- package/dist/icons/InfoIcon.js +17 -2
- package/dist/icons/InfoIcon.mjs +17 -2
- package/dist/icons/MailIcon.js +17 -2
- package/dist/icons/MailIcon.mjs +17 -2
- package/dist/icons/PhoneIcon.js +21 -2
- package/dist/icons/PhoneIcon.mjs +21 -2
- package/dist/icons/PlusIcon.js +17 -2
- package/dist/icons/PlusIcon.mjs +17 -2
- package/dist/icons/UndoIcon.js +21 -2
- package/dist/icons/UndoIcon.mjs +21 -2
- package/dist/index.js +50 -1
- package/dist/index.mjs +24 -1
- package/dist/store/store.js +104 -2
- package/dist/store/store.mjs +102 -2
- package/dist/store/usePillQueue.js +17 -2
- package/dist/store/usePillQueue.mjs +17 -2
- package/dist/styles.css +125 -35
- package/dist/utils/inputModality.js +23 -0
- package/dist/utils/inputModality.mjs +22 -0
- package/dist/utils/react.js +23 -2
- package/dist/utils/react.mjs +21 -2
- package/dist/utils/url.js +22 -2
- package/dist/utils/url.mjs +22 -2
- package/package.json +3 -2
- package/dist/MagicPill/MagicPill.js.map +0 -1
- package/dist/MagicPill/MagicPill.mjs.map +0 -1
- package/dist/MagicPill/choreography.js.map +0 -1
- package/dist/MagicPill/choreography.mjs.map +0 -1
- package/dist/Pill/Pill.js.map +0 -1
- package/dist/Pill/Pill.mjs.map +0 -1
- package/dist/Pill/PillAction.js.map +0 -1
- package/dist/Pill/PillAction.mjs.map +0 -1
- package/dist/Pill/PillContext.js.map +0 -1
- package/dist/Pill/PillContext.mjs.map +0 -1
- package/dist/Pill/PillPanel.js.map +0 -1
- package/dist/Pill/PillPanel.mjs.map +0 -1
- package/dist/Pill/parts.js.map +0 -1
- package/dist/Pill/parts.mjs.map +0 -1
- package/dist/PillNavbar/PillNavbar.js.map +0 -1
- package/dist/PillNavbar/PillNavbar.mjs.map +0 -1
- package/dist/PillNavbar/matchPath.js.map +0 -1
- package/dist/PillNavbar/matchPath.mjs.map +0 -1
- package/dist/PillNotification/PillNotification.js.map +0 -1
- package/dist/PillNotification/PillNotification.mjs.map +0 -1
- package/dist/icons/ArrowIcon.js.map +0 -1
- package/dist/icons/ArrowIcon.mjs.map +0 -1
- package/dist/icons/BoxIcon.js.map +0 -1
- package/dist/icons/BoxIcon.mjs.map +0 -1
- package/dist/icons/CheckIcon.js.map +0 -1
- package/dist/icons/CheckIcon.mjs.map +0 -1
- package/dist/icons/CloseIcon.js.map +0 -1
- package/dist/icons/CloseIcon.mjs.map +0 -1
- package/dist/icons/CouponIcon.js.map +0 -1
- package/dist/icons/CouponIcon.mjs.map +0 -1
- package/dist/icons/DocumentIcon.js.map +0 -1
- package/dist/icons/DocumentIcon.mjs.map +0 -1
- package/dist/icons/HelpIcon.js.map +0 -1
- package/dist/icons/HelpIcon.mjs.map +0 -1
- package/dist/icons/InfoIcon.js.map +0 -1
- package/dist/icons/InfoIcon.mjs.map +0 -1
- package/dist/icons/MailIcon.js.map +0 -1
- package/dist/icons/MailIcon.mjs.map +0 -1
- package/dist/icons/PhoneIcon.js.map +0 -1
- package/dist/icons/PhoneIcon.mjs.map +0 -1
- package/dist/icons/PlusIcon.js.map +0 -1
- package/dist/icons/PlusIcon.mjs.map +0 -1
- package/dist/icons/UndoIcon.js.map +0 -1
- package/dist/icons/UndoIcon.mjs.map +0 -1
- package/dist/store/store.js.map +0 -1
- package/dist/store/store.mjs.map +0 -1
- package/dist/store/usePillQueue.js.map +0 -1
- package/dist/store/usePillQueue.mjs.map +0 -1
- package/dist/utils/react.js.map +0 -1
- package/dist/utils/react.mjs.map +0 -1
- package/dist/utils/url.js.map +0 -1
- package/dist/utils/url.mjs.map +0 -1
package/dist/store/store.js
CHANGED
|
@@ -1,2 +1,104 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
1
|
+
const require_react = require("../utils/react.js");
|
|
2
|
+
|
|
3
|
+
// src/store/store.ts
|
|
4
|
+
const EMPTY_STATE = Object.freeze({
|
|
5
|
+
current: null,
|
|
6
|
+
queued: Object.freeze([])
|
|
7
|
+
});
|
|
8
|
+
let lastId = 0;
|
|
9
|
+
const isServer = () => typeof document === "undefined";
|
|
10
|
+
function createStore(name) {
|
|
11
|
+
let items = [];
|
|
12
|
+
let state = EMPTY_STATE;
|
|
13
|
+
let hosts = 0;
|
|
14
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
15
|
+
const commit = (next) => {
|
|
16
|
+
items = next;
|
|
17
|
+
state = next.length === 0 ? EMPTY_STATE : {
|
|
18
|
+
current: next[0],
|
|
19
|
+
queued: next.slice(1)
|
|
20
|
+
};
|
|
21
|
+
listeners.forEach((listener) => listener());
|
|
22
|
+
};
|
|
23
|
+
const replace = (id, input) => commit(items.map((item) => item.id === id ? {
|
|
24
|
+
id,
|
|
25
|
+
input
|
|
26
|
+
} : item));
|
|
27
|
+
return {
|
|
28
|
+
getState: () => state,
|
|
29
|
+
subscribe(listener) {
|
|
30
|
+
listeners.add(listener);
|
|
31
|
+
return () => listeners.delete(listener);
|
|
32
|
+
},
|
|
33
|
+
notify(input) {
|
|
34
|
+
const id = input.id ?? `mp-${++lastId}`;
|
|
35
|
+
if (isServer()) {
|
|
36
|
+
require_react.devWarn(`pill("${name}").notify() was called on the server and does nothing: call it from client code.`);
|
|
37
|
+
return id;
|
|
38
|
+
}
|
|
39
|
+
if (items.some((item) => item.id === id)) replace(id, input);
|
|
40
|
+
else commit([...items, {
|
|
41
|
+
id,
|
|
42
|
+
input
|
|
43
|
+
}]);
|
|
44
|
+
return id;
|
|
45
|
+
},
|
|
46
|
+
update(id, patch) {
|
|
47
|
+
const item = items.find((queued) => queued.id === id);
|
|
48
|
+
if (item) replace(id, {
|
|
49
|
+
...item.input,
|
|
50
|
+
...patch
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
dismiss(id, reason) {
|
|
54
|
+
const item = id === void 0 ? items[0] : items.find((queued) => queued.id === id);
|
|
55
|
+
if (!item) return;
|
|
56
|
+
commit(items.filter((queued) => queued !== item));
|
|
57
|
+
item.input.onDismiss?.(item.id, reason);
|
|
58
|
+
},
|
|
59
|
+
clear() {
|
|
60
|
+
const dismissed = items;
|
|
61
|
+
if (dismissed.length === 0) return;
|
|
62
|
+
commit([]);
|
|
63
|
+
dismissed.forEach((item) => item.input.onDismiss?.(item.id, "cleared"));
|
|
64
|
+
},
|
|
65
|
+
registerHost() {
|
|
66
|
+
hosts += 1;
|
|
67
|
+
if (hosts > 1) require_react.devWarn(`Several <MagicPill name="${name}"> are mounted. Give each one its own name.`);
|
|
68
|
+
return () => {
|
|
69
|
+
hosts -= 1;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const stores = /* @__PURE__ */ new Map();
|
|
75
|
+
/** The store of the pill with this name, created on first use. */
|
|
76
|
+
function getStore(name) {
|
|
77
|
+
let store = stores.get(name);
|
|
78
|
+
if (!store) {
|
|
79
|
+
store = createStore(name);
|
|
80
|
+
stores.set(name, store);
|
|
81
|
+
}
|
|
82
|
+
return store;
|
|
83
|
+
}
|
|
84
|
+
const controllers = /* @__PURE__ */ new Map();
|
|
85
|
+
/** Control the <MagicPill> with this name (default "main") from anywhere. */
|
|
86
|
+
function pill(name = "main") {
|
|
87
|
+
let controller = controllers.get(name);
|
|
88
|
+
if (!controller) {
|
|
89
|
+
const store = getStore(name);
|
|
90
|
+
controller = Object.freeze({
|
|
91
|
+
name,
|
|
92
|
+
notify: (input) => store.notify(input),
|
|
93
|
+
update: (id, patch) => store.update(id, patch),
|
|
94
|
+
dismiss: (id) => store.dismiss(id, "dismissed"),
|
|
95
|
+
clear: () => store.clear()
|
|
96
|
+
});
|
|
97
|
+
controllers.set(name, controller);
|
|
98
|
+
}
|
|
99
|
+
return controller;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
exports.EMPTY_STATE = EMPTY_STATE;
|
|
103
|
+
exports.getStore = getStore;
|
|
104
|
+
exports.pill = pill;
|
package/dist/store/store.mjs
CHANGED
|
@@ -1,2 +1,102 @@
|
|
|
1
|
-
import{devWarn
|
|
2
|
-
|
|
1
|
+
import { devWarn } from "../utils/react.mjs";
|
|
2
|
+
|
|
3
|
+
// src/store/store.ts
|
|
4
|
+
const EMPTY_STATE = Object.freeze({
|
|
5
|
+
current: null,
|
|
6
|
+
queued: Object.freeze([])
|
|
7
|
+
});
|
|
8
|
+
let lastId = 0;
|
|
9
|
+
const isServer = () => typeof document === "undefined";
|
|
10
|
+
function createStore(name) {
|
|
11
|
+
let items = [];
|
|
12
|
+
let state = EMPTY_STATE;
|
|
13
|
+
let hosts = 0;
|
|
14
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
15
|
+
const commit = (next) => {
|
|
16
|
+
items = next;
|
|
17
|
+
state = next.length === 0 ? EMPTY_STATE : {
|
|
18
|
+
current: next[0],
|
|
19
|
+
queued: next.slice(1)
|
|
20
|
+
};
|
|
21
|
+
listeners.forEach((listener) => listener());
|
|
22
|
+
};
|
|
23
|
+
const replace = (id, input) => commit(items.map((item) => item.id === id ? {
|
|
24
|
+
id,
|
|
25
|
+
input
|
|
26
|
+
} : item));
|
|
27
|
+
return {
|
|
28
|
+
getState: () => state,
|
|
29
|
+
subscribe(listener) {
|
|
30
|
+
listeners.add(listener);
|
|
31
|
+
return () => listeners.delete(listener);
|
|
32
|
+
},
|
|
33
|
+
notify(input) {
|
|
34
|
+
const id = input.id ?? `mp-${++lastId}`;
|
|
35
|
+
if (isServer()) {
|
|
36
|
+
devWarn(`pill("${name}").notify() was called on the server and does nothing: call it from client code.`);
|
|
37
|
+
return id;
|
|
38
|
+
}
|
|
39
|
+
if (items.some((item) => item.id === id)) replace(id, input);
|
|
40
|
+
else commit([...items, {
|
|
41
|
+
id,
|
|
42
|
+
input
|
|
43
|
+
}]);
|
|
44
|
+
return id;
|
|
45
|
+
},
|
|
46
|
+
update(id, patch) {
|
|
47
|
+
const item = items.find((queued) => queued.id === id);
|
|
48
|
+
if (item) replace(id, {
|
|
49
|
+
...item.input,
|
|
50
|
+
...patch
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
dismiss(id, reason) {
|
|
54
|
+
const item = id === void 0 ? items[0] : items.find((queued) => queued.id === id);
|
|
55
|
+
if (!item) return;
|
|
56
|
+
commit(items.filter((queued) => queued !== item));
|
|
57
|
+
item.input.onDismiss?.(item.id, reason);
|
|
58
|
+
},
|
|
59
|
+
clear() {
|
|
60
|
+
const dismissed = items;
|
|
61
|
+
if (dismissed.length === 0) return;
|
|
62
|
+
commit([]);
|
|
63
|
+
dismissed.forEach((item) => item.input.onDismiss?.(item.id, "cleared"));
|
|
64
|
+
},
|
|
65
|
+
registerHost() {
|
|
66
|
+
hosts += 1;
|
|
67
|
+
if (hosts > 1) devWarn(`Several <MagicPill name="${name}"> are mounted. Give each one its own name.`);
|
|
68
|
+
return () => {
|
|
69
|
+
hosts -= 1;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const stores = /* @__PURE__ */ new Map();
|
|
75
|
+
/** The store of the pill with this name, created on first use. */
|
|
76
|
+
function getStore(name) {
|
|
77
|
+
let store = stores.get(name);
|
|
78
|
+
if (!store) {
|
|
79
|
+
store = createStore(name);
|
|
80
|
+
stores.set(name, store);
|
|
81
|
+
}
|
|
82
|
+
return store;
|
|
83
|
+
}
|
|
84
|
+
const controllers = /* @__PURE__ */ new Map();
|
|
85
|
+
/** Control the <MagicPill> with this name (default "main") from anywhere. */
|
|
86
|
+
function pill(name = "main") {
|
|
87
|
+
let controller = controllers.get(name);
|
|
88
|
+
if (!controller) {
|
|
89
|
+
const store = getStore(name);
|
|
90
|
+
controller = Object.freeze({
|
|
91
|
+
name,
|
|
92
|
+
notify: (input) => store.notify(input),
|
|
93
|
+
update: (id, patch) => store.update(id, patch),
|
|
94
|
+
dismiss: (id) => store.dismiss(id, "dismissed"),
|
|
95
|
+
clear: () => store.clear()
|
|
96
|
+
});
|
|
97
|
+
controllers.set(name, controller);
|
|
98
|
+
}
|
|
99
|
+
return controller;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { EMPTY_STATE, getStore, pill };
|
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
let react = require("react");
|
|
4
|
+
const require_store = require("./store.js");
|
|
5
|
+
|
|
6
|
+
// src/store/usePillQueue.ts
|
|
7
|
+
const getServerSnapshot = () => require_store.EMPTY_STATE;
|
|
8
|
+
/**
|
|
9
|
+
* Headless access to a pill's queue, to build your own host or display
|
|
10
|
+
* (like react-hot-toast's useToaster). <MagicPill> is built on it.
|
|
11
|
+
*/
|
|
12
|
+
function usePillQueue(name = "main") {
|
|
13
|
+
const store = require_store.getStore(name);
|
|
14
|
+
return (0, react.useSyncExternalStore)(store.subscribe, store.getState, getServerSnapshot);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
exports.usePillQueue = usePillQueue;
|
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useSyncExternalStore } from "react";
|
|
4
|
+
import { EMPTY_STATE, getStore } from "./store.mjs";
|
|
5
|
+
|
|
6
|
+
// src/store/usePillQueue.ts
|
|
7
|
+
const getServerSnapshot = () => EMPTY_STATE;
|
|
8
|
+
/**
|
|
9
|
+
* Headless access to a pill's queue, to build your own host or display
|
|
10
|
+
* (like react-hot-toast's useToaster). <MagicPill> is built on it.
|
|
11
|
+
*/
|
|
12
|
+
function usePillQueue(name = "main") {
|
|
13
|
+
const store = getStore(name);
|
|
14
|
+
return useSyncExternalStore(store.subscribe, store.getState, getServerSnapshot);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { usePillQueue };
|
package/dist/styles.css
CHANGED
|
@@ -62,6 +62,8 @@
|
|
|
62
62
|
--_mp-enter: var(--mp-duration-enter, 500ms);
|
|
63
63
|
--_mp-exit: var(--mp-duration-exit, 300ms);
|
|
64
64
|
--_mp-change: var(--mp-duration-change, 250ms);
|
|
65
|
+
/* The content fades out before the pill shrinks, and in once it has grown */
|
|
66
|
+
--_mp-fade: 150ms;
|
|
65
67
|
/* The entry overshoot from the mockups' "pop" */
|
|
66
68
|
--_mp-ease-enter: cubic-bezier(0.34, 1.56, 0.64, 1);
|
|
67
69
|
|
|
@@ -84,6 +86,9 @@
|
|
|
84
86
|
font-size: var(--mp-text-size, clamp(0.9375rem, 0.875rem + 0.25vw, 1.0625rem));
|
|
85
87
|
line-height: 1.3;
|
|
86
88
|
scale: 1;
|
|
89
|
+
/* Where the pill pops in from and scales out to: its centre, or e.g.
|
|
90
|
+
bottom to come out of the edge a bottom pill sits on */
|
|
91
|
+
transform-origin: var(--mp-scale-origin, center);
|
|
87
92
|
/* Lets the width animate from its real size (fit-content) to the
|
|
88
93
|
circle's, where supported; elsewhere max-inline-size does the work */
|
|
89
94
|
interpolate-size: allow-keywords;
|
|
@@ -120,33 +125,67 @@
|
|
|
120
125
|
inline-size: var(--_mp-rest-size);
|
|
121
126
|
max-inline-size: var(--_mp-rest-size);
|
|
122
127
|
max-block-size: var(--_mp-rest-size);
|
|
123
|
-
/*
|
|
128
|
+
/* Fade the content out, shrink to a circle, scale out, then stop being visible */
|
|
124
129
|
transition:
|
|
125
|
-
scale var(--_mp-exit) ease-in var(--_mp-change),
|
|
126
|
-
inline-size var(--_mp-change) ease-in-out,
|
|
127
|
-
block-size var(--_mp-change) ease-in-out,
|
|
128
|
-
max-inline-size var(--_mp-change) ease-in-out,
|
|
129
|
-
max-block-size var(--_mp-change) ease-in-out,
|
|
130
|
-
border-radius var(--_mp-change) ease-in-out,
|
|
131
|
-
padding var(--_mp-change) ease-in-out,
|
|
132
|
-
background-color var(--_mp-change) ease-in-out,
|
|
133
|
-
visibility 0s linear calc(var(--_mp-change) + var(--_mp-exit));
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
130
|
+
scale var(--_mp-exit) ease-in calc(var(--_mp-fade) + var(--_mp-change)),
|
|
131
|
+
inline-size var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
132
|
+
block-size var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
133
|
+
max-inline-size var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
134
|
+
max-block-size var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
135
|
+
border-radius var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
136
|
+
padding var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
137
|
+
background-color var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
138
|
+
visibility 0s linear calc(var(--_mp-fade) + var(--_mp-change) + var(--_mp-exit));
|
|
139
|
+
/* An open horizontal pill has no height cap (none): without this, the
|
|
140
|
+
circle's cap would clip the content at once, while it still fades */
|
|
141
|
+
transition-behavior: allow-discrete;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* A circle: both axes clamped, whatever the hidden content would need.
|
|
145
|
+
It shrinks once the content has faded out */
|
|
137
146
|
&[data-presence="rest"] {
|
|
138
147
|
inline-size: var(--_mp-rest-size);
|
|
139
148
|
max-inline-size: var(--_mp-rest-size);
|
|
140
149
|
max-block-size: var(--_mp-rest-size);
|
|
150
|
+
transition:
|
|
151
|
+
scale var(--_mp-enter) var(--_mp-ease-enter),
|
|
152
|
+
inline-size var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
153
|
+
block-size var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
154
|
+
max-inline-size var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
155
|
+
max-block-size var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
156
|
+
border-radius var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
157
|
+
padding var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
158
|
+
background-color var(--_mp-change) ease-in-out var(--_mp-fade),
|
|
159
|
+
visibility 0s;
|
|
160
|
+
transition-behavior: allow-discrete;
|
|
141
161
|
}
|
|
142
162
|
|
|
143
|
-
/* The content
|
|
163
|
+
/* The content is never seen while the pill changes size, so long text
|
|
164
|
+
never overflows or reflows word by word. Closing: it fades out as it
|
|
165
|
+
is, then the message unwraps (out of sight) and the pill shrinks over
|
|
166
|
+
it. Opening: the pill grows with the message on one line, then it may
|
|
167
|
+
wrap and the content fades in (discrete transitions, where
|
|
168
|
+
supported). <MagicPill> renders new content closed first, so it opens
|
|
169
|
+
the same way (L21) */
|
|
144
170
|
& > :not([data-mp="icon"], [data-mp="panel"]) {
|
|
145
|
-
transition:
|
|
171
|
+
transition:
|
|
172
|
+
opacity var(--_mp-fade) ease-out var(--_mp-change),
|
|
173
|
+
visibility 0s,
|
|
174
|
+
white-space 0s linear var(--_mp-change);
|
|
175
|
+
transition-behavior: allow-discrete;
|
|
146
176
|
}
|
|
147
177
|
|
|
148
|
-
|
|
149
|
-
|
|
178
|
+
&:is([data-presence="rest"], [data-presence="hidden"]) > :not([data-mp="icon"], [data-mp="panel"]) {
|
|
179
|
+
opacity: 0;
|
|
180
|
+
transition:
|
|
181
|
+
opacity var(--_mp-fade) ease-in,
|
|
182
|
+
visibility 0s linear calc(var(--_mp-fade) + var(--_mp-change)),
|
|
183
|
+
white-space 0s linear var(--_mp-fade);
|
|
184
|
+
transition-behavior: allow-discrete;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
&:is([data-presence="rest"], [data-presence="hidden"]) > [data-mp="message"] {
|
|
188
|
+
white-space: nowrap;
|
|
150
189
|
}
|
|
151
190
|
|
|
152
191
|
&[data-presence="panel"] {
|
|
@@ -171,12 +210,10 @@
|
|
|
171
210
|
|
|
172
211
|
&[data-placement^="top"] {
|
|
173
212
|
inset-block-start: calc(var(--_mp-offset) + env(safe-area-inset-top, 0px));
|
|
174
|
-
transform-origin: top;
|
|
175
213
|
}
|
|
176
214
|
|
|
177
215
|
&[data-placement^="bottom"] {
|
|
178
216
|
inset-block-end: calc(var(--_mp-offset) + env(safe-area-inset-bottom, 0px));
|
|
179
|
-
transform-origin: bottom;
|
|
180
217
|
}
|
|
181
218
|
|
|
182
219
|
/* Centred with auto margins, not left: 50% + translate, which would
|
|
@@ -204,12 +241,10 @@
|
|
|
204
241
|
|
|
205
242
|
&[data-placement="left"] {
|
|
206
243
|
left: calc(var(--_mp-offset) + env(safe-area-inset-left, 0px));
|
|
207
|
-
transform-origin: left;
|
|
208
244
|
}
|
|
209
245
|
|
|
210
246
|
&[data-placement="right"] {
|
|
211
247
|
right: calc(var(--_mp-offset) + env(safe-area-inset-right, 0px));
|
|
212
|
-
transform-origin: right;
|
|
213
248
|
}
|
|
214
249
|
|
|
215
250
|
&[data-placement="overlay"] {
|
|
@@ -262,14 +297,17 @@
|
|
|
262
297
|
block-size: 1px;
|
|
263
298
|
}
|
|
264
299
|
|
|
265
|
-
/* Its own width
|
|
266
|
-
|
|
300
|
+
/* Its own width up to a cap, and it wraps when space runs out (phones,
|
|
301
|
+
narrow containers). While the pill grows it stays on one line (see
|
|
302
|
+
the presence rules), so the pill reveals it instead of it reflowing
|
|
303
|
+
word by word */
|
|
267
304
|
& [data-mp="message"] {
|
|
268
|
-
flex: 0
|
|
305
|
+
flex: 0 1 auto;
|
|
306
|
+
min-inline-size: 0;
|
|
269
307
|
inline-size: max-content;
|
|
270
|
-
max-inline-size:
|
|
308
|
+
max-inline-size: var(--mp-message-width, 24rem);
|
|
271
309
|
font-style: italic;
|
|
272
|
-
text-wrap: balance;
|
|
310
|
+
text-wrap-style: balance;
|
|
273
311
|
}
|
|
274
312
|
|
|
275
313
|
& [data-mp="actions"] {
|
|
@@ -283,27 +321,39 @@
|
|
|
283
321
|
flex-direction: column;
|
|
284
322
|
}
|
|
285
323
|
|
|
324
|
+
/* The <a>/<button> stays neutral and the look lives on its inner body:
|
|
325
|
+
an app's global resets (a { color: inherit }, button { background:
|
|
326
|
+
none; padding: 0 }…) are unlayered, so they'd beat this layer on
|
|
327
|
+
the element, but they don't reach the body */
|
|
286
328
|
& [data-mp="action"] {
|
|
287
329
|
appearance: none;
|
|
288
330
|
flex: none;
|
|
331
|
+
display: inline-flex;
|
|
332
|
+
margin: 0;
|
|
333
|
+
padding: 0;
|
|
334
|
+
border: 0;
|
|
335
|
+
border-radius: 9999px;
|
|
336
|
+
background: transparent;
|
|
337
|
+
color: inherit;
|
|
338
|
+
font: inherit;
|
|
339
|
+
text-decoration: none;
|
|
340
|
+
cursor: pointer;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
& [data-mp="action-body"] {
|
|
289
344
|
display: inline-flex;
|
|
290
345
|
align-items: center;
|
|
291
346
|
block-size: var(--_mp-action-size);
|
|
292
347
|
min-inline-size: var(--_mp-action-size);
|
|
293
|
-
margin: 0;
|
|
294
348
|
padding-inline: calc((var(--_mp-action-size) - 1.25rem) / 2);
|
|
295
349
|
overflow: hidden;
|
|
296
|
-
border: 0;
|
|
297
350
|
border-radius: 9999px;
|
|
298
351
|
background: var(--_mp-action);
|
|
299
352
|
color: var(--_mp-on-action);
|
|
300
|
-
font: inherit;
|
|
301
353
|
font-family: var(--mp-font-accent, inherit);
|
|
302
354
|
font-style: normal;
|
|
303
355
|
font-weight: 700;
|
|
304
356
|
white-space: nowrap;
|
|
305
|
-
text-decoration: none;
|
|
306
|
-
cursor: pointer;
|
|
307
357
|
transition: padding var(--_mp-change) ease-in-out;
|
|
308
358
|
}
|
|
309
359
|
|
|
@@ -342,9 +392,11 @@
|
|
|
342
392
|
margin var(--_mp-change) ease-in-out;
|
|
343
393
|
}
|
|
344
394
|
|
|
345
|
-
& [data-mp="action"]:is(:
|
|
395
|
+
& [data-mp="action"]:is(:focus-visible, [data-expanded], [aria-current="page"], [aria-current="true"]),
|
|
346
396
|
&:is([data-presence="expanded"], [data-presence="panel"]) [data-mp="action"] {
|
|
347
|
-
|
|
397
|
+
& [data-mp="action-body"] {
|
|
398
|
+
padding-inline-end: calc(var(--_mp-action-size) / 2.5);
|
|
399
|
+
}
|
|
348
400
|
|
|
349
401
|
& [data-mp="label"] {
|
|
350
402
|
max-inline-size: 20rem;
|
|
@@ -356,6 +408,25 @@
|
|
|
356
408
|
}
|
|
357
409
|
}
|
|
358
410
|
|
|
411
|
+
/* The same on hover, only where the device really hovers: a tap on a
|
|
412
|
+
touchscreen leaves a "sticky" hover behind until the next tap (L20) */
|
|
413
|
+
@media (hover: hover) {
|
|
414
|
+
& [data-mp="action"]:hover {
|
|
415
|
+
& [data-mp="action-body"] {
|
|
416
|
+
padding-inline-end: calc(var(--_mp-action-size) / 2.5);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
& [data-mp="label"] {
|
|
420
|
+
max-inline-size: 20rem;
|
|
421
|
+
opacity: 1;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
& [data-mp="action-icon"] + [data-mp="label"] {
|
|
425
|
+
margin-inline-start: 0.5rem;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
359
430
|
/* Panel */
|
|
360
431
|
& [data-mp="panel"] {
|
|
361
432
|
display: grid;
|
|
@@ -408,8 +479,27 @@
|
|
|
408
479
|
visibility 0s linear 150ms;
|
|
409
480
|
}
|
|
410
481
|
|
|
482
|
+
&[data-presence="rest"] {
|
|
483
|
+
transition:
|
|
484
|
+
opacity 150ms linear,
|
|
485
|
+
visibility 0s;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/* Nothing resizes, so the content doesn't wait: it fades with the
|
|
489
|
+
pill, and the message unwraps at once */
|
|
490
|
+
&:not([data-presence="rest"], [data-presence="hidden"]) > :not([data-mp="icon"], [data-mp="panel"]) {
|
|
491
|
+
transition: opacity 150ms linear;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
&:is([data-presence="rest"], [data-presence="hidden"]) > :not([data-mp="icon"], [data-mp="panel"]) {
|
|
495
|
+
transition:
|
|
496
|
+
opacity 150ms linear,
|
|
497
|
+
visibility 0s linear 150ms;
|
|
498
|
+
}
|
|
499
|
+
|
|
411
500
|
& [data-mp="label"],
|
|
412
|
-
& [data-mp="action"],
|
|
501
|
+
& [data-mp="action-body"],
|
|
502
|
+
& [data-mp="message"],
|
|
413
503
|
& [data-mp="panel"] {
|
|
414
504
|
transition: none;
|
|
415
505
|
}
|
|
@@ -419,7 +509,7 @@
|
|
|
419
509
|
/* Forced colours (e.g. Windows High Contrast): keep the shapes visible */
|
|
420
510
|
@media (forced-colors: active) {
|
|
421
511
|
[data-mp="pill"]:not([data-unstyled]),
|
|
422
|
-
[data-mp="pill"]:not([data-unstyled]) [data-mp="action"] {
|
|
512
|
+
[data-mp="pill"]:not([data-unstyled]) [data-mp="action-body"] {
|
|
423
513
|
border: 1px solid CanvasText;
|
|
424
514
|
}
|
|
425
515
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/utils/inputModality.ts
|
|
2
|
+
let modality = "keyboard";
|
|
3
|
+
let listening = false;
|
|
4
|
+
const onPointer = () => {
|
|
5
|
+
modality = "pointer";
|
|
6
|
+
};
|
|
7
|
+
const onKey = (event) => {
|
|
8
|
+
if (event.metaKey || event.ctrlKey || event.altKey) return;
|
|
9
|
+
modality = "keyboard";
|
|
10
|
+
};
|
|
11
|
+
/** Starts listening (once, in the capture phase, so nothing can stop it). */
|
|
12
|
+
function trackInputModality() {
|
|
13
|
+
if (listening || typeof document === "undefined") return;
|
|
14
|
+
listening = true;
|
|
15
|
+
document.addEventListener("pointerdown", onPointer, true);
|
|
16
|
+
document.addEventListener("keydown", onKey, true);
|
|
17
|
+
}
|
|
18
|
+
function getInputModality() {
|
|
19
|
+
return modality;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
exports.getInputModality = getInputModality;
|
|
23
|
+
exports.trackInputModality = trackInputModality;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/utils/inputModality.ts
|
|
2
|
+
let modality = "keyboard";
|
|
3
|
+
let listening = false;
|
|
4
|
+
const onPointer = () => {
|
|
5
|
+
modality = "pointer";
|
|
6
|
+
};
|
|
7
|
+
const onKey = (event) => {
|
|
8
|
+
if (event.metaKey || event.ctrlKey || event.altKey) return;
|
|
9
|
+
modality = "keyboard";
|
|
10
|
+
};
|
|
11
|
+
/** Starts listening (once, in the capture phase, so nothing can stop it). */
|
|
12
|
+
function trackInputModality() {
|
|
13
|
+
if (listening || typeof document === "undefined") return;
|
|
14
|
+
listening = true;
|
|
15
|
+
document.addEventListener("pointerdown", onPointer, true);
|
|
16
|
+
document.addEventListener("keydown", onKey, true);
|
|
17
|
+
}
|
|
18
|
+
function getInputModality() {
|
|
19
|
+
return modality;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { getInputModality, trackInputModality };
|
package/dist/utils/react.js
CHANGED
|
@@ -1,2 +1,23 @@
|
|
|
1
|
-
let
|
|
2
|
-
|
|
1
|
+
let react = require("react");
|
|
2
|
+
|
|
3
|
+
// src/utils/react.ts
|
|
4
|
+
/** Warn in development builds only. */
|
|
5
|
+
function devWarn(message) {
|
|
6
|
+
if (typeof process !== "undefined" && process.env.NODE_ENV !== "production") console.warn(`[magic-pill] ${message}`);
|
|
7
|
+
}
|
|
8
|
+
const INERT_VALUE = Number.parseInt(react.version, 10) >= 19 ? true : "";
|
|
9
|
+
/** Spread on an element to make it inert (unfocusable, hidden from assistive technology). */
|
|
10
|
+
function inertProps(inert) {
|
|
11
|
+
return inert ? { inert: INERT_VALUE } : {};
|
|
12
|
+
}
|
|
13
|
+
/** Combine refs (React 18 compatible: no ref cleanup functions). */
|
|
14
|
+
function mergeRefs(...refs) {
|
|
15
|
+
return (node) => {
|
|
16
|
+
for (const ref of refs) if (typeof ref === "function") ref(node);
|
|
17
|
+
else if (ref) ref.current = node;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
exports.devWarn = devWarn;
|
|
22
|
+
exports.inertProps = inertProps;
|
|
23
|
+
exports.mergeRefs = mergeRefs;
|
package/dist/utils/react.mjs
CHANGED
|
@@ -1,2 +1,21 @@
|
|
|
1
|
-
import{version
|
|
2
|
-
|
|
1
|
+
import { version } from "react";
|
|
2
|
+
|
|
3
|
+
// src/utils/react.ts
|
|
4
|
+
/** Warn in development builds only. */
|
|
5
|
+
function devWarn(message) {
|
|
6
|
+
if (typeof process !== "undefined" && process.env.NODE_ENV !== "production") console.warn(`[magic-pill] ${message}`);
|
|
7
|
+
}
|
|
8
|
+
const INERT_VALUE = Number.parseInt(version, 10) >= 19 ? true : "";
|
|
9
|
+
/** Spread on an element to make it inert (unfocusable, hidden from assistive technology). */
|
|
10
|
+
function inertProps(inert) {
|
|
11
|
+
return inert ? { inert: INERT_VALUE } : {};
|
|
12
|
+
}
|
|
13
|
+
/** Combine refs (React 18 compatible: no ref cleanup functions). */
|
|
14
|
+
function mergeRefs(...refs) {
|
|
15
|
+
return (node) => {
|
|
16
|
+
for (const ref of refs) if (typeof ref === "function") ref(node);
|
|
17
|
+
else if (ref) ref.current = node;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { devWarn, inertProps, mergeRefs };
|
package/dist/utils/url.js
CHANGED
|
@@ -1,2 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/utils/url.ts
|
|
2
|
+
const ALLOWED_SCHEMES = /* @__PURE__ */ new Set([
|
|
3
|
+
"http",
|
|
4
|
+
"https",
|
|
5
|
+
"mailto",
|
|
6
|
+
"tel"
|
|
7
|
+
]);
|
|
8
|
+
/**
|
|
9
|
+
* Whether an href is safe to render: relative URLs (including `#…`, `?…`
|
|
10
|
+
* and `//host`), or absolute URLs with an `http(s):`, `mailto:` or `tel:` scheme.
|
|
11
|
+
*
|
|
12
|
+
* It normalises the URL the way browsers do before reading the scheme
|
|
13
|
+
* (leading spaces and control characters dropped, tabs and newlines removed),
|
|
14
|
+
* so `" java\tscript:…"` is caught too.
|
|
15
|
+
*/
|
|
16
|
+
function isSafeHref(href) {
|
|
17
|
+
const normalised = href.replace(/^[\x00-\x20]+/, "").replace(/[\t\n\r]/g, "");
|
|
18
|
+
const scheme = /^([a-z][a-z\d+.-]*):/i.exec(normalised);
|
|
19
|
+
return scheme === null || ALLOWED_SCHEMES.has(scheme[1].toLowerCase());
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
exports.isSafeHref = isSafeHref;
|
package/dist/utils/url.mjs
CHANGED
|
@@ -1,2 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/utils/url.ts
|
|
2
|
+
const ALLOWED_SCHEMES = /* @__PURE__ */ new Set([
|
|
3
|
+
"http",
|
|
4
|
+
"https",
|
|
5
|
+
"mailto",
|
|
6
|
+
"tel"
|
|
7
|
+
]);
|
|
8
|
+
/**
|
|
9
|
+
* Whether an href is safe to render: relative URLs (including `#…`, `?…`
|
|
10
|
+
* and `//host`), or absolute URLs with an `http(s):`, `mailto:` or `tel:` scheme.
|
|
11
|
+
*
|
|
12
|
+
* It normalises the URL the way browsers do before reading the scheme
|
|
13
|
+
* (leading spaces and control characters dropped, tabs and newlines removed),
|
|
14
|
+
* so `" java\tscript:…"` is caught too.
|
|
15
|
+
*/
|
|
16
|
+
function isSafeHref(href) {
|
|
17
|
+
const normalised = href.replace(/^[\x00-\x20]+/, "").replace(/[\t\n\r]/g, "");
|
|
18
|
+
const scheme = /^([a-z][a-z\d+.-]*):/i.exec(normalised);
|
|
19
|
+
return scheme === null || ALLOWED_SCHEMES.has(scheme[1].toLowerCase());
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { isSafeHref };
|