@catconsult/simplify-modal-control 1.1.1 → 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/README.md +268 -189
- package/index.d.ts +96 -13
- package/index.d.ts.map +1 -1
- package/index.js +116 -10
- package/index.js.map +1 -1
- package/package.json +15 -12
package/README.md
CHANGED
|
@@ -2,255 +2,334 @@
|
|
|
2
2
|
|
|
3
3
|
# simplify-modal-control
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Simplify modal control flow in React. This library controls modals: opening and closing them, passing props, and making them reusable. You still choose your own modal component (MUI `Dialog`, `react-modal`, a React Native modal, and so on) as the wrapper.
|
|
6
6
|
|
|
7
7
|
# Features
|
|
8
8
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
- Extremely lightweight, just ~10 KB in package size
|
|
17
|
-
- Written in TypeScript, every pieces are well Typed!
|
|
9
|
+
- Open and close modals, and pass props into them, with one function call
|
|
10
|
+
- Declare global modals once and open them from anywhere, even outside React with `openModal`
|
|
11
|
+
- **New in v2:** register each modal in its own file, with optional lazy loading
|
|
12
|
+
- Type-safe: modal keys autocomplete and modal props are checked, including keys declared once with `ModalRegistry` module augmentation
|
|
13
|
+
- No content flicker during close transitions, and no re-renders while a modal is closed
|
|
14
|
+
- Works with any modal component
|
|
15
|
+
- No dependencies besides React 18 or later
|
|
18
16
|
|
|
19
|
-
#
|
|
20
|
-
|
|
21
|
-
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:
|
|
22
|
-
|
|
23
|
-
- Lots of boilerplate codes
|
|
24
|
-
- Hard to reuse and pass around props into the modal
|
|
25
|
-
- Introduce extra re-rendering caused by the parent component
|
|
26
|
-
- Modal component continue to trigger re-rendering in close state
|
|
27
|
-
- Modal content flickering during close transition caused by change modal state
|
|
28
|
-
|
|
29
|
-
This library addressed all the above issues by introduce a simpler modal control flow.
|
|
30
|
-
|
|
31
|
-
# Quick Installation
|
|
17
|
+
# Installation
|
|
32
18
|
|
|
33
19
|
```
|
|
34
|
-
|
|
20
|
+
npm i @catconsult/simplify-modal-control
|
|
35
21
|
```
|
|
36
22
|
|
|
37
|
-
|
|
23
|
+
# Getting started
|
|
38
24
|
|
|
39
|
-
|
|
40
|
-
npm i @catconsult/simplify-modal-control
|
|
41
|
-
```
|
|
25
|
+
## 1. Create your modal control
|
|
42
26
|
|
|
43
|
-
|
|
27
|
+
Create it once, for example in `modalControl.ts`, and export what you need.
|
|
44
28
|
|
|
45
29
|
```tsx
|
|
46
|
-
|
|
47
|
-
import { Dialog, DialogContent } from '@mui/material';
|
|
30
|
+
import { Dialog } from '@mui/material';
|
|
48
31
|
import makeModalControlSimpler from '@catconsult/simplify-modal-control';
|
|
49
32
|
|
|
50
|
-
|
|
51
|
-
const MyModal = ({
|
|
52
|
-
onClose,
|
|
53
|
-
customMessage,
|
|
54
|
-
}: DefaultModalProps & {
|
|
55
|
-
customMessage: string;
|
|
56
|
-
}) => {
|
|
57
|
-
return (
|
|
58
|
-
<>
|
|
59
|
-
<DialogContent>
|
|
60
|
-
<span>Modal Content: {customMessage}</span>
|
|
61
|
-
<button onClick={onClose}>Close</button>
|
|
62
|
-
</DialogContent>
|
|
63
|
-
</>
|
|
64
|
-
);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// construct modal controls
|
|
68
|
-
const {
|
|
33
|
+
export const {
|
|
69
34
|
useModal,
|
|
70
|
-
useModalContextSelector,
|
|
71
35
|
useModalContext,
|
|
36
|
+
useModalContextSelector,
|
|
72
37
|
ModalContextProvider,
|
|
73
38
|
openModal,
|
|
74
39
|
closeModal,
|
|
75
40
|
} = makeModalControlSimpler({
|
|
41
|
+
// the component that wraps every modal's content
|
|
76
42
|
ModalWrapper: Dialog,
|
|
77
|
-
//
|
|
43
|
+
// map the modal state to the wrapper's props
|
|
78
44
|
mapModalProps: ({ open, handleClose }, modalProps) => ({
|
|
79
45
|
...modalProps,
|
|
80
46
|
open,
|
|
81
47
|
onClose: handleClose,
|
|
82
48
|
}),
|
|
83
|
-
// define your global modals
|
|
84
|
-
useContextModals: useModal => ({
|
|
85
|
-
myModal0: useModal(MyModal),
|
|
86
|
-
myModal1: useModal(MyModal),
|
|
87
|
-
myModal2: useModal(MyModal, {
|
|
88
|
-
modalProps: {
|
|
89
|
-
// pass props to modal wrapper
|
|
90
|
-
maxWidth: 'xs',
|
|
91
|
-
},
|
|
92
|
-
}),
|
|
93
|
-
myModal3: useModal(MyModal, {
|
|
94
|
-
// pass default props to modal content
|
|
95
|
-
defaultProps: { customMessage: 'something' },
|
|
96
|
-
}),
|
|
97
|
-
}),
|
|
98
49
|
});
|
|
50
|
+
```
|
|
99
51
|
|
|
100
|
-
|
|
101
|
-
const MyConsumer = () => {
|
|
102
|
-
// select single from global context
|
|
103
|
-
const [handleOpenMyModal1, handleCloseMyModal1] =
|
|
104
|
-
useModalContextSelector('myModal1');
|
|
52
|
+
## 2. Write a modal
|
|
105
53
|
|
|
106
|
-
|
|
107
|
-
const {
|
|
108
|
-
myModal2: [handleOpenMyModal2],
|
|
109
|
-
myModal3: [handleOpenMyModal3],
|
|
110
|
-
} = useModalContext();
|
|
54
|
+
A modal is a component. Along with its own props, it receives `onClose`, `open` and `setModalProps`:
|
|
111
55
|
|
|
112
|
-
|
|
113
|
-
|
|
56
|
+
```tsx
|
|
57
|
+
import { DialogContent } from '@mui/material';
|
|
58
|
+
import { DefaultModalProps } from '@catconsult/simplify-modal-control';
|
|
114
59
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
60
|
+
type MessageModalProps = DefaultModalProps & { message: string };
|
|
61
|
+
|
|
62
|
+
export const MessageModal = ({ onClose, message }: MessageModalProps) => (
|
|
63
|
+
<DialogContent>
|
|
64
|
+
<p>{message}</p>
|
|
65
|
+
<button onClick={onClose}>Close</button>
|
|
66
|
+
</DialogContent>
|
|
67
|
+
);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## 3. Use it
|
|
71
|
+
|
|
72
|
+
There are three ways to use a modal. They can be mixed freely.
|
|
73
|
+
|
|
74
|
+
| Way | Best for | Defined in |
|
|
75
|
+
| -------------------- | ------------------------------------------------ | -------------------------------------------- |
|
|
76
|
+
| `useModal` | A modal used by a single component | That component |
|
|
77
|
+
| Registered modals | Global modals, one file per modal, optional lazy | `*.register.ts` files + `registry` (v2) |
|
|
78
|
+
| `useContextModals` | Global modals declared inline | The `makeModalControlSimpler` call |
|
|
79
|
+
|
|
80
|
+
### Local modal with `useModal`
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
const Page = () => {
|
|
84
|
+
const [messageModal, openMessage, closeMessage] = useModal(MessageModal);
|
|
131
85
|
|
|
132
|
-
export const App = () => {
|
|
133
86
|
return (
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
87
|
+
<>
|
|
88
|
+
{messageModal}
|
|
89
|
+
<button onClick={() => openMessage({ message: 'Hi!' })}>Open</button>
|
|
90
|
+
</>
|
|
137
91
|
);
|
|
138
92
|
};
|
|
139
93
|
```
|
|
140
94
|
|
|
141
|
-
|
|
95
|
+
### Registered modals (v2)
|
|
142
96
|
|
|
143
|
-
|
|
144
|
-
- [React Native `react-native-modalize`](https://snack.expo.dev/@dhananjay.soneji/catalyst-simplify-modal-control?platform=android)
|
|
97
|
+
Register each global modal next to the modal itself:
|
|
145
98
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
* @param options.useContextModals return modals to be use in modal context provider
|
|
159
|
-
*
|
|
160
|
-
* @returns { { useModal, useModalContext, useModalContextSelector, ModalContextProvider } }
|
|
161
|
-
*/
|
|
162
|
-
makeModalControlSimpler(options);
|
|
99
|
+
- **Lazy modals (`registerLazyModal`) need a separate register file.** The registry imports only that small file, and the modal's code stays in its own chunk. If the registration were in the modal's own file, the registry would import the whole modal and lazy loading would be lost. When this happens, the library logs a console error the first time that modal loads.
|
|
100
|
+
- **Eager modals (`registerModal`) can be registered in the component's own file**, because the component is loaded up front anyway. A separate `*.register.ts` file is still a good habit for consistency.
|
|
101
|
+
|
|
102
|
+
```tsx
|
|
103
|
+
// FindTrainingModal.register.ts
|
|
104
|
+
import { registerLazyModal } from '@catconsult/simplify-modal-control';
|
|
105
|
+
|
|
106
|
+
export const findTraining = registerLazyModal(
|
|
107
|
+
'findTraining',
|
|
108
|
+
() => import('./FindTrainingModal'), // must default-export the component
|
|
109
|
+
{ modalProps: { maxWidth: 'md' } },
|
|
110
|
+
);
|
|
163
111
|
```
|
|
164
112
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
## closeModal
|
|
182
|
-
|
|
183
|
-
````ts
|
|
184
|
-
/**
|
|
185
|
-
* Close modal without using hooks
|
|
186
|
-
* @param key modal key
|
|
187
|
-
* @example
|
|
188
|
-
* ```ts
|
|
189
|
-
* closeModal("fooModal");
|
|
190
|
-
* ```
|
|
191
|
-
*/
|
|
192
|
-
closeModal(key);
|
|
193
|
-
````
|
|
194
|
-
|
|
195
|
-
## useModal
|
|
196
|
-
|
|
197
|
-
```ts
|
|
198
|
-
/**
|
|
199
|
-
* @param ModalContent modal content component function
|
|
200
|
-
* @param options
|
|
201
|
-
* @param options.defaultProps props to feed modal content
|
|
202
|
-
* @param options.modalProps modal props
|
|
203
|
-
* @param options.customValues custom values to be consumed by mapModalProps. Similar purpose as useHookValue
|
|
204
|
-
* @returns { [ModalNode, handleOpen, handleClose] }
|
|
205
|
-
*/
|
|
206
|
-
useModal(MyModal, UseModalOptions);
|
|
113
|
+
```tsx
|
|
114
|
+
// ConfirmModal.register.ts
|
|
115
|
+
import { registerModal } from '@catconsult/simplify-modal-control';
|
|
116
|
+
import { ConfirmModal, ConfirmModalProps } from './ConfirmModal';
|
|
117
|
+
|
|
118
|
+
// optional: declare the key and props once for the whole app (see "Type support" below)
|
|
119
|
+
declare module '@catconsult/simplify-modal-control' {
|
|
120
|
+
interface ModalRegistry {
|
|
121
|
+
confirm: ConfirmModalProps;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export const confirm = registerModal('confirm', ConfirmModal, {
|
|
126
|
+
defaultProps: { message: 'Are you sure?' },
|
|
127
|
+
});
|
|
207
128
|
```
|
|
208
129
|
|
|
209
|
-
|
|
130
|
+
List the registrations in one file and pass them to `makeModalControlSimpler`:
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
// modals.ts — add one line per registered modal
|
|
134
|
+
import { findTraining } from './training/FindTrainingModal.register';
|
|
135
|
+
import { confirm } from './common/ConfirmModal.register';
|
|
210
136
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
137
|
+
export const modals = [findTraining, confirm];
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```tsx
|
|
141
|
+
// modalControl.ts
|
|
142
|
+
import { modals } from './modals';
|
|
143
|
+
|
|
144
|
+
export const { ModalContextProvider, openModal, closeModal } =
|
|
145
|
+
makeModalControlSimpler({
|
|
146
|
+
ModalWrapper: Dialog,
|
|
147
|
+
mapModalProps: ({ open, handleClose }, modalProps) => ({
|
|
148
|
+
...modalProps,
|
|
149
|
+
open,
|
|
150
|
+
onClose: handleClose,
|
|
151
|
+
}),
|
|
152
|
+
registry: {
|
|
153
|
+
modals,
|
|
154
|
+
fallback: <Spinner />, // default loading UI for lazy modals
|
|
155
|
+
},
|
|
156
|
+
});
|
|
217
157
|
```
|
|
218
158
|
|
|
219
|
-
|
|
159
|
+
Put the provider at the root of your app, then open modals by key. Keys autocomplete, and each modal's props are type-checked:
|
|
220
160
|
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
161
|
+
```tsx
|
|
162
|
+
<ModalContextProvider>
|
|
163
|
+
<App />
|
|
164
|
+
</ModalContextProvider>;
|
|
165
|
+
|
|
166
|
+
openModal('findTraining', { courseId: 42 });
|
|
167
|
+
openModal('confirm', { message: 'Delete this item?' }, { maxWidth: 'xs' });
|
|
168
|
+
closeModal('confirm');
|
|
228
169
|
```
|
|
229
170
|
|
|
230
|
-
|
|
171
|
+
#### Type support with `ModalRegistry`
|
|
231
172
|
|
|
232
|
-
|
|
173
|
+
> **Recommended for apps with many modals.** Declare each modal's key and props once, in the file that registers it, by augmenting the `ModalRegistry` interface. The `declare module` block in `ConfirmModal.register.ts` above is an example.
|
|
233
174
|
|
|
234
|
-
|
|
235
|
-
- Yarn
|
|
236
|
-
- Editor/IDE that support TypeScript
|
|
175
|
+
With this declaration:
|
|
237
176
|
|
|
238
|
-
|
|
177
|
+
- `openModal`, `closeModal`, `useModalContext` and `useModalContextSelector` autocomplete `'confirm'` and check its props, everywhere in the app.
|
|
178
|
+
- `registerModal('confirm', ...)` and `registerLazyModal('confirm', ...)` report an error if the component doesn't accept `ConfirmModalProps`.
|
|
179
|
+
- The `modals` list doesn't need literal types, so you can build it however you like, for example `const modals: RegisteredModal[] = [...]`. A list typed as `RegisteredModal[]` contributes no keys of its own, so the declarations are what provide the types.
|
|
239
180
|
|
|
240
|
-
|
|
181
|
+
Without a declaration, types are inferred from the `modals` array literal, as for `findTraining` above. Both work, and you can mix them.
|
|
241
182
|
|
|
183
|
+
> A declaration is a promise to TypeScript: if you declare a key but never register it, `openModal` with that key type-checks but does nothing at runtime. Declarations are global, so in an app with several `makeModalControlSimpler` instances, every instance gets the declared keys.
|
|
184
|
+
|
|
185
|
+
**`registerModal` or `registerLazyModal`?**
|
|
186
|
+
|
|
187
|
+
- `registerLazyModal(key, loader, options)` takes a loader such as `() => import('./X')`. The loader runs the first time the modal opens, not when the app starts. `options.fallback` shows while it loads and overrides `registry.fallback`. For a named export, use `() => import('./X').then(m => ({ default: m.X }))`. If the import fails, for example on a network error or after a deploy, the modal closes and the rest of the app keeps running. The next open tries the import again. React logs the error to the console.
|
|
188
|
+
- `registerModal(key, Component, options)` takes a component that is ready to render. This includes `next/dynamic` components, which handle their own loading:
|
|
189
|
+
|
|
190
|
+
```tsx
|
|
191
|
+
export const chart = registerModal(
|
|
192
|
+
'chart',
|
|
193
|
+
dynamic(() => import('./ChartModal')),
|
|
194
|
+
);
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Every modal key must be unique. A key used twice in `registry.modals` throws when `makeModalControlSimpler` runs. A key used in both the registry and `useContextModals` throws when the provider renders.
|
|
198
|
+
|
|
199
|
+
### Context modals with `useContextModals`
|
|
200
|
+
|
|
201
|
+
The v1 way to declare global modals still works, and you can use it alongside the registry:
|
|
202
|
+
|
|
203
|
+
```tsx
|
|
204
|
+
makeModalControlSimpler({
|
|
205
|
+
ModalWrapper: Dialog,
|
|
206
|
+
mapModalProps,
|
|
207
|
+
useContextModals: useModal => ({
|
|
208
|
+
message: useModal(MessageModal),
|
|
209
|
+
welcome: useModal(MessageModal, {
|
|
210
|
+
defaultProps: { message: 'Welcome!' }, // default content props
|
|
211
|
+
modalProps: { maxWidth: 'xs' }, // wrapper props
|
|
212
|
+
}),
|
|
213
|
+
}),
|
|
214
|
+
});
|
|
242
215
|
```
|
|
243
|
-
yarn
|
|
244
|
-
```
|
|
245
216
|
|
|
246
|
-
|
|
217
|
+
### Consuming global modals
|
|
218
|
+
|
|
219
|
+
Registered modals and context modals are consumed the same way:
|
|
220
|
+
|
|
221
|
+
```tsx
|
|
222
|
+
// anywhere, even outside React
|
|
223
|
+
openModal('message', { message: 'Saved' });
|
|
224
|
+
|
|
225
|
+
// select one modal in a component
|
|
226
|
+
const [openMessage, closeMessage] = useModalContextSelector('message');
|
|
247
227
|
|
|
228
|
+
// select several
|
|
229
|
+
const {
|
|
230
|
+
message: [openMessage],
|
|
231
|
+
welcome: [openWelcome],
|
|
232
|
+
} = useModalContext();
|
|
248
233
|
```
|
|
249
|
-
|
|
234
|
+
|
|
235
|
+
# Customizing the wrapper
|
|
236
|
+
|
|
237
|
+
`mapModalProps` receives the modal state, the `modalProps` of the modal, its `customValues`, and the result of `useHookValue`. Use these to change how the wrapper behaves for each modal:
|
|
238
|
+
|
|
239
|
+
```tsx
|
|
240
|
+
makeModalControlSimpler({
|
|
241
|
+
ModalWrapper: Dialog,
|
|
242
|
+
mapModalProps: (
|
|
243
|
+
{ open, handleClose },
|
|
244
|
+
modalProps,
|
|
245
|
+
customValues?: { disableBackdropClick?: boolean },
|
|
246
|
+
) => ({
|
|
247
|
+
fullWidth: true,
|
|
248
|
+
...modalProps,
|
|
249
|
+
open,
|
|
250
|
+
onClose: customValues?.disableBackdropClick ? undefined : handleClose,
|
|
251
|
+
}),
|
|
252
|
+
onOpen: hookValues => {}, // runs on every open
|
|
253
|
+
onClose: hookValues => {}, // runs on every close
|
|
254
|
+
useHookValue: () => useSomeHook(), // value passed to mapModalProps, onOpen and onClose
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
useModal(MessageModal, { customValues: { disableBackdropClick: true } });
|
|
250
258
|
```
|
|
251
259
|
|
|
252
|
-
|
|
260
|
+
# API
|
|
261
|
+
|
|
262
|
+
## makeModalControlSimpler(options)
|
|
263
|
+
|
|
264
|
+
| Option | Description |
|
|
265
|
+
| ------------------ | ---------------------------------------------------------------------------------------- |
|
|
266
|
+
| `ModalWrapper` | Component that wraps each modal's content |
|
|
267
|
+
| `mapModalProps` | `(modalControl, modalProps, customValues, hookValues) => wrapperProps` |
|
|
268
|
+
| `onOpen` | Called with `hookValues` whenever a modal opens |
|
|
269
|
+
| `onClose` | Called with `hookValues` whenever a modal closes |
|
|
270
|
+
| `useHookValue` | Hook whose return value goes to `mapModalProps`, `onOpen` and `onClose` |
|
|
271
|
+
| `useContextModals` | `useModal => ({ key: useModal(Component, options) })`, the v1 global modals |
|
|
272
|
+
| `registry` | `{ modals: RegisteredModal[], fallback?: ReactNode }`, the v2 registered modals |
|
|
273
|
+
|
|
274
|
+
Returns `{ useModal, useModalContext, useModalContextSelector, ModalContextProvider, openModal, closeModal }`.
|
|
275
|
+
|
|
276
|
+
## useModal(Component, options?)
|
|
277
|
+
|
|
278
|
+
Returns `[modalNode, handleOpen, handleClose]`. Render `modalNode` yourself. Options:
|
|
279
|
+
|
|
280
|
+
- `defaultProps`: default content props. Pass `open: true` to start the modal open.
|
|
281
|
+
- `modalProps`: wrapper props
|
|
282
|
+
- `customValues`: values passed to `mapModalProps`
|
|
283
|
+
|
|
284
|
+
`handleOpen(newProps?, newModalProps?)` opens the modal. It merges `newProps` over `defaultProps`, and `newModalProps` over the mapped wrapper props. `handleClose()` closes the modal and clears both.
|
|
285
|
+
|
|
286
|
+
## registerModal(key, Component, options?) / registerLazyModal(key, loader, options?)
|
|
287
|
+
|
|
288
|
+
These take the same options as `useModal`. `registerLazyModal` also accepts `fallback`. Both return a registration to put in `registry.modals`.
|
|
289
|
+
|
|
290
|
+
`modalProps` in a registration is not type-checked against the wrapper's props, because the register file does not import your modal control. The `newModalProps` argument of `openModal` is type-checked.
|
|
291
|
+
|
|
292
|
+
## ModalRegistry
|
|
293
|
+
|
|
294
|
+
An empty interface that you augment with `declare module '@catconsult/simplify-modal-control' { interface ModalRegistry { key: Props } }`. Each declared key is typed across the API and checked in `registerModal` and `registerLazyModal`. See [Type support with `ModalRegistry`](#type-support-with-modalregistry).
|
|
295
|
+
|
|
296
|
+
## openModal(key, newProps?, newModalProps?) / closeModal(key)
|
|
297
|
+
|
|
298
|
+
Open or close a global modal without a hook. `ModalContextProvider` must be mounted.
|
|
299
|
+
|
|
300
|
+
## useModalContext() / useModalContextSelector(key)
|
|
301
|
+
|
|
302
|
+
`useModalContext()` returns `{ [key]: [handleOpen, handleClose] }` for every global modal. `useModalContextSelector(key)` returns the `[handleOpen, handleClose]` pair for one modal.
|
|
303
|
+
|
|
304
|
+
## DefaultModalProps
|
|
305
|
+
|
|
306
|
+
`{ onClose: () => void; open?: boolean; setModalProps?: (modalProps) => void }`. These props are passed to every modal component.
|
|
307
|
+
|
|
308
|
+
# Migrating from v1
|
|
309
|
+
|
|
310
|
+
Code written for v1 keeps working. You can move to registered modals one modal at a time:
|
|
311
|
+
|
|
312
|
+
1. Create `MyModal.register.ts` with `registerModal('myModal', MyModal, sameOptions)` or `registerLazyModal(...)`. Optionally declare `myModal: MyModalProps` in `ModalRegistry` in the same file.
|
|
313
|
+
2. Add the registration to your `modals` list.
|
|
314
|
+
3. Remove the `myModal` entry from `useContextModals`. Calls such as `openModal('myModal', ...)` stay the same.
|
|
315
|
+
|
|
316
|
+
Breaking changes in v2:
|
|
317
|
+
|
|
318
|
+
- The generic parameters of `makeModalControlSimpler` changed. This matters only if you passed them explicitly. Let them be inferred.
|
|
319
|
+
- Without `useContextModals` or `registry`, `openModal` and `closeModal` no longer accept arbitrary string keys.
|
|
320
|
+
|
|
321
|
+
# Demo
|
|
322
|
+
|
|
323
|
+
- [`React` + `MUI Dialog`](https://codesandbox.io/s/objective-agnesi-wercyl?file=/src/App.tsx)
|
|
324
|
+
- [React Native `react-native-modalize`](https://snack.expo.dev/@dhananjay.soneji/catalyst-simplify-modal-control?platform=android)
|
|
325
|
+
|
|
326
|
+
# Local development
|
|
327
|
+
|
|
328
|
+
Requires Node.js 20 or later and npm.
|
|
253
329
|
|
|
254
330
|
```
|
|
255
|
-
|
|
331
|
+
npm ci # install dependencies
|
|
332
|
+
npm test # run tests in watch mode
|
|
333
|
+
npm run test:ci # run tests once
|
|
334
|
+
npm run build # build to ./dist
|
|
256
335
|
```
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ComponentType, Dispatch, SetStateAction, ReactNode, FC } from 'react';
|
|
1
|
+
import { ComponentType, Dispatch, SetStateAction, ReactNode, FC, JSX } from 'react';
|
|
2
2
|
/**
|
|
3
3
|
* Make modal control simpler!
|
|
4
4
|
* @param options.ModalWrapper the modal wrapper component function
|
|
@@ -7,6 +7,7 @@ import { ComponentType, Dispatch, SetStateAction, ReactNode, FC } from 'react';
|
|
|
7
7
|
* @param options.onClose function trigger when close modal
|
|
8
8
|
* @param options.useHookValue custom hooks to return any hook value to be consumed by mapModalProps. Similar purpose as customValues
|
|
9
9
|
* @param options.useContextModals return modals to be use in modal context provider
|
|
10
|
+
* @param options.registry modals registered with `registerModal` / `registerLazyModal`, plus a default loading `fallback`
|
|
10
11
|
*
|
|
11
12
|
* @example
|
|
12
13
|
* ```ts
|
|
@@ -47,39 +48,116 @@ import { ComponentType, Dispatch, SetStateAction, ReactNode, FC } from 'react';
|
|
|
47
48
|
* } = simplerModalControl;
|
|
48
49
|
* ```
|
|
49
50
|
*/
|
|
50
|
-
declare const makeModalControlSimpler: <TModalProps, TCustomValues, THookValues, TContextModals extends Record<string, UseModalReturnType
|
|
51
|
+
declare const makeModalControlSimpler: <TModalProps, TCustomValues, THookValues, TContextModals extends Record<string, UseModalReturnType> = {}, TRegistry extends readonly RegisteredModal[] = []>({ ModalWrapper, mapModalProps, onOpen, onClose, useHookValue, useContextModals, registry, }: MakeModalControlSimplerProps<TModalProps, TCustomValues, THookValues, TContextModals, TRegistry>) => {
|
|
51
52
|
useModal: UseModalFn<TModalProps, TCustomValues>;
|
|
52
53
|
ModalContextProvider: FC<{
|
|
53
54
|
children: ReactNode;
|
|
54
55
|
}>;
|
|
55
|
-
useModalContext: () =>
|
|
56
|
-
useModalContextSelector: <
|
|
57
|
-
openModal: <
|
|
58
|
-
closeModal: <
|
|
56
|
+
useModalContext: () => ModalContextValue<TContextModals, TRegistry, TModalProps>;
|
|
57
|
+
useModalContextSelector: <K extends keyof TContextModals | keyof { [R in TRegistry[number] as string extends R["key"] ? never : R["key"]]: R extends RegisteredModal<string, infer T> ? [(newProps?: NewPropsType<T> | undefined, newModalProps?: Partial<TModalProps> | undefined) => void, () => void] : never; }>(key: K) => ModalContextValue<TContextModals, TRegistry, TModalProps>[K];
|
|
58
|
+
openModal: <K extends keyof TContextModals | keyof { [R in TRegistry[number] as string extends R["key"] ? never : R["key"]]: R extends RegisteredModal<string, infer T> ? [(newProps?: NewPropsType<T> | undefined, newModalProps?: Partial<TModalProps> | undefined) => void, () => void] : never; }>(key: K, newProps?: Parameters<ModalContextValue<TContextModals, TRegistry, TModalProps>[K][0]>[0], newModalProps?: Partial<TModalProps>) => void;
|
|
59
|
+
closeModal: <K extends keyof TContextModals | keyof { [R in TRegistry[number] as string extends R["key"] ? never : R["key"]]: R extends RegisteredModal<string, infer T> ? [(newProps?: NewPropsType<T> | undefined, newModalProps?: Partial<TModalProps> | undefined) => void, () => void] : never; }>(key: K) => void;
|
|
59
60
|
};
|
|
60
61
|
export default makeModalControlSimpler;
|
|
62
|
+
/**
|
|
63
|
+
* Register a modal in its own file, then pass it to
|
|
64
|
+
* `makeModalControlSimpler({ registry: { modals: [...] } })`.
|
|
65
|
+
* Use for regular components, including `next/dynamic` components.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* export const confirm = registerModal('confirm', ConfirmModal, {
|
|
70
|
+
* defaultProps: { message: 'Are you sure?' },
|
|
71
|
+
* modalProps: { maxWidth: 'xs' },
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare const registerModal: <K extends string, T extends Partial<DefaultModalProps>>(key: K, component: ComponentType<T> & DeclaredComponent<K>, options?: RegisterModalOptions<T>) => RegisteredModal<K, T>;
|
|
76
|
+
/**
|
|
77
|
+
* Register a lazily loaded modal. The loader runs the first time the modal opens;
|
|
78
|
+
* `fallback` (or the registry's default fallback) shows while it loads.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* export const findTraining = registerLazyModal(
|
|
83
|
+
* 'findTraining',
|
|
84
|
+
* () => import('./FindTrainingModal'),
|
|
85
|
+
* { modalProps: { maxWidth: 'md' }, fallback: <Spinner /> },
|
|
86
|
+
* );
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare const registerLazyModal: <K extends string, T extends Partial<DefaultModalProps>>(key: K, loader: () => Promise<{
|
|
90
|
+
default: ComponentType<T> & DeclaredComponent<K>;
|
|
91
|
+
}>, options?: RegisterLazyModalOptions<T>) => RegisteredModal<K, T>;
|
|
61
92
|
/**
|
|
62
93
|
* Types
|
|
63
94
|
*/
|
|
64
|
-
export
|
|
95
|
+
export type DefaultModalProps = {
|
|
65
96
|
onClose: () => void;
|
|
66
97
|
open?: boolean;
|
|
67
98
|
setModalProps?: (modalProps: any) => void;
|
|
68
99
|
};
|
|
69
|
-
export
|
|
100
|
+
export type UseModalReturnType<T = any, TModal = any> = [
|
|
70
101
|
JSX.Element,
|
|
71
102
|
(newProps?: NewPropsType<T>, newModalProps?: Partial<TModal>) => void,
|
|
72
103
|
() => void
|
|
73
104
|
];
|
|
74
|
-
|
|
75
|
-
|
|
105
|
+
type NewPropsType<T> = Omit<T, 'onClose'>;
|
|
106
|
+
export type RegisterModalOptions<T> = {
|
|
107
|
+
defaultProps?: NewPropsType<T>;
|
|
108
|
+
modalProps?: Record<string, any>;
|
|
109
|
+
customValues?: any;
|
|
110
|
+
};
|
|
111
|
+
export type RegisterLazyModalOptions<T> = RegisterModalOptions<T> & {
|
|
112
|
+
/** shown while the modal content loads; overrides `registry.fallback` */
|
|
113
|
+
fallback?: ReactNode;
|
|
114
|
+
};
|
|
115
|
+
export type RegisteredModal<K extends string = string, T = any> = {
|
|
116
|
+
key: K;
|
|
117
|
+
component?: ComponentType<T>;
|
|
118
|
+
loader?: () => Promise<{
|
|
119
|
+
default: ComponentType<T>;
|
|
120
|
+
}>;
|
|
121
|
+
options?: RegisterLazyModalOptions<T>;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Declare registered modal keys and their props once, for the whole app, with module augmentation.
|
|
125
|
+
* Declared keys are typed in `openModal`, `closeModal` and the context hooks without
|
|
126
|
+
* passing a typed `registry.modals` array, and `registerModal` checks the component against them.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* declare module '@catconsult/simplify-modal-control' {
|
|
131
|
+
* interface ModalRegistry {
|
|
132
|
+
* confirm: ConfirmModalProps;
|
|
133
|
+
* }
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export interface ModalRegistry {
|
|
138
|
+
}
|
|
139
|
+
type DeclaredComponent<K> = K extends keyof ModalRegistry ? ComponentType<ModalRegistry[K] & DefaultModalProps> : unknown;
|
|
140
|
+
type ModalContextValue<TContextModals extends Record<string, UseModalReturnType>, TRegistry extends readonly RegisteredModal[], TModalProps> = {
|
|
141
|
+
[K in keyof TContextModals]: [TContextModals[K][1], TContextModals[K][2]];
|
|
142
|
+
} & {
|
|
143
|
+
[K in keyof ModalRegistry]: [
|
|
144
|
+
UseModalReturnType<ModalRegistry[K], TModalProps>[1],
|
|
145
|
+
UseModalReturnType<ModalRegistry[K], TModalProps>[2]
|
|
146
|
+
];
|
|
147
|
+
} & {
|
|
148
|
+
[R in TRegistry[number] as string extends R['key'] ? never : R['key']]: R extends RegisteredModal<string, infer T> ? [
|
|
149
|
+
UseModalReturnType<T, TModalProps>[1],
|
|
150
|
+
UseModalReturnType<T, TModalProps>[2]
|
|
151
|
+
] : never;
|
|
152
|
+
};
|
|
153
|
+
type UseModalFn<TModalProps, TCustomValues> = <T extends Partial<DefaultModalProps>>(ModalContent: ComponentType<T>, options?: {
|
|
76
154
|
defaultProps?: NewPropsType<T> | undefined;
|
|
77
155
|
modalProps?: Partial<TModalProps> | undefined;
|
|
78
156
|
customValues?: TCustomValues;
|
|
79
157
|
}) => UseModalReturnType<T, TModalProps>;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
158
|
+
type UseContextModalsFn<TModalProps, TContextModals, TCustomValues> = (useModal: UseModalFn<TModalProps, TCustomValues>) => TContextModals;
|
|
159
|
+
type UseHookValueFn<THookValues> = () => THookValues;
|
|
160
|
+
type MakeModalControlSimplerProps<TModalProps, TCustomValues, THookValues, TContextModals, TRegistry> = {
|
|
83
161
|
ModalWrapper: ComponentType<TModalProps>;
|
|
84
162
|
mapModalProps: <T>(modalControl: {
|
|
85
163
|
open: boolean;
|
|
@@ -92,6 +170,11 @@ declare type MakeModalControlSimplerProps<TModalProps, TCustomValues, THookValue
|
|
|
92
170
|
onClose?: (hookValues?: THookValues) => void;
|
|
93
171
|
useHookValue?: UseHookValueFn<THookValues>;
|
|
94
172
|
useContextModals?: UseContextModalsFn<TModalProps, TContextModals, TCustomValues>;
|
|
173
|
+
registry?: {
|
|
174
|
+
modals: TRegistry;
|
|
175
|
+
/** default loading fallback for lazy modals */
|
|
176
|
+
fallback?: ReactNode;
|
|
177
|
+
};
|
|
95
178
|
};
|
|
96
179
|
export declare const EMPTY_FUNCTION: () => void;
|
|
97
180
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +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,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,E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K,IAAI,CAAC;IAC7C,YAAY,CAAC,EAAE,cAAc,CAAC,WAAW,CAAC,CAAC;IAC3C,gBAAgB,CAAC,EAAE,kBAAkB,CACnC,WAAW,EACX,cAAc,EACd,aAAa,CACd,CAAC;IACF,QAAQ,CAAC,EAAE;QACT,MAAM,EAAE,SAAS,CAAC;QAClB,+CAA+C;QAC/C,QAAQ,CAAC,EAAE,SAAS,CAAC;KACtB,CAAC;CACH,CAAC;AAKF,eAAO,MAAM,cAAc,YAAW,CAAC"}
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EMPTY_FUNCTION = void 0;
|
|
3
|
+
exports.EMPTY_FUNCTION = exports.registerLazyModal = exports.registerModal = void 0;
|
|
4
4
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
5
|
const react_1 = require("react");
|
|
6
6
|
/**
|
|
@@ -11,6 +11,7 @@ const react_1 = require("react");
|
|
|
11
11
|
* @param options.onClose function trigger when close modal
|
|
12
12
|
* @param options.useHookValue custom hooks to return any hook value to be consumed by mapModalProps. Similar purpose as customValues
|
|
13
13
|
* @param options.useContextModals return modals to be use in modal context provider
|
|
14
|
+
* @param options.registry modals registered with `registerModal` / `registerLazyModal`, plus a default loading `fallback`
|
|
14
15
|
*
|
|
15
16
|
* @example
|
|
16
17
|
* ```ts
|
|
@@ -51,7 +52,8 @@ const react_1 = require("react");
|
|
|
51
52
|
* } = simplerModalControl;
|
|
52
53
|
* ```
|
|
53
54
|
*/
|
|
54
|
-
const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose, useHookValue = exports.EMPTY_FUNCTION, useContextModals = _ => EMPTY_OBJECT, }) => {
|
|
55
|
+
const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose, useHookValue = exports.EMPTY_FUNCTION, useContextModals = _ => EMPTY_OBJECT, registry, }) => {
|
|
56
|
+
var _a;
|
|
55
57
|
/**
|
|
56
58
|
*
|
|
57
59
|
* @param ModalContent modal content component function
|
|
@@ -133,6 +135,22 @@ const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose,
|
|
|
133
135
|
const modal = ((0, jsx_runtime_1.jsx)(ModalWrapper, Object.assign({}, mappedModalProps, modalProps, { children: (0, jsx_runtime_1.jsx)(ModalContentMemo, Object.assign({}, defaultProps, props, { open: open, onClose: handleClose, setModalProps: setModalProps })) })));
|
|
134
136
|
return [modal, handleOpen, handleClose];
|
|
135
137
|
};
|
|
138
|
+
const registryKeys = new Set();
|
|
139
|
+
const registeredModals = ((_a = registry === null || registry === void 0 ? void 0 : registry.modals) !== null && _a !== void 0 ? _a : []).map(modal => {
|
|
140
|
+
var _a, _b, _c;
|
|
141
|
+
if (registryKeys.has(modal.key))
|
|
142
|
+
throw new Error(`simplify-modal-control: duplicate modal key "${modal.key}"`);
|
|
143
|
+
registryKeys.add(modal.key);
|
|
144
|
+
return Object.assign(Object.assign({}, modal), { component: modal.loader
|
|
145
|
+
? withSuspense(modal.loader, (_c = (_b = (_a = modal.options) === null || _a === void 0 ? void 0 : _a.fallback) !== null && _b !== void 0 ? _b : registry === null || registry === void 0 ? void 0 : registry.fallback) !== null && _c !== void 0 ? _c : null)
|
|
146
|
+
: modal.component });
|
|
147
|
+
});
|
|
148
|
+
// one component per registered modal keeps hook order stable
|
|
149
|
+
const RegisteredModalHost = ({ modal, handlers, }) => {
|
|
150
|
+
const result = useModal(modal.component, modal.options);
|
|
151
|
+
handlers[modal.key] = result;
|
|
152
|
+
return result[0];
|
|
153
|
+
};
|
|
136
154
|
const ModalContext = (0, react_1.createContext)(EMPTY_OBJECT);
|
|
137
155
|
/**
|
|
138
156
|
* Select all modal handlers from global modal context
|
|
@@ -213,15 +231,26 @@ const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose,
|
|
|
213
231
|
*/
|
|
214
232
|
const ModalContextProvider = ({ children }) => {
|
|
215
233
|
const modals = useContextModals(useModal);
|
|
234
|
+
for (const key of registryKeys)
|
|
235
|
+
if (key in modals)
|
|
236
|
+
throw new Error(`simplify-modal-control: duplicate modal key "${key}"`);
|
|
237
|
+
// latest handlers reported by each RegisteredModalHost
|
|
238
|
+
const registryHandlers = (0, react_1.useRef)({}).current;
|
|
216
239
|
// context value will be map of handlers. Only need to initialize once
|
|
217
|
-
const contextValue = (0, react_1.useMemo)(() =>
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
240
|
+
const contextValue = (0, react_1.useMemo)(() => {
|
|
241
|
+
const value = {};
|
|
242
|
+
for (const [key, [_, ...handlers]] of Object.entries(modals))
|
|
243
|
+
value[key] = handlers;
|
|
244
|
+
// stable proxies that always call the host's latest handlers
|
|
245
|
+
for (const key of registryKeys)
|
|
246
|
+
value[key] = [
|
|
247
|
+
(...args) => { var _a; return (_a = registryHandlers[key]) === null || _a === void 0 ? void 0 : _a[1](...args); },
|
|
248
|
+
() => { var _a; return (_a = registryHandlers[key]) === null || _a === void 0 ? void 0 : _a[2](); },
|
|
249
|
+
];
|
|
250
|
+
return (modalContextHandlersRef = value);
|
|
251
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
252
|
+
}, []);
|
|
253
|
+
return ((0, jsx_runtime_1.jsxs)(ModalContext.Provider, { value: contextValue, children: [Object.values(modals).map(([modalNode], i) => ((0, jsx_runtime_1.jsx)(react_1.Fragment, { children: modalNode }, i))), registeredModals.map(modal => ((0, jsx_runtime_1.jsx)(RegisteredModalHost, { modal: modal, handlers: registryHandlers }, modal.key))), children] }));
|
|
225
254
|
};
|
|
226
255
|
return {
|
|
227
256
|
useModal,
|
|
@@ -233,6 +262,83 @@ const makeModalControlSimpler = ({ ModalWrapper, mapModalProps, onOpen, onClose,
|
|
|
233
262
|
};
|
|
234
263
|
};
|
|
235
264
|
exports.default = makeModalControlSimpler;
|
|
265
|
+
/**
|
|
266
|
+
* Register a modal in its own file, then pass it to
|
|
267
|
+
* `makeModalControlSimpler({ registry: { modals: [...] } })`.
|
|
268
|
+
* Use for regular components, including `next/dynamic` components.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```ts
|
|
272
|
+
* export const confirm = registerModal('confirm', ConfirmModal, {
|
|
273
|
+
* defaultProps: { message: 'Are you sure?' },
|
|
274
|
+
* modalProps: { maxWidth: 'xs' },
|
|
275
|
+
* });
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
const registerModal = (key, component, options) => ({ key, component, options });
|
|
279
|
+
exports.registerModal = registerModal;
|
|
280
|
+
/**
|
|
281
|
+
* Register a lazily loaded modal. The loader runs the first time the modal opens;
|
|
282
|
+
* `fallback` (or the registry's default fallback) shows while it loads.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* export const findTraining = registerLazyModal(
|
|
287
|
+
* 'findTraining',
|
|
288
|
+
* () => import('./FindTrainingModal'),
|
|
289
|
+
* { modalProps: { maxWidth: 'md' }, fallback: <Spinner /> },
|
|
290
|
+
* );
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
const registerLazyModal = (key, loader, options) => {
|
|
294
|
+
const registration = {
|
|
295
|
+
key,
|
|
296
|
+
options,
|
|
297
|
+
loader: () => loader().then(module => {
|
|
298
|
+
// the loaded module exports this registration: it lives in the modal's own file, so the modal was never split out
|
|
299
|
+
if (Object.values(module).includes(registration))
|
|
300
|
+
console.error(`simplify-modal-control: registerLazyModal("${key}") is in the same file it lazy-loads, so the modal is bundled eagerly. Move the registration to a separate file.`);
|
|
301
|
+
return module;
|
|
302
|
+
}),
|
|
303
|
+
};
|
|
304
|
+
return registration;
|
|
305
|
+
};
|
|
306
|
+
exports.registerLazyModal = registerLazyModal;
|
|
307
|
+
// keeps a failed load (or render) inside its own modal instead of unmounting the whole app
|
|
308
|
+
class LoadErrorBoundary extends react_1.Component {
|
|
309
|
+
constructor() {
|
|
310
|
+
super(...arguments);
|
|
311
|
+
this.state = { failed: false };
|
|
312
|
+
}
|
|
313
|
+
static getDerivedStateFromError() {
|
|
314
|
+
return { failed: true };
|
|
315
|
+
}
|
|
316
|
+
componentDidCatch() {
|
|
317
|
+
this.props.onError();
|
|
318
|
+
}
|
|
319
|
+
render() {
|
|
320
|
+
return this.state.failed ? null : this.props.children;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
// mount the lazy component only after the first open, so its chunk is not loaded on app start
|
|
324
|
+
const withSuspense = (loader, fallback) => {
|
|
325
|
+
let LazyContent = (0, react_1.lazy)(loader);
|
|
326
|
+
const SuspenseContent = (props) => {
|
|
327
|
+
const [opened, setOpened] = (0, react_1.useState)(Boolean(props.open));
|
|
328
|
+
// props object seen when loading failed; a new one means the modal was opened again
|
|
329
|
+
const [failedProps, setFailedProps] = (0, react_1.useState)();
|
|
330
|
+
if (props.open && !opened)
|
|
331
|
+
setOpened(true);
|
|
332
|
+
if (!opened || failedProps === props)
|
|
333
|
+
return null;
|
|
334
|
+
return ((0, jsx_runtime_1.jsx)(LoadErrorBoundary, { onError: () => {
|
|
335
|
+
LazyContent = (0, react_1.lazy)(loader); // React.lazy caches the rejection; a new one retries
|
|
336
|
+
setFailedProps(props);
|
|
337
|
+
props.onClose();
|
|
338
|
+
}, children: (0, jsx_runtime_1.jsx)(react_1.Suspense, { fallback: fallback, children: (0, jsx_runtime_1.jsx)(LazyContent, Object.assign({}, props)) }) }));
|
|
339
|
+
};
|
|
340
|
+
return SuspenseContent;
|
|
341
|
+
};
|
|
236
342
|
const EMPTY_LIST = [];
|
|
237
343
|
const EMPTY_OBJECT = {};
|
|
238
344
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":";;;;AAAA,
|
|
1
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AAA,gBAAQ,GAAW,CAAC;QAC1D,IAAI,KAAK,CAAC,IAAI,IAAI,CAAC,MAAM;YAAE,SAAS,CAAC,IAAI,CAAC,CAAC;QAC3C,IAAI,CAAC,MAAM,IAAI,WAAW,KAAK,KAAK;YAAE,OAAO,IAAI,CAAC;QAClD,OAAO,CACL,uBAAC,iBAAiB,IAChB,OAAO,EAAE,GAAG,EAAE;gBACZ,WAAW,GAAG,IAAA,YAAI,EAAC,MAAM,CAAC,CAAC,CAAC,qDAAqD;gBACjF,cAAc,CAAC,KAAK,CAAC,CAAC;gBACtB,KAAK,CAAC,OAAO,EAAE,CAAC;YAClB,CAAC,YACD,uBAAC,gBAAQ,IAAC,QAAQ,EAAE,QAAQ,YAC1B,uBAAC,WAAW,oBAAK,KAAK,EAAI,GACjB,GACO,CACrB,CAAC;IACJ,CAAC,CAAC;IACF,OAAO,eAAe,CAAC;AACzB,CAAC,CAAC;AA2IF,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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@catconsult/simplify-modal-control",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Simplify React modal control flow",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -32,18 +32,21 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://github.com/Catalyst-Consulting-Group/simplify-modal-control#readme",
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"react": "
|
|
35
|
+
"react": ">=18.0.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@testing-library/
|
|
39
|
-
"@
|
|
40
|
-
"@types/
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"react": "^
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
38
|
+
"@testing-library/dom": "^10.4.1",
|
|
39
|
+
"@testing-library/react": "^16.3.0",
|
|
40
|
+
"@types/jest": "^30.0.0",
|
|
41
|
+
"@types/node": "^22.18.12",
|
|
42
|
+
"@types/react": "^19.2.2",
|
|
43
|
+
"@types/react-dom": "^19.3.0",
|
|
44
|
+
"jest": "^30.2.0",
|
|
45
|
+
"jest-environment-jsdom": "^30.2.0",
|
|
46
|
+
"react": "^19.2.0",
|
|
47
|
+
"react-dom": "^19.2.0",
|
|
48
|
+
"shx": "^0.4.0",
|
|
49
|
+
"ts-jest": "^29.4.5",
|
|
50
|
+
"typescript": "^5.6.3"
|
|
48
51
|
}
|
|
49
52
|
}
|