react-flow-modal 0.2.0 → 0.3.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 +100 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# react-flow-modal
|
|
2
|
+
|
|
3
|
+
Promise-based modal flows for React.
|
|
4
|
+
|
|
5
|
+
`react-flow-modal` lets you treat modals as async flows using
|
|
6
|
+
`Promise` and `async/await`, without coupling your UI to state-driven logic.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add react-flow-modal
|
|
14
|
+
# or
|
|
15
|
+
npm install react-flow-modal
|
|
16
|
+
# or
|
|
17
|
+
yarn add react-flow-modal
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Basic Usage
|
|
21
|
+
|
|
22
|
+
```JSX
|
|
23
|
+
import { ModalProvider, ModalHost, useModal } from "react-flow-modal";
|
|
24
|
+
|
|
25
|
+
function App() {
|
|
26
|
+
const modal = useModal();
|
|
27
|
+
|
|
28
|
+
const onClick = async () => {
|
|
29
|
+
const result = await modal.open("confirm",
|
|
30
|
+
(resolve, reject) => (
|
|
31
|
+
<ConfirmModal
|
|
32
|
+
onConfirm={() => resolve(true)}
|
|
33
|
+
onCancel={() => resolve(false)}
|
|
34
|
+
/>
|
|
35
|
+
));
|
|
36
|
+
|
|
37
|
+
// Resolving or rejecting the promise will also remove the modal from the stack
|
|
38
|
+
|
|
39
|
+
console.log(result);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return <button onClick={onClick}>Open modal</button>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default function Root() {
|
|
46
|
+
return (
|
|
47
|
+
<ModalProvider>
|
|
48
|
+
<App />
|
|
49
|
+
<ModalHost />
|
|
50
|
+
</ModalProvider>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
### open
|
|
58
|
+
```TS
|
|
59
|
+
open<T>(
|
|
60
|
+
key: string,
|
|
61
|
+
render: (
|
|
62
|
+
resolve: (value: T) => void,
|
|
63
|
+
reject: (reason?: unknown) => void
|
|
64
|
+
) => React.ReactNode
|
|
65
|
+
): Promise<T>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Important
|
|
69
|
+
|
|
70
|
+
> ⚠️ Make sure to always resolve or reject the promise.
|
|
71
|
+
> Leaving it pending will block the async flow.
|
|
72
|
+
|
|
73
|
+
## Why react-flow-modal?
|
|
74
|
+
|
|
75
|
+
Most modal libraries are state-driven:
|
|
76
|
+
```JSX
|
|
77
|
+
setOpen(true);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
This makes modal control implicit and tightly coupled to rendering.
|
|
81
|
+
|
|
82
|
+
react-flow-modal treats modals as explicit async control points:
|
|
83
|
+
|
|
84
|
+
```JSX
|
|
85
|
+
const result = await open(...);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
This keeps control flow readable, composable, and testable.
|
|
89
|
+
|
|
90
|
+
## Features
|
|
91
|
+
|
|
92
|
+
- Headless API (no styles, no UI constraints)
|
|
93
|
+
- Promise-based modal control
|
|
94
|
+
- Stack-based modal rendering
|
|
95
|
+
- Fully controlled by user events
|
|
96
|
+
- Works naturally with async / await
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
MIT
|