-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathllms.txt
More file actions
397 lines (289 loc) · 10.1 KB
/
Copy pathllms.txt
File metadata and controls
397 lines (289 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# @reactleaf/modal
> `@reactleaf/modal` is a type-safe React modal library for opening modal components from anywhere in an app, stacking multiple modals, and receiving modal close results as Promise values.
`@reactleaf/modal` is a React and TypeScript package. It provides a `ModalManager` controller, a `ModalProvider` React integration, and a `useModalInstance()` hook for modal components. Use it when app code needs to open confirmation, alert, input, form, or multi-step modal UI and continue after the user closes the modal.
## Installation
```sh
npm install @reactleaf/modal
# or
yarn add @reactleaf/modal
# or
pnpm add @reactleaf/modal
```
Import the default stylesheet once in the app entry if using the built-in layer and dim styles.
```ts
import '@reactleaf/modal/style.css';
```
## Basic Setup
Create one shared `ModalManager` instance. The `ModalProvider` and every caller of `modal.open(...)` must use the same manager instance.
```ts
// modal.ts
import { ModalManager } from '@reactleaf/modal';
export const modal = new ModalManager();
```
Wrap the app with `ModalProvider`.
```tsx
import { ModalProvider } from '@reactleaf/modal';
import { modal } from './modal';
function App() {
return (
<ModalProvider
manager={modal}
defaultLayerOptions={{ closeDelay: 180, closeOnOutsideClick: true, dim: true }}
rootOptions={{ preventScroll: true }}
>
<YourApp />
</ModalProvider>
);
}
```
`ModalProvider` renders `children` normally and also renders the modal layer container for open modals.
## Modal Components
Modal components are plain React components. Use `useModalInstance()` inside a modal component to read the current modal layer state and close or replace that layer.
```tsx
import { type ModalComponent, useModalInstance } from '@reactleaf/modal';
type ConfirmProps = {
message: string;
};
export const Confirm: ModalComponent<ConfirmProps, boolean> = ({ message }) => {
const { visible, closeSelf } = useModalInstance<boolean>();
return (
<div className={visible ? 'confirm visible' : 'confirm'}>
<p>{message}</p>
<button type="button" onClick={() => void closeSelf(false)}>
Cancel
</button>
<button type="button" onClick={() => void closeSelf(true)}>
OK
</button>
</div>
);
};
Confirm.layerOptions = {
closeOnOutsideClick: false,
};
```
## Opening Modals
Open a modal by passing the component and its props to `modal.open(Component, props?, options?)`. The returned Promise resolves when that modal closes.
```tsx
import { modal } from './modal';
import Confirm from './modals/Confirm';
async function deleteItem() {
const confirmed = await modal.open(Confirm, {
message: 'Delete this item?',
});
if (!confirmed) return;
await requestDelete();
}
```
If the modal component has no required props, omit the second argument. To pass only options for a no-props modal, use `null` as the second argument.
```tsx
await modal.open(EmptyModal);
await modal.open(EmptyModal, null, { closeOnOutsideClick: false });
```
## Provider Props
`ModalProvider` props:
```ts
type ModalProviderProps = {
manager: ModalManager;
defaultLayerOptions?: Partial<LayerOptions>;
rootOptions?: Partial<RootOptions>;
children: React.ReactNode;
};
```
- `manager`: The `ModalManager` instance that owns this modal stack.
- `defaultLayerOptions`: Defaults applied to every modal layer rendered by this provider.
- `rootOptions`: Options for the root modal behavior.
- `children`: The app content rendered alongside the modal root.
## Modal Options
```ts
interface LayerOptions {
className?: string;
closeDelay?: number;
closeOnOutsideClick?: boolean;
dim?: boolean | string;
}
interface RootOptions {
preventScroll?: boolean;
}
interface ModalOptions extends LayerOptions {
abortController?: AbortController;
}
```
- `className`: Additional class name for the modal layer.
- `closeDelay`: Delay in milliseconds before completing close. Use this to match CSS exit animation duration.
- `closeOnOutsideClick`: Whether clicking the top modal backdrop closes the modal.
- `dim`: `true` adds the `dim` class. A string value adds that string as a custom dim class.
- `preventScroll`: Locks `document.body` scrolling while at least one modal is open.
- `abortController`: Closes the modal when the controller is aborted.
Layer options merge in this priority order:
1. `defaultLayerOptions` on `ModalProvider`
2. `Component.layerOptions` on the modal component
3. Options passed to `modal.open(...)`
## ModalManager API
### `new ModalManager()`
Creates a controller that owns a modal stack. Multiple managers can exist in one app, but each `modal.open(...)` caller must use the manager attached to the relevant `ModalProvider`.
### `modal.open(Component, props?, options?)`
Opens a modal and returns a Promise. If the modal has required props, pass them as the second argument.
```ts
const result = await modal.open(Alert, {
message: 'Saved.',
});
```
The Promise may resolve to:
- The value passed to `closeSelf(value)`
- `undefined`, when the modal closes without an explicit result
- `MODAL_ABORTED`, when an attached `AbortController` aborts
- `MODAL_REPLACED`, when the current modal is replaced by another modal
### `modal.closeWithResult(id, result, options?)`
Closes the modal with the given `id` and resolves its Promise with `result`.
```ts
modal.closeWithResult(id, { confirmed: true });
modal.closeWithResult(id, { confirmed: true }, { historyBack: true });
```
### `modal.close(id, options?)`
Closes the modal with the given `id` and resolves its Promise with `undefined`.
```ts
modal.close(id);
```
### `modal.closeTop(options?)`
Closes the top-most modal.
```ts
modal.closeTop();
modal.closeTop({ historyBack: true });
```
### `modal.closeAll(options?)`
Closes every open modal.
```ts
modal.closeAll();
```
### `modal.hasOpenModals()`
Returns whether at least one modal is open.
```ts
if (modal.hasOpenModals()) {
console.log('A modal is open');
}
```
### `modal.getSnapshot()`
Returns a read-only snapshot of the current modal stack. Each item includes `id`, `Component`, `props`, and `options`.
```ts
const opened = modal.getSnapshot();
if (opened.some((item) => item.Component === Confirm)) {
console.log('Confirm is already open');
}
```
### `modal.subscribe(listener)`
Subscribes to stack changes. This is usually useful for debug panels or external synchronization.
```ts
const unsubscribe = modal.subscribe((stack) => {
console.log('open modals:', stack.length);
});
unsubscribe();
```
## `useModalInstance()`
Call `useModalInstance()` inside a modal component.
```tsx
const { visible, closeSelf, replaceSelf } = useModalInstance();
```
Returned values:
- `visible: boolean`: `true` while the modal content should be shown. Useful for enter and exit animation classes.
- `closeSelf(result?): Promise<void>`: Closes the current modal and resolves the matching `modal.open(...)` Promise. Use `useModalInstance<Result>()` to type the result accepted by `closeSelf`.
- `replaceSelf(Component, props?, options?): Promise`: Replaces the current layer with another modal component and resolves with the new modal's result. If the replacement is typed as `ModalComponent<Props, Result>`, `replaceSelf(...)` infers that result type.
`useModalInstance()` only works inside components opened through this modal system.
## Replace Example
Use `replaceSelf(...)` for multi-step flows that should keep the same layer while changing content.
```tsx
import { type ModalComponent, useModalInstance } from '@reactleaf/modal';
import CodeModal from './CodeModal';
type EmailModalProps = {
onVerified: () => Promise<void>;
};
const EmailModal: ModalComponent<EmailModalProps, never> = ({ onVerified }) => {
const { replaceSelf } = useModalInstance();
async function submitEmail(email: string) {
await sendVerificationCode(email);
const verified = await replaceSelf(CodeModal, {
email,
});
if (verified) {
await onVerified();
}
}
return <EmailForm onSubmit={submitEmail} />;
};
```
When a modal is replaced, the previous modal's original `open()` Promise resolves to `MODAL_REPLACED`. The `replaceSelf(...)` call resolves with the result of the new modal.
## AbortController Example
```tsx
import { MODAL_ABORTED } from '@reactleaf/modal';
import { modal } from './modal';
import Alert from './Alert';
const controller = new AbortController();
const timer = window.setTimeout(() => controller.abort(), 3000);
const result = await modal.open(
Alert,
{ message: 'Closes automatically in 3s.' },
{ abortController: controller },
);
window.clearTimeout(timer);
if (result === MODAL_ABORTED) {
console.log('Modal closed via abort');
}
```
## Default Behavior
- `Escape` closes the top modal.
- Browser back closes the top modal.
- Unless `closeOnOutsideClick` is `false`, clicking the top modal backdrop closes it.
- Modals stack in open order.
- `rootOptions.preventScroll: true` locks body scroll while any modal is open.
- `dim: true` adds the `dim` class to that modal layer.
- A string `dim` value adds that string as a custom dim class on the layer.
## Styling
Default CSS selectors:
- `.modal-layer`
- `.modal-layer.visible`
- `.modal-layer[data-content-visible='true']`
- `.modal-layer.dim`
Basic animation pattern:
```css
.modal-layer {
opacity: 0;
transition: opacity 180ms ease;
}
.modal-layer.visible {
opacity: 1;
}
.modal-layer > * {
transform: translateY(8px) scale(0.98);
transition: transform 180ms ease;
}
.modal-layer[data-content-visible='true'] > * {
transform: translateY(0) scale(1);
}
```
Set `closeDelay` to the same duration as the CSS exit transition.
```tsx
<ModalProvider manager={modal} defaultLayerOptions={{ closeDelay: 180 }}>
<App />
</ModalProvider>
```
## Public Exports
```ts
export { ModalManager, MODAL_ABORTED, MODAL_REPLACED } from '@reactleaf/modal';
export { ModalProvider } from '@reactleaf/modal';
export { useModalInstance } from '@reactleaf/modal';
export type {
CloseOptions,
LayerOptions,
ModalAborted,
ModalClosedSignal,
ModalComponent,
ModalComponentProps,
ModalComponentResult,
ModalInstanceContextType,
ModalOptions,
ModalReplaced,
ModalState,
ReplaceSelf,
RootOptions,
} from '@reactleaf/modal';
```