@catconsult/simplify-modal-control 0.0.1
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 +215 -0
- package/index.d.ts +93 -0
- package/index.js +201 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# simplify-modal-control
|
|
2
|
+
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
# Features
|
|
6
|
+
|
|
7
|
+
- Handle open and close modal
|
|
8
|
+
- Pass props into modal content
|
|
9
|
+
- Declare reusable modals and access it anywhere
|
|
10
|
+
- Prevent data flickering during modal transition
|
|
11
|
+
- Prevent re-rendering when modal is in close state
|
|
12
|
+
- Super customizable, not tied to any modal components
|
|
13
|
+
- ZERO dependencies beside React v16.8.0 or above
|
|
14
|
+
- Extremely lightweight, just ~10 KB in package size
|
|
15
|
+
- Written in TypeScript, every pieces are well Typed!
|
|
16
|
+
|
|
17
|
+
# Problems Addressed
|
|
18
|
+
|
|
19
|
+
In React world, normally when you want to use a modal, you'll have to go through the process of declaring a openModal state, creating open and close handlers, and finally returning the modal and content component in the component block. This process is tedious and cause several issues:
|
|
20
|
+
|
|
21
|
+
- Lots of boilerplate codes
|
|
22
|
+
- Hard to reuse and pass around props into the modal
|
|
23
|
+
- Introduce extra re-rendering caused by the parent component
|
|
24
|
+
- Modal component continue to trigger re-rendering in close state
|
|
25
|
+
- Modal content flickering during close transition caused by change modal state
|
|
26
|
+
|
|
27
|
+
This library addressed all the above issues by introduce a simpler modal control flow.
|
|
28
|
+
|
|
29
|
+
# Quick Installation
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
yarn add @catconsult/simplify-modal-control
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
or
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
npm i @catconsult/simplify-modal-control
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
# Quick Usage
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
// use MUI Dialog as Example, choose you favorite component wrapper
|
|
45
|
+
import { Dialog, DialogContent } from "@mui/material";
|
|
46
|
+
import makeModalControlSimpler from "@catconsult/simplify-modal-control";
|
|
47
|
+
|
|
48
|
+
// define your modal
|
|
49
|
+
const MyModal = ({
|
|
50
|
+
onClose,
|
|
51
|
+
customMessage,
|
|
52
|
+
}: DefaultModalProps & {
|
|
53
|
+
customMessage: string;
|
|
54
|
+
}) => {
|
|
55
|
+
return (
|
|
56
|
+
<>
|
|
57
|
+
<DialogContent>
|
|
58
|
+
<span>Modal Content: {customMessage}</span>
|
|
59
|
+
<button onClick={onClose}>Close</button>
|
|
60
|
+
</DialogContent>
|
|
61
|
+
</>
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// construct modal controls
|
|
66
|
+
const {
|
|
67
|
+
useModal,
|
|
68
|
+
useModalContextSelector,
|
|
69
|
+
useModalContext,
|
|
70
|
+
ModalContextProvider,
|
|
71
|
+
} = makeModalControlSimpler({
|
|
72
|
+
ModalWrapper: Dialog,
|
|
73
|
+
// pass props to modal wrapper
|
|
74
|
+
mapModalProps: ({ open, handleClose }, modalProps) => ({
|
|
75
|
+
...modalProps,
|
|
76
|
+
open,
|
|
77
|
+
onClose: handleClose,
|
|
78
|
+
}),
|
|
79
|
+
// define your global modals
|
|
80
|
+
useContextModals: (useModal) => ({
|
|
81
|
+
myModal1: useModal(MyModal),
|
|
82
|
+
myModal2: useModal(MyModal, {
|
|
83
|
+
modalProps: {
|
|
84
|
+
// pass props to modal wrapper
|
|
85
|
+
maxWidth: "xs",
|
|
86
|
+
},
|
|
87
|
+
}),
|
|
88
|
+
myModal3: useModal(MyModal, {
|
|
89
|
+
// pass default props to modal content
|
|
90
|
+
defaultProps: { customMessage: "something" },
|
|
91
|
+
}),
|
|
92
|
+
}),
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// three ways to consume modal
|
|
96
|
+
const MyConsumer = () => {
|
|
97
|
+
// select single from global context
|
|
98
|
+
const [handleOpenMyModal1, handleCloseMyModal1] =
|
|
99
|
+
useModalContextSelector("myModal1");
|
|
100
|
+
|
|
101
|
+
// select multiple from global context
|
|
102
|
+
const {
|
|
103
|
+
myModal2: [handleOpenMyModal2],
|
|
104
|
+
myModal3: [handleOpenMyModal3],
|
|
105
|
+
} = useModalContext();
|
|
106
|
+
|
|
107
|
+
// use modal within component scope
|
|
108
|
+
const [MyModal4Node, handleOpenMyModal4] = useModal(MyModal);
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<div>
|
|
112
|
+
{MyModal4Node}
|
|
113
|
+
<button
|
|
114
|
+
onClick={() => {
|
|
115
|
+
handleOpenMyModal1({ customMessage: "modal1" });
|
|
116
|
+
handleOpenMyModal2({ customMessage: "modal2" });
|
|
117
|
+
handleOpenMyModal3({ customMessage: "modal3" });
|
|
118
|
+
handleOpenMyModal4({ customMessage: "modal4" });
|
|
119
|
+
}}
|
|
120
|
+
>
|
|
121
|
+
Open
|
|
122
|
+
</button>
|
|
123
|
+
</div>
|
|
124
|
+
);
|
|
125
|
+
};
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
# Demo
|
|
129
|
+
|
|
130
|
+
TODO: add CodePen link and demo
|
|
131
|
+
|
|
132
|
+
- `MUI Dialog` example with custom way to disable backdrop click
|
|
133
|
+
- React Native `react-native-modalize`
|
|
134
|
+
|
|
135
|
+
# Documentations
|
|
136
|
+
|
|
137
|
+
## makeModalControlSimpler
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
/**
|
|
141
|
+
* Make modal control simpler!
|
|
142
|
+
* @param options.ModalWrapper the modal wrapper component function
|
|
143
|
+
* @param options.mapModalProps function that return modal wrapper props
|
|
144
|
+
* @param options.onOpen function trigger when open modal
|
|
145
|
+
* @param options.onClose function trigger when close modal
|
|
146
|
+
* @param options.useHookValue custom hooks to return any hook value to be consumed by mapModalProps. Similar purpose as customValues
|
|
147
|
+
* @param options.useContextModals return modals to be use in modal context provider
|
|
148
|
+
*
|
|
149
|
+
* @returns { { useModal, useModalContext, useModalContextSelector, ModalContextProvider } }
|
|
150
|
+
*/
|
|
151
|
+
makeModalControlSimpler(options);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## useModal
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
/**
|
|
158
|
+
* @param ModalContent modal content component function
|
|
159
|
+
* @param options
|
|
160
|
+
* @param options.defaultProps props to feed modal content
|
|
161
|
+
* @param options.modalProps modal props
|
|
162
|
+
* @param options.customValues custom values to be consumed by mapModalProps. Similar purpose as useHookValue
|
|
163
|
+
* @returns { [ModalNode, handleOpen, handleClose] }
|
|
164
|
+
*/
|
|
165
|
+
useModal(MyModal, UseModalOptions);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## useModalContext
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
/**
|
|
172
|
+
* Select all modal handlers from global modal context
|
|
173
|
+
* @returns { { [modalKey]: [handleOpen, handleClose] } }
|
|
174
|
+
*/
|
|
175
|
+
useModalContext();
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## useModalContextSelector
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
/**
|
|
182
|
+
* Select a single modal open and close handler from global modal context
|
|
183
|
+
* @param key modal key
|
|
184
|
+
* @returns { [handleOpen, handleClose] }
|
|
185
|
+
*/
|
|
186
|
+
useModalContextSelector(key);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
# Local Development
|
|
190
|
+
|
|
191
|
+
## Prerequisites
|
|
192
|
+
|
|
193
|
+
- NodeJS and NPM
|
|
194
|
+
- Yarn
|
|
195
|
+
- Editor/IDE that support TypeScript
|
|
196
|
+
|
|
197
|
+
## Quick Start
|
|
198
|
+
|
|
199
|
+
install dependencies using yarn
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
yarn
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
run test watch
|
|
206
|
+
|
|
207
|
+
```
|
|
208
|
+
yarn test
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
test build
|
|
212
|
+
|
|
213
|
+
```
|
|
214
|
+
yarn build
|
|
215
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ComponentType, Dispatch, SetStateAction, ReactNode, FC } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Make modal control simpler!
|
|
4
|
+
* @param options.ModalWrapper the modal wrapper component function
|
|
5
|
+
* @param options.mapModalProps function that return modal wrapper props
|
|
6
|
+
* @param options.onOpen function trigger when open modal
|
|
7
|
+
* @param options.onClose function trigger when close modal
|
|
8
|
+
* @param options.useHookValue custom hooks to return any hook value to be consumed by mapModalProps. Similar purpose as customValues
|
|
9
|
+
* @param options.useContextModals return modals to be use in modal context provider
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // A MUI Dialog Example with Custom Way to Disable Backdrop Click Exit
|
|
14
|
+
* const simplerModalControl = makeModalControlSimpler({
|
|
15
|
+
* ModalWrapper: Dialog,
|
|
16
|
+
* mapModalProps: (
|
|
17
|
+
* { open, handleClose },
|
|
18
|
+
* modalProps,
|
|
19
|
+
* customProps?: { disableBackdropClick?: boolean }
|
|
20
|
+
* ) => ({
|
|
21
|
+
* fullWidth: true,
|
|
22
|
+
* ...restProps,
|
|
23
|
+
* open,
|
|
24
|
+
* onClose: customProps?.disableBackdropClick ? undefined : handleClose,
|
|
25
|
+
* }),
|
|
26
|
+
* useContextModals: useModal => ({
|
|
27
|
+
* fooGlobalModal: useModal(FooGlobalModal, {
|
|
28
|
+
* defaultProps:{
|
|
29
|
+
* foo: "Some value pass into modal content"
|
|
30
|
+
* },
|
|
31
|
+
* modalProps: {
|
|
32
|
+
* maxWidth: 'xs',
|
|
33
|
+
* },
|
|
34
|
+
* customValues: {
|
|
35
|
+
* disableBackdropClick: true,
|
|
36
|
+
* }
|
|
37
|
+
* }),
|
|
38
|
+
* otherGLobalModal: useModal(OtherGlobalModal)
|
|
39
|
+
* })
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* const {
|
|
43
|
+
* useModal,
|
|
44
|
+
* useModalContextSelector,
|
|
45
|
+
* useModalContext,
|
|
46
|
+
* ModalContextProvider
|
|
47
|
+
* } = simplerModalControl;
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare const makeModalControlSimpler: <TModalProps, TCustomValues, THookValues, TContextModals extends Record<string, UseModalReturnType<any>>, TContextModalsKey extends keyof TContextModals, TModalContextValue extends { [K in TContextModalsKey]: [TContextModals[K][1], TContextModals[K][2]]; }>({ ModalWrapper, mapModalProps, onOpen, onClose, useHookValue, useContextModals, }: MakeModalControlSimplerProps<TModalProps, TCustomValues, THookValues, TContextModals>) => {
|
|
51
|
+
useModal: UseModalFn<TModalProps, TCustomValues>;
|
|
52
|
+
ModalContextProvider: FC<{
|
|
53
|
+
children: ReactNode;
|
|
54
|
+
}>;
|
|
55
|
+
useModalContext: () => TModalContextValue;
|
|
56
|
+
useModalContextSelector: <K_1 extends TContextModalsKey>(key: K_1) => TModalContextValue[K_1];
|
|
57
|
+
};
|
|
58
|
+
export default makeModalControlSimpler;
|
|
59
|
+
/**
|
|
60
|
+
* Types
|
|
61
|
+
*/
|
|
62
|
+
export declare type DefaultModalProps = {
|
|
63
|
+
onClose: () => void;
|
|
64
|
+
open?: boolean;
|
|
65
|
+
};
|
|
66
|
+
export declare type UseModalReturnType<T = any> = [
|
|
67
|
+
JSX.Element,
|
|
68
|
+
(newProps?: NewPropsType<T>) => void,
|
|
69
|
+
() => void
|
|
70
|
+
];
|
|
71
|
+
declare type NewPropsType<T> = Omit<T, "onClose">;
|
|
72
|
+
declare type UseModalFn<TModalProps, TCustomValues> = <T extends Partial<DefaultModalProps>>(ModalContent: ComponentType<T>, options?: {
|
|
73
|
+
defaultProps?: NewPropsType<T> | undefined;
|
|
74
|
+
modalProps?: Partial<TModalProps> | undefined;
|
|
75
|
+
customValues?: TCustomValues;
|
|
76
|
+
}) => UseModalReturnType<T>;
|
|
77
|
+
declare type UseContextModalsFn<TModalProps, TContextModals, TCustomValues> = (useModal: UseModalFn<TModalProps, TCustomValues>) => TContextModals;
|
|
78
|
+
declare type UseHookValueFn<THookValues> = () => THookValues;
|
|
79
|
+
declare type MakeModalControlSimplerProps<TModalProps, TCustomValues, THookValues, TContextModals> = {
|
|
80
|
+
ModalWrapper: ComponentType<TModalProps>;
|
|
81
|
+
mapModalProps: <T>(modalControl: {
|
|
82
|
+
open: boolean;
|
|
83
|
+
handleClose: () => void;
|
|
84
|
+
handleOpen: () => void;
|
|
85
|
+
setOpen: Dispatch<SetStateAction<boolean>>;
|
|
86
|
+
setProps: Dispatch<SetStateAction<T>>;
|
|
87
|
+
}, modalProps?: Partial<TModalProps>, customValues?: TCustomValues, hookValues?: THookValues) => TModalProps;
|
|
88
|
+
onOpen?: (hookValues?: THookValues) => void;
|
|
89
|
+
onClose?: (hookValues?: THookValues) => void;
|
|
90
|
+
useHookValue?: UseHookValueFn<THookValues>;
|
|
91
|
+
useContextModals?: UseContextModalsFn<TModalProps, TContextModals, TCustomValues>;
|
|
92
|
+
};
|
|
93
|
+
export declare const EMPTY_FUNCTION: () => void;
|
package/index.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState, useCallback, useMemo, memo, createContext, useContext, Fragment, } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* Make modal control simpler!
|
|
5
|
+
* @param options.ModalWrapper the modal wrapper component function
|
|
6
|
+
* @param options.mapModalProps function that return modal wrapper props
|
|
7
|
+
* @param options.onOpen function trigger when open modal
|
|
8
|
+
* @param options.onClose function trigger when close modal
|
|
9
|
+
* @param options.useHookValue custom hooks to return any hook value to be consumed by mapModalProps. Similar purpose as customValues
|
|
10
|
+
* @param options.useContextModals return modals to be use in modal context provider
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* // A MUI Dialog Example with Custom Way to Disable Backdrop Click Exit
|
|
15
|
+
* const simplerModalControl = makeModalControlSimpler({
|
|
16
|
+
* ModalWrapper: Dialog,
|
|
17
|
+
* mapModalProps: (
|
|
18
|
+
* { open, handleClose },
|
|
19
|
+
* modalProps,
|
|
20
|
+
* customProps?: { disableBackdropClick?: boolean }
|
|
21
|
+
* ) => ({
|
|
22
|
+
* fullWidth: true,
|
|
23
|
+
* ...restProps,
|
|
24
|
+
* open,
|
|
25
|
+
* onClose: customProps?.disableBackdropClick ? undefined : handleClose,
|
|
26
|
+
* }),
|
|
27
|
+
* useContextModals: useModal => ({
|
|
28
|
+
* fooGlobalModal: useModal(FooGlobalModal, {
|
|
29
|
+
* defaultProps:{
|
|
30
|
+
* foo: "Some value pass into modal content"
|
|
31
|
+
* },
|
|
32
|
+
* modalProps: {
|
|
33
|
+
* maxWidth: 'xs',
|
|
34
|
+
* },
|
|
35
|
+
* customValues: {
|
|
36
|
+
* disableBackdropClick: true,
|
|
37
|
+
* }
|
|
38
|
+
* }),
|
|
39
|
+
* otherGLobalModal: useModal(OtherGlobalModal)
|
|
40
|
+
* })
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* const {
|
|
44
|
+
* useModal,
|
|
45
|
+
* useModalContextSelector,
|
|
46
|
+
* useModalContext,
|
|
47
|
+
* ModalContextProvider
|
|
48
|
+
* } = simplerModalControl;
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose, useHookValue = EMPTY_FUNCTION, useContextModals = (_) => EMPTY_OBJECT, }) => {
|
|
52
|
+
/**
|
|
53
|
+
*
|
|
54
|
+
* @param ModalContent modal content component function
|
|
55
|
+
* @param options
|
|
56
|
+
* @param options.defaultProps props to feed modal content
|
|
57
|
+
* @param options.modalProps modal props
|
|
58
|
+
* @param options.customValues custom values to be consumed by mapModalProps. Similar purpose as useHookValue
|
|
59
|
+
* @returns
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```
|
|
63
|
+
* interface MyModalProps extends DefaultModalProps {
|
|
64
|
+
* customMessage: string
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* const MyModal = ({ onClose, customMessage }: MyModalProps) => {
|
|
68
|
+
* return (
|
|
69
|
+
* <div>
|
|
70
|
+
* <span>Some Modal Content: {customMessage}</span>
|
|
71
|
+
* <button onClick={onClose}>Close</button>
|
|
72
|
+
* </div>
|
|
73
|
+
* );
|
|
74
|
+
* }
|
|
75
|
+
*
|
|
76
|
+
* const MyConsumer = () => {
|
|
77
|
+
* const [MyModalNode, handleOpen, handleClose] = useModal(MyModal, {
|
|
78
|
+
* defaultProps:{
|
|
79
|
+
* customMessage: "initial message"
|
|
80
|
+
* },
|
|
81
|
+
* modalProps: {
|
|
82
|
+
* maxWidth: 'xs',
|
|
83
|
+
* },
|
|
84
|
+
* customValues: {
|
|
85
|
+
* disableBackdropClick: true,
|
|
86
|
+
* }
|
|
87
|
+
* });
|
|
88
|
+
* return (
|
|
89
|
+
* <div>
|
|
90
|
+
* {MyModalNode}
|
|
91
|
+
* <button onClick={() => handleOpen({ customMessage:"updated message" })}>Open</button>
|
|
92
|
+
* </div>
|
|
93
|
+
* );
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
const useModal = (ModalContent, options) => {
|
|
98
|
+
const defaultProps = options === null || options === void 0 ? void 0 : options.defaultProps;
|
|
99
|
+
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
|
+
setOpen(true);
|
|
104
|
+
setProps(newProps);
|
|
105
|
+
if (onOpen)
|
|
106
|
+
onOpen(hookValues);
|
|
107
|
+
}, [hookValues]);
|
|
108
|
+
const handleClose = useCallback(() => {
|
|
109
|
+
setOpen(false);
|
|
110
|
+
setProps(undefined);
|
|
111
|
+
if (onClose)
|
|
112
|
+
onClose(hookValues);
|
|
113
|
+
}, [hookValues]);
|
|
114
|
+
const ModalContentMemo = useMemo(
|
|
115
|
+
// 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), [
|
|
118
|
+
handleClose,
|
|
119
|
+
handleOpen,
|
|
120
|
+
hookValues,
|
|
121
|
+
open,
|
|
122
|
+
options === null || options === void 0 ? void 0 : options.customValues,
|
|
123
|
+
options === null || options === void 0 ? void 0 : options.modalProps,
|
|
124
|
+
]);
|
|
125
|
+
const modal = (_jsx(ModalWrapper, Object.assign({}, mappedModalProps, { children: _jsx(ModalContentMemo, Object.assign({}, defaultProps, props, { open: open, onClose: handleClose })) })));
|
|
126
|
+
return [modal, handleOpen, handleClose];
|
|
127
|
+
};
|
|
128
|
+
const ModalContext = createContext(EMPTY_OBJECT);
|
|
129
|
+
/**
|
|
130
|
+
* Select all modal handlers from global modal context
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* const {
|
|
135
|
+
* modal1: [handleOpenModal1, handleCloseModal1],
|
|
136
|
+
* modal2: [handleOpenModal2, handleCloseModal2]
|
|
137
|
+
* } = useModalContext();
|
|
138
|
+
*
|
|
139
|
+
* // open modal and pass props
|
|
140
|
+
* handleOpenModal1({ fooProp : "pass in props to the modal"});
|
|
141
|
+
* handleOpenModal2({ diffModalProp : "something else"});
|
|
142
|
+
*
|
|
143
|
+
* // close modal
|
|
144
|
+
* handleCloseModal1();
|
|
145
|
+
* handleCloseModal2();
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
const useModalContext = () => useContext(ModalContext);
|
|
149
|
+
/**
|
|
150
|
+
* Select a single modal open and close handler from global modal context
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* const [handleOpen, handleClose] = useModalContextSelector("fooModal")
|
|
155
|
+
*
|
|
156
|
+
* // open modal and pass props
|
|
157
|
+
* handleOpen({fooProp:"pass in props to the modal"});
|
|
158
|
+
*
|
|
159
|
+
* // close modal
|
|
160
|
+
* handleClose();
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
const useModalContextSelector = (key) => { var _a; return ((_a = useContext(ModalContext)) === null || _a === void 0 ? void 0 : _a[key]) || EMPTY_LIST; };
|
|
164
|
+
/**
|
|
165
|
+
* A global context provider to store reusable modals
|
|
166
|
+
* Consumer can open, close, pass props to any modals using context value handlers.
|
|
167
|
+
* Use `useModalContextSelect` to select one modal handler
|
|
168
|
+
* Use `useModalContext` to select multiple modal handlers
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* // put provider in root level
|
|
173
|
+
* <ModalContextProvider>
|
|
174
|
+
* <App/>
|
|
175
|
+
* </ModalContextProvider>
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
const ModalContextProvider = ({ children }) => {
|
|
179
|
+
const modals = useContextModals(useModal);
|
|
180
|
+
// context value will be map of handlers. Only need to initialize once
|
|
181
|
+
const contextValue = useMemo(() => Object.entries(modals).reduce((acc, [key, [_, ...handlers]]) => {
|
|
182
|
+
acc[key] =
|
|
183
|
+
handlers;
|
|
184
|
+
return acc;
|
|
185
|
+
}, {}),
|
|
186
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
187
|
+
[]);
|
|
188
|
+
return (_jsxs(ModalContext.Provider, Object.assign({ value: contextValue }, { children: [Object.values(modals).map(([modalNode], i) => (_jsx(Fragment, { children: modalNode }, i))), children] })));
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
useModal,
|
|
192
|
+
ModalContextProvider,
|
|
193
|
+
useModalContext,
|
|
194
|
+
useModalContextSelector,
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
export default makeModalControlSimpler;
|
|
198
|
+
const EMPTY_LIST = [];
|
|
199
|
+
const EMPTY_OBJECT = {};
|
|
200
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
201
|
+
export const EMPTY_FUNCTION = () => { };
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@catconsult/simplify-modal-control",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Simplify React modal control flow",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "jest --watch",
|
|
12
|
+
"test:ci": "jest --ci",
|
|
13
|
+
"clean": "shx rm -rf dist",
|
|
14
|
+
"build": "npm run clean && tsc && shx cp package.json README.md ./dist"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/Catalyst-Consulting-Group/simplify-modal-control.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"modal",
|
|
22
|
+
"modal-control",
|
|
23
|
+
"popup",
|
|
24
|
+
"simplify-modal",
|
|
25
|
+
"open-modal",
|
|
26
|
+
"close-modal"
|
|
27
|
+
],
|
|
28
|
+
"author": "Wei J. Zheng",
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/Catalyst-Consulting-Group/simplify-modal-control/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/Catalyst-Consulting-Group/simplify-modal-control#readme",
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@testing-library/react": "^13.4.0",
|
|
39
|
+
"@types/jest": "^29.1.2",
|
|
40
|
+
"@types/react": "^18.0.21",
|
|
41
|
+
"jest": "^29.1.2",
|
|
42
|
+
"jest-environment-jsdom": "^29.1.2",
|
|
43
|
+
"react": "^18.2.0",
|
|
44
|
+
"react-dom": "^18.2.0",
|
|
45
|
+
"shx": "^0.3.4",
|
|
46
|
+
"ts-jest": "^29.0.3",
|
|
47
|
+
"typescript": "^4.8.4"
|
|
48
|
+
}
|
|
49
|
+
}
|