modalio 0.9.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 +157 -0
- package/dist/index.cjs +912 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +618 -0
- package/dist/index.d.ts +618 -0
- package/dist/index.js +874 -0
- package/dist/index.js.map +1 -0
- package/package.json +76 -0
- package/src/adapters.ts +567 -0
- package/src/api.ts +418 -0
- package/src/context.tsx +206 -0
- package/src/core.ts +226 -0
- package/src/hoc.tsx +114 -0
- package/src/hooks.ts +250 -0
- package/src/index.ts +81 -0
- package/src/modal-manager.ts +136 -0
- package/src/types.ts +243 -0
package/README.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# modalio
|
|
2
|
+
|
|
3
|
+
A lightweight, type-safe, and animation-aware modal management library for React.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/achromaticlabs/react-modalio/actions)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **🚀 Promise-based API** - `open()` returns promises for `afterOpened()` and `afterClosed()`.
|
|
11
|
+
- **🛡️ Type-safe** - Full TypeScript support with generics for modal data and results.
|
|
12
|
+
- **✨ Animation-aware** - Automatically waits for enter/exit animations/transitions before cleanup.
|
|
13
|
+
- **🔌 Pre-built Adapters** - Seamless integration with **Shadcn UI**, **Radix UI**, and **Base UI**.
|
|
14
|
+
- **📦 Lightweight** - Zero dependencies beyond React.
|
|
15
|
+
- **🧠 Flexible** - Manage modals via programmatic API or declarative JSX.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install modalio
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
### 1. Wrap your app with the Provider
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { ModalManager } from "modalio";
|
|
29
|
+
|
|
30
|
+
function Root() {
|
|
31
|
+
return (
|
|
32
|
+
<ModalManager.Provider>
|
|
33
|
+
<App />
|
|
34
|
+
</ModalManager.Provider>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 2. Define a modal
|
|
40
|
+
|
|
41
|
+
Use pre-built adapters to bridge your UI library with the modal manager logic.
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
import { ModalManager, useModal, shadcnUiDialog, shadcnUiDialogContent } from "modalio";
|
|
45
|
+
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
|
46
|
+
|
|
47
|
+
const MyModal = ModalManager.create(({ title }: { title: string }) => {
|
|
48
|
+
const modal = useModal();
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<Dialog {...shadcnUiDialog(modal)}>
|
|
52
|
+
<DialogContent {...shadcnUiDialogContent(modal)}>
|
|
53
|
+
<DialogHeader>
|
|
54
|
+
<DialogTitle>{title}</DialogTitle>
|
|
55
|
+
</DialogHeader>
|
|
56
|
+
<p>This is a managed modal!</p>
|
|
57
|
+
<button onClick={() => modal.close("success")}>Close with Result</button>
|
|
58
|
+
</DialogContent>
|
|
59
|
+
</Dialog>
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 3. Open the modal
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
// 1. Programmatic Usage (e.g., in an event handler)
|
|
68
|
+
const openConfirmModal = async () => {
|
|
69
|
+
const result = await ModalManager.open(MyModal, {
|
|
70
|
+
data: { title: "Are you sure?" }
|
|
71
|
+
}).afterClosed();
|
|
72
|
+
|
|
73
|
+
if (result === "success") {
|
|
74
|
+
// Handle success (programmatic close)
|
|
75
|
+
} else {
|
|
76
|
+
// result is undefined if the modal was dismissed (cancelled)
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// 2. Declarative Usage (via JSX)
|
|
81
|
+
function MyPage() {
|
|
82
|
+
return (
|
|
83
|
+
<>
|
|
84
|
+
{/* Registers the modal for later use by its ID */}
|
|
85
|
+
<ModalManager.Def id="optional-id" component={MyModal} props={{ title: "Static Modal" }} />
|
|
86
|
+
<button onClick={() => ModalManager.open("optional-id")}>Open by ID</button>
|
|
87
|
+
</>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Common Patterns
|
|
93
|
+
|
|
94
|
+
### **1. Returning Results**
|
|
95
|
+
The `open()` function is generically typed to support results.
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
const openConfirm = async () => {
|
|
99
|
+
// result is typed as "confirmed" | "cancelled" | undefined
|
|
100
|
+
const result = await ModalManager.open<boolean>(MyModal).afterClosed();
|
|
101
|
+
|
|
102
|
+
if (result) {
|
|
103
|
+
console.log("User confirmed!");
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### **2. Lifecycle Hooks**
|
|
109
|
+
Wait for the modal to be fully opened or perform actions before it closes.
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
const ref = ModalManager.open(MyModal);
|
|
113
|
+
|
|
114
|
+
ref.afterOpened().then(() => {
|
|
115
|
+
console.log("Modal is now fully visible and animations are done.");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const result = await ref.afterClosed();
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### **3. Closing All Modals**
|
|
122
|
+
Useful for navigation changes or resetting application state.
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
// Closes all modals and waits for their exit animations
|
|
126
|
+
await ModalManager.closeAll();
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Supported Libraries (Adapters)
|
|
130
|
+
|
|
131
|
+
We provide pre-built adapters to make integration seamless:
|
|
132
|
+
|
|
133
|
+
### **Shadcn UI**
|
|
134
|
+
```tsx
|
|
135
|
+
import { shadcnUiDialog, shadcnUiDialogContent, shadcnUiDrawer } from "modalio";
|
|
136
|
+
```
|
|
137
|
+
Adapters: `shadcnUiDialog`, `shadcnUiDialogContent`, `shadcnUiAlertDialog`, `shadcnUiSheet`, `shadcnUiDrawer`, `shadcnUiPopover`.
|
|
138
|
+
|
|
139
|
+
### **Radix UI**
|
|
140
|
+
```tsx
|
|
141
|
+
import { radixUiDialog, radixUiDialogContent, radixUiPopover } from "modalio";
|
|
142
|
+
```
|
|
143
|
+
Adapters: `radixUiDialog`, `radixUiDialogContent`, `radixUiAlertDialog`, `radixUiAlertDialogContent`, `radixUiPopover`, `radixUiSheet`.
|
|
144
|
+
|
|
145
|
+
### **Base UI**
|
|
146
|
+
```tsx
|
|
147
|
+
import { baseUiDialog, baseUiPopover } from "modalio";
|
|
148
|
+
```
|
|
149
|
+
Adapters: `baseUiDialog`, `baseUiDialogPopup`, `baseUiDialogPortal`, `baseUiAlertDialog`, `baseUiAlertDialogPopup`, `baseUiPopover`, `baseUiPopoverPopup`, `baseUiPopoverPortal`, `baseUiSheet`.
|
|
150
|
+
|
|
151
|
+
## Documentation
|
|
152
|
+
|
|
153
|
+
For full documentation, including advanced usage, custom adapters, and API reference, visit [achromatic.dev/docs/react-modalio](https://achromatic.dev/docs/react-modalio).
|
|
154
|
+
|
|
155
|
+
## License
|
|
156
|
+
|
|
157
|
+
MIT
|