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/README.md CHANGED
@@ -1,175 +1,83 @@
1
1
  # react-motion-modal
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/react-motion-modal)](https://www.npmjs.com/package/react-motion-modal)
4
- [![npm downloads](https://img.shields.io/npm/dm/react-motion-modal)](https://www.npmjs.com/package/react-motion-modal)
5
- [![License](https://img.shields.io/npm/l/react-motion-modal)](https://github.com/sonnv1912/react-motion-modal/blob/main/LICENSE)
6
- [![Bundle size](https://img.shields.io/bundlephobia/minzip/react-motion-modal)](https://bundlephobia.com/package/react-motion-modal)
7
- [![TypeScript types](https://img.shields.io/npm/types/react-motion-modal)](https://www.npmjs.com/package/react-motion-modal)
8
- [![Node version](https://img.shields.io/node/v/react-motion-modal)](https://www.npmjs.com/package/react-motion-modal)
9
- [![GitHub issues](https://img.shields.io/github/issues/sonnv1912/react-motion-modal)](https://github.com/sonnv1912/react-motion-modal/issues)
10
- [![Socket](https://socket.dev/api/badge/npm/package/react-motion-modal)](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
- `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.
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
- ### 2. Define modal types
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 { MODAL_POSITIONS, modalStore } from 'react-motion-modal';
92
-
93
- const { openModal } = modalStore();
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
- openModal('ConfirmModal', {
96
- title: 'Are you sure you want to delete?',
97
- position: MODAL_POSITIONS.RIGHT,
98
- closeOnClickOutside: true,
43
+ const modals = createModalSystem({
44
+ confirm: defineModal({
45
+ component: ConfirmModal,
46
+ preset: motionPresets.dialog(),
47
+ policy: 'singleton',
48
+ }),
99
49
  });
100
- ```
101
-
102
- ### 5. Use `closeModal` inside your modal component
103
50
 
104
- ```tsx
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
- ### `BaseModalParams`
127
-
128
- Each modal automatically receives the following props:
129
-
130
- | Prop | Type | Description |
131
- |-----------------------|------------------------------------------------------------------|-------------------------------------------------------------------------------|
132
- | `closeModal` | `() => void` | Close function for the modal |
133
- | `onClose` | `() => void` | Callback triggered when modal is closed |
134
- | `closeOnClickOutside` | `boolean` | Close modal when clicking outside |
135
- | `closeOnPressEsc` | `boolean` | Close modal when Escape key is pressed |
136
- | `position` | `string` | Modal placement. Unknown values fall back to `center`. |
137
- | `backdrop` | `{ className?: string; style?: CSSProperties; }` | Customize the backdrop layer |
138
- | `body` | `{ className?: string; style?: CSSProperties; }` | Customize the modal body wrapper |
139
- | `animate` | `{ animate?: TargetAndTransition; exit?: TargetAndTransition; }` | Animation powered by Framer Motion. Custom animation disables defaults. |
140
- | `blur` | `boolean` | Apply backdrop blur |
141
-
142
- ### `MODAL_POSITIONS`
143
-
144
56
  ```ts
145
- MODAL_POSITIONS.TOP;
146
- MODAL_POSITIONS.TOP_LEFT;
147
- MODAL_POSITIONS.TOP_RIGHT;
148
- MODAL_POSITIONS.TOP_CENTER;
149
- MODAL_POSITIONS.LEFT;
150
- MODAL_POSITIONS.RIGHT;
151
- MODAL_POSITIONS.CENTER;
152
- MODAL_POSITIONS.CENTER_FULL;
153
- MODAL_POSITIONS.BOTTOM;
154
- MODAL_POSITIONS.BOTTOM_LEFT;
155
- MODAL_POSITIONS.BOTTOM_RIGHT;
156
- MODAL_POSITIONS.BOTTOM_CENTER;
157
- ```
57
+ const outcome = await modals.open('confirm', {
58
+ title: 'Delete this item?',
59
+ });
158
60
 
159
- Default body animation follows the position. For example, `left` slides in from the left, `right` slides in from the right, `top` slides down, and `bottom` slides up.
61
+ if (outcome.status === 'resolved' && outcome.value) {
62
+ await deleteItem();
63
+ }
64
+ ```
160
65
 
161
- ## 🔐 Release Security
66
+ ## API
162
67
 
163
- - CI runs from `.github/workflows/ci.yml` on pushes and pull requests.
164
- - npm publishing is defined in `.github/workflows/publish.yml` and uses `npm publish --provenance --access public`.
165
- - Trusted publishing still needs to be enabled in npm package settings for this repository before the workflow can publish official releases.
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.
166
77
 
167
- ## 📝 Notes
78
+ Policies are `stack` (default), `replace`, and `singleton`.
168
79
 
169
- - Zustand-based, framework-agnostic state management
170
- - Built-in animation system powered by [Framer Motion](https://www.framer.com/motion/)
171
- - You can implement a `ModalRenderer` to dynamically render modals from `modals` state
172
- - Great for complex apps requiring nested or dynamic modal stacks
80
+ The stylesheet contains structural layout only. Applications own all visual
81
+ styling.
173
82
 
174
- ---
175
- 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:#0006}:where(.react-motion-modal__backdrop--custom){background-color:transparent}:where(.react-motion-modal__body){position:absolute;max-height:100vh;max-width:100vw;overflow:hidden;padding:20px}: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){inset:0 50% 50% 0}:where(.react-motion-modal__body--top-right){inset:0 0 50% 50%}:where(.react-motion-modal__body--top-center){top:0;left:50%;transform:translate(-50%);bottom:50%}:where(.react-motion-modal__body--bottom){right:0;bottom:0;left:0;width:100%}:where(.react-motion-modal__body--bottom-left){inset:50% 50% 0 0}:where(.react-motion-modal__body--bottom-right){inset:50% 0 0 50%}:where(.react-motion-modal__body--bottom-center){bottom:0;left:50%;transform:translate(-50%);top:50%}:where(.react-motion-modal__body--left){inset:0 50% 0 0}:where(.react-motion-modal__body--right){inset:0 0 0 50%}:where(.react-motion-modal__body--center){top:50%;left:50%;transform:translate(-50%,-50%);bottom:0}:where(.react-motion-modal__body--center-full){inset:0;width:100%;height:100%}
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%}