@catconsult/simplify-modal-control 0.0.1 → 1.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/README.md +2 -0
- package/index.d.ts +4 -3
- package/index.d.ts.map +1 -0
- package/index.js +23 -18
- package/index.js.map +1 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
[](https://badge.fury.io/js/@catconsult%2Fsimplify-modal-control)
|
|
2
|
+
|
|
1
3
|
# simplify-modal-control
|
|
2
4
|
|
|
3
5
|
Like the name suggested, this library is created to simplify modal control flow for React. Note, this library is for controlling modals, you still have to pick your favorite modal component as the wrapper.
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ComponentType, Dispatch, SetStateAction, ReactNode, FC } from
|
|
1
|
+
import { ComponentType, Dispatch, SetStateAction, ReactNode, FC } from 'react';
|
|
2
2
|
/**
|
|
3
3
|
* Make modal control simpler!
|
|
4
4
|
* @param options.ModalWrapper the modal wrapper component function
|
|
@@ -68,7 +68,7 @@ export declare type UseModalReturnType<T = any> = [
|
|
|
68
68
|
(newProps?: NewPropsType<T>) => void,
|
|
69
69
|
() => void
|
|
70
70
|
];
|
|
71
|
-
declare type NewPropsType<T> = Omit<T,
|
|
71
|
+
declare type NewPropsType<T> = Omit<T, 'onClose'>;
|
|
72
72
|
declare type UseModalFn<TModalProps, TCustomValues> = <T extends Partial<DefaultModalProps>>(ModalContent: ComponentType<T>, options?: {
|
|
73
73
|
defaultProps?: NewPropsType<T> | undefined;
|
|
74
74
|
modalProps?: Partial<TModalProps> | undefined;
|
|
@@ -83,7 +83,7 @@ declare type MakeModalControlSimplerProps<TModalProps, TCustomValues, THookValue
|
|
|
83
83
|
handleClose: () => void;
|
|
84
84
|
handleOpen: () => void;
|
|
85
85
|
setOpen: Dispatch<SetStateAction<boolean>>;
|
|
86
|
-
setProps: Dispatch<SetStateAction<T>>;
|
|
86
|
+
setProps: Dispatch<SetStateAction<T | null | undefined>>;
|
|
87
87
|
}, modalProps?: Partial<TModalProps>, customValues?: TCustomValues, hookValues?: THookValues) => TModalProps;
|
|
88
88
|
onOpen?: (hookValues?: THookValues) => void;
|
|
89
89
|
onClose?: (hookValues?: THookValues) => void;
|
|
@@ -91,3 +91,4 @@ declare type MakeModalControlSimplerProps<TModalProps, TCustomValues, THookValue
|
|
|
91
91
|
useContextModals?: UseContextModalsFn<TModalProps, TContextModals, TCustomValues>;
|
|
92
92
|
};
|
|
93
93
|
export declare const EMPTY_FUNCTION: () => void;
|
|
94
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,QAAQ,EACR,cAAc,EAQd,SAAS,EACT,EAAE,EACH,MAAM,OAAO,CAAC;AAEf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,QAAA,MAAM,uBAAuB;;;kBA8Lf,SAAS;;;;CAgCtB,CAAC;AAEF,eAAe,uBAAuB,CAAC;AAEvC;;GAEG;AACH,oBAAY,iBAAiB,GAAG;IAC9B,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,oBAAY,kBAAkB,CAAC,CAAC,GAAG,GAAG,IAAI;IAExC,GAAG,CAAC,OAAO;IAEX,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,IAAI;IAEpC,MAAM,IAAI;CACX,CAAC;AAEF,aAAK,YAAY,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;AAE1C,aAAK,UAAU,CAAC,WAAW,EAAE,aAAa,IAAI,CAC5C,CAAC,SAAS,OAAO,CAAC,i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C,WAAW,CAAC,CAAC;IAC3C,gBAAgB,CAAC,EAAE,kBAAkB,CACnC,WAAW,EACX,cAAc,EACd,aAAa,CACd,CAAC;CACH,CAAC;AAKF,eAAO,MAAM,cAAc,YAAW,CAAC"}
|
package/index.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EMPTY_FUNCTION = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_1 = require("react");
|
|
3
6
|
/**
|
|
4
7
|
* Make modal control simpler!
|
|
5
8
|
* @param options.ModalWrapper the modal wrapper component function
|
|
@@ -48,7 +51,7 @@ import { useState, useCallback, useMemo, memo, createContext, useContext, Fragme
|
|
|
48
51
|
* } = simplerModalControl;
|
|
49
52
|
* ```
|
|
50
53
|
*/
|
|
51
|
-
const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose, useHookValue = EMPTY_FUNCTION, useContextModals =
|
|
54
|
+
const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose, useHookValue = exports.EMPTY_FUNCTION, useContextModals = _ => EMPTY_OBJECT, }) => {
|
|
52
55
|
/**
|
|
53
56
|
*
|
|
54
57
|
* @param ModalContent modal content component function
|
|
@@ -97,24 +100,24 @@ const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose,
|
|
|
97
100
|
const useModal = (ModalContent, options) => {
|
|
98
101
|
const defaultProps = options === null || options === void 0 ? void 0 : options.defaultProps;
|
|
99
102
|
const hookValues = useHookValue();
|
|
100
|
-
const [open, setOpen] = useState(Boolean(defaultProps === null || defaultProps === void 0 ? void 0 : defaultProps.open));
|
|
101
|
-
const [props, setProps] = useState();
|
|
102
|
-
const handleOpen = useCallback((newProps) => {
|
|
103
|
+
const [open, setOpen] = (0, react_1.useState)(Boolean(defaultProps === null || defaultProps === void 0 ? void 0 : defaultProps.open));
|
|
104
|
+
const [props, setProps] = (0, react_1.useState)();
|
|
105
|
+
const handleOpen = (0, react_1.useCallback)((newProps) => {
|
|
103
106
|
setOpen(true);
|
|
104
107
|
setProps(newProps);
|
|
105
108
|
if (onOpen)
|
|
106
109
|
onOpen(hookValues);
|
|
107
110
|
}, [hookValues]);
|
|
108
|
-
const handleClose = useCallback(() => {
|
|
111
|
+
const handleClose = (0, react_1.useCallback)(() => {
|
|
109
112
|
setOpen(false);
|
|
110
113
|
setProps(undefined);
|
|
111
114
|
if (onClose)
|
|
112
115
|
onClose(hookValues);
|
|
113
116
|
}, [hookValues]);
|
|
114
|
-
const ModalContentMemo = useMemo(
|
|
117
|
+
const ModalContentMemo = (0, react_1.useMemo)(
|
|
115
118
|
// prevent modal content rerender when modal is not open
|
|
116
|
-
() => memo(ModalContent, (_, next) => !next.open), [ModalContent]);
|
|
117
|
-
const mappedModalProps = useMemo(() => mapModalProps({ open, handleClose, handleOpen, setOpen, setProps }, options === null || options === void 0 ? void 0 : options.modalProps, options === null || options === void 0 ? void 0 : options.customValues, hookValues), [
|
|
119
|
+
() => (0, react_1.memo)(ModalContent, (_, next) => !next.open), [ModalContent]);
|
|
120
|
+
const mappedModalProps = (0, react_1.useMemo)(() => mapModalProps({ open, handleClose, handleOpen, setOpen, setProps }, options === null || options === void 0 ? void 0 : options.modalProps, options === null || options === void 0 ? void 0 : options.customValues, hookValues), [
|
|
118
121
|
handleClose,
|
|
119
122
|
handleOpen,
|
|
120
123
|
hookValues,
|
|
@@ -122,10 +125,10 @@ const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose,
|
|
|
122
125
|
options === null || options === void 0 ? void 0 : options.customValues,
|
|
123
126
|
options === null || options === void 0 ? void 0 : options.modalProps,
|
|
124
127
|
]);
|
|
125
|
-
const modal = (
|
|
128
|
+
const modal = ((0, jsx_runtime_1.jsx)(ModalWrapper, Object.assign({}, mappedModalProps, { children: (0, jsx_runtime_1.jsx)(ModalContentMemo, Object.assign({}, defaultProps, props, { open: open, onClose: handleClose })) })));
|
|
126
129
|
return [modal, handleOpen, handleClose];
|
|
127
130
|
};
|
|
128
|
-
const ModalContext = createContext(EMPTY_OBJECT);
|
|
131
|
+
const ModalContext = (0, react_1.createContext)(EMPTY_OBJECT);
|
|
129
132
|
/**
|
|
130
133
|
* Select all modal handlers from global modal context
|
|
131
134
|
*
|
|
@@ -145,7 +148,7 @@ const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose,
|
|
|
145
148
|
* handleCloseModal2();
|
|
146
149
|
* ```
|
|
147
150
|
*/
|
|
148
|
-
const useModalContext = () => useContext(ModalContext);
|
|
151
|
+
const useModalContext = () => (0, react_1.useContext)(ModalContext);
|
|
149
152
|
/**
|
|
150
153
|
* Select a single modal open and close handler from global modal context
|
|
151
154
|
*
|
|
@@ -160,7 +163,7 @@ const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose,
|
|
|
160
163
|
* handleClose();
|
|
161
164
|
* ```
|
|
162
165
|
*/
|
|
163
|
-
const useModalContextSelector = (key) => { var _a; return ((_a = useContext(ModalContext)) === null || _a === void 0 ? void 0 : _a[key]) || EMPTY_LIST; };
|
|
166
|
+
const useModalContextSelector = (key) => { var _a; return ((_a = (0, react_1.useContext)(ModalContext)) === null || _a === void 0 ? void 0 : _a[key]) || EMPTY_LIST; };
|
|
164
167
|
/**
|
|
165
168
|
* A global context provider to store reusable modals
|
|
166
169
|
* Consumer can open, close, pass props to any modals using context value handlers.
|
|
@@ -178,14 +181,14 @@ const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose,
|
|
|
178
181
|
const ModalContextProvider = ({ children }) => {
|
|
179
182
|
const modals = useContextModals(useModal);
|
|
180
183
|
// context value will be map of handlers. Only need to initialize once
|
|
181
|
-
const contextValue = useMemo(() => Object.entries(modals).reduce((acc, [key, [_, ...handlers]]) => {
|
|
184
|
+
const contextValue = (0, react_1.useMemo)(() => Object.entries(modals).reduce((acc, [key, [_, ...handlers]]) => {
|
|
182
185
|
acc[key] =
|
|
183
186
|
handlers;
|
|
184
187
|
return acc;
|
|
185
188
|
}, {}),
|
|
186
189
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
187
190
|
[]);
|
|
188
|
-
return (
|
|
191
|
+
return ((0, jsx_runtime_1.jsxs)(ModalContext.Provider, Object.assign({ value: contextValue }, { children: [Object.values(modals).map(([modalNode], i) => ((0, jsx_runtime_1.jsx)(react_1.Fragment, { children: modalNode }, i))), children] })));
|
|
189
192
|
};
|
|
190
193
|
return {
|
|
191
194
|
useModal,
|
|
@@ -194,8 +197,10 @@ const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose,
|
|
|
194
197
|
useModalContextSelector,
|
|
195
198
|
};
|
|
196
199
|
};
|
|
197
|
-
|
|
200
|
+
exports.default = makeModalControlSimpler;
|
|
198
201
|
const EMPTY_LIST = [];
|
|
199
202
|
const EMPTY_OBJECT = {};
|
|
200
203
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
201
|
-
|
|
204
|
+
const EMPTY_FUNCTION = () => { };
|
|
205
|
+
exports.EMPTY_FUNCTION = EMPTY_FUNCTION;
|
|
206
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":";;;;AAAA,iCAae;AAEf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,uBAAuB,GAAG,CAY9B,EACA,YAAY,EACZ,aAAa,EACb,MAAM,EACN,OAAO,EACP,YAAY,GAAG,sBAA6C,EAC5D,gBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC,YAA8B,GAMvD,EAAE,EAAE;IACH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4CG;IAC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,CAAC,CAAC,EAAE,EAAE;YAC7D,GAAG,CAAC,GAAwB,CAAC;gBAC3B,QAAiD,CAAC;YACpD,OAAO,GAAG,CAAC;QACb,CAAC,EAAE,EAAwB,CAAC;QAC9B,uDAAuD;QACvD,EAAE,CACH,CAAC;QAEF,OAAO,CACL,wBAAC,YAAY,CAAC,QAAQ,kBAAC,KAAK,EAAE,YAAY,iBACvC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAC7C,uBAAC,gBAAQ,cAAU,SAAS,IAAb,CAAC,CAAwB,CACzC,CAAC,EACD,QAAQ,KACa,CACzB,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO;QACL,QAAQ;QACR,oBAAoB;QACpB,eAAe;QACf,uBAAuB;KACxB,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,uBAAuB,CAAC;AAmEvC,MAAM,UAAU,GAAG,EAAS,CAAC;AAC7B,MAAM,YAAY,GAAG,EAAS,CAAC;AAC/B,gEAAgE;AACzD,MAAM,cAAc,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;AAA1B,QAAA,cAAc,kBAAY"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@catconsult/simplify-modal-control",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Simplify React modal control flow",
|
|
5
|
-
"main": "
|
|
6
|
-
"types": "
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"types": "./index.d.ts",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public"
|
|
9
9
|
},
|
|
@@ -46,4 +46,4 @@
|
|
|
46
46
|
"ts-jest": "^29.0.3",
|
|
47
47
|
"typescript": "^4.8.4"
|
|
48
48
|
}
|
|
49
|
-
}
|
|
49
|
+
}
|