react-motion-modal 1.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 +103 -0
- package/dist/index.css +1 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.js +5771 -0
- package/package.json +72 -0
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# react-motion-modal
|
|
2
|
+
|
|
3
|
+
`react-motion-modal` is a flexible modal management library built with [Zustand](https://zustand-demo.pmnd.rs/) for state management and powered by [Framer Motion](https://www.framer.com/motion/) for beautiful animations. It offers full TypeScript type-safety and allows dynamic modal opening with custom parameters.
|
|
4
|
+
|
|
5
|
+
## ✨ Features
|
|
6
|
+
|
|
7
|
+
- Stack-based modal management (multiple modals at once)
|
|
8
|
+
- Type-safe parameters per modal
|
|
9
|
+
- Auto-injection of `closeModal()` into each modal props
|
|
10
|
+
- Full intellisense for modal names and parameters
|
|
11
|
+
- Easily extensible via module augmentation
|
|
12
|
+
|
|
13
|
+
## 🧱 Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install react-motion-modal
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
or with yarn:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
yarn add react-motion-modal
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 🚀 Usage
|
|
26
|
+
|
|
27
|
+
### 1. Define modal types
|
|
28
|
+
|
|
29
|
+
Create a `modal.d.ts` file in your project (make sure to include it in tsconfig.json):
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import type { ReactElement } from 'react';
|
|
33
|
+
import type { ModalStackParams } from 'react-motion-modal';
|
|
34
|
+
|
|
35
|
+
declare module 'react-motion-modal' {
|
|
36
|
+
interface ModalStackParams {
|
|
37
|
+
AlertModal: {
|
|
38
|
+
title: string;
|
|
39
|
+
};
|
|
40
|
+
ConfirmModal: {
|
|
41
|
+
onConfirm: () => void;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 2. Open modal anywhere
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
import { useModal } from 'react-motion-modal';
|
|
51
|
+
|
|
52
|
+
const { openModal } = useModal();
|
|
53
|
+
|
|
54
|
+
openModal('ConfirmModal', {1
|
|
55
|
+
title: 'Are you sure you want to delete?',
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 3. Use `closeModal` inside your modal component
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
const ConfirmModal = ({ title, closeModal }: ModalStackParams<'ConfirmModal'>) => (
|
|
63
|
+
<div>
|
|
64
|
+
<h1>{title}</h1>
|
|
65
|
+
<button onClick={closeModal}>Close</button>
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## 🧩 API
|
|
71
|
+
|
|
72
|
+
### `openModal(name, params)`
|
|
73
|
+
|
|
74
|
+
Opens a modal by name. The `params` will be inferred based on modal type.
|
|
75
|
+
|
|
76
|
+
### `closeModal(name?)`
|
|
77
|
+
|
|
78
|
+
- If `name` is provided, closes that specific modal.
|
|
79
|
+
- If not, closes the most recent modal (top of the stack).
|
|
80
|
+
|
|
81
|
+
### `BaseModalParams`
|
|
82
|
+
|
|
83
|
+
Each modal automatically receives the following props:
|
|
84
|
+
|
|
85
|
+
| Prop | Type | Description |
|
|
86
|
+
|-------------------------|-------------------------------------|------------------------------------------------|
|
|
87
|
+
| `closeModal` | `() => void` | Close function for the modal |
|
|
88
|
+
| `closeOnClickOutside` | `boolean` | Close modal when clicking outside |
|
|
89
|
+
| `closeOnPressEsc` | `boolean` | Close modal when Escape key is pressed |
|
|
90
|
+
| `container` | `{ className?: string; override?: boolean; }` | Customize wrapper styles |
|
|
91
|
+
| `body` | `{ className?: string; override?: boolean; }` | Customize inner modal styles |
|
|
92
|
+
| `animate` | `{ animate?: TargetAndTransition; exit?: TargetAndTransition; }` | Animation powered by Framer Motion. Customize how the modal enters and exits. |
|
|
93
|
+
| `onClose` | `() => void` | Callback triggered when modal is closed |
|
|
94
|
+
|
|
95
|
+
## 📝 Notes
|
|
96
|
+
|
|
97
|
+
- Zustand-based, framework-agnostic state management
|
|
98
|
+
- Built-in animation system powered by [Framer Motion](https://www.framer.com/motion/)
|
|
99
|
+
- You can implement a `ModalRenderer` to dynamically render modals from `activeModals` state
|
|
100
|
+
- Great for complex apps requiring nested or dynamic modal stacks
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
Made with ❤️ by react-motion-modal.
|
package/dist/index.css
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/*! tailwindcss v4.1.11 | MIT License | https://tailwindcss.com */@layer properties{@supports (((-webkit-hyphens:none)) and (not (margin-trim:inline))) or ((-moz-orient:inline) and (not (color:rgb(from red r g b)))){*,:before,:after,::backdrop{--tw-translate-x:0;--tw-translate-y:0;--tw-translate-z:0;--tw-font-weight:initial;--tw-backdrop-blur:initial;--tw-backdrop-brightness:initial;--tw-backdrop-contrast:initial;--tw-backdrop-grayscale:initial;--tw-backdrop-hue-rotate:initial;--tw-backdrop-invert:initial;--tw-backdrop-opacity:initial;--tw-backdrop-saturate:initial;--tw-backdrop-sepia:initial}}}@layer theme{:root,:host{--font-sans:ui-sans-serif,system-ui,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";--font-mono:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;--color-red-500:oklch(63.7% .237 25.331);--color-blue-400:oklch(70.7% .165 254.624);--color-blue-500:oklch(62.3% .214 259.815);--color-blue-600:oklch(54.6% .245 262.881);--color-gray-500:oklch(55.1% .027 264.364);--color-gray-600:oklch(44.6% .03 256.802);--color-black:#000;--color-white:#fff;--spacing:.25rem;--text-xl:1.25rem;--text-xl--line-height:calc(1.75/1.25);--font-weight-semibold:600;--radius-md:.375rem;--radius-xl:.75rem;--blur-sm:8px;--default-font-family:var(--font-sans);--default-mono-font-family:var(--font-mono)}}@layer base{*,:after,:before,::backdrop{box-sizing:border-box;border:0 solid;margin:0;padding:0}::file-selector-button{box-sizing:border-box;border:0 solid;margin:0;padding:0}html,:host{-webkit-text-size-adjust:100%;tab-size:4;line-height:1.5;font-family:var(--default-font-family,ui-sans-serif,system-ui,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji");font-feature-settings:var(--default-font-feature-settings,normal);font-variation-settings:var(--default-font-variation-settings,normal);-webkit-tap-highlight-color:transparent}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;-webkit-text-decoration:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,samp,pre{font-family:var(--default-mono-font-family,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace);font-feature-settings:var(--default-mono-font-feature-settings,normal);font-variation-settings:var(--default-mono-font-variation-settings,normal);font-size:1em}small{font-size:80%}sub,sup{vertical-align:baseline;font-size:75%;line-height:0;position:relative}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}:-moz-focusring{outline:auto}progress{vertical-align:baseline}summary{display:list-item}ol,ul,menu{list-style:none}img,svg,video,canvas,audio,iframe,embed,object{vertical-align:middle;display:block}img,video{max-width:100%;height:auto}button,input,select,optgroup,textarea{font:inherit;font-feature-settings:inherit;font-variation-settings:inherit;letter-spacing:inherit;color:inherit;opacity:1;background-color:#0000;border-radius:0}::file-selector-button{font:inherit;font-feature-settings:inherit;font-variation-settings:inherit;letter-spacing:inherit;color:inherit;opacity:1;background-color:#0000;border-radius:0}:where(select:is([multiple],[size])) optgroup{font-weight:bolder}:where(select:is([multiple],[size])) optgroup option{padding-inline-start:20px}::file-selector-button{margin-inline-end:4px}::placeholder{opacity:1}@supports (not ((-webkit-appearance:-apple-pay-button))) or (contain-intrinsic-size:1px){::placeholder{color:currentColor}@supports (color:color-mix(in lab,red,red)){::placeholder{color:color-mix(in oklab,currentcolor 50%,transparent)}}}textarea{resize:vertical}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-date-and-time-value{min-height:1lh;text-align:inherit}::-webkit-datetime-edit{display:inline-flex}::-webkit-datetime-edit-fields-wrapper{padding:0}::-webkit-datetime-edit{padding-block:0}::-webkit-datetime-edit-year-field{padding-block:0}::-webkit-datetime-edit-month-field{padding-block:0}::-webkit-datetime-edit-day-field{padding-block:0}::-webkit-datetime-edit-hour-field{padding-block:0}::-webkit-datetime-edit-minute-field{padding-block:0}::-webkit-datetime-edit-second-field{padding-block:0}::-webkit-datetime-edit-millisecond-field{padding-block:0}::-webkit-datetime-edit-meridiem-field{padding-block:0}:-moz-ui-invalid{box-shadow:none}button,input:where([type=button],[type=reset],[type=submit]){appearance:button}::file-selector-button{appearance:button}::-webkit-inner-spin-button{height:auto}::-webkit-outer-spin-button{height:auto}[hidden]:where(:not([hidden=until-found])){display:none!important}}@layer components;@layer utilities{.absolute{position:absolute}.fixed{position:fixed}.top-0{top:calc(var(--spacing)*0)}.top-1\/2{top:50%}.right-0{right:calc(var(--spacing)*0)}.bottom-0{bottom:calc(var(--spacing)*0)}.left-0{left:calc(var(--spacing)*0)}.container{width:100%}@media (min-width:40rem){.container{max-width:40rem}}@media (min-width:48rem){.container{max-width:48rem}}@media (min-width:64rem){.container{max-width:64rem}}@media (min-width:80rem){.container{max-width:80rem}}@media (min-width:96rem){.container{max-width:96rem}}.mt-4{margin-top:calc(var(--spacing)*4)}.mb-4{margin-bottom:calc(var(--spacing)*4)}.flex{display:flex}.h-screen{height:100vh}.max-h-screen{max-height:100vh}.w-screen{width:100vw}.max-w-screen{max-width:100vw}.-translate-y-1\/2{--tw-translate-y: -50% ;translate:var(--tw-translate-x)var(--tw-translate-y)}.cursor-pointer{cursor:pointer}.items-center{align-items:center}.justify-center{justify-content:center}.justify-end{justify-content:flex-end}.gap-2{gap:calc(var(--spacing)*2)}.overflow-auto{overflow:auto}.rounded-md{border-radius:var(--radius-md)}.rounded-xl{border-radius:var(--radius-xl)}.bg-black{background-color:var(--color-black)}.bg-black\/40{background-color:#0006}@supports (color:color-mix(in lab,red,red)){.bg-black\/40{background-color:color-mix(in oklab,var(--color-black)40%,transparent)}}.bg-blue-400{background-color:var(--color-blue-400)}.bg-gray-500{background-color:var(--color-gray-500)}.bg-white{background-color:var(--color-white)}.p-4{padding:calc(var(--spacing)*4)}.p-5{padding:calc(var(--spacing)*5)}.px-4{padding-inline:calc(var(--spacing)*4)}.py-2{padding-block:calc(var(--spacing)*2)}.text-xl{font-size:var(--text-xl);line-height:var(--tw-leading,var(--text-xl--line-height))}.font-semibold{--tw-font-weight:var(--font-weight-semibold);font-weight:var(--font-weight-semibold)}.break-all{word-break:break-all}.text-blue-500{color:var(--color-blue-500)}.text-red-500{color:var(--color-red-500)}.text-white{color:var(--color-white)}.backdrop-blur-sm{--tw-backdrop-blur:blur(var(--blur-sm));-webkit-backdrop-filter:var(--tw-backdrop-blur,)var(--tw-backdrop-brightness,)var(--tw-backdrop-contrast,)var(--tw-backdrop-grayscale,)var(--tw-backdrop-hue-rotate,)var(--tw-backdrop-invert,)var(--tw-backdrop-opacity,)var(--tw-backdrop-saturate,)var(--tw-backdrop-sepia,);backdrop-filter:var(--tw-backdrop-blur,)var(--tw-backdrop-brightness,)var(--tw-backdrop-contrast,)var(--tw-backdrop-grayscale,)var(--tw-backdrop-hue-rotate,)var(--tw-backdrop-invert,)var(--tw-backdrop-opacity,)var(--tw-backdrop-saturate,)var(--tw-backdrop-sepia,)}@media (hover:hover){.hover\:bg-blue-600:hover{background-color:var(--color-blue-600)}.hover\:bg-gray-600:hover{background-color:var(--color-gray-600)}.hover\:opacity-80:hover{opacity:.8}}@media (min-width:64rem){.lg\:left-1\/2{left:50%}.lg\:-translate-x-1\/2{--tw-translate-x: -50% ;translate:var(--tw-translate-x)var(--tw-translate-y)}}}@property --tw-translate-x{syntax:"*";inherits:false;initial-value:0}@property --tw-translate-y{syntax:"*";inherits:false;initial-value:0}@property --tw-translate-z{syntax:"*";inherits:false;initial-value:0}@property --tw-font-weight{syntax:"*";inherits:false}@property --tw-backdrop-blur{syntax:"*";inherits:false}@property --tw-backdrop-brightness{syntax:"*";inherits:false}@property --tw-backdrop-contrast{syntax:"*";inherits:false}@property --tw-backdrop-grayscale{syntax:"*";inherits:false}@property --tw-backdrop-hue-rotate{syntax:"*";inherits:false}@property --tw-backdrop-invert{syntax:"*";inherits:false}@property --tw-backdrop-opacity{syntax:"*";inherits:false}@property --tw-backdrop-saturate{syntax:"*";inherits:false}@property --tw-backdrop-sepia{syntax:"*";inherits:false}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ReactElement } from 'react';
|
|
2
|
+
import { ReactPortal } from 'react';
|
|
3
|
+
import { StoreApi } from 'zustand/vanilla';
|
|
4
|
+
import { TargetAndTransition } from 'motion/react';
|
|
5
|
+
import { UseBoundStore } from 'zustand/react';
|
|
6
|
+
|
|
7
|
+
declare type Action = {
|
|
8
|
+
closeModal: (name?: ModalName) => void;
|
|
9
|
+
openModal: <T extends ModalName>(name: T, params?: Omit<ModalParams<T>, 'closeModal'>) => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
declare type ActiveModal = {
|
|
13
|
+
id: string;
|
|
14
|
+
name: ModalName;
|
|
15
|
+
params?: ModalParams<ModalName>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export declare type BaseModalParams = {
|
|
19
|
+
/**
|
|
20
|
+
* Function to close the modal. Automatically injected.
|
|
21
|
+
*/
|
|
22
|
+
closeModal: () => void;
|
|
23
|
+
/**
|
|
24
|
+
* Callback triggered when the modal is closed.
|
|
25
|
+
*/
|
|
26
|
+
onClose?: () => void;
|
|
27
|
+
/**
|
|
28
|
+
* Whether to close the modal when clicking outside of it.
|
|
29
|
+
*/
|
|
30
|
+
closeOnClickOutside?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Whether to close the modal when pressing the Escape key.
|
|
33
|
+
*/
|
|
34
|
+
closeOnPressEsc?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Styling options for the outer container of the modal.
|
|
37
|
+
*/
|
|
38
|
+
container?: {
|
|
39
|
+
className?: string;
|
|
40
|
+
override?: boolean;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Styling options for the modal content/body.
|
|
44
|
+
*/
|
|
45
|
+
body?: {
|
|
46
|
+
className?: string;
|
|
47
|
+
override?: boolean;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Framer Motion animation settings for entrance and exit.
|
|
51
|
+
*/
|
|
52
|
+
animate?: {
|
|
53
|
+
exit?: TargetAndTransition;
|
|
54
|
+
animate?: TargetAndTransition;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
declare type ModalComponent<T = any> = (props: T) => ReactElement;
|
|
59
|
+
|
|
60
|
+
export declare type ModalConfig = {
|
|
61
|
+
[K in ModalName]: ModalComponent<ModalParams<K>>;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export declare type ModalName = keyof ModalStackParams;
|
|
65
|
+
|
|
66
|
+
export declare type ModalParams<T extends ModalName> = ModalStackParams[T] & BaseModalParams;
|
|
67
|
+
|
|
68
|
+
export declare const ModalProvider: ({ modals }: Props) => ReactPortal | null;
|
|
69
|
+
|
|
70
|
+
export declare interface ModalStackParams {
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
declare type Props = {
|
|
74
|
+
modals: ModalConfig;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
declare type State = {
|
|
78
|
+
activeModals: ActiveModal[];
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export declare const useModal: UseBoundStore<StoreApi<State & Action>>;
|
|
82
|
+
|
|
83
|
+
export { }
|