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/README.md +57 -160
- package/dist/index.css +1 -1
- package/dist/index.d.ts +97 -426
- package/dist/index.js +304 -235
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,186 +1,83 @@
|
|
|
1
1
|
# react-motion-modal
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[](https://github.com/sonnv1912/react-motion-modal/blob/main/LICENSE)
|
|
6
|
-
[](https://bundlephobia.com/package/react-motion-modal)
|
|
7
|
-
[](https://www.npmjs.com/package/react-motion-modal)
|
|
8
|
-
[](https://www.npmjs.com/package/react-motion-modal)
|
|
9
|
-
[](https://github.com/sonnv1912/react-motion-modal/issues)
|
|
10
|
-
[](https://socket.dev/npm/package/react-motion-modal)
|
|
3
|
+
Typed, awaitable modal flows for React, with headless Motion presets and
|
|
4
|
+
deterministic orchestration.
|
|
11
5
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
## ✨ Features
|
|
15
|
-
|
|
16
|
-
- Stack-based modal management (multiple modals at once)
|
|
17
|
-
- Type-safe parameters per modal
|
|
18
|
-
- Auto-injection of `closeModal()` into each modal props
|
|
19
|
-
- Full intellisense for modal names and parameters
|
|
20
|
-
- Easily extensible via module augmentation
|
|
21
|
-
|
|
22
|
-
## 🧱 Installation
|
|
6
|
+
## Installation
|
|
23
7
|
|
|
24
8
|
```bash
|
|
25
9
|
npm install react-motion-modal motion zustand
|
|
26
10
|
```
|
|
27
11
|
|
|
28
|
-
or with yarn:
|
|
29
|
-
|
|
30
|
-
```bash
|
|
31
|
-
yarn add react-motion-modal motion zustand
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
`react` and `react-dom` are expected to already exist in your app.
|
|
35
|
-
|
|
36
|
-
## 🚀 Usage
|
|
37
|
-
|
|
38
|
-
### 1. Import styles
|
|
39
|
-
|
|
40
|
-
Import the package stylesheet once in your app entry:
|
|
41
|
-
|
|
42
12
|
```ts
|
|
43
13
|
import 'react-motion-modal/style.css';
|
|
44
14
|
```
|
|
45
15
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
Create a `modal.d.ts` file in your project (make sure to include it in tsconfig.json):
|
|
49
|
-
|
|
50
|
-
```ts
|
|
51
|
-
import type { ModalDefinition } from 'react-motion-modal';
|
|
52
|
-
|
|
53
|
-
declare module 'react-motion-modal' {
|
|
54
|
-
interface ModalDefinition {
|
|
55
|
-
AlertModal: {
|
|
56
|
-
title: string;
|
|
57
|
-
};
|
|
58
|
-
ConfirmModal: {
|
|
59
|
-
onConfirm: () => void;
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### 3. Import <ModalProvider />
|
|
66
|
-
|
|
67
|
-
Import <ModalProvider /> to define your modals
|
|
68
|
-
|
|
69
|
-
```ts
|
|
70
|
-
import { ModalProvider } from 'react-motion-modal';
|
|
71
|
-
|
|
72
|
-
export const App = () => {
|
|
73
|
-
return (
|
|
74
|
-
<div>
|
|
75
|
-
{/* rest of your app */}
|
|
76
|
-
|
|
77
|
-
<ModalProvider
|
|
78
|
-
modals={{
|
|
79
|
-
AlertModal,
|
|
80
|
-
ConfirmModal,
|
|
81
|
-
}}
|
|
82
|
-
/>
|
|
83
|
-
</div>
|
|
84
|
-
);
|
|
85
|
-
};
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### 4. Open modal anywhere
|
|
16
|
+
## Usage
|
|
89
17
|
|
|
90
18
|
```tsx
|
|
91
|
-
import {
|
|
92
|
-
|
|
93
|
-
|
|
19
|
+
import {
|
|
20
|
+
createModalSystem,
|
|
21
|
+
defineModal,
|
|
22
|
+
ModalProvider,
|
|
23
|
+
type ModalRenderProps,
|
|
24
|
+
motionPresets,
|
|
25
|
+
} from 'react-motion-modal';
|
|
26
|
+
|
|
27
|
+
type ConfirmInput = { title: string };
|
|
28
|
+
|
|
29
|
+
function ConfirmModal({
|
|
30
|
+
input,
|
|
31
|
+
resolve,
|
|
32
|
+
cancel,
|
|
33
|
+
}: ModalRenderProps<ConfirmInput, boolean>) {
|
|
34
|
+
return (
|
|
35
|
+
<section>
|
|
36
|
+
<h2>{input.title}</h2>
|
|
37
|
+
<button onClick={() => cancel()}>Cancel</button>
|
|
38
|
+
<button onClick={() => resolve(true)}>Confirm</button>
|
|
39
|
+
</section>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
94
42
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
43
|
+
const modals = createModalSystem({
|
|
44
|
+
confirm: defineModal({
|
|
45
|
+
component: ConfirmModal,
|
|
46
|
+
preset: motionPresets.dialog(),
|
|
47
|
+
policy: 'singleton',
|
|
48
|
+
}),
|
|
99
49
|
});
|
|
100
|
-
```
|
|
101
50
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
import type { ModalParams } from 'react-motion-modal';
|
|
106
|
-
|
|
107
|
-
const ConfirmModal = ({ title, closeModal }: ModalParams<'ConfirmModal'>) => (
|
|
108
|
-
<div>
|
|
109
|
-
<h1>{title}</h1>
|
|
110
|
-
<button onClick={closeModal}>Close</button>
|
|
111
|
-
</div>
|
|
112
|
-
);
|
|
51
|
+
export function App() {
|
|
52
|
+
return <ModalProvider system={modals} />;
|
|
53
|
+
}
|
|
113
54
|
```
|
|
114
55
|
|
|
115
|
-
## 🧩 API
|
|
116
|
-
|
|
117
|
-
### `openModal(name, params)`
|
|
118
|
-
|
|
119
|
-
Opens a modal by name. The `params` will be inferred based on modal type.
|
|
120
|
-
|
|
121
|
-
### `closeModal(name?)`
|
|
122
|
-
|
|
123
|
-
- If `name` is provided, closes that specific modal.
|
|
124
|
-
- If not, closes the most recent modal (top of the stack).
|
|
125
|
-
|
|
126
|
-
### `closeAllModal()`
|
|
127
|
-
|
|
128
|
-
Closes every modal from top to bottom and invokes each instance's `onClose`
|
|
129
|
-
callback once.
|
|
130
|
-
|
|
131
|
-
### `BaseModalParams`
|
|
132
|
-
|
|
133
|
-
Each modal automatically receives the following props:
|
|
134
|
-
|
|
135
|
-
| Prop | Type | Description |
|
|
136
|
-
|-----------------------|------------------------------------------------------------------|-------------------------------------------------------------------------------|
|
|
137
|
-
| `closeModal` | `() => void` | Close function for the modal |
|
|
138
|
-
| `onClose` | `() => void` | Callback triggered when modal is closed |
|
|
139
|
-
| `ariaLabel` | `string` | Accessible dialog name. Defaults to `Modal` |
|
|
140
|
-
| `ariaLabelledBy` | `string` | ID of the visible element that labels the dialog |
|
|
141
|
-
| `ariaDescribedBy` | `string` | ID of the element that describes the dialog |
|
|
142
|
-
| `closeOnClickOutside` | `boolean` | Close modal when clicking outside |
|
|
143
|
-
| `closeOnPressEsc` | `boolean` | Close modal when Escape key is pressed |
|
|
144
|
-
| `position` | `string` | Modal placement. Unknown values fall back to `center`. |
|
|
145
|
-
| `backdrop` | `{ className?: string; style?: CSSProperties; }` | Customize the backdrop layer |
|
|
146
|
-
| `body` | `{ className?: string; style?: CSSProperties; }` | Customize the modal body wrapper |
|
|
147
|
-
| `animate` | `{ animate?: TargetAndTransition; exit?: TargetAndTransition; }` | Animation powered by Framer Motion. Custom animation disables defaults. |
|
|
148
|
-
| `blur` | `boolean` | Apply backdrop blur |
|
|
149
|
-
|
|
150
|
-
The package also exports `Params`, `BaseModalParams`, `ModalConfig`,
|
|
151
|
-
`ModalInitialParams`, and the store state/action types for advanced usage.
|
|
152
|
-
|
|
153
|
-
### `MODAL_POSITIONS`
|
|
154
|
-
|
|
155
56
|
```ts
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
MODAL_POSITIONS.TOP_CENTER;
|
|
160
|
-
MODAL_POSITIONS.LEFT;
|
|
161
|
-
MODAL_POSITIONS.RIGHT;
|
|
162
|
-
MODAL_POSITIONS.CENTER;
|
|
163
|
-
MODAL_POSITIONS.CENTER_FULL;
|
|
164
|
-
MODAL_POSITIONS.BOTTOM;
|
|
165
|
-
MODAL_POSITIONS.BOTTOM_LEFT;
|
|
166
|
-
MODAL_POSITIONS.BOTTOM_RIGHT;
|
|
167
|
-
MODAL_POSITIONS.BOTTOM_CENTER;
|
|
168
|
-
```
|
|
57
|
+
const outcome = await modals.open('confirm', {
|
|
58
|
+
title: 'Delete this item?',
|
|
59
|
+
});
|
|
169
60
|
|
|
170
|
-
|
|
61
|
+
if (outcome.status === 'resolved' && outcome.value) {
|
|
62
|
+
await deleteItem();
|
|
63
|
+
}
|
|
64
|
+
```
|
|
171
65
|
|
|
172
|
-
##
|
|
66
|
+
## API
|
|
173
67
|
|
|
174
|
-
-
|
|
175
|
-
|
|
176
|
-
-
|
|
68
|
+
- `defineModal()` binds a component to its input, result, preset, defaults, and
|
|
69
|
+
policy.
|
|
70
|
+
- `createModalSystem()` creates an isolated registry and stack.
|
|
71
|
+
- `system.open()` returns `Promise<ModalOutcome<Result>>`.
|
|
72
|
+
- `system.cancel()` cancels the active modal.
|
|
73
|
+
- `system.cancelAll()` cancels the full stack.
|
|
74
|
+
- `ModalProvider` handles portal rendering, focus, Escape, backdrop
|
|
75
|
+
cancellation, scroll lock, and provider cleanup.
|
|
76
|
+
- `motionPresets.dialog()`, `.drawer()`, and `.sheet()` provide headless motion.
|
|
177
77
|
|
|
178
|
-
|
|
78
|
+
Policies are `stack` (default), `replace`, and `singleton`.
|
|
179
79
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
- You can implement a `ModalRenderer` to dynamically render modals from `modals` state
|
|
183
|
-
- Great for complex apps requiring nested or dynamic modal stacks
|
|
80
|
+
The stylesheet contains structural layout only. Applications own all visual
|
|
81
|
+
styling.
|
|
184
82
|
|
|
185
|
-
|
|
186
|
-
Made with ❤️ by react-motion-modal.
|
|
83
|
+
Full documentation: https://sonnv1912.github.io/react-motion-modal/
|
package/dist/index.css
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
:where(.react-motion-modal__root){position:fixed;inset:0}:where(.react-motion-modal__backdrop){position:absolute;inset:0;background-color
|
|
1
|
+
:where(.react-motion-modal__root){position:fixed;inset:0}:where(.react-motion-modal__backdrop){position:absolute;inset:0;background-color:transparent}:where(.react-motion-modal__body){position:absolute;box-sizing:border-box;max-height:100vh;max-height:100dvh;max-width:100vw;max-width:100dvw;overflow-x:hidden;overflow-y:auto}:where(.react-motion-modal__content){width:100%;height:100%}:where(.react-motion-modal__body--top){top:0;left:0;right:0;width:100%}:where(.react-motion-modal__body--top-left){top:0;left:0}:where(.react-motion-modal__body--top-right){top:0;right:0}:where(.react-motion-modal__body--top-center){top:0;left:50%;transform:translate(-50%)}:where(.react-motion-modal__body--bottom){right:0;bottom:0;left:0;width:100%}:where(.react-motion-modal__body--bottom-left){bottom:0;left:0}:where(.react-motion-modal__body--bottom-right){right:0;bottom:0}:where(.react-motion-modal__body--bottom-center){bottom:0;left:50%;transform:translate(-50%)}:where(.react-motion-modal__body--left){top:0;bottom:0;left:0}:where(.react-motion-modal__body--right){top:0;right:0;bottom:0}:where(.react-motion-modal__body--center){top:50%;left:50%;transform:translate(-50%,-50%)}:where(.react-motion-modal__body--center-full){inset:0;width:100%;height:100%}
|