react-motion-modal 1.4.0 → 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/dist/index.d.ts CHANGED
@@ -1,464 +1,135 @@
1
+ import { ComponentType } from 'react';
1
2
  import { CSSProperties } from 'react';
2
- import { ReactElement } from 'react';
3
3
  import { ReactPortal } from 'react';
4
- import { StoreApi } from 'zustand/vanilla';
5
4
  import { TargetAndTransition } from 'motion';
6
- import { UseBoundStore } from 'zustand/react';
5
+
6
+ export declare type AnyModalDefinition = ModalDefinition<any, any>;
7
7
 
8
8
  /**
9
- * Represents a currently active (opened) modal instance.
10
- * Each modal gets a unique ID and maintains its state in the modal stack.
9
+ * Creates an isolated typed modal registry and lifecycle controller.
11
10
  *
12
- * @example
13
- * ```typescript
14
- * const modal: ActiveModal = {
15
- * id: 'uuid-123',
16
- * name: 'alert',
17
- * params: { message: 'Hello', closeModal: () => {}, onClose: () => {} }
18
- * };
19
- * ```
11
+ * Each `open()` operation settles exactly once with a resolved or cancelled
12
+ * outcome.
20
13
  */
21
- export declare type ActiveModal = {
22
- /**
23
- * Unique identifier for this modal instance.
24
- * Generated automatically when the modal is opened.
25
- */
26
- id: string;
27
- /**
28
- * The name/type of the modal (key from ModalDefinition).
29
- */
30
- name: ModalName;
31
- /**
32
- * Parameters passed to the modal, including both custom params and base modal params.
33
- */
34
- params?: Params<ModalName>;
35
- };
14
+ export declare const createModalSystem: <Registry extends ModalRegistry>(registry: Registry) => ModalSystem<Registry>;
36
15
 
37
16
  /**
38
- * Base parameters available for all modals.
39
- * These parameters control modal behavior, styling, and animation.
17
+ * Defines one modal contract while preserving its input and result types.
40
18
  */
41
- export declare type BaseModalParams = {
42
- /**
43
- * Function to close the modal programmatically.
44
- * This is automatically injected by the modal system and should not be provided when opening a modal.
45
- */
46
- closeModal: () => void;
47
- /**
48
- * Callback triggered when the modal is closed (either by user action or programmatically).
49
- * Use this for cleanup operations or side effects after modal close.
50
- */
51
- onClose?: () => void;
52
- /**
53
- * Accessible name for the dialog when there is no visible heading that can
54
- * be referenced with `ariaLabelledBy`.
55
- * @default "Modal"
56
- */
57
- ariaLabel?: string;
58
- /**
59
- * ID of the visible element that labels the dialog.
60
- * Takes precedence over `ariaLabel` when provided.
61
- */
62
- ariaLabelledBy?: string;
63
- /**
64
- * ID of the element that describes the dialog.
65
- */
66
- ariaDescribedBy?: string;
67
- /**
68
- * Controls whether the modal should close when the user clicks outside the modal content.
69
- * @default true
70
- */
71
- closeOnClickOutside?: boolean;
72
- /**
73
- * Controls whether the modal should close when the user presses the Escape key.
74
- * @default true
75
- */
76
- closeOnPressEsc?: boolean;
77
- /**
78
- * Styling options for the outer backdrop (overlay) of the modal.
79
- * This backdrop typically provides the backdrop and centers the modal content.
80
- */
81
- backdrop?: {
82
- /**
83
- * CSS class name to apply to the modal backdrop.
84
- * Useful for custom styling or responsive behavior.
85
- */
86
- className?: string;
87
- /**
88
- * Inline styles to merge after the default backdrop styles.
89
- * Useful when consumers need deterministic overrides without relying on global CSS order.
90
- */
91
- style?: CSSProperties;
92
- };
93
- /**
94
- * Styling options for the modal content/body.
95
- * This is the actual modal content area where your component is rendered.
96
- */
97
- body?: {
98
- /**
99
- * CSS class name to apply to the modal body.
100
- * Useful for custom styling, sizing, or positioning of modal content.
101
- */
102
- className?: string;
103
- /**
104
- * Inline styles to merge after the default body styles.
105
- * Useful for overriding default padding, sizing, or positioning.
106
- */
107
- style?: CSSProperties;
108
- };
109
- /**
110
- * Framer Motion animation settings for modal entrance and exit animations.
111
- * These settings define how the modal appears and disappears.
112
- *
113
- * @example
114
- * ```typescript
115
- * // Simple fade animation
116
- * animate: {
117
- * animate: { opacity: 1 },
118
- * exit: { opacity: 0 }
119
- * }
120
- *
121
- * // Scale and fade animation
122
- * animate: {
123
- * animate: {
124
- * opacity: 1,
125
- * scale: 1,
126
- * transition: { duration: 0.3, ease: "easeOut" }
127
- * },
128
- * exit: {
129
- * opacity: 0,
130
- * scale: 0.9,
131
- * transition: { duration: 0.2, ease: "easeIn" }
132
- * }
133
- * }
134
- *
135
- * // Slide up animation
136
- * animate: {
137
- * animate: {
138
- * opacity: 1,
139
- * y: 0,
140
- * transition: { type: "spring", stiffness: 300, damping: 30 }
141
- * },
142
- * exit: {
143
- * opacity: 0,
144
- * y: 50,
145
- * transition: { duration: 0.25 }
146
- * }
147
- * }
148
- * ```
149
- */
150
- animate?: {
151
- /**
152
- * Animation properties for the exit transition (when modal is closing).
153
- * Accepts any valid Framer Motion TargetAndTransition properties.
154
- *
155
- * @example
156
- * ```typescript
157
- * // Simple fade out
158
- * exit: { opacity: 0 }
159
- *
160
- * // Scale down and fade out
161
- * exit: {
162
- * opacity: 0,
163
- * scale: 0.8,
164
- * transition: { duration: 0.2 }
165
- * }
166
- * ```
167
- */
168
- exit?: TargetAndTransition;
169
- /**
170
- * Animation properties for the animate state (when modal is visible).
171
- * Accepts any valid Framer Motion TargetAndTransition properties.
172
- *
173
- * @example
174
- * ```typescript
175
- * // Simple fade in
176
- * animate: { opacity: 1 }
177
- *
178
- * // Scale up and fade in with spring
179
- * animate: {
180
- * opacity: 1,
181
- * scale: 1,
182
- * transition: { type: "spring", stiffness: 400 }
183
- * }
184
- * ```
185
- */
186
- animate?: TargetAndTransition;
187
- };
188
- /**
189
- * Optional positioning string for modal placement.
190
- * This can be used to override default modal positioning behavior.
191
- *
192
- * @example
193
- * ```typescript
194
- * import { MODAL_POSITIONS } from 'react-motion-modal';
195
- *
196
- * // Available positions
197
- * MODAL_POSITIONS.TOP // 'top' full width at the top
198
- * MODAL_POSITIONS.TOP_LEFT // 'top-left'
199
- * MODAL_POSITIONS.TOP_RIGHT // 'top-right'
200
- * MODAL_POSITIONS.TOP_CENTER // 'top-center'
201
- * MODAL_POSITIONS.LEFT // 'left' full height at the left
202
- * MODAL_POSITIONS.RIGHT // 'right' full height at the right
203
- * MODAL_POSITIONS.CENTER // 'center' (default)
204
- * MODAL_POSITIONS.CENTER_FULL // 'center-full'
205
- * MODAL_POSITIONS.BOTTOM // 'bottom' full width at the bottom
206
- * MODAL_POSITIONS.BOTTOM_LEFT // 'bottom-left'
207
- * MODAL_POSITIONS.BOTTOM_RIGHT // 'bottom-right'
208
- * MODAL_POSITIONS.BOTTOM_CENTER// 'bottom-center'
209
- * ```
210
- */
211
- position?: string;
212
- /**
213
- * Enables backdrop blur effect when modal is open.
214
- * When set to true, the background content will be blurred.
215
- * @default true
216
- */
217
- blur?: boolean;
19
+ export declare const defineModal: <Input, Result>(definition: ModalDefinition<Input, Result>) => ModalDefinition<Input, Result>;
20
+
21
+ export declare const MODAL_POSITIONS: {
22
+ readonly TOP: "top";
23
+ readonly TOP_LEFT: "top-left";
24
+ readonly TOP_RIGHT: "top-right";
25
+ readonly TOP_CENTER: "top-center";
26
+ readonly LEFT: "left";
27
+ readonly RIGHT: "right";
28
+ readonly CENTER: "center";
29
+ readonly CENTER_FULL: "center-full";
30
+ readonly BOTTOM: "bottom";
31
+ readonly BOTTOM_LEFT: "bottom-left";
32
+ readonly BOTTOM_RIGHT: "bottom-right";
33
+ readonly BOTTOM_CENTER: "bottom-center";
218
34
  };
219
35
 
220
- export declare const MODAL_BODY_ANIMATION_VISIBLE: TargetAndTransition;
36
+ export declare const MODAL_Z_INDEX = 1000;
221
37
 
222
- export declare const MODAL_ENTER_TRANSITION: {
223
- duration: number;
224
- ease: "easeOut";
225
- };
38
+ export declare type ModalCancelReason = 'escape' | 'backdrop' | 'programmatic' | 'replaced' | 'provider-unmounted';
226
39
 
227
- export declare const MODAL_EXIT_TRANSITION: {
228
- duration: number;
229
- ease: "easeIn";
40
+ export declare type ModalDefinition<Input, Result> = {
41
+ component: ComponentType<ModalRenderProps<Input, Result>>;
42
+ policy?: ModalPolicy;
43
+ preset?: ModalMotionPreset;
44
+ defaultOptions?: ModalOpenOptions;
230
45
  };
231
46
 
232
- export declare const MODAL_POSITIONS: {
233
- TOP: string;
234
- TOP_LEFT: string;
235
- TOP_RIGHT: string;
236
- TOP_CENTER: string;
237
- LEFT: string;
238
- RIGHT: string;
239
- CENTER: string;
240
- CENTER_FULL: string;
241
- BOTTOM: string;
242
- BOTTOM_LEFT: string;
243
- BOTTOM_RIGHT: string;
244
- BOTTOM_CENTER: string;
47
+ export declare type ModalInput<Definition> = Definition extends ModalDefinition<infer Input, any> ? Input : never;
48
+
49
+ export declare type ModalMotion = {
50
+ backdrop?: ModalMotionState;
51
+ content: ModalMotionState;
245
52
  };
246
53
 
247
- export declare const MODAL_Z_INDEX = 1000;
54
+ export declare type ModalMotionPreset = {
55
+ position: ModalPosition;
56
+ motion: ModalMotion;
57
+ };
248
58
 
249
- /**
250
- * Actions that can be performed on the modal system.
251
- * These are the functions available to control modal behavior.
252
- */
253
- export declare type ModalAction = {
254
- /**
255
- * Closes a specific modal by name or the topmost modal if no name is provided.
256
- *
257
- * @param name - Optional modal name to close. If not provided, closes the topmost modal.
258
- *
259
- * @example
260
- * ```typescript
261
- * closeModal('alert'); // Closes the alert modal
262
- * closeModal(); // Closes the current (topmost) modal
263
- * ```
264
- */
265
- closeModal: (name?: ModalName) => void;
266
- /**
267
- * Closes all open modals immediately.
268
- * This is useful for clearing the modal stack, such as during route navigation.
269
- *
270
- * @example
271
- * ```typescript
272
- * closeAllModal(); // Closes all modals
273
- * ```
274
- */
275
- closeAllModal: () => void;
276
- /**
277
- * Opens a new modal with optional parameters.
278
- *
279
- * @template T - The modal name type
280
- * @param name - The name of the modal to open
281
- * @param params - Optional parameters to pass to the modal (excluding closeModal which is auto-injected)
282
- *
283
- * @example
284
- * ```typescript
285
- * openModal('alert', {
286
- * message: 'Hello World',
287
- * onClose: () => console.log('Alert closed'),
288
- * closeOnClickOutside: false
289
- * });
290
- * ```
291
- */
292
- openModal: <T extends ModalName>(name: T, params?: Omit<Params<T>, 'closeModal'>) => void;
59
+ export declare type ModalMotionState = {
60
+ initial?: TargetAndTransition;
61
+ animate?: TargetAndTransition;
62
+ exit?: TargetAndTransition;
293
63
  };
294
64
 
295
- /**
296
- * Represents a modal component that accepts props and returns a React element.
297
- * This is the type definition for modal components that can be registered in the modal system.
298
- *
299
- * @template T - The type of props the component accepts, defaults to any
300
- *
301
- * @example
302
- * ```typescript
303
- * // Basic modal component
304
- * const AlertModal: ModalComponent<{ message: string; closeModal: () => void }> =
305
- * ({ message, closeModal }) => (
306
- * <div className="bg-white p-6 rounded-lg shadow-lg">
307
- * <p>{message}</p>
308
- * <button onClick={closeModal}>Close</button>
309
- * </div>
310
- * );
311
- *
312
- * // Modal component with advanced features
313
- * const ConfirmModal: ModalComponent<{
314
- * message: string;
315
- * onConfirm: () => void;
316
- * closeModal: () => void;
317
- * closeOnClickOutside?: boolean;
318
- * }> = ({ message, onConfirm, closeModal, closeOnClickOutside = true }) => (
319
- * <div className="bg-white p-6 rounded-lg shadow-lg max-w-sm mx-auto">
320
- * <h3 className="text-lg font-semibold mb-4">Confirm Action</h3>
321
- * <p className="mb-6">{message}</p>
322
- * <div className="flex justify-end gap-3">
323
- * <button
324
- * onClick={closeModal}
325
- * className="px-4 py-2 text-gray-600 hover:text-gray-800"
326
- * >
327
- * Cancel
328
- * </button>
329
- * <button
330
- * onClick={() => {
331
- * onConfirm();
332
- * closeModal();
333
- * }}
334
- * className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
335
- * >
336
- * Confirm
337
- * </button>
338
- * </div>
339
- * </div>
340
- * );
341
- * ```
342
- */
343
- export declare type ModalComponent<T = any> = (props: T) => ReactElement;
65
+ export declare type ModalOpenOptions = {
66
+ ariaLabel?: string;
67
+ ariaLabelledBy?: string;
68
+ ariaDescribedBy?: string;
69
+ closeOnClickOutside?: boolean;
70
+ closeOnPressEsc?: boolean;
71
+ position?: ModalPosition;
72
+ backdrop?: ModalSlotOptions;
73
+ body?: ModalSlotOptions;
74
+ blur?: boolean;
75
+ motion?: ModalMotion;
76
+ };
344
77
 
345
- /**
346
- * Configuration object that maps modal names to their corresponding components.
347
- * This is used to register all available modals in the system.
348
- *
349
- * @example
350
- * ```typescript
351
- * // First, define your modal parameters
352
- * type ModalDefinition = {
353
- * alert: { message: string; type?: 'info' | 'warning' | 'error' };
354
- * confirm: { message: string; onConfirm: () => void; onCancel?: () => void };
355
- * form: { title: string; fields: FormField[]; onSubmit: (data: any) => void };
356
- * };
357
- *
358
- * // Then create your modal components
359
- * const AlertModal: ModalComponent<Params<'alert'>> = ({ message, type, closeModal }) => (
360
- * <div className={`alert alert-${type}`}>
361
- * {message}
362
- * <button onClick={closeModal}>Dismiss</button>
363
- * </div>
364
- * );
365
- *
366
- * const ConfirmModal: ModalComponent<Params<'confirm'>> = ({
367
- * message, onConfirm, onCancel, closeModal
368
- * }) => (
369
- * <div className="confirm-dialog">
370
- * <p>{message}</p>
371
- * <div className="actions">
372
- * <button onClick={() => { onCancel?.(); closeModal(); }}>No</button>
373
- * <button onClick={() => { onConfirm(); closeModal(); }}>Yes</button>
374
- * </div>
375
- * </div>
376
- * );
377
- *
378
- * // Finally, create the configuration
379
- * const modalConfig: ModalConfig = {
380
- * alert: AlertModal,
381
- * confirm: ConfirmModal,
382
- * form: FormModal,
383
- * };
384
- *
385
- * // Use with ModalProvider
386
- * <ModalProvider modals={modalConfig}>
387
- * <App />
388
- * </ModalProvider>
389
- * ```
390
- */
391
- export declare type ModalConfig = {
392
- [K in ModalName]: ModalComponent<Params<K>>;
78
+ export declare type ModalOutcome<Result> = {
79
+ status: 'resolved';
80
+ value: Result;
81
+ } | {
82
+ status: 'cancelled';
83
+ reason: ModalCancelReason;
393
84
  };
394
85
 
395
- export declare interface ModalDefinition {
396
- }
86
+ export declare type ModalPolicy = 'stack' | 'replace' | 'singleton';
397
87
 
398
- /**
399
- * Provider-level defaults for modal rendering and interaction.
400
- * Lifecycle callbacks and the injected close handler belong to each modal
401
- * instance, so they cannot be configured as provider defaults.
402
- */
403
- export declare type ModalInitialParams = Partial<Omit<BaseModalParams, 'closeModal' | 'onClose'>>;
88
+ export declare type ModalPosition = (typeof MODAL_POSITIONS)[keyof typeof MODAL_POSITIONS];
404
89
 
405
90
  /**
406
- * Extracts the key type from the ModalDefinition interface.
407
- * This represents all available modal names that can be opened in the application.
408
- *
409
- * @example
410
- * ```typescript
411
- * // If ModalDefinition is { alert: AlertParams, confirm: ConfirmParams }
412
- * type ModalName = 'alert' | 'confirm';
413
- * ```
91
+ * Renders one modal system into a portal and owns its browser accessibility
92
+ * lifecycle.
414
93
  */
415
- export declare type ModalName = keyof ModalDefinition;
94
+ export declare const ModalProvider: <Registry extends ModalRegistry>({ system, defaultOptions, portalRoot: requestedPortalRoot, }: ModalProviderProps<Registry>) => ReactPortal | null;
416
95
 
417
- /**
418
- * Extracts only the closeModal function from the modal parameters.
419
- * This is useful when you need to pass only the close functionality to components.
420
- *
421
- * @template T - The modal name type
422
- */
423
- export declare type ModalParams<T extends ModalName> = ModalDefinition[T] & Pick<BaseModalParams, 'closeModal'>;
96
+ export declare type ModalProviderProps<Registry extends ModalRegistry = ModalRegistry> = {
97
+ system: ModalSystem<Registry>;
98
+ defaultOptions?: ModalOpenOptions;
99
+ portalRoot?: HTMLElement | null;
100
+ };
424
101
 
425
- export declare const ModalProvider: ({ modals, initialParams, }: ModalProviderProps) => ReactPortal | null;
102
+ export declare type ModalRegistry = Record<string, AnyModalDefinition>;
426
103
 
427
- export declare type ModalProviderProps = {
428
- modals: ModalConfig;
429
- initialParams?: ModalInitialParams;
104
+ export declare type ModalRenderProps<Input, Result> = {
105
+ input: Input;
106
+ resolve: (value: Result) => void;
107
+ cancel: (reason?: 'programmatic') => void;
430
108
  };
431
109
 
432
- /**
433
- * Represents the current state of all modals in the application.
434
- * This includes the modal stack and the currently active modal.
435
- */
436
- export declare type ModalState = {
437
- /**
438
- * Array of all currently open modals, ordered by their opening sequence.
439
- * The last modal in the array is the topmost modal.
440
- */
441
- modals: ActiveModal[];
442
- /**
443
- * The currently active (topmost) modal, or undefined if no modals are open.
444
- */
445
- active?: ActiveModal;
110
+ export declare type ModalResult<Definition> = Definition extends ModalDefinition<any, infer Result> ? Result : never;
111
+
112
+ export declare type ModalSlotOptions = {
113
+ className?: string;
114
+ style?: CSSProperties;
446
115
  };
447
116
 
448
- export declare const modalStore: UseBoundStore<StoreApi<ModalState & ModalAction>>;
117
+ export declare type ModalSystem<Registry extends ModalRegistry> = {
118
+ open<Name extends keyof Registry & string>(name: Name, ...args: OpenArgs<ModalInput<Registry[Name]>>): Promise<ModalOutcome<ModalResult<Registry[Name]>>>;
119
+ cancel(reason?: ModalCancelReason): void;
120
+ cancelAll(reason?: ModalCancelReason): void;
121
+ };
449
122
 
450
- /**
451
- * Combines the modal-specific parameters from ModalDefinition with the base modal parameters.
452
- * This represents the complete set of props that a modal component will receive.
453
- *
454
- * @template T - The modal name type
455
- *
456
- * @example
457
- * ```typescript
458
- * // For a modal named 'alert' with AlertParams defined in ModalDefinition
459
- * type AlertModalParams = Params<'alert'>; // AlertParams & BaseModalParams
460
- * ```
461
- */
462
- export declare type Params<T extends ModalName> = ModalDefinition[T] & BaseModalParams;
123
+ export declare const motionPresets: {
124
+ dialog: () => ModalMotionPreset;
125
+ drawer: (options?: {
126
+ side?: "left" | "right";
127
+ }) => ModalMotionPreset;
128
+ sheet: (options?: {
129
+ edge?: "top" | "bottom";
130
+ }) => ModalMotionPreset;
131
+ };
132
+
133
+ declare type OpenArgs<Input> = [Input] extends [void] ? [input?: Input, options?: ModalOpenOptions] : [input: Input, options?: ModalOpenOptions];
463
134
 
464
135
  export { }