react-confirm 0.5.0-4 → 0.5.0-5
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 +24 -3
- package/dist/mounter/domTree.js +7 -4
- package/package.json +1 -1
- package/src/mounter/domTree.tsx +9 -5
package/README.md
CHANGED
|
@@ -72,19 +72,40 @@ const handleDelete = async (): Promise<void> => {
|
|
|
72
72
|
|
|
73
73
|
## External Control
|
|
74
74
|
|
|
75
|
-
You can
|
|
75
|
+
You can control pending confirmation dialogs from outside using the `proceed` and `dismiss` functions.
|
|
76
|
+
|
|
77
|
+
### proceed
|
|
78
|
+
|
|
79
|
+
Resolves the promise with a value and closes the dialog.
|
|
76
80
|
|
|
77
81
|
```typescript
|
|
78
82
|
import { proceed } from 'react-confirm';
|
|
79
83
|
|
|
80
84
|
const p = confirm({ message: 'Continue?' });
|
|
81
85
|
|
|
82
|
-
// Auto-close after 10 seconds
|
|
86
|
+
// Auto-close after 10 seconds with a result
|
|
83
87
|
setTimeout(() => {
|
|
84
88
|
proceed(p, false);
|
|
85
89
|
}, 10000);
|
|
86
90
|
|
|
87
|
-
const result = await p;
|
|
91
|
+
const result = await p; // false
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### dismiss
|
|
95
|
+
|
|
96
|
+
Closes the dialog without resolving or rejecting the promise. The promise remains pending.
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import { dismiss } from 'react-confirm';
|
|
100
|
+
|
|
101
|
+
const p = confirm({ message: 'Continue?' });
|
|
102
|
+
|
|
103
|
+
// Close dialog after 10 seconds without resolving
|
|
104
|
+
setTimeout(() => {
|
|
105
|
+
dismiss(p);
|
|
106
|
+
}, 10000);
|
|
107
|
+
|
|
108
|
+
// Note: await p will never complete since the promise is not resolved
|
|
88
109
|
```
|
|
89
110
|
|
|
90
111
|
## Using with React Context
|
package/dist/mounter/domTree.js
CHANGED
|
@@ -21,17 +21,20 @@ function createDomTreeMounter(defaultMountNode) {
|
|
|
21
21
|
var key = Math.floor(Math.random() * (1 << 30)).toString(16);
|
|
22
22
|
var parent = (mountNode || defaultMountNode || document.body);
|
|
23
23
|
var wrapper = parent.appendChild(document.createElement('div'));
|
|
24
|
-
confirms[key] = wrapper;
|
|
25
24
|
var root = (0, client_1.createRoot)(wrapper);
|
|
26
25
|
root.render((0, jsx_runtime_1.jsx)(Component, __assign({}, props)));
|
|
26
|
+
confirms[key] = { wrapper: wrapper, root: root };
|
|
27
27
|
callbacks.mounted && callbacks.mounted();
|
|
28
28
|
return key;
|
|
29
29
|
}
|
|
30
30
|
function unmount(key) {
|
|
31
|
-
var
|
|
31
|
+
var entry = confirms[key];
|
|
32
32
|
delete confirms[key];
|
|
33
|
-
if (
|
|
34
|
-
|
|
33
|
+
if (entry) {
|
|
34
|
+
entry.root.unmount();
|
|
35
|
+
if (entry.wrapper.parentNode) {
|
|
36
|
+
entry.wrapper.parentNode.removeChild(entry.wrapper);
|
|
37
|
+
}
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
40
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-confirm",
|
|
3
|
-
"version": "0.5.0-
|
|
3
|
+
"version": "0.5.0-5",
|
|
4
4
|
"description": "A lightweight React library that simplifies confirmation dialogs with a Promise-based API — like window.confirm(), but fully customizable.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
package/src/mounter/domTree.tsx
CHANGED
|
@@ -3,27 +3,31 @@ import { createRoot } from 'react-dom/client';
|
|
|
3
3
|
import type { Mounter } from '../types';
|
|
4
4
|
|
|
5
5
|
export function createDomTreeMounter(defaultMountNode?: Element | DocumentFragment | HTMLElement): Mounter {
|
|
6
|
-
const confirms: Record<string, HTMLElement> = {};
|
|
6
|
+
const confirms: Record<string, { wrapper: HTMLElement; root: ReturnType<typeof createRoot> }> = {};
|
|
7
7
|
const callbacks: { mounted?: () => void } = {};
|
|
8
8
|
|
|
9
9
|
function mount(Component: React.ComponentType<any>, props: any, mountNode?: HTMLElement) {
|
|
10
10
|
const key = Math.floor(Math.random() * (1 << 30)).toString(16);
|
|
11
11
|
const parent = (mountNode || (defaultMountNode as Element | DocumentFragment) || document.body) as Element | DocumentFragment;
|
|
12
12
|
const wrapper = parent.appendChild(document.createElement('div'));
|
|
13
|
-
confirms[key] = wrapper;
|
|
14
13
|
|
|
15
14
|
const root = createRoot(wrapper);
|
|
16
15
|
root.render(<Component {...props} />);
|
|
16
|
+
|
|
17
|
+
confirms[key] = { wrapper, root };
|
|
17
18
|
callbacks.mounted && callbacks.mounted();
|
|
18
19
|
return key;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
function unmount(key: string) {
|
|
22
|
-
const
|
|
23
|
+
const entry = confirms[key];
|
|
23
24
|
delete confirms[key];
|
|
24
25
|
|
|
25
|
-
if (
|
|
26
|
-
|
|
26
|
+
if (entry) {
|
|
27
|
+
entry.root.unmount();
|
|
28
|
+
if (entry.wrapper.parentNode) {
|
|
29
|
+
entry.wrapper.parentNode.removeChild(entry.wrapper);
|
|
30
|
+
}
|
|
27
31
|
}
|
|
28
32
|
}
|
|
29
33
|
|