react-motion-modal 1.4.0 → 2.0.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.js CHANGED
@@ -1,9 +1,10 @@
1
- import { jsxs as g, jsx as f } from "react/jsx-runtime";
2
- import { create as A } from "zustand/react";
3
- import { motion as I, AnimatePresence as F } from "motion/react";
4
- import { useState as P, useRef as h, useEffect as y } from "react";
5
- import { createPortal as k } from "react-dom";
6
- const t = {
1
+ import { jsxs as A, 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 = {
7
8
  TOP: "top",
8
9
  TOP_LEFT: "top-left",
9
10
  TOP_RIGHT: "top-right",
@@ -16,218 +17,214 @@ const t = {
16
17
  BOTTOM_LEFT: "bottom-left",
17
18
  BOTTOM_RIGHT: "bottom-right",
18
19
  BOTTOM_CENTER: "bottom-center"
19
- }, M = 1e3, w = {
20
+ }, O = 1e3, M = {
20
21
  duration: 0.24,
21
22
  ease: "easeOut"
22
- }, N = {
23
+ }, g = {
23
24
  duration: 0.18,
24
25
  ease: "easeIn"
25
- }, R = {
26
- opacity: 1,
27
- scale: 1,
28
- x: 0,
29
- y: 0,
30
- transition: w
31
- }, D = () => `modal-${Date.now()}-${Math.random().toString(36).slice(2)}`, E = A((e, a) => {
32
- const s = (o) => {
33
- const n = [...a().modals];
34
- if (o < 0 || o >= n.length)
35
- return;
36
- const [c] = n.splice(o, 1);
37
- e({
38
- modals: n,
39
- active: n[n.length - 1]
40
- }), c?.params?.onClose?.();
41
- };
42
- return {
43
- modals: [],
44
- openModal(o, n) {
45
- const c = D(), m = {
46
- ...n,
47
- closeModal: () => {
48
- const r = a().modals.findIndex((i) => i.id === c);
49
- s(r);
50
- }
51
- }, u = {
52
- id: c,
53
- name: o,
54
- params: m
55
- }, d = [...a().modals, u];
56
- e({
57
- modals: d,
58
- active: u
59
- });
60
- },
61
- closeAllModal() {
62
- const o = a().modals;
63
- e({
64
- modals: [],
65
- active: void 0
66
- });
67
- for (const n of [...o].reverse())
68
- n.params?.onClose?.();
26
+ }, I = {
27
+ initial: { opacity: 0 },
28
+ animate: {
29
+ opacity: 1,
30
+ transition: M
31
+ },
32
+ exit: {
33
+ opacity: 0,
34
+ transition: g
35
+ }
36
+ }, R = ({
37
+ x: o = 0,
38
+ y: t = 0
39
+ }) => ({
40
+ backdrop: I,
41
+ content: {
42
+ initial: { opacity: 0, x: o, y: t },
43
+ animate: {
44
+ opacity: 1,
45
+ x: 0,
46
+ y: 0,
47
+ transition: M
69
48
  },
70
- closeModal(o) {
71
- const n = a().modals, c = o ? n.findIndex((l) => l.name === o) : n.length - 1;
72
- s(c);
49
+ exit: {
50
+ opacity: 0,
51
+ x: o,
52
+ y: t,
53
+ transition: g
73
54
  }
74
- };
75
- }), L = (...e) => e.filter(Boolean).join(" "), T = (e, a) => {
76
- const s = {
77
- opacity: 0,
78
- x: e,
79
- y: a,
80
- transition: N
81
- };
82
- return {
83
- initial: s,
84
- animate: R,
85
- exit: s
86
- };
87
- }, C = (e) => {
88
- switch (e) {
89
- case t.LEFT:
90
- return T("-100%", 0);
91
- case t.RIGHT:
92
- return T("100%", 0);
93
- case t.TOP:
94
- case t.TOP_CENTER:
95
- return T(0, "-100%");
96
- case t.BOTTOM:
97
- case t.BOTTOM_CENTER:
98
- return T(0, "100%");
99
- case t.TOP_LEFT:
100
- return T("-100%", "-100%");
101
- case t.TOP_RIGHT:
102
- return T("100%", "-100%");
103
- case t.BOTTOM_LEFT:
104
- return T("-100%", "100%");
105
- case t.BOTTOM_RIGHT:
106
- return T("100%", "100%");
107
- case t.CENTER:
108
- return {
109
- initial: {
110
- opacity: 0,
111
- scale: 0.96
55
+ }
56
+ }), et = {
57
+ dialog: () => ({
58
+ position: a.CENTER,
59
+ motion: {
60
+ backdrop: I,
61
+ content: {
62
+ initial: { opacity: 0, scale: 0.96 },
63
+ animate: {
64
+ opacity: 1,
65
+ scale: 1,
66
+ transition: M
112
67
  },
113
- animate: R,
114
68
  exit: {
115
69
  opacity: 0,
116
70
  scale: 0.96,
117
- transition: N
71
+ transition: g
118
72
  }
119
- };
120
- case t.CENTER_FULL:
121
- return {
122
- initial: {
123
- opacity: 0
124
- },
125
- animate: R,
126
- exit: {
127
- opacity: 0,
128
- transition: N
129
- }
130
- };
131
- default:
132
- return C(t.CENTER);
73
+ }
74
+ }
75
+ }),
76
+ drawer: (o = {}) => {
77
+ const t = o.side ?? "right";
78
+ return {
79
+ position: t === "left" ? a.LEFT : a.RIGHT,
80
+ motion: R({
81
+ x: t === "left" ? "-100%" : "100%"
82
+ })
83
+ };
84
+ },
85
+ sheet: (o = {}) => {
86
+ const t = o.edge ?? "bottom";
87
+ return {
88
+ position: t === "top" ? a.TOP : a.BOTTOM,
89
+ motion: R({
90
+ y: t === "top" ? "-100%" : "100%"
91
+ })
92
+ };
133
93
  }
134
- }, B = (e) => {
135
- switch (e) {
136
- case t.TOP:
94
+ }, N = (...o) => o.filter(Boolean).join(" "), S = (o) => {
95
+ switch (o) {
96
+ case a.TOP:
137
97
  return "react-motion-modal__body--top";
138
- case t.TOP_LEFT:
98
+ case a.TOP_LEFT:
139
99
  return "react-motion-modal__body--top-left";
140
- case t.TOP_RIGHT:
100
+ case a.TOP_RIGHT:
141
101
  return "react-motion-modal__body--top-right";
142
- case t.TOP_CENTER:
102
+ case a.TOP_CENTER:
143
103
  return "react-motion-modal__body--top-center";
144
- case t.BOTTOM:
104
+ case a.BOTTOM:
145
105
  return "react-motion-modal__body--bottom";
146
- case t.BOTTOM_LEFT:
106
+ case a.BOTTOM_LEFT:
147
107
  return "react-motion-modal__body--bottom-left";
148
- case t.BOTTOM_RIGHT:
108
+ case a.BOTTOM_RIGHT:
149
109
  return "react-motion-modal__body--bottom-right";
150
- case t.BOTTOM_CENTER:
110
+ case a.BOTTOM_CENTER:
151
111
  return "react-motion-modal__body--bottom-center";
152
- case t.LEFT:
112
+ case a.LEFT:
153
113
  return "react-motion-modal__body--left";
154
- case t.RIGHT:
114
+ case a.RIGHT:
155
115
  return "react-motion-modal__body--right";
156
- case t.CENTER:
116
+ case a.CENTER:
157
117
  return "react-motion-modal__body--center";
158
- case t.CENTER_FULL:
118
+ case a.CENTER_FULL:
159
119
  return "react-motion-modal__body--center-full";
160
120
  default:
161
- return B(t.CENTER);
121
+ return S(a.CENTER);
122
+ }
123
+ }, k = (o, t) => {
124
+ if (!(!o && !t))
125
+ return {
126
+ ...o,
127
+ ...t,
128
+ style: {
129
+ ...o?.style,
130
+ ...t?.style
131
+ }
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;
162
138
  }
163
- }, S = ({ data: e, modals: a, index: s, initialParams: o }) => {
164
- const n = a[e.name], c = e.params, l = {
165
- ...o,
166
- ...c
167
- }, m = l?.animate ?? o?.animate, u = l?.blur ?? o?.blur ?? !0, d = l?.backdrop ?? o?.backdrop, r = l?.body ?? o?.body, i = l?.position ?? o?.position ?? t.CENTER, O = l?.closeOnClickOutside ?? o?.closeOnClickOutside ?? !0, b = m ? void 0 : C(i), _ = E((v) => v.active)?.id === e.id;
168
- return /* @__PURE__ */ g(
169
- I.div,
139
+ return t;
140
+ }, C = {
141
+ initial: { opacity: 1 },
142
+ animate: { opacity: 1, transition: { duration: 0 } },
143
+ exit: { opacity: 1, transition: { duration: 0 } }
144
+ }, z = ({ data: o, index: t, isActive: r, defaultOptions: u }) => {
145
+ const s = D(), n = G(), m = r && n, {
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
154
+ } : void 0,
155
+ l,
156
+ o.options
157
+ ), i = e.position ?? a.CENTER, f = e.closeOnClickOutside ?? !0, y = s ? C : e.motion?.content, b = s ? C : e.motion?.backdrop;
158
+ return /* @__PURE__ */ A(
159
+ E.div,
170
160
  {
171
161
  className: "react-motion-modal__root",
172
- exit: m?.exit ?? { opacity: 0 },
173
- initial: m?.exit ?? { opacity: 0 },
174
- animate: m?.animate ?? { opacity: 1 },
175
- layout: !0,
162
+ initial: !1,
163
+ exit: {},
176
164
  style: {
177
- zIndex: M + s,
178
- pointerEvents: _ ? void 0 : "none",
179
- backdropFilter: u ? "blur(8px)" : void 0,
180
- WebkitBackdropFilter: u ? "blur(8px)" : void 0
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
181
169
  },
182
170
  children: [
183
- /* @__PURE__ */ f(
184
- "div",
171
+ /* @__PURE__ */ _(
172
+ E.div,
185
173
  {
186
- className: L(
174
+ className: N(
187
175
  "react-motion-modal__backdrop",
188
- _ && "react-motion-modal__active",
189
- d?.className && "react-motion-modal__backdrop--custom",
190
- d?.className
176
+ m && "react-motion-modal__active",
177
+ e.backdrop?.className
191
178
  ),
179
+ initial: b?.initial ?? { opacity: 0 },
180
+ animate: b?.animate ?? { opacity: 1 },
181
+ exit: b?.exit ?? { opacity: 0 },
192
182
  style: {
193
- zIndex: M + s,
194
- ...d?.style
183
+ zIndex: O + t,
184
+ ...e.backdrop?.style
195
185
  },
196
186
  onClick: () => {
197
- O && l.closeModal();
187
+ f && o.cancel("backdrop");
198
188
  }
199
189
  }
200
190
  ),
201
- /* @__PURE__ */ f(
191
+ /* @__PURE__ */ _(
202
192
  "div",
203
193
  {
204
194
  role: "dialog",
205
- "aria-modal": _ ? "true" : void 0,
206
- "aria-hidden": _ ? void 0 : !0,
207
- "aria-label": l.ariaLabelledBy ? void 0 : l.ariaLabel ?? "Modal",
208
- "aria-labelledby": l.ariaLabelledBy,
209
- "aria-describedby": l.ariaDescribedBy,
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,
210
200
  tabIndex: -1,
211
- "data-modal-id": e.id,
212
- className: L(
201
+ "data-modal-id": o.id,
202
+ className: N(
213
203
  "react-motion-modal__body",
214
- B(i),
215
- _ && "react-motion-modal__body--active",
216
- r?.className
204
+ S(i),
205
+ m && "react-motion-modal__body--active",
206
+ e.body?.className
217
207
  ),
218
208
  style: {
219
- ...r?.style,
220
- zIndex: M + s + 1
209
+ ...e.body?.style,
210
+ zIndex: O + t + 1
221
211
  },
222
- onClick: (v) => v.stopPropagation(),
223
- children: /* @__PURE__ */ f(
224
- I.div,
212
+ onClick: (T) => T.stopPropagation(),
213
+ children: /* @__PURE__ */ _(
214
+ E.div,
225
215
  {
226
216
  className: "react-motion-modal__content",
227
- initial: b?.initial,
228
- animate: b?.animate,
229
- exit: b?.exit,
230
- children: /* @__PURE__ */ f(n, { ...l })
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
+ )
231
228
  }
232
229
  )
233
230
  }
@@ -235,98 +232,170 @@ const t = {
235
232
  ]
236
233
  }
237
234
  );
238
- }, G = [
235
+ }, q = [
239
236
  "a[href]",
240
237
  "button:not([disabled])",
241
238
  "textarea:not([disabled])",
242
239
  "input:not([disabled])",
243
240
  "select:not([disabled])",
244
241
  '[tabindex]:not([tabindex="-1"])'
245
- ].join(","), x = (e) => e ? Array.from(
246
- e.querySelectorAll(G)
247
- ).filter((a) => a.getClientRects().length > 0) : [], H = (e, a) => {
248
- if (!e || a.key !== "Tab")
242
+ ].join(","), P = (o) => o ? Array.from(
243
+ o.querySelectorAll(q)
244
+ ).filter((t) => t.getClientRects().length > 0) : [], W = (o, t) => {
245
+ if (!o || t.key !== "Tab")
249
246
  return;
250
- const s = x(e), o = s[0], n = s[s.length - 1], c = document.activeElement;
251
- if (!o || !n) {
252
- a.preventDefault(), e.focus();
247
+ const r = P(o), u = r[0], s = r[r.length - 1], n = document.activeElement;
248
+ if (!u || !s) {
249
+ t.preventDefault(), o.focus();
253
250
  return;
254
251
  }
255
- if (!c || !e.contains(c)) {
256
- a.preventDefault(), (a.shiftKey ? n : o).focus();
252
+ if (!n || !o.contains(n)) {
253
+ t.preventDefault(), (t.shiftKey ? s : u).focus();
257
254
  return;
258
255
  }
259
- a.shiftKey && c === o ? (a.preventDefault(), n.focus()) : !a.shiftKey && c === n && (a.preventDefault(), o.focus());
260
- }, z = ({
261
- modals: e,
262
- initialParams: a
256
+ t.shiftKey && n === u ? (t.preventDefault(), s.focus()) : !t.shiftKey && n === s && (t.preventDefault(), u.focus());
257
+ }, w = /* @__PURE__ */ new WeakMap(), x = (o) => {
258
+ const t = w.get(o);
259
+ if (!t)
260
+ 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;
266
+ return () => {
267
+ r && (r = !1, t.providerCount -= 1, queueMicrotask(() => {
268
+ t.providerCount === 0 && o.cancelAll("provider-unmounted");
269
+ }));
270
+ };
271
+ }, nt = (o) => {
272
+ const t = U(() => ({
273
+ activeModals: []
274
+ }));
275
+ let r = 0;
276
+ const u = (c) => {
277
+ t.setState((l) => ({
278
+ activeModals: l.activeModals.filter((d) => d.id !== c)
279
+ }));
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((F) => F.name === c);
290
+ if (p)
291
+ return p.promise;
292
+ }
293
+ e.policy === "replace" && s("replaced");
294
+ const i = `${c}-${++r}`;
295
+ let f = !1, y;
296
+ const b = new Promise((p) => {
297
+ y = p;
298
+ }), T = (p) => {
299
+ f || (f = !0, u(i), y?.(p));
300
+ }, B = {
301
+ id: i,
302
+ name: c,
303
+ input: l,
304
+ definition: e,
305
+ options: d,
306
+ promise: b,
307
+ resolve(p) {
308
+ T({ status: "resolved", value: p });
309
+ },
310
+ cancel(p = "programmatic") {
311
+ T({ status: "cancelled", reason: p });
312
+ }
313
+ };
314
+ return t.setState((p) => ({
315
+ activeModals: [...p.activeModals, B]
316
+ })), b;
317
+ },
318
+ cancel: s,
319
+ cancelAll: (c = "programmatic") => {
320
+ const l = [...t.getState().activeModals].reverse();
321
+ for (const d of l)
322
+ d.cancel(c);
323
+ }
324
+ };
325
+ return w.set(m, {
326
+ store: t,
327
+ providerCount: 0
328
+ }), m;
329
+ }, it = ({
330
+ system: o,
331
+ defaultOptions: t,
332
+ portalRoot: r
263
333
  }) => {
264
- const s = E((r) => r.modals), o = E((r) => r.active), n = E((r) => r.closeModal), [c, l] = P(null), m = h(null), u = h(null);
265
- y(() => {
266
- l(document.body);
267
- }, []);
268
- const d = s.length > 0;
269
- return y(() => {
270
- const i = s[s.length - 1]?.params?.closeOnPressEsc ?? a?.closeOnPressEsc ?? !0;
271
- if (!d)
334
+ const u = X(o), s = K(u, (i) => i.activeModals), n = 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
+ if (!e)
272
341
  return;
273
- const O = (b) => {
274
- if (b.key === "Escape" && i) {
275
- n();
342
+ const f = L(
343
+ t,
344
+ n?.definition.preset ? {
345
+ position: n.definition.preset.position,
346
+ motion: n.definition.preset.motion
347
+ } : void 0,
348
+ n?.definition.defaultOptions,
349
+ n?.options
350
+ ).closeOnPressEsc ?? !0, y = (b) => {
351
+ if (b.key === "Escape" && f) {
352
+ n?.cancel("escape");
276
353
  return;
277
354
  }
278
- const p = o?.id, _ = p ? m.current?.querySelector(
279
- `[data-modal-id="${p}"]`
355
+ const T = n ? l.current?.querySelector(
356
+ `[data-modal-id="${n.id}"]`
280
357
  ) : void 0;
281
- H(_, b);
358
+ W(T, b);
282
359
  };
283
- return window.addEventListener("keydown", O), () => window.removeEventListener("keydown", O);
284
- }, [
285
- s,
286
- o?.id,
287
- n,
288
- a?.closeOnPressEsc,
289
- d
290
- ]), y(() => {
291
- if (!d)
360
+ return window.addEventListener("keydown", y), () => window.removeEventListener("keydown", y);
361
+ }, [n, t, e]), v(() => {
362
+ if (!e)
292
363
  return;
293
- const { body: r } = document, i = r.style.overflow;
294
- return r.style.overflow = "hidden", () => {
295
- r.style.overflow = i;
364
+ const { body: i } = document, f = i.style.overflow;
365
+ return i.style.overflow = "hidden", () => {
366
+ i.style.overflow = f;
296
367
  };
297
- }, [d]), y(() => {
298
- if (!c)
368
+ }, [e]), v(() => {
369
+ if (!m)
299
370
  return;
300
- const r = o?.id;
301
- if (!d) {
302
- u.current?.focus?.(), u.current = null;
371
+ if (!e) {
372
+ d.current?.focus?.(), d.current = null;
303
373
  return;
304
374
  }
305
- u.current === null && (u.current = document.activeElement ?? null);
306
- const i = r ? m.current?.querySelector(
307
- `[data-modal-id="${r}"]`
375
+ d.current === null && (d.current = document.activeElement ?? null);
376
+ const i = n ? l.current?.querySelector(
377
+ `[data-modal-id="${n.id}"]`
308
378
  ) : void 0;
309
- i && (x(i)[0] ?? i).focus();
310
- }, [o, d, c]), c && k(
311
- /* @__PURE__ */ f("div", { className: "react-motion-modal", ref: m, children: /* @__PURE__ */ f(F, { initial: !0, mode: "sync", children: s?.map((r, i) => /* @__PURE__ */ f(
312
- S,
379
+ i && (P(i)[0] ?? i).focus();
380
+ }, [n, e, m]), m ? $(
381
+ /* @__PURE__ */ _("div", { className: "react-motion-modal", ref: l, children: /* @__PURE__ */ _(H, { initial: !0, mode: "sync", children: s.map((i, f) => /* @__PURE__ */ _(
382
+ z,
313
383
  {
314
- data: r,
315
- index: i,
316
- initialParams: a,
317
- modals: e
384
+ data: i,
385
+ index: f,
386
+ isActive: n?.id === i.id,
387
+ defaultOptions: t
318
388
  },
319
- r.id
389
+ i.id
320
390
  )) }) }),
321
- c
322
- );
323
- };
391
+ m
392
+ ) : null;
393
+ }, at = (o) => o;
324
394
  export {
325
- R as MODAL_BODY_ANIMATION_VISIBLE,
326
- w as MODAL_ENTER_TRANSITION,
327
- N as MODAL_EXIT_TRANSITION,
328
- t as MODAL_POSITIONS,
329
- M as MODAL_Z_INDEX,
330
- z as ModalProvider,
331
- E as modalStore
395
+ a as MODAL_POSITIONS,
396
+ O as MODAL_Z_INDEX,
397
+ it as ModalProvider,
398
+ nt as createModalSystem,
399
+ at as defineModal,
400
+ et as motionPresets
332
401
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-motion-modal",
3
- "version": "1.4.0",
3
+ "version": "2.0.0",
4
4
  "type": "module",
5
5
  "sideEffects": [
6
6
  "**/*.css"