react-motion-modal 2.0.1 → 2.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/dist/index.css CHANGED
@@ -1 +1 @@
1
- :where(.react-motion-modal__root){position:fixed;inset:0}:where(.react-motion-modal__backdrop){position:absolute;inset:0;background-color:transparent}:where(.react-motion-modal__body){position:absolute;box-sizing:border-box;max-height:100vh;max-height:100dvh;max-width:100vw;max-width:100dvw;overflow-x:hidden;overflow-y:auto}:where(.react-motion-modal__content){width:100%;height:100%}:where(.react-motion-modal__body--top){top:0;left:0;right:0;width:100%}:where(.react-motion-modal__body--top-left){top:0;left:0}:where(.react-motion-modal__body--top-right){top:0;right:0}:where(.react-motion-modal__body--top-center){top:0;left:50%;transform:translate(-50%)}:where(.react-motion-modal__body--bottom){right:0;bottom:0;left:0;width:100%}:where(.react-motion-modal__body--bottom-left){bottom:0;left:0}:where(.react-motion-modal__body--bottom-right){right:0;bottom:0}:where(.react-motion-modal__body--bottom-center){bottom:0;left:50%;transform:translate(-50%)}:where(.react-motion-modal__body--left){top:0;bottom:0;left:0}:where(.react-motion-modal__body--right){top:0;right:0;bottom:0}:where(.react-motion-modal__body--center){top:50%;left:50%;transform:translate(-50%,-50%)}:where(.react-motion-modal__body--center-full){inset:0;width:100%;height:100%}
1
+ :where(.react-motion-modal__root){position:fixed;inset:0;overflow:clip}:where(.react-motion-modal__backdrop){position:absolute;inset:0;background-color:transparent}:where(.react-motion-modal__body){position:absolute;box-sizing:border-box;max-height:100vh;max-height:100dvh;max-width:100vw;max-width:100dvw;overflow-x:hidden;overflow-y:auto}:where(.react-motion-modal__content){width:100%;height:100%}:where(.react-motion-modal__body--top){top:0;left:0;right:0;width:100%}:where(.react-motion-modal__body--top-left){top:0;left:0}:where(.react-motion-modal__body--top-right){top:0;right:0}:where(.react-motion-modal__body--top-center){top:0;left:50%;translate:-50% 0}:where(.react-motion-modal__body--bottom){right:0;bottom:0;left:0;width:100%}:where(.react-motion-modal__body--bottom-left){bottom:0;left:0}:where(.react-motion-modal__body--bottom-right){right:0;bottom:0}:where(.react-motion-modal__body--bottom-center){bottom:0;left:50%;translate:-50% 0}:where(.react-motion-modal__body--left){top:0;bottom:0;left:0}:where(.react-motion-modal__body--right){top:0;right:0;bottom:0}:where(.react-motion-modal__body--center){top:50%;left:50%;translate:-50% -50%}:where(.react-motion-modal__body--center-full){inset:0;width:100%;height:100%}
package/dist/index.d.ts CHANGED
@@ -89,7 +89,10 @@ export declare type ModalPosition = (typeof MODAL_POSITIONS)[keyof typeof MODAL_
89
89
 
90
90
  /**
91
91
  * Renders one modal system into a portal and owns its browser accessibility
92
- * lifecycle.
92
+ * lifecycle. Providers that share one document coordinate body scroll
93
+ * locking, keyboard ownership and visual layering through a per-document
94
+ * coordinator, so independent systems never unlock, cancel or cover each
95
+ * other accidentally.
93
96
  */
94
97
  export declare const ModalProvider: <Registry extends ModalRegistry>({ system, defaultOptions, portalRoot: requestedPortalRoot, }: ModalProviderProps<Registry>) => ReactPortal | null;
95
98
 
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import { jsxs as F, jsx as _ } from "react/jsx-runtime";
2
- import { useReducedMotion as D, useIsPresent as G, motion as E, AnimatePresence as H } from "motion/react";
3
- import { useState as j, useRef as h, useEffect as v } from "react";
4
- import { createPortal as $ } from "react-dom";
5
- import { useStore as K } from "zustand/react";
6
- import { createStore as U } from "zustand/vanilla";
7
- const a = {
1
+ import { jsxs as ot, jsx as M } from "react/jsx-runtime";
2
+ import { useReducedMotion as nt, useIsPresent as it, motion as A, AnimatePresence as rt } from "motion/react";
3
+ import { useState as $, useRef as R, useCallback as st, useEffect as O } from "react";
4
+ import { createPortal as at } from "react-dom";
5
+ import { useStore as ct } from "zustand/react";
6
+ import { createStore as lt } from "zustand/vanilla";
7
+ const u = {
8
8
  TOP: "top",
9
9
  TOP_LEFT: "top-left",
10
10
  TOP_RIGHT: "top-right",
@@ -17,388 +17,499 @@ const a = {
17
17
  BOTTOM_LEFT: "bottom-left",
18
18
  BOTTOM_RIGHT: "bottom-right",
19
19
  BOTTOM_CENTER: "bottom-center"
20
- }, O = 1e3, M = {
20
+ }, I = 1e3, G = {
21
21
  duration: 0.24,
22
22
  ease: "easeOut"
23
- }, g = {
23
+ }, K = {
24
24
  duration: 0.18,
25
25
  ease: "easeIn"
26
- }, C = {
26
+ }, Z = {
27
27
  initial: { opacity: 0 },
28
28
  animate: {
29
29
  opacity: 1,
30
- transition: M
30
+ transition: G
31
31
  },
32
32
  exit: {
33
33
  opacity: 0,
34
- transition: g
34
+ transition: K
35
35
  }
36
- }, R = ({
37
- x: o = 0,
38
- y: t = 0
36
+ }, j = ({
37
+ x: t = 0,
38
+ y: e = 0
39
39
  }) => ({
40
- backdrop: C,
40
+ backdrop: Z,
41
41
  content: {
42
- initial: { opacity: 0, x: o, y: t },
42
+ initial: { opacity: 0, x: t, y: e },
43
43
  animate: {
44
44
  opacity: 1,
45
45
  x: 0,
46
46
  y: 0,
47
- transition: M
47
+ transition: G
48
48
  },
49
49
  exit: {
50
50
  opacity: 0,
51
- x: o,
52
- y: t,
53
- transition: g
51
+ x: t,
52
+ y: e,
53
+ transition: K
54
54
  }
55
55
  }
56
- }), et = {
56
+ }), wt = {
57
57
  dialog: () => ({
58
- position: a.CENTER,
58
+ position: u.CENTER,
59
59
  motion: {
60
- backdrop: C,
60
+ backdrop: Z,
61
61
  content: {
62
62
  initial: { opacity: 0, scale: 0.96 },
63
63
  animate: {
64
64
  opacity: 1,
65
65
  scale: 1,
66
- transition: M
66
+ transition: G
67
67
  },
68
68
  exit: {
69
69
  opacity: 0,
70
70
  scale: 0.96,
71
- transition: g
71
+ transition: K
72
72
  }
73
73
  }
74
74
  }
75
75
  }),
76
- drawer: (o = {}) => {
77
- const t = o.side ?? "right";
76
+ drawer: (t = {}) => {
77
+ const e = t.side ?? "right";
78
78
  return {
79
- position: t === "left" ? a.LEFT : a.RIGHT,
80
- motion: R({
81
- x: t === "left" ? "-100%" : "100%"
79
+ position: e === "left" ? u.LEFT : u.RIGHT,
80
+ motion: j({
81
+ x: e === "left" ? "-100%" : "100%"
82
82
  })
83
83
  };
84
84
  },
85
- sheet: (o = {}) => {
86
- const t = o.edge ?? "bottom";
85
+ sheet: (t = {}) => {
86
+ const e = t.edge ?? "bottom";
87
87
  return {
88
- position: t === "top" ? a.TOP : a.BOTTOM,
89
- motion: R({
90
- y: t === "top" ? "-100%" : "100%"
88
+ position: e === "top" ? u.TOP : u.BOTTOM,
89
+ motion: j({
90
+ y: e === "top" ? "-100%" : "100%"
91
91
  })
92
92
  };
93
93
  }
94
- }, N = (...o) => o.filter(Boolean).join(" "), I = (o) => {
95
- switch (o) {
96
- case a.TOP:
94
+ }, z = (...t) => t.filter(Boolean).join(" "), J = (t) => {
95
+ switch (t) {
96
+ case u.TOP:
97
97
  return "react-motion-modal__body--top";
98
- case a.TOP_LEFT:
98
+ case u.TOP_LEFT:
99
99
  return "react-motion-modal__body--top-left";
100
- case a.TOP_RIGHT:
100
+ case u.TOP_RIGHT:
101
101
  return "react-motion-modal__body--top-right";
102
- case a.TOP_CENTER:
102
+ case u.TOP_CENTER:
103
103
  return "react-motion-modal__body--top-center";
104
- case a.BOTTOM:
104
+ case u.BOTTOM:
105
105
  return "react-motion-modal__body--bottom";
106
- case a.BOTTOM_LEFT:
106
+ case u.BOTTOM_LEFT:
107
107
  return "react-motion-modal__body--bottom-left";
108
- case a.BOTTOM_RIGHT:
108
+ case u.BOTTOM_RIGHT:
109
109
  return "react-motion-modal__body--bottom-right";
110
- case a.BOTTOM_CENTER:
110
+ case u.BOTTOM_CENTER:
111
111
  return "react-motion-modal__body--bottom-center";
112
- case a.LEFT:
112
+ case u.LEFT:
113
113
  return "react-motion-modal__body--left";
114
- case a.RIGHT:
114
+ case u.RIGHT:
115
115
  return "react-motion-modal__body--right";
116
- case a.CENTER:
116
+ case u.CENTER:
117
117
  return "react-motion-modal__body--center";
118
- case a.CENTER_FULL:
118
+ case u.CENTER_FULL:
119
119
  return "react-motion-modal__body--center-full";
120
120
  default:
121
- return I(a.CENTER);
121
+ return J(u.CENTER);
122
122
  }
123
- }, k = (o, t) => {
124
- if (!(!o && !t))
123
+ }, V = (t, e) => {
124
+ if (!(!t && !e))
125
125
  return {
126
- ...o,
127
126
  ...t,
127
+ ...e,
128
128
  style: {
129
- ...o?.style,
130
- ...t?.style
129
+ ...t?.style,
130
+ ...e?.style
131
131
  }
132
132
  };
133
- }, L = (...o) => {
134
- const t = {};
135
- for (const r of o) {
136
- const u = k(t.backdrop, r?.backdrop), s = k(t.body, r?.body);
137
- Object.assign(t, r), t.backdrop = u, t.body = s;
133
+ }, Q = (...t) => {
134
+ const e = {};
135
+ for (const o of t) {
136
+ const n = V(e.backdrop, o?.backdrop), r = V(e.body, o?.body);
137
+ Object.assign(e, o), e.backdrop = n, e.body = r;
138
138
  }
139
- return t;
140
- }, w = {
139
+ return e;
140
+ }, q = {
141
141
  initial: { opacity: 1 },
142
142
  animate: { opacity: 1, transition: { duration: 0 } },
143
143
  exit: { opacity: 1, transition: { duration: 0 } }
144
- }, z = ({ data: o, index: t, isActive: r, defaultOptions: u }) => {
145
- const s = D(), i = G(), m = r && i, {
146
- component: c,
147
- defaultOptions: l,
148
- preset: d
149
- } = o.definition, e = L(
150
- u,
151
- d ? {
152
- position: d.position,
153
- motion: d.motion
144
+ }, dt = ({ data: t, index: e, isActive: o, defaultOptions: n }) => {
145
+ const r = nt(), s = it(), l = o && s, {
146
+ component: m,
147
+ defaultOptions: d,
148
+ preset: b
149
+ } = t.definition, a = Q(
150
+ n,
151
+ b ? {
152
+ position: b.position,
153
+ motion: b.motion
154
154
  } : void 0,
155
- l,
156
- o.options
157
- ), n = e.position ?? a.CENTER, f = e.closeOnClickOutside ?? !0, y = s ? w : e.motion?.content, b = s ? w : e.motion?.backdrop;
158
- return /* @__PURE__ */ F(
159
- E.div,
155
+ d,
156
+ t.options
157
+ ), E = a.position ?? u.CENTER, k = a.closeOnClickOutside ?? !0, v = r ? q : a.motion?.content, i = r ? q : a.motion?.backdrop;
158
+ return /* @__PURE__ */ ot(
159
+ A.div,
160
160
  {
161
161
  className: "react-motion-modal__root",
162
162
  initial: !1,
163
163
  exit: {},
164
+ inert: !l,
164
165
  style: {
165
- zIndex: O + t,
166
- pointerEvents: m ? void 0 : "none",
167
- backdropFilter: e.blur ? "blur(8px)" : void 0,
168
- WebkitBackdropFilter: e.blur ? "blur(8px)" : void 0
166
+ zIndex: I + e,
167
+ pointerEvents: l ? void 0 : "none"
169
168
  },
170
169
  children: [
171
- /* @__PURE__ */ _(
172
- E.div,
170
+ /* @__PURE__ */ M(
171
+ A.div,
173
172
  {
174
- className: N(
173
+ className: z(
175
174
  "react-motion-modal__backdrop",
176
- m && "react-motion-modal__active",
177
- e.backdrop?.className
175
+ l && "react-motion-modal__active",
176
+ a.backdrop?.className
178
177
  ),
179
- initial: b?.initial ?? { opacity: 0 },
180
- animate: b?.animate ?? { opacity: 1 },
181
- exit: b?.exit ?? { opacity: 0 },
178
+ initial: i?.initial ?? { opacity: 0 },
179
+ animate: i?.animate ?? { opacity: 1 },
180
+ exit: i?.exit ?? { opacity: 0 },
182
181
  style: {
183
- zIndex: O + t,
184
- ...e.backdrop?.style
182
+ zIndex: I + e,
183
+ // Lives on the backdrop so the blur fades with its opacity.
184
+ backdropFilter: a.blur ? "blur(8px)" : void 0,
185
+ WebkitBackdropFilter: a.blur ? "blur(8px)" : void 0,
186
+ ...a.backdrop?.style
185
187
  },
186
188
  onClick: () => {
187
- f && o.cancel("backdrop");
189
+ k && t.cancel("backdrop");
188
190
  }
189
191
  }
190
192
  ),
191
- /* @__PURE__ */ _(
192
- "div",
193
+ /* @__PURE__ */ M(
194
+ A.div,
193
195
  {
194
196
  role: "dialog",
195
- "aria-modal": m ? "true" : void 0,
196
- "aria-hidden": m ? void 0 : !0,
197
- "aria-label": e.ariaLabelledBy ? void 0 : e.ariaLabel ?? "Modal",
198
- "aria-labelledby": e.ariaLabelledBy,
199
- "aria-describedby": e.ariaDescribedBy,
197
+ initial: v?.initial,
198
+ animate: v?.animate,
199
+ exit: v?.exit,
200
+ "aria-modal": l ? "true" : void 0,
201
+ "aria-hidden": l ? void 0 : !0,
202
+ "aria-label": a.ariaLabelledBy ? void 0 : a.ariaLabel ?? "Modal",
203
+ "aria-labelledby": a.ariaLabelledBy,
204
+ "aria-describedby": a.ariaDescribedBy,
200
205
  tabIndex: -1,
201
- "data-modal-id": o.id,
202
- className: N(
206
+ "data-modal-id": t.id,
207
+ className: z(
203
208
  "react-motion-modal__body",
204
- I(n),
205
- m && "react-motion-modal__body--active",
206
- e.body?.className
209
+ J(E),
210
+ l && "react-motion-modal__body--active",
211
+ a.body?.className
207
212
  ),
208
213
  style: {
209
- ...e.body?.style,
210
- zIndex: O + t + 1
214
+ // Keep dialog scrolling from chaining to the locked page.
215
+ overscrollBehavior: "contain",
216
+ ...a.body?.style,
217
+ zIndex: I + e + 1
211
218
  },
212
- onClick: (T) => T.stopPropagation(),
213
- children: /* @__PURE__ */ _(
214
- E.div,
219
+ onClick: (g) => g.stopPropagation(),
220
+ children: /* @__PURE__ */ M("div", { className: "react-motion-modal__content", children: /* @__PURE__ */ M(
221
+ m,
215
222
  {
216
- className: "react-motion-modal__content",
217
- initial: y?.initial,
218
- animate: y?.animate,
219
- exit: y?.exit,
220
- children: /* @__PURE__ */ _(
221
- c,
222
- {
223
- input: o.input,
224
- resolve: o.resolve,
225
- cancel: (T) => o.cancel(T)
226
- }
227
- )
223
+ input: t.input,
224
+ resolve: t.resolve,
225
+ cancel: (g) => t.cancel(g)
228
226
  }
229
- )
227
+ ) })
230
228
  }
231
229
  )
232
230
  ]
233
231
  }
234
232
  );
235
- }, q = [
233
+ }, H = /* @__PURE__ */ new WeakMap(), P = (t) => {
234
+ let e = H.get(t);
235
+ return e || (e = { scrollLock: null, keyboardOwners: [], layerEpoch: 0 }, H.set(t, e)), e;
236
+ }, ut = (t) => {
237
+ const e = t.documentElement, o = e.offsetWidth;
238
+ t.body.style.overflow = "hidden";
239
+ const n = e.offsetWidth - o, r = t.defaultView;
240
+ if (n <= 0 || !r)
241
+ return null;
242
+ const s = e.style.paddingRight, l = Number.parseFloat(r.getComputedStyle(e).paddingRight) || 0;
243
+ return e.style.paddingRight = `${l + n}px`, s;
244
+ }, ft = (t, e) => {
245
+ const o = P(t);
246
+ if (!o.scrollLock) {
247
+ const n = {
248
+ overflow: t.body.style.overflow,
249
+ overscrollBehavior: t.body.style.overscrollBehavior
250
+ };
251
+ o.scrollLock = {
252
+ tokens: /* @__PURE__ */ new Set([e]),
253
+ initialStyles: n,
254
+ initialRootPadding: ut(t)
255
+ }, t.body.style.overscrollBehavior = "none";
256
+ return;
257
+ }
258
+ o.scrollLock.tokens.add(e);
259
+ }, pt = (t, e) => {
260
+ const o = P(t), { scrollLock: n } = o;
261
+ n?.tokens.has(e) && (n.tokens.delete(e), n.tokens.size === 0 && (o.scrollLock = null, t.body.style.overflow = n.initialStyles.overflow, t.body.style.overscrollBehavior = n.initialStyles.overscrollBehavior, n.initialRootPadding !== null && (t.documentElement.style.paddingRight = n.initialRootPadding)));
262
+ }, U = (t, e) => {
263
+ const o = P(t), n = o.keyboardOwners, r = n[n.length - 1];
264
+ if (r?.token === e)
265
+ return r.layer;
266
+ const s = ++o.layerEpoch;
267
+ return o.keyboardOwners = [
268
+ ...n.filter((l) => l.token !== e),
269
+ { token: e, layer: s }
270
+ ], s;
271
+ }, mt = (t, e) => {
272
+ const o = P(t);
273
+ o.keyboardOwners = o.keyboardOwners.filter(
274
+ (n) => n.token !== e
275
+ ), o.keyboardOwners.length === 0 && (o.layerEpoch = 0);
276
+ }, D = (t, e) => H.get(t)?.keyboardOwners.at(-1)?.token === e, yt = [
236
277
  "a[href]",
237
278
  "button:not([disabled])",
238
279
  "textarea:not([disabled])",
239
- "input:not([disabled])",
280
+ 'input:not([disabled]):not([type="hidden"])',
240
281
  "select:not([disabled])",
241
282
  '[tabindex]:not([tabindex="-1"])'
242
- ].join(","), S = (o) => o ? Array.from(
243
- o.querySelectorAll(q)
244
- ).filter((t) => t.getClientRects().length > 0) : [], W = (o, t) => {
245
- if (!o || t.key !== "Tab")
283
+ ].join(","), bt = (t) => t.closest("[inert]") ? !1 : typeof t.checkVisibility == "function" ? t.checkVisibility({ checkVisibilityCSS: !0 }) : t.getClientRects().length > 0, N = (t) => t ? Array.from(
284
+ t.querySelectorAll(yt)
285
+ ).filter(bt) : [], vt = (t, e) => {
286
+ if (!t || e.key !== "Tab")
246
287
  return;
247
- const r = S(o), u = r[0], s = r[r.length - 1], i = document.activeElement;
248
- if (!u || !s) {
249
- t.preventDefault(), o.focus();
288
+ const o = N(t), n = o[0], r = o[o.length - 1], s = document.activeElement;
289
+ if (!n || !r) {
290
+ e.preventDefault(), t.focus();
250
291
  return;
251
292
  }
252
- if (!i || !o.contains(i)) {
253
- t.preventDefault(), (t.shiftKey ? s : u).focus();
293
+ if (!s || !t.contains(s)) {
294
+ e.preventDefault(), (e.shiftKey ? r : n).focus();
254
295
  return;
255
296
  }
256
- t.shiftKey && i === u ? (t.preventDefault(), s.focus()) : !t.shiftKey && i === s && (t.preventDefault(), u.focus());
257
- }, P = /* @__PURE__ */ new WeakMap(), x = (o) => {
258
- const t = P.get(o);
259
- if (!t)
297
+ e.shiftKey && s === n ? (e.preventDefault(), r.focus()) : !e.shiftKey && s === r && (e.preventDefault(), n.focus());
298
+ }, Y = /* @__PURE__ */ new WeakMap(), tt = (t) => {
299
+ const e = Y.get(t);
300
+ if (!e)
260
301
  throw new Error("Invalid modal system instance.");
261
- return t;
262
- }, X = (o) => x(o).store, Z = (o) => {
263
- const t = x(o);
264
- t.providerCount += 1;
265
- let r = !0;
302
+ return e;
303
+ }, ht = (t) => tt(t).store, gt = (t) => {
304
+ const e = tt(t);
305
+ e.providerCount += 1;
306
+ let o = !0;
266
307
  return () => {
267
- r && (r = !1, t.providerCount -= 1, queueMicrotask(() => {
268
- t.providerCount === 0 && o.cancelAll("provider-unmounted");
308
+ o && (o = !1, e.providerCount -= 1, queueMicrotask(() => {
309
+ e.providerCount === 0 && t.cancelAll("provider-unmounted");
269
310
  }));
270
311
  };
271
- }, nt = (o) => {
272
- const t = U(() => ({
312
+ }, Lt = (t) => {
313
+ const e = lt(() => ({
273
314
  activeModals: []
274
315
  }));
275
- let r = 0;
276
- const u = (c) => {
277
- t.setState((l) => ({
278
- activeModals: l.activeModals.filter((d) => d.id !== c)
316
+ let o = 0;
317
+ const n = (m) => {
318
+ e.setState((d) => ({
319
+ activeModals: d.activeModals.filter((b) => b.id !== m)
279
320
  }));
280
- }, s = (c = "programmatic") => {
281
- const l = t.getState().activeModals;
282
- l[l.length - 1]?.cancel(c);
283
- }, m = {
284
- open(c, l, d) {
285
- const e = o[c];
286
- if (!e)
287
- throw new Error(`Unknown modal "${c}".`);
288
- if (e.policy === "singleton") {
289
- const p = t.getState().activeModals.find((A) => A.name === c);
321
+ }, r = (m = "programmatic") => {
322
+ const d = e.getState().activeModals;
323
+ d[d.length - 1]?.cancel(m);
324
+ }, l = {
325
+ open(m, d, b) {
326
+ const a = t[m];
327
+ if (!a)
328
+ throw new Error(`Unknown modal "${m}".`);
329
+ if (a.policy === "singleton") {
330
+ const p = e.getState().activeModals.find((c) => c.name === m);
290
331
  if (p)
291
332
  return p.promise;
292
333
  }
293
- e.policy === "replace" && s("replaced");
294
- const n = `${c}-${++r}`;
295
- let f = !1, y;
296
- const b = new Promise((p) => {
297
- y = p;
298
- }), T = (p) => {
299
- f || (f = !0, u(n), y?.(p));
300
- }, B = {
301
- id: n,
302
- name: c,
303
- input: l,
304
- definition: e,
305
- options: d,
306
- promise: b,
334
+ a.policy === "replace" && r("replaced");
335
+ const E = `${m}-${++o}`;
336
+ let k = !1, v;
337
+ const i = new Promise((p) => {
338
+ v = p;
339
+ }), g = (p) => {
340
+ k || (k = !0, n(E), v?.(p));
341
+ }, S = {
342
+ id: E,
343
+ name: m,
344
+ input: d,
345
+ definition: a,
346
+ options: b,
347
+ promise: i,
307
348
  resolve(p) {
308
- T({ status: "resolved", value: p });
349
+ g({ status: "resolved", value: p });
309
350
  },
310
351
  cancel(p = "programmatic") {
311
- T({ status: "cancelled", reason: p });
352
+ g({ status: "cancelled", reason: p });
312
353
  }
313
354
  };
314
- return t.setState((p) => ({
315
- activeModals: [...p.activeModals, B]
316
- })), b;
355
+ return e.setState((p) => ({
356
+ activeModals: [...p.activeModals, S]
357
+ })), i;
317
358
  },
318
- cancel: s,
319
- cancelAll: (c = "programmatic") => {
320
- const l = [...t.getState().activeModals].reverse();
321
- for (const d of l)
322
- d.cancel(c);
359
+ cancel: r,
360
+ cancelAll: (m = "programmatic") => {
361
+ const d = [...e.getState().activeModals].reverse();
362
+ for (const b of d)
363
+ b.cancel(m);
323
364
  }
324
365
  };
325
- return P.set(m, {
326
- store: t,
366
+ return Y.set(l, {
367
+ store: e,
327
368
  providerCount: 0
328
- }), m;
329
- }, it = ({
330
- system: o,
331
- defaultOptions: t,
332
- portalRoot: r
369
+ }), l;
370
+ }, X = (t, e) => {
371
+ t && (t.style.position = "relative", t.style.zIndex = String(I + e));
372
+ }, Rt = ({
373
+ system: t,
374
+ defaultOptions: e,
375
+ portalRoot: o
333
376
  }) => {
334
- const u = X(o), s = K(u, (n) => n.activeModals), i = s[s.length - 1], [m, c] = j(
335
- r ?? null
336
- ), l = h(null), d = h(null), e = s.length > 0;
337
- return v(() => {
338
- c(r ?? document.body);
339
- }, [r]), v(() => Z(o), [o]), v(() => {
340
- const n = () => o.cancelAll("programmatic");
341
- return window.addEventListener("popstate", n), () => window.removeEventListener("popstate", n);
342
- }, [o]), v(() => {
343
- if (!e)
377
+ const n = ht(t), r = ct(n, (c) => c.activeModals), s = r[r.length - 1], [l, m] = $(
378
+ o ?? null
379
+ ), d = R(null), b = R({}), a = R(null), E = R(/* @__PURE__ */ new Map()), k = R([]), v = r.length > 0, i = l?.ownerDocument ?? null, [g, S] = $(v);
380
+ v && !g && S(!0);
381
+ const p = st(() => {
382
+ n.getState().activeModals.length === 0 && S(!1);
383
+ }, [n]);
384
+ return O(() => {
385
+ l && p();
386
+ }, [l, p]), O(() => {
387
+ m(o ?? document.body);
388
+ }, [o]), O(() => gt(t), [t]), O(() => {
389
+ const c = () => t.cancelAll("programmatic");
390
+ return window.addEventListener("popstate", c), () => window.removeEventListener("popstate", c);
391
+ }, [t]), O(() => {
392
+ if (!i || !v)
393
+ return;
394
+ const c = b.current;
395
+ return X(d.current, U(i, c)), () => mt(i, c);
396
+ }, [i, v]), O(() => {
397
+ if (!i || !v)
398
+ return;
399
+ const c = b.current, h = i.defaultView;
400
+ if (!h)
344
401
  return;
345
- const f = L(
346
- t,
347
- i?.definition.preset ? {
348
- position: i.definition.preset.position,
349
- motion: i.definition.preset.motion
402
+ const x = Q(
403
+ e,
404
+ s?.definition.preset ? {
405
+ position: s.definition.preset.position,
406
+ motion: s.definition.preset.motion
350
407
  } : void 0,
351
- i?.definition.defaultOptions,
352
- i?.options
353
- ).closeOnPressEsc ?? !0, y = (b) => {
354
- if (b.key === "Escape" && f) {
355
- i?.cancel("escape");
408
+ s?.definition.defaultOptions,
409
+ s?.options
410
+ ).closeOnPressEsc ?? !0, _ = (y) => d.current?.querySelector(
411
+ `[data-modal-id="${y}"]`
412
+ ), f = (y) => {
413
+ if (D(i, c)) {
414
+ if (y.key === "Escape" && x) {
415
+ s?.cancel("escape");
416
+ return;
417
+ }
418
+ vt(_(s?.id ?? ""), y);
419
+ }
420
+ }, T = (y) => {
421
+ const L = d.current, C = y.target;
422
+ if (!L)
423
+ return;
424
+ if (C && L.contains(C)) {
425
+ a.current = C instanceof HTMLElement ? C : null;
356
426
  return;
357
427
  }
358
- const T = i ? l.current?.querySelector(
359
- `[data-modal-id="${i.id}"]`
360
- ) : void 0;
361
- W(T, b);
428
+ if (!D(i, c) || !s)
429
+ return;
430
+ const W = _(s.id), et = [
431
+ a.current,
432
+ ...N(W)
433
+ ];
434
+ for (const F of et)
435
+ if (F?.isConnected && (F.focus(), i.activeElement === F))
436
+ return;
437
+ W?.focus();
438
+ }, w = (y) => {
439
+ if (!D(i, c) || !y.cancelable)
440
+ return;
441
+ const L = d.current;
442
+ L && y.composedPath().includes(L) || y.preventDefault();
362
443
  };
363
- return window.addEventListener("keydown", y), () => window.removeEventListener("keydown", y);
364
- }, [i, t, e]), v(() => {
365
- if (!e)
366
- return;
367
- const { body: n } = document, f = n.style.overflow;
368
- return n.style.overflow = "hidden", () => {
369
- n.style.overflow = f;
444
+ return h.addEventListener("keydown", f), i.addEventListener("focusin", T), i.addEventListener("touchmove", w, { passive: !1 }), () => {
445
+ h.removeEventListener("keydown", f), i.removeEventListener("focusin", T), i.removeEventListener("touchmove", w);
370
446
  };
371
- }, [e]), v(() => {
372
- if (!m)
447
+ }, [s, e, i, v]), O(() => {
448
+ if (!i || !g)
449
+ return;
450
+ const c = b.current;
451
+ return ft(i, c), () => pt(i, c);
452
+ }, [i, g]), O(() => {
453
+ if (!i || !l)
373
454
  return;
374
- if (!e) {
375
- d.current?.focus?.(), d.current = null;
455
+ const c = b.current, h = r.map((f) => f.id), B = k.current, x = h.filter((f) => !B.includes(f)), _ = B.filter((f) => !h.includes(f));
456
+ for (const f of r)
457
+ E.current.has(f.id) || E.current.set(
458
+ f.id,
459
+ i.activeElement ?? null
460
+ );
461
+ if (k.current = h, x.length > 0) {
462
+ X(d.current, U(i, c));
463
+ const f = h[h.length - 1];
464
+ if (f) {
465
+ const T = d.current?.querySelector(
466
+ `[data-modal-id="${f}"]`
467
+ );
468
+ (N(T)[0] ?? T)?.focus();
469
+ }
376
470
  return;
377
471
  }
378
- d.current === null && (d.current = document.activeElement ?? null);
379
- const n = i ? l.current?.querySelector(
380
- `[data-modal-id="${i.id}"]`
381
- ) : void 0;
382
- n && (S(n)[0] ?? n).focus();
383
- }, [i, e, m]), m ? $(
384
- /* @__PURE__ */ _("div", { className: "react-motion-modal", ref: l, children: /* @__PURE__ */ _(H, { initial: !0, mode: "sync", children: s.map((n, f) => /* @__PURE__ */ _(
385
- z,
472
+ if (_.length > 0) {
473
+ const f = _[_.length - 1], T = f ? E.current.get(f) ?? null : null;
474
+ for (const y of _)
475
+ E.current.delete(y);
476
+ if (T?.isConnected && (T.focus(), i.activeElement === T))
477
+ return;
478
+ const w = r[r.length - 1];
479
+ if (w) {
480
+ const y = d.current?.querySelector(
481
+ `[data-modal-id="${w.id}"]`
482
+ );
483
+ (N(y)[0] ?? y)?.focus();
484
+ }
485
+ }
486
+ }, [r, i, l]), l ? at(
487
+ /* @__PURE__ */ M("div", { className: "react-motion-modal", ref: d, children: /* @__PURE__ */ M(
488
+ rt,
386
489
  {
387
- data: n,
388
- index: f,
389
- isActive: i?.id === n.id,
390
- defaultOptions: t
391
- },
392
- n.id
393
- )) }) }),
394
- m
490
+ initial: !0,
491
+ mode: "sync",
492
+ onExitComplete: p,
493
+ children: r.map((c, h) => /* @__PURE__ */ M(
494
+ dt,
495
+ {
496
+ data: c,
497
+ index: h,
498
+ isActive: s?.id === c.id,
499
+ defaultOptions: e
500
+ },
501
+ c.id
502
+ ))
503
+ }
504
+ ) }),
505
+ l
395
506
  ) : null;
396
- }, at = (o) => o;
507
+ }, St = (t) => t;
397
508
  export {
398
- a as MODAL_POSITIONS,
399
- O as MODAL_Z_INDEX,
400
- it as ModalProvider,
401
- nt as createModalSystem,
402
- at as defineModal,
403
- et as motionPresets
509
+ u as MODAL_POSITIONS,
510
+ I as MODAL_Z_INDEX,
511
+ Rt as ModalProvider,
512
+ Lt as createModalSystem,
513
+ St as defineModal,
514
+ wt as motionPresets
404
515
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-motion-modal",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "type": "module",
5
5
  "sideEffects": [
6
6
  "**/*.css"