react-motion-modal 1.3.2 → 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 x, jsx as T } from "react/jsx-runtime";
2
- import { create as B } from "zustand/react";
3
- import { motion as v, AnimatePresence as g } from "motion/react";
4
- import { useRef as y, useEffect as p } from "react";
5
- import { createPortal as A } from "react-dom";
6
- const o = {
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,195 +17,214 @@ const o = {
16
17
  BOTTOM_LEFT: "bottom-left",
17
18
  BOTTOM_RIGHT: "bottom-right",
18
19
  BOTTOM_CENTER: "bottom-center"
19
- }, f = 1e3, F = {
20
+ }, O = 1e3, M = {
20
21
  duration: 0.24,
21
22
  ease: "easeOut"
22
- }, M = {
23
+ }, g = {
23
24
  duration: 0.18,
24
25
  ease: "easeIn"
25
- }, N = {
26
- opacity: 1,
27
- scale: 1,
28
- x: 0,
29
- y: 0,
30
- transition: F
31
- }, P = () => `modal-${Date.now()}-${Math.random().toString(36).slice(2)}`, b = B((t, r) => ({
32
- modals: [],
33
- openModal(a, e) {
34
- const c = {
35
- ...e,
36
- closeModal: () => r().closeModal(a)
37
- }, s = {
38
- id: P(),
39
- name: a,
40
- params: c
41
- }, l = [...r().modals, s];
42
- t({
43
- modals: l,
44
- active: s
45
- });
46
- },
47
- closeAllModal() {
48
- t({
49
- modals: [],
50
- active: void 0
51
- });
26
+ }, I = {
27
+ initial: { opacity: 0 },
28
+ animate: {
29
+ opacity: 1,
30
+ transition: M
52
31
  },
53
- closeModal(a) {
54
- const e = [...r().modals], m = a ? e.findIndex((l) => l.name === a) : e.length - 1, s = e[m].params;
55
- m > -1 && (s?.onClose?.(), e.splice(m, 1), t({
56
- modals: e,
57
- active: e[e.length - 1]
58
- }));
59
- }
60
- })), I = (...t) => t.filter(Boolean).join(" "), u = (t, r) => {
61
- const a = {
32
+ exit: {
62
33
  opacity: 0,
63
- x: t,
64
- y: r,
65
- transition: M
66
- };
67
- return {
68
- initial: a,
69
- animate: N,
70
- exit: a
71
- };
72
- }, C = (t) => {
73
- switch (t) {
74
- case o.LEFT:
75
- return u("-100%", 0);
76
- case o.RIGHT:
77
- return u("100%", 0);
78
- case o.TOP:
79
- case o.TOP_CENTER:
80
- return u(0, "-100%");
81
- case o.BOTTOM:
82
- case o.BOTTOM_CENTER:
83
- return u(0, "100%");
84
- case o.TOP_LEFT:
85
- return u("-100%", "-100%");
86
- case o.TOP_RIGHT:
87
- return u("100%", "-100%");
88
- case o.BOTTOM_LEFT:
89
- return u("-100%", "100%");
90
- case o.BOTTOM_RIGHT:
91
- return u("100%", "100%");
92
- case o.CENTER:
93
- return {
94
- initial: {
95
- opacity: 0,
96
- scale: 0.96
97
- },
98
- animate: N,
99
- exit: {
100
- opacity: 0,
101
- scale: 0.96,
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
48
+ },
49
+ exit: {
50
+ opacity: 0,
51
+ x: o,
52
+ y: t,
53
+ transition: g
54
+ }
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,
102
66
  transition: M
103
- }
104
- };
105
- case o.CENTER_FULL:
106
- return {
107
- initial: {
108
- opacity: 0
109
67
  },
110
- animate: N,
111
68
  exit: {
112
69
  opacity: 0,
113
- transition: M
70
+ scale: 0.96,
71
+ transition: g
114
72
  }
115
- };
116
- default:
117
- return C(o.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
+ };
118
93
  }
119
- }, L = (t) => {
120
- switch (t) {
121
- case o.TOP:
94
+ }, N = (...o) => o.filter(Boolean).join(" "), S = (o) => {
95
+ switch (o) {
96
+ case a.TOP:
122
97
  return "react-motion-modal__body--top";
123
- case o.TOP_LEFT:
98
+ case a.TOP_LEFT:
124
99
  return "react-motion-modal__body--top-left";
125
- case o.TOP_RIGHT:
100
+ case a.TOP_RIGHT:
126
101
  return "react-motion-modal__body--top-right";
127
- case o.TOP_CENTER:
102
+ case a.TOP_CENTER:
128
103
  return "react-motion-modal__body--top-center";
129
- case o.BOTTOM:
104
+ case a.BOTTOM:
130
105
  return "react-motion-modal__body--bottom";
131
- case o.BOTTOM_LEFT:
106
+ case a.BOTTOM_LEFT:
132
107
  return "react-motion-modal__body--bottom-left";
133
- case o.BOTTOM_RIGHT:
108
+ case a.BOTTOM_RIGHT:
134
109
  return "react-motion-modal__body--bottom-right";
135
- case o.BOTTOM_CENTER:
110
+ case a.BOTTOM_CENTER:
136
111
  return "react-motion-modal__body--bottom-center";
137
- case o.LEFT:
112
+ case a.LEFT:
138
113
  return "react-motion-modal__body--left";
139
- case o.RIGHT:
114
+ case a.RIGHT:
140
115
  return "react-motion-modal__body--right";
141
- case o.CENTER:
116
+ case a.CENTER:
142
117
  return "react-motion-modal__body--center";
143
- case o.CENTER_FULL:
118
+ case a.CENTER_FULL:
144
119
  return "react-motion-modal__body--center-full";
145
120
  default:
146
- return L(o.CENTER);
121
+ return S(a.CENTER);
147
122
  }
148
- }, k = ({ data: t, modals: r, index: a, initialParams: e }) => {
149
- const m = r[t.name], c = t.params ?? e, s = c?.animate ?? e?.animate, l = c?.blur ?? e?.blur ?? !0, d = c?.backdrop ?? e?.backdrop, n = c?.body ?? e?.body, i = c?.position ?? e?.position ?? o.CENTER, _ = c?.closeOnClickOutside ?? e?.closeOnClickOutside ?? !0, O = s ? void 0 : C(i), R = b((E) => E.active), h = b((E) => E.closeModal);
150
- return /* @__PURE__ */ x(
151
- v.div,
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;
138
+ }
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,
152
160
  {
153
161
  className: "react-motion-modal__root",
154
- exit: s?.exit ?? { opacity: 0 },
155
- initial: s?.exit ?? { opacity: 0 },
156
- animate: s?.animate ?? { opacity: 1 },
157
- layout: !0,
162
+ initial: !1,
163
+ exit: {},
158
164
  style: {
159
- zIndex: f + a,
160
- backdropFilter: l ? "blur(8px)" : void 0,
161
- WebkitBackdropFilter: l ? "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
162
169
  },
163
170
  children: [
164
- /* @__PURE__ */ T(
165
- "div",
171
+ /* @__PURE__ */ _(
172
+ E.div,
166
173
  {
167
- className: I(
174
+ className: N(
168
175
  "react-motion-modal__backdrop",
169
- R?.id === t.id && "react-motion-modal__active",
170
- d?.className && "react-motion-modal__backdrop--custom",
171
- d?.className
176
+ m && "react-motion-modal__active",
177
+ e.backdrop?.className
172
178
  ),
179
+ initial: b?.initial ?? { opacity: 0 },
180
+ animate: b?.animate ?? { opacity: 1 },
181
+ exit: b?.exit ?? { opacity: 0 },
173
182
  style: {
174
- zIndex: f + a,
175
- ...d?.style
183
+ zIndex: O + t,
184
+ ...e.backdrop?.style
176
185
  },
177
186
  onClick: () => {
178
- _ && h();
187
+ f && o.cancel("backdrop");
179
188
  }
180
189
  }
181
190
  ),
182
- /* @__PURE__ */ T(
191
+ /* @__PURE__ */ _(
183
192
  "div",
184
193
  {
185
194
  role: "dialog",
186
- "aria-modal": "true",
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,
187
200
  tabIndex: -1,
188
- "data-modal-id": t.id,
189
- className: I(
201
+ "data-modal-id": o.id,
202
+ className: N(
190
203
  "react-motion-modal__body",
191
- L(i),
192
- R?.id === t.id && "react-motion-modal__body--active",
193
- n?.className
204
+ S(i),
205
+ m && "react-motion-modal__body--active",
206
+ e.body?.className
194
207
  ),
195
208
  style: {
196
- ...n?.style,
197
- zIndex: f + a + 1
209
+ ...e.body?.style,
210
+ zIndex: O + t + 1
198
211
  },
199
- onClick: (E) => E.stopPropagation(),
200
- children: /* @__PURE__ */ T(
201
- v.div,
212
+ onClick: (T) => T.stopPropagation(),
213
+ children: /* @__PURE__ */ _(
214
+ E.div,
202
215
  {
203
216
  className: "react-motion-modal__content",
204
- initial: O?.initial,
205
- animate: O?.animate,
206
- exit: O?.exit,
207
- children: /* @__PURE__ */ T(m, { ...c })
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
+ )
208
228
  }
209
229
  )
210
230
  }
@@ -212,71 +232,170 @@ const o = {
212
232
  ]
213
233
  }
214
234
  );
215
- }, w = [
235
+ }, q = [
216
236
  "a[href]",
217
237
  "button:not([disabled])",
218
238
  "textarea:not([disabled])",
219
239
  "input:not([disabled])",
220
240
  "select:not([disabled])",
221
241
  '[tabindex]:not([tabindex="-1"])'
222
- ].join(","), S = (t) => t ? Array.from(
223
- t.querySelectorAll(w)
224
- ).filter((r) => r.getClientRects().length > 0) : [], z = ({ modals: t, initialParams: r }) => {
225
- const a = b((n) => n.modals), e = b((n) => n.active), m = b((n) => n.closeModal), c = y(null), s = y(null), l = y(null);
226
- p(() => {
227
- c.current = document.body;
228
- }, []);
229
- const d = a.length > 0;
230
- return p(() => {
231
- if (!d || !(a[a.length - 1]?.params?.closeOnPressEsc ?? r?.closeOnPressEsc ?? !0))
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")
246
+ return;
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();
250
+ return;
251
+ }
252
+ if (!n || !o.contains(n)) {
253
+ t.preventDefault(), (t.shiftKey ? s : u).focus();
254
+ return;
255
+ }
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
333
+ }) => {
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)
232
341
  return;
233
- const _ = (O) => {
234
- O.key === "Escape" && m();
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");
353
+ return;
354
+ }
355
+ const T = n ? l.current?.querySelector(
356
+ `[data-modal-id="${n.id}"]`
357
+ ) : void 0;
358
+ W(T, b);
235
359
  };
236
- return window.addEventListener("keydown", _), () => window.removeEventListener("keydown", _);
237
- }, [
238
- a,
239
- m,
240
- r?.closeOnPressEsc,
241
- d
242
- ]), p(() => {
243
- if (!d)
360
+ return window.addEventListener("keydown", y), () => window.removeEventListener("keydown", y);
361
+ }, [n, t, e]), v(() => {
362
+ if (!e)
244
363
  return;
245
- const { body: n } = document, i = n.style.overflow;
246
- return n.style.overflow = "hidden", () => {
247
- n.style.overflow = i;
364
+ const { body: i } = document, f = i.style.overflow;
365
+ return i.style.overflow = "hidden", () => {
366
+ i.style.overflow = f;
248
367
  };
249
- }, [d]), p(() => {
250
- const n = e?.id;
251
- if (!d) {
252
- l.current?.focus?.(), l.current = null;
368
+ }, [e]), v(() => {
369
+ if (!m)
370
+ return;
371
+ if (!e) {
372
+ d.current?.focus?.(), d.current = null;
253
373
  return;
254
374
  }
255
- l.current === null && (l.current = document.activeElement ?? null);
256
- const i = n ? s.current?.querySelector(
257
- `[data-modal-id="${n}"]`
375
+ d.current === null && (d.current = document.activeElement ?? null);
376
+ const i = n ? l.current?.querySelector(
377
+ `[data-modal-id="${n.id}"]`
258
378
  ) : void 0;
259
- i && (S(i)[0] ?? i).focus();
260
- }, [e, d]), c.current && A(
261
- /* @__PURE__ */ T("div", { className: "react-motion-modal", ref: s, children: /* @__PURE__ */ T(g, { initial: !0, mode: "sync", children: a?.map((n, i) => /* @__PURE__ */ T(
262
- k,
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,
263
383
  {
264
- data: n,
265
- index: i,
266
- initialParams: r,
267
- modals: t
384
+ data: i,
385
+ index: f,
386
+ isActive: n?.id === i.id,
387
+ defaultOptions: t
268
388
  },
269
- n.id
389
+ i.id
270
390
  )) }) }),
271
- c.current
272
- );
273
- };
391
+ m
392
+ ) : null;
393
+ }, at = (o) => o;
274
394
  export {
275
- N as MODAL_BODY_ANIMATION_VISIBLE,
276
- F as MODAL_ENTER_TRANSITION,
277
- M as MODAL_EXIT_TRANSITION,
278
- o as MODAL_POSITIONS,
279
- f as MODAL_Z_INDEX,
280
- z as ModalProvider,
281
- b 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
282
401
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-motion-modal",
3
- "version": "1.3.2",
3
+ "version": "2.0.0",
4
4
  "type": "module",
5
5
  "sideEffects": [
6
6
  "**/*.css"
@@ -49,13 +49,15 @@
49
49
  "build": "tsc -b && vite build",
50
50
  "ci": "yarn build",
51
51
  "check-types": "tsc -b --pretty false",
52
- "test": "vitest run",
52
+ "test": "yarn test:unit && yarn test:browser",
53
+ "test:unit": "vitest run",
54
+ "test:browser": "vitest run --config vite.browser.config.ts",
53
55
  "test:watch": "vitest",
54
56
  "preview": "vite preview",
55
57
  "bump:patch": "yarn version --patch --no-git-tag-version",
56
58
  "bump:minor": "yarn version --minor --no-git-tag-version",
57
59
  "bump:major": "yarn version --major --no-git-tag-version",
58
- "release:check": "yarn build && npm pack --dry-run"
60
+ "release:check": "yarn test && yarn build && npm pack --dry-run"
59
61
  },
60
62
  "dependencies": {},
61
63
  "devDependencies": {