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