react-imperial-modal 2.0.0 → 3.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/constants.d.ts
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ModalProvider } from './ModalProvider';
|
|
2
2
|
import { useModal } from './useModal';
|
|
3
|
+
import { useModalDangerously } from './useModalDangerously';
|
|
3
4
|
import { ModalProps, ModalProviderConfig } from './types';
|
|
4
|
-
export { ModalProvider, useModal };
|
|
5
|
+
export { ModalProvider, useModal, useModalDangerously };
|
|
5
6
|
export type { ModalProps, ModalProviderConfig };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import
|
|
3
|
-
const
|
|
4
|
-
let s,
|
|
5
|
-
return { promise: new Promise((
|
|
6
|
-
s =
|
|
7
|
-
}), resolve: s, reject:
|
|
8
|
-
},
|
|
1
|
+
import { jsx as j, jsxs as A } from "react/jsx-runtime";
|
|
2
|
+
import D, { useRef as R, useContext as O, useLayoutEffect as K, useCallback as m, useState as L, useMemo as q } from "react";
|
|
3
|
+
const S = () => {
|
|
4
|
+
let s, o;
|
|
5
|
+
return { promise: new Promise((l, n) => {
|
|
6
|
+
s = l, o = n;
|
|
7
|
+
}), resolve: s, reject: o };
|
|
8
|
+
}, N = "Escape", _ = [
|
|
9
9
|
"a[href]:not([tabindex='-1'])",
|
|
10
10
|
"area[href]:not([tabindex='-1'])",
|
|
11
11
|
"input:not([disabled]):not([tabindex='-1'])",
|
|
@@ -15,128 +15,141 @@ const q = () => {
|
|
|
15
15
|
"iframe:not([tabindex='-1'])",
|
|
16
16
|
"[tabindex]:not([tabindex='-1'])",
|
|
17
17
|
"[contentEditable=true]:not([tabindex='-1'])"
|
|
18
|
-
].join(", "),
|
|
19
|
-
|
|
18
|
+
].join(", "), k = /* @__PURE__ */ (() => {
|
|
19
|
+
let s = 1;
|
|
20
|
+
return () => "modal_" + (s++ * 1664525 + 1013904223 >>> 0).toString(36);
|
|
21
|
+
})(), B = function({ className: s, entry: o }) {
|
|
22
|
+
const { role: M = "dialog", label: l, labelledby: n, componentProps: v, Component: b } = o, d = R(null), { removeModal: c } = O(C);
|
|
20
23
|
K(() => {
|
|
21
24
|
requestAnimationFrame(() => {
|
|
22
|
-
var
|
|
23
|
-
|
|
25
|
+
var a;
|
|
26
|
+
d.current && (d.current.showModal(), (a = d.current.querySelector(_)) == null || a.focus());
|
|
24
27
|
});
|
|
25
28
|
}, []);
|
|
26
|
-
const f =
|
|
27
|
-
(
|
|
28
|
-
|
|
29
|
+
const f = m(
|
|
30
|
+
(a) => {
|
|
31
|
+
a.key === N && !o.ignoreEscape && c(o.id);
|
|
29
32
|
},
|
|
30
|
-
[
|
|
33
|
+
[o, c]
|
|
31
34
|
);
|
|
32
|
-
return /* @__PURE__ */
|
|
35
|
+
return /* @__PURE__ */ j(
|
|
33
36
|
"dialog",
|
|
34
37
|
{
|
|
35
|
-
ref:
|
|
36
|
-
role:
|
|
37
|
-
"aria-label":
|
|
38
|
-
"aria-labelledby":
|
|
38
|
+
ref: d,
|
|
39
|
+
role: M,
|
|
40
|
+
"aria-label": l,
|
|
41
|
+
"aria-labelledby": n,
|
|
39
42
|
className: s,
|
|
40
43
|
onKeyDown: f,
|
|
41
|
-
children: /* @__PURE__ */
|
|
42
|
-
|
|
44
|
+
children: /* @__PURE__ */ j(
|
|
45
|
+
b,
|
|
43
46
|
{
|
|
44
|
-
close:
|
|
45
|
-
resolve:
|
|
46
|
-
reject:
|
|
47
|
-
...
|
|
47
|
+
close: o.closeModal,
|
|
48
|
+
resolve: o.resolveModal,
|
|
49
|
+
reject: o.rejectModal,
|
|
50
|
+
...v
|
|
48
51
|
}
|
|
49
52
|
)
|
|
50
53
|
}
|
|
51
54
|
);
|
|
52
|
-
},
|
|
55
|
+
}, y = () => {
|
|
53
56
|
throw new Error(
|
|
54
57
|
"Attempted to call useModal outside of modal context. Make sure your component is inside ModalProvider."
|
|
55
58
|
);
|
|
56
|
-
},
|
|
57
|
-
addModal:
|
|
58
|
-
removeModal:
|
|
59
|
-
resolveModal:
|
|
60
|
-
rejectModal:
|
|
61
|
-
}),
|
|
62
|
-
|
|
63
|
-
modalContainerClass: "modals",
|
|
64
|
-
modalClass: "modal"
|
|
65
|
-
}, w = document.documentElement, k = document.body, Y = ({ children: s, config: t = {} }) => {
|
|
66
|
-
const [a, i] = L([]), l = R(() => ({ ...F, ...t }), [t]), d = P(null), M = P([]), n = a.at(-1), m = u(() => {
|
|
59
|
+
}, C = D.createContext({
|
|
60
|
+
addModal: y,
|
|
61
|
+
removeModal: y,
|
|
62
|
+
resolveModal: y,
|
|
63
|
+
rejectModal: y
|
|
64
|
+
}), g = document.documentElement, P = document.body, T = ({ children: s, config: o = {} }) => {
|
|
65
|
+
const [M, l] = L([]), n = R(null), v = R([]), b = m(() => {
|
|
67
66
|
var e;
|
|
68
|
-
|
|
69
|
-
}, [
|
|
67
|
+
o.bodyOpenClass && P.classList.add(o.bodyOpenClass), (e = n == null ? void 0 : n.current) == null || e.setAttribute("aria-hidden", "true"), g.style.overflow = "hidden";
|
|
68
|
+
}, [o]), d = m(() => {
|
|
70
69
|
var e;
|
|
71
|
-
|
|
72
|
-
}, [
|
|
70
|
+
o.bodyOpenClass && P.classList.remove(o.bodyOpenClass), (e = n == null ? void 0 : n.current) == null || e.removeAttribute("aria-hidden"), g.style.overflow = "";
|
|
71
|
+
}, [o]), c = m(
|
|
73
72
|
(e) => {
|
|
74
|
-
|
|
73
|
+
l((t) => t.includes(e) ? t : (t.length === 0 && b(), v.current.push(document.activeElement), [...t, e]));
|
|
75
74
|
},
|
|
76
|
-
[
|
|
77
|
-
),
|
|
75
|
+
[b]
|
|
76
|
+
), f = m(
|
|
78
77
|
(e) => {
|
|
79
|
-
|
|
80
|
-
const E =
|
|
81
|
-
return !
|
|
78
|
+
l((t) => {
|
|
79
|
+
const u = t.at(-1), E = t.find((w) => w.id === e), x = e === void 0 ? u : E, r = t.length === 1, h = v.current.pop();
|
|
80
|
+
return !x || !t.includes(x) ? t : (r && h && g.contains(h) && h.focus(), r && d(), t.filter((w) => x !== w));
|
|
82
81
|
});
|
|
83
82
|
},
|
|
84
|
-
[
|
|
85
|
-
),
|
|
86
|
-
(
|
|
87
|
-
const E =
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
() => ({ addModal: r, removeModal: p, resolveModal: b, rejectModal: x }),
|
|
99
|
-
[r, p, b, x]
|
|
83
|
+
[d]
|
|
84
|
+
), a = m((e, t) => {
|
|
85
|
+
l((u) => {
|
|
86
|
+
const E = u.at(-1), x = u.find((h) => h.id === t), r = t === void 0 ? E : x;
|
|
87
|
+
return r == null || r.resolveModal(e || null), u;
|
|
88
|
+
});
|
|
89
|
+
}, []), i = m((e, t) => {
|
|
90
|
+
l((u) => {
|
|
91
|
+
const E = u.at(-1), x = u.find((h) => h.id === t), r = t === void 0 ? E : x;
|
|
92
|
+
return r == null || r.rejectModal(e), u;
|
|
93
|
+
});
|
|
94
|
+
}, []), p = q(
|
|
95
|
+
() => ({ addModal: c, removeModal: f, resolveModal: a, rejectModal: i }),
|
|
96
|
+
[c, f, a, i]
|
|
100
97
|
);
|
|
101
|
-
return /* @__PURE__ */
|
|
102
|
-
/* @__PURE__ */
|
|
103
|
-
/* @__PURE__ */
|
|
104
|
-
B,
|
|
105
|
-
{
|
|
106
|
-
className: l.modalClass,
|
|
107
|
-
entry: e
|
|
108
|
-
},
|
|
109
|
-
e.instanceId
|
|
110
|
-
)) })
|
|
98
|
+
return /* @__PURE__ */ A(C.Provider, { value: p, children: [
|
|
99
|
+
/* @__PURE__ */ j("div", { ref: n, children: s }),
|
|
100
|
+
/* @__PURE__ */ j("div", { className: o.modalContainerClass, children: M.map((e) => /* @__PURE__ */ j(B, { className: o.modalClass, entry: e }, e.id)) })
|
|
111
101
|
] });
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
Component: a,
|
|
121
|
-
componentProps: i,
|
|
102
|
+
}, V = () => {
|
|
103
|
+
const s = O(C);
|
|
104
|
+
return m(
|
|
105
|
+
(o, M, l = !1, n, v, b = "dialog") => {
|
|
106
|
+
const { promise: d, resolve: c, reject: f } = S(), a = k(), i = () => s.removeModal(a), p = Object.assign(d, {
|
|
107
|
+
id: a,
|
|
108
|
+
Component: o,
|
|
109
|
+
componentProps: M,
|
|
122
110
|
ignoreEscape: l,
|
|
123
|
-
labelledby:
|
|
124
|
-
label:
|
|
125
|
-
role:
|
|
126
|
-
resolveModal: (
|
|
127
|
-
|
|
111
|
+
labelledby: v,
|
|
112
|
+
label: n,
|
|
113
|
+
role: b,
|
|
114
|
+
resolveModal: (e) => {
|
|
115
|
+
c(e), i();
|
|
116
|
+
},
|
|
117
|
+
rejectModal: (e) => {
|
|
118
|
+
f(e), i();
|
|
119
|
+
},
|
|
120
|
+
closeModal: i
|
|
121
|
+
});
|
|
122
|
+
return s.addModal(p), p;
|
|
123
|
+
},
|
|
124
|
+
[s]
|
|
125
|
+
);
|
|
126
|
+
}, W = () => {
|
|
127
|
+
const s = O(C);
|
|
128
|
+
return [m(
|
|
129
|
+
(M, l, n = !1, v, b, d = "dialog") => {
|
|
130
|
+
const { promise: c, resolve: f, reject: a } = S(), i = k(), p = () => s.removeModal(i), e = Object.assign(c, {
|
|
131
|
+
id: i,
|
|
132
|
+
Component: M,
|
|
133
|
+
componentProps: l,
|
|
134
|
+
ignoreEscape: n,
|
|
135
|
+
labelledby: b,
|
|
136
|
+
label: v,
|
|
137
|
+
role: d,
|
|
138
|
+
resolveModal: (t) => {
|
|
139
|
+
f(t), p();
|
|
128
140
|
},
|
|
129
|
-
rejectModal: (
|
|
130
|
-
|
|
141
|
+
rejectModal: (t) => {
|
|
142
|
+
a(t), p();
|
|
131
143
|
},
|
|
132
|
-
closeModal:
|
|
144
|
+
closeModal: p
|
|
133
145
|
});
|
|
134
|
-
return s.addModal(
|
|
146
|
+
return s.addModal(e), e;
|
|
135
147
|
},
|
|
136
148
|
[s]
|
|
137
|
-
), s.resolveModal, s.rejectModal];
|
|
149
|
+
), s.removeModal, s.resolveModal, s.rejectModal];
|
|
138
150
|
};
|
|
139
151
|
export {
|
|
140
|
-
|
|
141
|
-
|
|
152
|
+
T as ModalProvider,
|
|
153
|
+
V as useModal,
|
|
154
|
+
W as useModalDangerously
|
|
142
155
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(c,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],i):(c=typeof globalThis<"u"?globalThis:c||self,i(c["react-imperial-modal"]={},c.jsxRuntime,c.React))})(this,(function(c,i,s){"use strict";const P=()=>{let l,o;return{promise:new Promise((d,n)=>{l=d,o=n}),resolve:l,reject:o}},q="Escape",A=["a[href]:not([tabindex='-1'])","area[href]:not([tabindex='-1'])","input:not([disabled]):not([tabindex='-1'])","select:not([disabled]):not([tabindex='-1'])","textarea:not([disabled]):not([tabindex='-1'])","button:not([disabled]):not([tabindex='-1'])","iframe:not([tabindex='-1'])","[tabindex]:not([tabindex='-1'])","[contentEditable=true]:not([tabindex='-1'])"].join(", "),S=(()=>{let l=1;return()=>"modal_"+(l++*1664525+1013904223>>>0).toString(36)})(),D=function({className:l,entry:o}){const{role:f="dialog",label:d,labelledby:n,componentProps:v,Component:p}=o,r=s.useRef(null),{removeModal:m}=s.useContext(k);s.useLayoutEffect(()=>{requestAnimationFrame(()=>{var a;r.current&&(r.current.showModal(),(a=r.current.querySelector(A))==null||a.focus())})},[]);const x=s.useCallback(a=>{a.key===q&&!o.ignoreEscape&&m(o.id)},[o,m]);return i.jsx("dialog",{ref:r,role:f,"aria-label":d,"aria-labelledby":n,className:l,onKeyDown:x,children:i.jsx(p,{close:o.closeModal,resolve:o.resolveModal,reject:o.rejectModal,...v})})},E=()=>{throw new Error("Attempted to call useModal outside of modal context. Make sure your component is inside ModalProvider.")},k=s.createContext({addModal:E,removeModal:E,resolveModal:E,rejectModal:E}),g=document.documentElement,O=document.body,T=({children:l,config:o={}})=>{const[f,d]=s.useState([]),n=s.useRef(null),v=s.useRef([]),p=s.useCallback(()=>{var e;o.bodyOpenClass&&O.classList.add(o.bodyOpenClass),(e=n==null?void 0:n.current)==null||e.setAttribute("aria-hidden","true"),g.style.overflow="hidden"},[o]),r=s.useCallback(()=>{var e;o.bodyOpenClass&&O.classList.remove(o.bodyOpenClass),(e=n==null?void 0:n.current)==null||e.removeAttribute("aria-hidden"),g.style.overflow=""},[o]),m=s.useCallback(e=>{d(t=>t.includes(e)?t:(t.length===0&&p(),v.current.push(document.activeElement),[...t,e]))},[p]),x=s.useCallback(e=>{d(t=>{const b=t.at(-1),y=t.find(w=>w.id===e),j=e===void 0?b:y,u=t.length===1,C=v.current.pop();return!j||!t.includes(j)?t:(u&&C&&g.contains(C)&&C.focus(),u&&r(),t.filter(w=>j!==w))})},[r]),a=s.useCallback((e,t)=>{d(b=>{const y=b.at(-1),j=b.find(C=>C.id===t),u=t===void 0?y:j;return u==null||u.resolveModal(e||null),b})},[]),M=s.useCallback((e,t)=>{d(b=>{const y=b.at(-1),j=b.find(C=>C.id===t),u=t===void 0?y:j;return u==null||u.rejectModal(e),b})},[]),h=s.useMemo(()=>({addModal:m,removeModal:x,resolveModal:a,rejectModal:M}),[m,x,a,M]);return i.jsxs(k.Provider,{value:h,children:[i.jsx("div",{ref:n,children:l}),i.jsx("div",{className:o.modalContainerClass,children:f.map(e=>i.jsx(D,{className:o.modalClass,entry:e},e.id))})]})},K=()=>{const l=s.useContext(k);return s.useCallback((o,f,d=!1,n,v,p="dialog")=>{const{promise:r,resolve:m,reject:x}=P(),a=S(),M=()=>l.removeModal(a),h=Object.assign(r,{id:a,Component:o,componentProps:f,ignoreEscape:d,labelledby:v,label:n,role:p,resolveModal:e=>{m(e),M()},rejectModal:e=>{x(e),M()},closeModal:M});return l.addModal(h),h},[l])},L=()=>{const l=s.useContext(k);return[s.useCallback((f,d,n=!1,v,p,r="dialog")=>{const{promise:m,resolve:x,reject:a}=P(),M=S(),h=()=>l.removeModal(M),e=Object.assign(m,{id:M,Component:f,componentProps:d,ignoreEscape:n,labelledby:p,label:v,role:r,resolveModal:t=>{x(t),h()},rejectModal:t=>{a(t),h()},closeModal:h});return l.addModal(e),e},[l]),l.removeModal,l.resolveModal,l.rejectModal]};c.ModalProvider=T,c.useModal=K,c.useModalDangerously=L,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/types.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
|
-
export interface ModalProps<T> {
|
|
2
|
+
export interface ModalProps<T = void> {
|
|
3
3
|
resolve: (val: T | null) => void;
|
|
4
4
|
reject: (reason?: unknown) => void;
|
|
5
5
|
close: () => void;
|
|
6
6
|
}
|
|
7
7
|
export type ModalEntry<T, P> = Promise<T | null> & {
|
|
8
|
-
|
|
8
|
+
id: string;
|
|
9
9
|
Component: React.ComponentType<P & ModalProps<T>>;
|
|
10
10
|
componentProps: P;
|
|
11
11
|
resolveModal: (val: T | null) => void;
|
|
@@ -27,7 +27,7 @@ export type ModalProviderProps = {
|
|
|
27
27
|
};
|
|
28
28
|
export type ModalContextValue = {
|
|
29
29
|
addModal: (entry: ModalEntry<unknown, unknown>) => void;
|
|
30
|
-
removeModal: (
|
|
31
|
-
resolveModal: (val: unknown | null,
|
|
32
|
-
rejectModal: (
|
|
30
|
+
removeModal: (id?: string) => void;
|
|
31
|
+
resolveModal: (val: unknown | null, id?: string) => void;
|
|
32
|
+
rejectModal: (reason?: unknown, id?: string) => void;
|
|
33
33
|
};
|
package/dist/useModal.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { ModalEntry, ModalProps } from './types';
|
|
2
|
-
export declare const useModal: () =>
|
|
2
|
+
export declare const useModal: () => <T, P>(Component: React.ComponentType<P & ModalProps<T>>, componentProps: P, ignoreEscape?: boolean, label?: string, labelledby?: string, role?: string) => ModalEntry<T, P>;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { ModalEntry, ModalProps } from './types';
|
|
2
|
+
export declare const useModalDangerously: () => readonly [<T, P>(Component: React.ComponentType<P & ModalProps<T>>, componentProps: P, ignoreEscape?: boolean, label?: string, labelledby?: string, role?: string) => ModalEntry<T, P>, (id?: string) => void, (val: unknown | null, id?: string) => void, (reason?: unknown, id?: string) => void];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-imperial-modal",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "imperative modal api for react",
|
|
5
5
|
"author": "Greg Archer (greg.taff@gmail.com)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"react": ">=
|
|
36
|
-
"react-dom": ">=
|
|
35
|
+
"react": ">=17.0.0",
|
|
36
|
+
"react-dom": ">=17.0.0"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {},
|
|
39
39
|
"scripts": {
|