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