react-confirm-alert 3.0.6 → 3.1.1
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 +56 -89
- package/dist/index.esm.js +1058 -0
- package/dist/package.json +1 -0
- package/index.d.ts +2 -31
- package/lib/index.js +1071 -295
- package/package.json +36 -40
- package/src/react-confirm-alert.css +64 -54
- package/style.css +127 -0
- package/typings/Dialog.d.ts +27 -0
- package/typings/DialogFrame.d.ts +28 -0
- package/typings/DialogHost.d.ts +17 -0
- package/typings/DialogStack.d.ts +5 -0
- package/typings/api.d.ts +21 -0
- package/typings/classNames.d.ts +16 -0
- package/typings/compat.d.ts +48 -0
- package/typings/fallbackRoot.d.ts +4 -0
- package/typings/index.d.ts +9 -0
- package/typings/lifecycle.d.ts +7 -0
- package/typings/page.d.ts +2 -0
- package/typings/store.d.ts +59 -0
- package/typings/types.d.ts +138 -0
|
@@ -0,0 +1,1058 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Component, createContext, useContext, useEffect, useId, useLayoutEffect, useReducer, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { flushSync } from "react-dom";
|
|
4
|
+
import { createRoot } from "react-dom/client";
|
|
5
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
// src/store.ts
|
|
7
|
+
var emptyState = {
|
|
8
|
+
entries: [],
|
|
9
|
+
hosts: []
|
|
10
|
+
};
|
|
11
|
+
var isVisible = (entry) => entry.phase !== "queued";
|
|
12
|
+
function push(state, entry, strategy) {
|
|
13
|
+
const busy = state.entries.some(isVisible);
|
|
14
|
+
if (strategy === "queue" && busy) return {
|
|
15
|
+
...state,
|
|
16
|
+
entries: [...state.entries, {
|
|
17
|
+
...entry,
|
|
18
|
+
phase: "queued"
|
|
19
|
+
}]
|
|
20
|
+
};
|
|
21
|
+
const entries = strategy === "replace" ? state.entries.map((e) => e.phase === "open" ? {
|
|
22
|
+
...e,
|
|
23
|
+
phase: "closing"
|
|
24
|
+
} : e) : state.entries;
|
|
25
|
+
return {
|
|
26
|
+
...state,
|
|
27
|
+
entries: [...entries, {
|
|
28
|
+
...entry,
|
|
29
|
+
phase: "open"
|
|
30
|
+
}]
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function beginClose(state, id) {
|
|
34
|
+
const entry = state.entries.find((e) => e.id === id);
|
|
35
|
+
if (!entry || entry.phase !== "open") return state;
|
|
36
|
+
return {
|
|
37
|
+
...state,
|
|
38
|
+
entries: state.entries.map((e) => e === entry ? {
|
|
39
|
+
...e,
|
|
40
|
+
phase: "closing"
|
|
41
|
+
} : e)
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function remove(state, id) {
|
|
45
|
+
const entry = state.entries.find((e) => e.id === id);
|
|
46
|
+
if (!entry || entry.phase === "open") return state;
|
|
47
|
+
return promoteQueued({
|
|
48
|
+
...state,
|
|
49
|
+
entries: state.entries.filter((e) => e !== entry)
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function update(state, id, patch) {
|
|
53
|
+
if (!state.entries.some((e) => e.id === id)) return state;
|
|
54
|
+
return {
|
|
55
|
+
...state,
|
|
56
|
+
entries: state.entries.map((e) => e.id === id ? {
|
|
57
|
+
...e,
|
|
58
|
+
options: {
|
|
59
|
+
...e.options,
|
|
60
|
+
...patch
|
|
61
|
+
}
|
|
62
|
+
} : e)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function addHost(state, host) {
|
|
66
|
+
return {
|
|
67
|
+
...state,
|
|
68
|
+
hosts: [...state.hosts, host]
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function removeHost(state, host) {
|
|
72
|
+
return {
|
|
73
|
+
...state,
|
|
74
|
+
hosts: state.hosts.filter((h) => h !== host)
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function promoteQueued(state) {
|
|
78
|
+
if (state.entries.some(isVisible)) return state;
|
|
79
|
+
const next = state.entries.find((e) => e.phase === "queued");
|
|
80
|
+
if (!next) return state;
|
|
81
|
+
return {
|
|
82
|
+
...state,
|
|
83
|
+
entries: state.entries.map((e) => e === next ? {
|
|
84
|
+
...e,
|
|
85
|
+
phase: "open"
|
|
86
|
+
} : e)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function topmost(state) {
|
|
90
|
+
return [...state.entries].reverse().find((e) => e.phase === "open");
|
|
91
|
+
}
|
|
92
|
+
var key = Symbol.for("react-confirm-alert/runtime");
|
|
93
|
+
var scope = globalThis;
|
|
94
|
+
function runtime() {
|
|
95
|
+
var _scope$key;
|
|
96
|
+
return (_scope$key = scope[key]) !== null && _scope$key !== void 0 ? _scope$key : scope[key] = {
|
|
97
|
+
state: emptyState,
|
|
98
|
+
counter: 0,
|
|
99
|
+
listeners: /* @__PURE__ */ new Set(),
|
|
100
|
+
containers: /* @__PURE__ */ new Map(),
|
|
101
|
+
frames: /* @__PURE__ */ new Map(),
|
|
102
|
+
blurred: null,
|
|
103
|
+
busy: /* @__PURE__ */ new Set(),
|
|
104
|
+
pageSynced: false,
|
|
105
|
+
inPlace: 0,
|
|
106
|
+
defaults: {},
|
|
107
|
+
watchingContainers: false
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
var store = {
|
|
111
|
+
get: () => runtime().state,
|
|
112
|
+
set(next) {
|
|
113
|
+
const rt = runtime();
|
|
114
|
+
if (next === rt.state) return;
|
|
115
|
+
rt.state = next;
|
|
116
|
+
for (const listener of [...rt.listeners]) listener();
|
|
117
|
+
},
|
|
118
|
+
subscribe(listener) {
|
|
119
|
+
const { listeners } = runtime();
|
|
120
|
+
listeners.add(listener);
|
|
121
|
+
return () => {
|
|
122
|
+
listeners.delete(listener);
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
nextId: () => `rca-${++runtime().counter}`
|
|
126
|
+
};
|
|
127
|
+
// src/classNames.ts
|
|
128
|
+
var builtIn = {
|
|
129
|
+
dialog: "react-confirm-alert-overlay",
|
|
130
|
+
panel: "react-confirm-alert",
|
|
131
|
+
body: "react-confirm-alert-body",
|
|
132
|
+
actions: "react-confirm-alert-button-group"
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Style layers from widest to narrowest. confirmAlert keeps the v3 look unless the app sets
|
|
136
|
+
* confirmAlert defaults, so app-wide unstyled or classNames never reach it by surprise.
|
|
137
|
+
*/
|
|
138
|
+
function styleLayers(options, legacy) {
|
|
139
|
+
var _defaults$confirmAler;
|
|
140
|
+
const defaults = runtime().defaults;
|
|
141
|
+
return [legacy ? (_defaults$confirmAler = defaults.confirmAlert) !== null && _defaults$confirmAler !== void 0 ? _defaults$confirmAler : {} : defaults, options];
|
|
142
|
+
}
|
|
143
|
+
function classFor(slot, layers, extra) {
|
|
144
|
+
return [
|
|
145
|
+
layers.reduce((u, l) => {
|
|
146
|
+
var _l$unstyled;
|
|
147
|
+
return (_l$unstyled = l.unstyled) !== null && _l$unstyled !== void 0 ? _l$unstyled : u;
|
|
148
|
+
}, void 0) ? void 0 : builtIn[slot],
|
|
149
|
+
...layers.map((l) => {
|
|
150
|
+
var _l$classNames;
|
|
151
|
+
return (_l$classNames = l.classNames) === null || _l$classNames === void 0 ? void 0 : _l$classNames[slot];
|
|
152
|
+
}),
|
|
153
|
+
extra
|
|
154
|
+
].filter(Boolean).join(" ") || void 0;
|
|
155
|
+
}
|
|
156
|
+
var slotsFor = (layers) => (slot, extra) => ({
|
|
157
|
+
className: classFor(slot, layers, extra),
|
|
158
|
+
"data-slot": slot
|
|
159
|
+
});
|
|
160
|
+
var SlotContext = createContext(slotsFor([]));
|
|
161
|
+
var useSlot = () => useContext(SlotContext);
|
|
162
|
+
function configureDialogs(defaults) {
|
|
163
|
+
runtime().defaults = defaults;
|
|
164
|
+
}
|
|
165
|
+
// src/DialogStack.tsx
|
|
166
|
+
var serverState = () => emptyState;
|
|
167
|
+
function DialogStack({ target, host }) {
|
|
168
|
+
const { entries, hosts } = useSyncExternalStore(store.subscribe, store.get, serverState);
|
|
169
|
+
const [, rerender] = useReducer((n) => n + 1, 0);
|
|
170
|
+
useLayoutEffect(() => store.subscribe(rerender), []);
|
|
171
|
+
if (!(host ? hosts[hosts.length - 1] === host : target !== null || hosts.length === 0)) return null;
|
|
172
|
+
const openIds = entries.filter((e) => e.phase === "open").map((e) => e.id);
|
|
173
|
+
const top = openIds[openIds.length - 1];
|
|
174
|
+
return /* @__PURE__ */ jsx(Fragment, { children: entries.filter((e) => e.target === target && e.phase !== "queued").map((e) => /* @__PURE__ */ jsx(EntryView, {
|
|
175
|
+
entry: e,
|
|
176
|
+
depth: openIds.indexOf(e.id),
|
|
177
|
+
topmost: e.id === top
|
|
178
|
+
}, e.id)) });
|
|
179
|
+
}
|
|
180
|
+
function EntryView({ entry, depth, topmost }) {
|
|
181
|
+
var _ref, _options$modal, _ref2, _options$closeOnClick, _ref3, _options$closeOnEscap, _options$buttons;
|
|
182
|
+
const { id, options } = entry;
|
|
183
|
+
const titleId = useId();
|
|
184
|
+
const descriptionId = useId();
|
|
185
|
+
useLayoutEffect(() => () => {
|
|
186
|
+
var _options$willUnmount, _options$afterClose;
|
|
187
|
+
if (store.get().entries.some((e) => e.id === id)) return;
|
|
188
|
+
(_options$willUnmount = options.willUnmount) === null || _options$willUnmount === void 0 || _options$willUnmount.call(options);
|
|
189
|
+
(_options$afterClose = options.afterClose) === null || _options$afterClose === void 0 || _options$afterClose.call(options);
|
|
190
|
+
entry.settle(void 0);
|
|
191
|
+
}, []);
|
|
192
|
+
const dismiss = () => closeDialog$1(id);
|
|
193
|
+
const close = (value) => {
|
|
194
|
+
entry.settle(value);
|
|
195
|
+
closeDialog$1(id);
|
|
196
|
+
};
|
|
197
|
+
const defaults = runtime().defaults;
|
|
198
|
+
const { render: Render } = options;
|
|
199
|
+
return /* @__PURE__ */ jsx(DialogFrame, {
|
|
200
|
+
id,
|
|
201
|
+
modal: (_ref = (_options$modal = options.modal) !== null && _options$modal !== void 0 ? _options$modal : defaults.modal) !== null && _ref !== void 0 ? _ref : true,
|
|
202
|
+
state: entry.phase === "closing" ? "closing" : "open",
|
|
203
|
+
kind: options.kind,
|
|
204
|
+
layers: styleLayers(options, !!options.legacy),
|
|
205
|
+
extraClass: options.overlayClassName,
|
|
206
|
+
tone: options.tone,
|
|
207
|
+
depth,
|
|
208
|
+
topmost,
|
|
209
|
+
labelledBy: options.title ? titleId : void 0,
|
|
210
|
+
describedBy: options.message ? descriptionId : void 0,
|
|
211
|
+
role: options.kind === "alert" || options.kind === "confirm" ? "alertdialog" : "dialog",
|
|
212
|
+
onKeyDown: options.onKeyDown,
|
|
213
|
+
closeOnClickOutside: (_ref2 = (_options$closeOnClick = options.closeOnClickOutside) !== null && _options$closeOnClick !== void 0 ? _options$closeOnClick : defaults.closeOnClickOutside) !== null && _ref2 !== void 0 ? _ref2 : true,
|
|
214
|
+
closeOnEscape: (_ref3 = (_options$closeOnEscap = options.closeOnEscape) !== null && _options$closeOnEscap !== void 0 ? _options$closeOnEscap : defaults.closeOnEscape) !== null && _ref3 !== void 0 ? _ref3 : true,
|
|
215
|
+
legacy: !!options.legacy,
|
|
216
|
+
onClickOutside: options.onClickOutside,
|
|
217
|
+
onDismiss: dismiss,
|
|
218
|
+
children: /* @__PURE__ */ jsx(Render, {
|
|
219
|
+
id,
|
|
220
|
+
titleId,
|
|
221
|
+
descriptionId,
|
|
222
|
+
title: options.title,
|
|
223
|
+
message: options.message,
|
|
224
|
+
buttons: (_options$buttons = options.buttons) !== null && _options$buttons !== void 0 ? _options$buttons : [],
|
|
225
|
+
close,
|
|
226
|
+
dismiss,
|
|
227
|
+
onClose: dismiss
|
|
228
|
+
})
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
// src/page.ts
|
|
232
|
+
var BODY_CLASS = "react-confirm-alert-body-element";
|
|
233
|
+
var BLUR_CLASS = "react-confirm-alert-blur";
|
|
234
|
+
function syncPage() {
|
|
235
|
+
const rt = runtime();
|
|
236
|
+
const open = store.get().entries.filter((e) => e.phase === "open");
|
|
237
|
+
document.body.classList.toggle(BODY_CLASS, rt.defaults.lockScroll !== false && open.length + rt.inPlace > 0);
|
|
238
|
+
const wantsBlur = open.some((e) => e.blur);
|
|
239
|
+
if (wantsBlur && !rt.blurred) {
|
|
240
|
+
var _document$body$childr, _rt$blurred;
|
|
241
|
+
rt.blurred = (_document$body$childr = document.body.children[0]) !== null && _document$body$childr !== void 0 ? _document$body$childr : null;
|
|
242
|
+
(_rt$blurred = rt.blurred) === null || _rt$blurred === void 0 || _rt$blurred.classList.add(BLUR_CLASS);
|
|
243
|
+
} else if (!wantsBlur && rt.blurred) {
|
|
244
|
+
rt.blurred.classList.remove(BLUR_CLASS);
|
|
245
|
+
rt.blurred = null;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function installPageEffects() {
|
|
249
|
+
const rt = runtime();
|
|
250
|
+
if (rt.pageSynced) return;
|
|
251
|
+
rt.pageSynced = true;
|
|
252
|
+
store.subscribe(syncPage);
|
|
253
|
+
}
|
|
254
|
+
// src/fallbackRoot.tsx
|
|
255
|
+
var DEFAULT_ID = "react-confirm-alert";
|
|
256
|
+
function ensureContainer(target) {
|
|
257
|
+
installPageEffects();
|
|
258
|
+
if (target !== null && !document.getElementById(target)) {
|
|
259
|
+
console.error("React Confirm Alert:", `Can not get element id (#${target})`);
|
|
260
|
+
target = null;
|
|
261
|
+
}
|
|
262
|
+
const { containers } = runtime();
|
|
263
|
+
if (target === null && store.get().hosts.length > 0) return {
|
|
264
|
+
target,
|
|
265
|
+
blur: false
|
|
266
|
+
};
|
|
267
|
+
let container = containers.get(target);
|
|
268
|
+
if (!container || !container.element.isConnected) {
|
|
269
|
+
var _target;
|
|
270
|
+
let element = document.getElementById((_target = target) !== null && _target !== void 0 ? _target : DEFAULT_ID);
|
|
271
|
+
const created = !element;
|
|
272
|
+
if (!element) {
|
|
273
|
+
element = document.createElement("div");
|
|
274
|
+
element.id = DEFAULT_ID;
|
|
275
|
+
document.body.appendChild(element);
|
|
276
|
+
}
|
|
277
|
+
const root = createRoot(element);
|
|
278
|
+
root.render(/* @__PURE__ */ jsx(DialogStack, { target }));
|
|
279
|
+
container = {
|
|
280
|
+
element,
|
|
281
|
+
root,
|
|
282
|
+
created
|
|
283
|
+
};
|
|
284
|
+
containers.set(target, container);
|
|
285
|
+
watchForEmptyContainers();
|
|
286
|
+
}
|
|
287
|
+
return {
|
|
288
|
+
target,
|
|
289
|
+
blur: target === null && container.created
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function watchForEmptyContainers() {
|
|
293
|
+
const rt = runtime();
|
|
294
|
+
if (rt.watchingContainers) return;
|
|
295
|
+
rt.watchingContainers = true;
|
|
296
|
+
const { containers } = rt;
|
|
297
|
+
store.subscribe(() => {
|
|
298
|
+
queueMicrotask(() => {
|
|
299
|
+
for (const [target, container] of containers) {
|
|
300
|
+
if (store.get().entries.some((e) => e.target === target)) continue;
|
|
301
|
+
containers.delete(target);
|
|
302
|
+
container.root.unmount();
|
|
303
|
+
if (container.created) container.element.remove();
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
// src/lifecycle.ts
|
|
309
|
+
function registerFrame(id, frame, panel) {
|
|
310
|
+
if (frame) runtime().frames.set(id, {
|
|
311
|
+
frame,
|
|
312
|
+
panel
|
|
313
|
+
});
|
|
314
|
+
else runtime().frames.delete(id);
|
|
315
|
+
}
|
|
316
|
+
function openEntry(options, target, strategy, settle = () => {}) {
|
|
317
|
+
const id = store.nextId();
|
|
318
|
+
const resolved = ensureContainer(target);
|
|
319
|
+
const entry = {
|
|
320
|
+
id,
|
|
321
|
+
options,
|
|
322
|
+
target: resolved.target,
|
|
323
|
+
blur: !!options.legacy && resolved.blur,
|
|
324
|
+
settle
|
|
325
|
+
};
|
|
326
|
+
const before = store.get().entries.filter((e) => e.phase === "open").map((e) => e.id);
|
|
327
|
+
store.set(push(store.get(), entry, strategy));
|
|
328
|
+
if (strategy === "replace") for (const old of before) store.set(remove(store.get(), old));
|
|
329
|
+
return id;
|
|
330
|
+
}
|
|
331
|
+
function commit(update, sync) {
|
|
332
|
+
if (sync && typeof window !== "undefined" && window.event !== void 0) flushSync(update);
|
|
333
|
+
else update();
|
|
334
|
+
}
|
|
335
|
+
function closeDialog$1(id, { sync = true } = {}) {
|
|
336
|
+
const entry = store.get().entries.find((e) => e.id === id);
|
|
337
|
+
if (!entry) return;
|
|
338
|
+
entry.settle(void 0);
|
|
339
|
+
if (entry.phase === "queued") {
|
|
340
|
+
commit(() => store.set(remove(store.get(), id)), sync);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const node = runtime().frames.get(id);
|
|
344
|
+
const before = new Set(node ? animationsOf(node) : []);
|
|
345
|
+
commit(() => store.set(beginClose(store.get(), id)), sync);
|
|
346
|
+
const exits = node ? animationsOf(node).filter((a) => !before.has(a)) : [];
|
|
347
|
+
if (exits.length === 0) {
|
|
348
|
+
commit(() => store.set(remove(store.get(), id)), sync);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
Promise.all(exits.map((a) => a.finished)).finally(() => store.set(remove(store.get(), id)));
|
|
352
|
+
}
|
|
353
|
+
var reopenQueued = false;
|
|
354
|
+
function reopenClosedFrames() {
|
|
355
|
+
if (reopenQueued) return;
|
|
356
|
+
reopenQueued = true;
|
|
357
|
+
queueMicrotask(() => {
|
|
358
|
+
reopenQueued = false;
|
|
359
|
+
for (const entry of store.get().entries) {
|
|
360
|
+
var _runtime$frames$get;
|
|
361
|
+
const frame = (_runtime$frames$get = runtime().frames.get(entry.id)) === null || _runtime$frames$get === void 0 ? void 0 : _runtime$frames$get.frame;
|
|
362
|
+
if (entry.phase === "open" && frame instanceof HTMLDialogElement && !frame.open) frame.showModal();
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
function animationsOf({ frame, panel }) {
|
|
367
|
+
if (typeof frame.getAnimations !== "function") return [];
|
|
368
|
+
return frame.getAnimations({ subtree: true }).filter((a) => {
|
|
369
|
+
var _a$effect;
|
|
370
|
+
const target = (_a$effect = a.effect) === null || _a$effect === void 0 ? void 0 : _a$effect.target;
|
|
371
|
+
return (target === frame || target === panel) && a.playState !== "finished";
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
// src/DialogFrame.tsx
|
|
375
|
+
var canShowModal = () => typeof HTMLDialogElement !== "undefined" && typeof HTMLDialogElement.prototype.showModal === "function";
|
|
376
|
+
function DialogFrame(props) {
|
|
377
|
+
var _props$tone, _props$depth, _props$topmost;
|
|
378
|
+
const frame = useRef(null);
|
|
379
|
+
const panel = useRef(null);
|
|
380
|
+
const pressedOn = useRef(null);
|
|
381
|
+
const stay = useRef(null);
|
|
382
|
+
const native = props.modal && canShowModal();
|
|
383
|
+
useLayoutEffect(() => {
|
|
384
|
+
var _panel$current, _panel$current2, _panel$current3;
|
|
385
|
+
const element = frame.current;
|
|
386
|
+
const previous = document.activeElement;
|
|
387
|
+
if (props.id) registerFrame(props.id, element, panel.current);
|
|
388
|
+
if (native) {
|
|
389
|
+
const dialog = element;
|
|
390
|
+
if (!dialog.open) dialog.showModal();
|
|
391
|
+
}
|
|
392
|
+
const marked = (_panel$current = panel.current) === null || _panel$current === void 0 ? void 0 : _panel$current.querySelector("[data-autofocus]");
|
|
393
|
+
const inside = (_panel$current2 = panel.current) === null || _panel$current2 === void 0 ? void 0 : _panel$current2.contains(document.activeElement);
|
|
394
|
+
if (props.legacy || !marked && !inside) (_panel$current3 = panel.current) === null || _panel$current3 === void 0 || _panel$current3.focus({ preventScroll: true });
|
|
395
|
+
else if (marked) marked.focus({ preventScroll: true });
|
|
396
|
+
return () => {
|
|
397
|
+
if (props.id) registerFrame(props.id, null, null);
|
|
398
|
+
const dialog = element;
|
|
399
|
+
if (native && dialog.open) dialog.close();
|
|
400
|
+
queueMicrotask(() => {
|
|
401
|
+
if (previous instanceof HTMLElement && previous.isConnected) previous.focus({ preventScroll: true });
|
|
402
|
+
});
|
|
403
|
+
};
|
|
404
|
+
}, []);
|
|
405
|
+
const onPointerDown = (e) => {
|
|
406
|
+
pressedOn.current = e.target;
|
|
407
|
+
};
|
|
408
|
+
const onClick = (e) => {
|
|
409
|
+
const pressed = pressedOn.current;
|
|
410
|
+
pressedOn.current = null;
|
|
411
|
+
const outside = e.target === frame.current && (pressed === null || pressed === frame.current);
|
|
412
|
+
if (props.closeOnClickOutside && outside && !isBusy()) {
|
|
413
|
+
var _props$onClickOutside;
|
|
414
|
+
(_props$onClickOutside = props.onClickOutside) === null || _props$onClickOutside === void 0 || _props$onClickOutside.call(props);
|
|
415
|
+
props.onDismiss();
|
|
416
|
+
}
|
|
417
|
+
e.stopPropagation();
|
|
418
|
+
};
|
|
419
|
+
const isTop = () => {
|
|
420
|
+
var _topmost;
|
|
421
|
+
return !props.id || !store.get().entries.some((e) => e.id === props.id) || ((_topmost = topmost(store.get())) === null || _topmost === void 0 ? void 0 : _topmost.id) === props.id;
|
|
422
|
+
};
|
|
423
|
+
const mustStay = () => !isTop() || !props.closeOnEscape || isBusy();
|
|
424
|
+
const onCancel = (e) => {
|
|
425
|
+
e.preventDefault();
|
|
426
|
+
stay.current = mustStay();
|
|
427
|
+
if (!stay.current && !props.legacy) props.onDismiss();
|
|
428
|
+
};
|
|
429
|
+
const onClose = (e) => {
|
|
430
|
+
var _stay$current;
|
|
431
|
+
const dialog = e.currentTarget;
|
|
432
|
+
if (dialog.open) return;
|
|
433
|
+
const keep = (_stay$current = stay.current) !== null && _stay$current !== void 0 ? _stay$current : mustStay();
|
|
434
|
+
stay.current = null;
|
|
435
|
+
if (props.state !== "open") return;
|
|
436
|
+
if (!keep) props.onDismiss();
|
|
437
|
+
else if (props.id && store.get().entries.some((x) => x.id === props.id)) reopenClosedFrames();
|
|
438
|
+
else dialog.showModal();
|
|
439
|
+
};
|
|
440
|
+
const isBusy = () => !!props.id && runtime().busy.has(props.id);
|
|
441
|
+
const onKeyDown = (e) => {
|
|
442
|
+
var _props$onKeyDown;
|
|
443
|
+
(_props$onKeyDown = props.onKeyDown) === null || _props$onKeyDown === void 0 || _props$onKeyDown.call(props, e, props.onDismiss);
|
|
444
|
+
if (!native && !props.legacy && e.key === "Escape" && props.closeOnEscape && !isBusy()) props.onDismiss();
|
|
445
|
+
};
|
|
446
|
+
const slot = slotsFor(props.layers);
|
|
447
|
+
const shared = {
|
|
448
|
+
...slot("dialog", props.extraClass),
|
|
449
|
+
role: props.role,
|
|
450
|
+
"aria-label": props.label,
|
|
451
|
+
"aria-labelledby": props.labelledBy,
|
|
452
|
+
"aria-describedby": props.describedBy,
|
|
453
|
+
"data-rca": "",
|
|
454
|
+
"data-state": props.state,
|
|
455
|
+
"data-kind": props.kind,
|
|
456
|
+
"data-tone": (_props$tone = props.tone) !== null && _props$tone !== void 0 ? _props$tone : "default",
|
|
457
|
+
"data-depth": String((_props$depth = props.depth) !== null && _props$depth !== void 0 ? _props$depth : 0),
|
|
458
|
+
"data-topmost": ((_props$topmost = props.topmost) !== null && _props$topmost !== void 0 ? _props$topmost : true) ? "" : void 0,
|
|
459
|
+
onPointerDown,
|
|
460
|
+
onClick,
|
|
461
|
+
onKeyDown
|
|
462
|
+
};
|
|
463
|
+
const content = /* @__PURE__ */ jsx("div", {
|
|
464
|
+
ref: panel,
|
|
465
|
+
...slot("panel"),
|
|
466
|
+
tabIndex: -1,
|
|
467
|
+
children: /* @__PURE__ */ jsx(SlotContext.Provider, {
|
|
468
|
+
value: slot,
|
|
469
|
+
children: props.children
|
|
470
|
+
})
|
|
471
|
+
});
|
|
472
|
+
return native ? /* @__PURE__ */ jsx("dialog", {
|
|
473
|
+
ref: (el) => {
|
|
474
|
+
frame.current = el;
|
|
475
|
+
},
|
|
476
|
+
...shared,
|
|
477
|
+
onCancel,
|
|
478
|
+
onClose,
|
|
479
|
+
children: content
|
|
480
|
+
}) : /* @__PURE__ */ jsx("div", {
|
|
481
|
+
ref: (el) => {
|
|
482
|
+
frame.current = el;
|
|
483
|
+
},
|
|
484
|
+
...shared,
|
|
485
|
+
children: content
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
// src/compat.tsx
|
|
489
|
+
var defaultButtons = [{
|
|
490
|
+
label: "Cancel",
|
|
491
|
+
onClick: () => null
|
|
492
|
+
}, {
|
|
493
|
+
label: "Confirm",
|
|
494
|
+
onClick: () => null
|
|
495
|
+
}];
|
|
496
|
+
function CustomUIHost({ render, args }) {
|
|
497
|
+
return /* @__PURE__ */ jsx(Fragment, { children: render(args) });
|
|
498
|
+
}
|
|
499
|
+
function LegacyContent({ props, titleId, close, id }) {
|
|
500
|
+
var _props$buttons;
|
|
501
|
+
const latest = useRef({
|
|
502
|
+
props,
|
|
503
|
+
close
|
|
504
|
+
});
|
|
505
|
+
latest.current = {
|
|
506
|
+
props,
|
|
507
|
+
close
|
|
508
|
+
};
|
|
509
|
+
useLayoutEffect(() => {
|
|
510
|
+
const keyboard = (event) => {
|
|
511
|
+
var _topmost, _props$keyCodeForClos, _props$closeOnEscape, _props$onkeyPress;
|
|
512
|
+
if (id && ((_topmost = topmost(store.get())) === null || _topmost === void 0 ? void 0 : _topmost.id) !== id) return;
|
|
513
|
+
const { props, close } = latest.current;
|
|
514
|
+
const keyCode = event.keyCode;
|
|
515
|
+
if (((_props$keyCodeForClos = props.keyCodeForClose) !== null && _props$keyCodeForClos !== void 0 ? _props$keyCodeForClos : []).includes(keyCode)) close();
|
|
516
|
+
if (((_props$closeOnEscape = props.closeOnEscape) !== null && _props$closeOnEscape !== void 0 ? _props$closeOnEscape : true) && keyCode === 27) {
|
|
517
|
+
var _props$onKeypressEsca;
|
|
518
|
+
(_props$onKeypressEsca = props.onKeypressEscape) === null || _props$onKeypressEsca === void 0 || _props$onKeypressEsca.call(props, event);
|
|
519
|
+
close();
|
|
520
|
+
}
|
|
521
|
+
(_props$onkeyPress = props.onkeyPress) === null || _props$onkeyPress === void 0 || _props$onkeyPress.call(props, event, close);
|
|
522
|
+
};
|
|
523
|
+
document.addEventListener("keydown", keyboard, false);
|
|
524
|
+
return () => document.removeEventListener("keydown", keyboard, false);
|
|
525
|
+
}, []);
|
|
526
|
+
const slot = useSlot();
|
|
527
|
+
const { title, message, customUI, childrenElement } = props;
|
|
528
|
+
const buttons = (_props$buttons = props.buttons) !== null && _props$buttons !== void 0 ? _props$buttons : defaultButtons;
|
|
529
|
+
if (customUI) return /* @__PURE__ */ jsx(CustomUIHost, {
|
|
530
|
+
render: customUI,
|
|
531
|
+
args: {
|
|
532
|
+
title,
|
|
533
|
+
message,
|
|
534
|
+
buttons,
|
|
535
|
+
onClose: close
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
539
|
+
...slot("body"),
|
|
540
|
+
children: [
|
|
541
|
+
title && /* @__PURE__ */ jsx("h1", {
|
|
542
|
+
id: titleId,
|
|
543
|
+
...slot("title"),
|
|
544
|
+
children: title
|
|
545
|
+
}),
|
|
546
|
+
message,
|
|
547
|
+
childrenElement === null || childrenElement === void 0 ? void 0 : childrenElement(),
|
|
548
|
+
/* @__PURE__ */ jsx("div", {
|
|
549
|
+
...slot("actions"),
|
|
550
|
+
children: buttons.map((button, i) => /* @__PURE__ */ jsx("button", {
|
|
551
|
+
...button,
|
|
552
|
+
...slot("button", button.className),
|
|
553
|
+
onClick: () => {
|
|
554
|
+
if (button.onClick) button.onClick();
|
|
555
|
+
close();
|
|
556
|
+
},
|
|
557
|
+
children: button.label
|
|
558
|
+
}, i))
|
|
559
|
+
})
|
|
560
|
+
]
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
function confirmAlert(properties) {
|
|
564
|
+
var _ref, _properties$modal, _runtime$defaults$con, _properties$closeOnCl, _properties$closeOnEs, _properties$targetId, _ref2, _properties$strategy, _runtime$defaults$con2;
|
|
565
|
+
function Content({ id, titleId, dismiss }) {
|
|
566
|
+
return /* @__PURE__ */ jsx(LegacyContent, {
|
|
567
|
+
props: properties,
|
|
568
|
+
titleId,
|
|
569
|
+
close: dismiss,
|
|
570
|
+
id
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
openEntry({
|
|
574
|
+
kind: "custom",
|
|
575
|
+
title: properties.title,
|
|
576
|
+
modal: (_ref = (_properties$modal = properties.modal) !== null && _properties$modal !== void 0 ? _properties$modal : (_runtime$defaults$con = runtime().defaults.confirmAlert) === null || _runtime$defaults$con === void 0 ? void 0 : _runtime$defaults$con.modal) !== null && _ref !== void 0 ? _ref : false,
|
|
577
|
+
unstyled: properties.unstyled,
|
|
578
|
+
classNames: properties.classNames,
|
|
579
|
+
closeOnClickOutside: (_properties$closeOnCl = properties.closeOnClickOutside) !== null && _properties$closeOnCl !== void 0 ? _properties$closeOnCl : true,
|
|
580
|
+
closeOnEscape: (_properties$closeOnEs = properties.closeOnEscape) !== null && _properties$closeOnEs !== void 0 ? _properties$closeOnEs : true,
|
|
581
|
+
overlayClassName: properties.overlayClassName,
|
|
582
|
+
onClickOutside: properties.onClickOutside,
|
|
583
|
+
legacy: true,
|
|
584
|
+
willUnmount: properties.willUnmount,
|
|
585
|
+
afterClose: properties.afterClose,
|
|
586
|
+
render: Content
|
|
587
|
+
}, (_properties$targetId = properties.targetId) !== null && _properties$targetId !== void 0 ? _properties$targetId : null, (_ref2 = (_properties$strategy = properties.strategy) !== null && _properties$strategy !== void 0 ? _properties$strategy : (_runtime$defaults$con2 = runtime().defaults.confirmAlert) === null || _runtime$defaults$con2 === void 0 ? void 0 : _runtime$defaults$con2.strategy) !== null && _ref2 !== void 0 ? _ref2 : "replace");
|
|
588
|
+
}
|
|
589
|
+
function DeclarativeDialog(props) {
|
|
590
|
+
var _props$modal, _props$closeOnClickOu, _props$closeOnEscape2;
|
|
591
|
+
const titleId = useId();
|
|
592
|
+
const close = () => {
|
|
593
|
+
var _props$afterClose;
|
|
594
|
+
return (_props$afterClose = props.afterClose) === null || _props$afterClose === void 0 ? void 0 : _props$afterClose.call(props);
|
|
595
|
+
};
|
|
596
|
+
return /* @__PURE__ */ jsx(DialogFrame, {
|
|
597
|
+
modal: (_props$modal = props.modal) !== null && _props$modal !== void 0 ? _props$modal : false,
|
|
598
|
+
state: "open",
|
|
599
|
+
kind: "custom",
|
|
600
|
+
layers: styleLayers({
|
|
601
|
+
unstyled: props.unstyled,
|
|
602
|
+
classNames: props.classNames
|
|
603
|
+
}, true),
|
|
604
|
+
extraClass: props.overlayClassName,
|
|
605
|
+
labelledBy: props.title ? titleId : void 0,
|
|
606
|
+
closeOnClickOutside: (_props$closeOnClickOu = props.closeOnClickOutside) !== null && _props$closeOnClickOu !== void 0 ? _props$closeOnClickOu : true,
|
|
607
|
+
closeOnEscape: (_props$closeOnEscape2 = props.closeOnEscape) !== null && _props$closeOnEscape2 !== void 0 ? _props$closeOnEscape2 : true,
|
|
608
|
+
legacy: true,
|
|
609
|
+
role: "dialog",
|
|
610
|
+
onClickOutside: props.onClickOutside,
|
|
611
|
+
onDismiss: close,
|
|
612
|
+
children: /* @__PURE__ */ jsx(LegacyContent, {
|
|
613
|
+
props,
|
|
614
|
+
titleId,
|
|
615
|
+
close
|
|
616
|
+
})
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Rendered in place, open while mounted. The parent unmounts it from a button's onClick.
|
|
621
|
+
* @deprecated use <ConfirmDialog open>
|
|
622
|
+
*/
|
|
623
|
+
var ReactConfirmAlert = class extends Component {
|
|
624
|
+
componentWillUnmount() {
|
|
625
|
+
var _this$props$willUnmou, _this$props;
|
|
626
|
+
(_this$props$willUnmou = (_this$props = this.props).willUnmount) === null || _this$props$willUnmou === void 0 || _this$props$willUnmou.call(_this$props);
|
|
627
|
+
}
|
|
628
|
+
render() {
|
|
629
|
+
return /* @__PURE__ */ jsx(DeclarativeDialog, { ...this.props });
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
// src/api.tsx
|
|
633
|
+
function open(options, dismissValue, body) {
|
|
634
|
+
var _ref, _options$strategy, _options$signal2, _options$signal3;
|
|
635
|
+
if (typeof document === "undefined") return {
|
|
636
|
+
id: "",
|
|
637
|
+
result: Promise.reject(/* @__PURE__ */ new Error("react-confirm-alert: dialogs need a browser"))
|
|
638
|
+
};
|
|
639
|
+
const controller = new AbortController();
|
|
640
|
+
let resolve;
|
|
641
|
+
const result = new Promise((r) => {
|
|
642
|
+
resolve = r;
|
|
643
|
+
});
|
|
644
|
+
const settle = (value) => {
|
|
645
|
+
var _options$signal;
|
|
646
|
+
if (controller.signal.aborted) return;
|
|
647
|
+
controller.abort();
|
|
648
|
+
(_options$signal = options.signal) === null || _options$signal === void 0 || _options$signal.removeEventListener("abort", onAbort);
|
|
649
|
+
resolve(value === void 0 ? dismissValue : value);
|
|
650
|
+
};
|
|
651
|
+
const render = (props) => /* @__PURE__ */ jsx(Fragment, { children: body(props, controller.signal) });
|
|
652
|
+
const onAbort = () => closeDialog$1(id);
|
|
653
|
+
const id = openEntry({
|
|
654
|
+
...options,
|
|
655
|
+
render
|
|
656
|
+
}, null, (_ref = (_options$strategy = options.strategy) !== null && _options$strategy !== void 0 ? _options$strategy : runtime().defaults.strategy) !== null && _ref !== void 0 ? _ref : "stack", settle);
|
|
657
|
+
(_options$signal2 = options.signal) === null || _options$signal2 === void 0 || _options$signal2.addEventListener("abort", onAbort, { once: true });
|
|
658
|
+
if ((_options$signal3 = options.signal) === null || _options$signal3 === void 0 ? void 0 : _options$signal3.aborted) closeDialog$1(id);
|
|
659
|
+
return {
|
|
660
|
+
id,
|
|
661
|
+
result
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
function Body({ props, children }) {
|
|
665
|
+
const slot = useSlot();
|
|
666
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
667
|
+
...slot("body"),
|
|
668
|
+
children: [
|
|
669
|
+
props.title && /* @__PURE__ */ jsx("h1", {
|
|
670
|
+
id: props.titleId,
|
|
671
|
+
...slot("title"),
|
|
672
|
+
children: props.title
|
|
673
|
+
}),
|
|
674
|
+
props.message != null && /* @__PURE__ */ jsx("p", {
|
|
675
|
+
id: props.descriptionId,
|
|
676
|
+
...slot("message"),
|
|
677
|
+
children: props.message
|
|
678
|
+
}),
|
|
679
|
+
children
|
|
680
|
+
]
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
function Buttons({ props, signal: getSignal, focus }) {
|
|
684
|
+
const slot = useSlot();
|
|
685
|
+
const [busy, setBusy] = useState(null);
|
|
686
|
+
const markBusy = (on) => {
|
|
687
|
+
var _runtime$frames$get;
|
|
688
|
+
return (_runtime$frames$get = runtime().frames.get(props.id)) === null || _runtime$frames$get === void 0 ? void 0 : _runtime$frames$get.frame.toggleAttribute("data-busy", on);
|
|
689
|
+
};
|
|
690
|
+
const press = async (button, index) => {
|
|
691
|
+
const signal = getSignal();
|
|
692
|
+
let waiting = false;
|
|
693
|
+
try {
|
|
694
|
+
var _button$onClick;
|
|
695
|
+
const outcome = (_button$onClick = button.onClick) === null || _button$onClick === void 0 ? void 0 : _button$onClick.call(button, {
|
|
696
|
+
close: props.close,
|
|
697
|
+
dismiss: props.dismiss,
|
|
698
|
+
signal
|
|
699
|
+
});
|
|
700
|
+
if (!(outcome instanceof Promise)) {
|
|
701
|
+
if (outcome !== false) props.close(button.value);
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
waiting = true;
|
|
705
|
+
setBusy(index);
|
|
706
|
+
markBusy(true);
|
|
707
|
+
runtime().busy.add(props.id);
|
|
708
|
+
if (await outcome !== false && !signal.aborted) props.close(button.value);
|
|
709
|
+
} catch (error) {
|
|
710
|
+
console.error(error);
|
|
711
|
+
} finally {
|
|
712
|
+
if (!waiting) return;
|
|
713
|
+
runtime().busy.delete(props.id);
|
|
714
|
+
markBusy(false);
|
|
715
|
+
if (!signal.aborted) setBusy(null);
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
return /* @__PURE__ */ jsx("div", {
|
|
719
|
+
...slot("actions"),
|
|
720
|
+
children: props.buttons.map(({ label, value, variant = "secondary", onClick, disabled, className, ...rest }, i) => /* @__PURE__ */ jsx("button", {
|
|
721
|
+
type: "button",
|
|
722
|
+
...rest,
|
|
723
|
+
...slot("button", className),
|
|
724
|
+
"data-variant": variant,
|
|
725
|
+
"data-busy": busy === i ? "" : void 0,
|
|
726
|
+
"data-autofocus": i === focus ? "" : void 0,
|
|
727
|
+
"aria-busy": busy === i || void 0,
|
|
728
|
+
disabled: disabled || busy !== null,
|
|
729
|
+
onClick: () => {
|
|
730
|
+
press(props.buttons[i], i);
|
|
731
|
+
},
|
|
732
|
+
children: label
|
|
733
|
+
}, i))
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
var choice = (focus) => (props, signal) => /* @__PURE__ */ jsx(Body, {
|
|
737
|
+
props,
|
|
738
|
+
children: /* @__PURE__ */ jsx(Buttons, {
|
|
739
|
+
props,
|
|
740
|
+
signal: () => signal,
|
|
741
|
+
focus
|
|
742
|
+
})
|
|
743
|
+
});
|
|
744
|
+
var normalize = (input) => typeof input === "string" ? { title: input } : input;
|
|
745
|
+
function alert(input) {
|
|
746
|
+
var _ref2, _options$okLabel, _runtime$defaults$lab;
|
|
747
|
+
const options = normalize(input);
|
|
748
|
+
const buttons = [{
|
|
749
|
+
label: (_ref2 = (_options$okLabel = options.okLabel) !== null && _options$okLabel !== void 0 ? _options$okLabel : (_runtime$defaults$lab = runtime().defaults.labels) === null || _runtime$defaults$lab === void 0 ? void 0 : _runtime$defaults$lab.ok) !== null && _ref2 !== void 0 ? _ref2 : "OK",
|
|
750
|
+
variant: "primary"
|
|
751
|
+
}];
|
|
752
|
+
return open({
|
|
753
|
+
...options,
|
|
754
|
+
kind: "alert",
|
|
755
|
+
buttons
|
|
756
|
+
}, void 0, choice(0)).result;
|
|
757
|
+
}
|
|
758
|
+
function confirm(input) {
|
|
759
|
+
var _ref3, _options$cancelLabel, _runtime$defaults$lab2, _ref4, _options$confirmLabel, _runtime$defaults$lab3;
|
|
760
|
+
const options = normalize(input);
|
|
761
|
+
const danger = options.tone === "danger";
|
|
762
|
+
const { onConfirm } = options;
|
|
763
|
+
const buttons = [{
|
|
764
|
+
label: (_ref3 = (_options$cancelLabel = options.cancelLabel) !== null && _options$cancelLabel !== void 0 ? _options$cancelLabel : (_runtime$defaults$lab2 = runtime().defaults.labels) === null || _runtime$defaults$lab2 === void 0 ? void 0 : _runtime$defaults$lab2.cancel) !== null && _ref3 !== void 0 ? _ref3 : "Cancel",
|
|
765
|
+
value: false
|
|
766
|
+
}, {
|
|
767
|
+
label: (_ref4 = (_options$confirmLabel = options.confirmLabel) !== null && _options$confirmLabel !== void 0 ? _options$confirmLabel : (_runtime$defaults$lab3 = runtime().defaults.labels) === null || _runtime$defaults$lab3 === void 0 ? void 0 : _runtime$defaults$lab3.confirm) !== null && _ref4 !== void 0 ? _ref4 : "Confirm",
|
|
768
|
+
value: true,
|
|
769
|
+
variant: danger ? "danger" : "primary",
|
|
770
|
+
onClick: onConfirm && (({ signal }) => onConfirm({ signal }))
|
|
771
|
+
}];
|
|
772
|
+
return open({
|
|
773
|
+
...options,
|
|
774
|
+
kind: "confirm",
|
|
775
|
+
tone: options.tone,
|
|
776
|
+
buttons
|
|
777
|
+
}, false, choice(danger ? 0 : 1)).result;
|
|
778
|
+
}
|
|
779
|
+
function PromptBody({ props, options }) {
|
|
780
|
+
var _options$inputProps, _ref5, _options$cancelLabel2, _runtime$defaults$lab4, _ref6, _options$submitLabel, _runtime$defaults$lab5;
|
|
781
|
+
const [error, setError] = useState(null);
|
|
782
|
+
const [busy, setBusy] = useState(false);
|
|
783
|
+
const slot = useSlot();
|
|
784
|
+
const inputId = useId();
|
|
785
|
+
const errorId = useId();
|
|
786
|
+
const onSubmit = async (event) => {
|
|
787
|
+
event.preventDefault();
|
|
788
|
+
const value = event.currentTarget.elements.namedItem("value").value;
|
|
789
|
+
let message;
|
|
790
|
+
try {
|
|
791
|
+
var _options$validate, _options$validate2;
|
|
792
|
+
const check = (_options$validate = (_options$validate2 = options.validate) === null || _options$validate2 === void 0 ? void 0 : _options$validate2.call(options, value)) !== null && _options$validate !== void 0 ? _options$validate : null;
|
|
793
|
+
if (check instanceof Promise) setBusy(true);
|
|
794
|
+
message = await check;
|
|
795
|
+
} catch (error) {
|
|
796
|
+
console.error(error);
|
|
797
|
+
return;
|
|
798
|
+
} finally {
|
|
799
|
+
setBusy(false);
|
|
800
|
+
}
|
|
801
|
+
if (message) setError(message);
|
|
802
|
+
else props.close(value);
|
|
803
|
+
};
|
|
804
|
+
return /* @__PURE__ */ jsx("form", {
|
|
805
|
+
method: "dialog",
|
|
806
|
+
noValidate: true,
|
|
807
|
+
onSubmit: (e) => {
|
|
808
|
+
onSubmit(e);
|
|
809
|
+
},
|
|
810
|
+
children: /* @__PURE__ */ jsxs(Body, {
|
|
811
|
+
props,
|
|
812
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
813
|
+
...slot("field"),
|
|
814
|
+
children: [
|
|
815
|
+
options.label && /* @__PURE__ */ jsx("label", {
|
|
816
|
+
htmlFor: inputId,
|
|
817
|
+
...slot("label"),
|
|
818
|
+
children: options.label
|
|
819
|
+
}),
|
|
820
|
+
/* @__PURE__ */ jsx("input", {
|
|
821
|
+
id: inputId,
|
|
822
|
+
name: "value",
|
|
823
|
+
"data-autofocus": "",
|
|
824
|
+
defaultValue: options.defaultValue,
|
|
825
|
+
...options.inputProps,
|
|
826
|
+
...slot("input", (_options$inputProps = options.inputProps) === null || _options$inputProps === void 0 ? void 0 : _options$inputProps.className),
|
|
827
|
+
"aria-invalid": error ? true : void 0,
|
|
828
|
+
"aria-describedby": error ? errorId : void 0
|
|
829
|
+
}),
|
|
830
|
+
error && /* @__PURE__ */ jsx("p", {
|
|
831
|
+
id: errorId,
|
|
832
|
+
...slot("error"),
|
|
833
|
+
children: error
|
|
834
|
+
})
|
|
835
|
+
]
|
|
836
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
837
|
+
...slot("actions"),
|
|
838
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
839
|
+
type: "button",
|
|
840
|
+
...slot("button"),
|
|
841
|
+
"data-variant": "secondary",
|
|
842
|
+
onClick: props.dismiss,
|
|
843
|
+
children: (_ref5 = (_options$cancelLabel2 = options.cancelLabel) !== null && _options$cancelLabel2 !== void 0 ? _options$cancelLabel2 : (_runtime$defaults$lab4 = runtime().defaults.labels) === null || _runtime$defaults$lab4 === void 0 ? void 0 : _runtime$defaults$lab4.cancel) !== null && _ref5 !== void 0 ? _ref5 : "Cancel"
|
|
844
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
845
|
+
type: "submit",
|
|
846
|
+
...slot("button"),
|
|
847
|
+
"data-variant": "primary",
|
|
848
|
+
"aria-busy": busy || void 0,
|
|
849
|
+
disabled: busy,
|
|
850
|
+
children: (_ref6 = (_options$submitLabel = options.submitLabel) !== null && _options$submitLabel !== void 0 ? _options$submitLabel : (_runtime$defaults$lab5 = runtime().defaults.labels) === null || _runtime$defaults$lab5 === void 0 ? void 0 : _runtime$defaults$lab5.submit) !== null && _ref6 !== void 0 ? _ref6 : "OK"
|
|
851
|
+
})]
|
|
852
|
+
})]
|
|
853
|
+
})
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
function prompt(input) {
|
|
857
|
+
const options = normalize(input);
|
|
858
|
+
return open({
|
|
859
|
+
...options,
|
|
860
|
+
kind: "prompt"
|
|
861
|
+
}, null, (props) => /* @__PURE__ */ jsx(PromptBody, {
|
|
862
|
+
props,
|
|
863
|
+
options
|
|
864
|
+
})).result;
|
|
865
|
+
}
|
|
866
|
+
function openDialog(options) {
|
|
867
|
+
const { render: Render, ...rest } = options;
|
|
868
|
+
const { id, result } = open({
|
|
869
|
+
...rest,
|
|
870
|
+
kind: "custom"
|
|
871
|
+
}, void 0, (props) => /* @__PURE__ */ jsx(Render, { ...props }));
|
|
872
|
+
return {
|
|
873
|
+
id,
|
|
874
|
+
result,
|
|
875
|
+
close: (value) => {
|
|
876
|
+
var _store$get$entries$fi;
|
|
877
|
+
(_store$get$entries$fi = store.get().entries.find((e) => e.id === id)) === null || _store$get$entries$fi === void 0 || _store$get$entries$fi.settle(value);
|
|
878
|
+
closeDialog$1(id);
|
|
879
|
+
},
|
|
880
|
+
dismiss: () => closeDialog$1(id),
|
|
881
|
+
update: (patch) => store.set(update(store.get(), id, patch))
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
function closeDialog(id) {
|
|
885
|
+
closeDialog$1(id);
|
|
886
|
+
}
|
|
887
|
+
function closeAll() {
|
|
888
|
+
for (const entry of [...store.get().entries].reverse()) closeDialog$1(entry.id);
|
|
889
|
+
}
|
|
890
|
+
// src/DialogHost.tsx
|
|
891
|
+
function DialogHost({ dismissKey }) {
|
|
892
|
+
const [host] = useState(() => Symbol("DialogHost"));
|
|
893
|
+
useLayoutEffect(() => {
|
|
894
|
+
installPageEffects();
|
|
895
|
+
store.set(addHost(store.get(), host));
|
|
896
|
+
return () => {
|
|
897
|
+
store.set(removeHost(store.get(), host));
|
|
898
|
+
if (store.get().hosts.length === 0 && store.get().entries.some((e) => e.target === null)) ensureContainer(null);
|
|
899
|
+
};
|
|
900
|
+
}, []);
|
|
901
|
+
const previous = useRef(dismissKey);
|
|
902
|
+
useEffect(() => {
|
|
903
|
+
if (Object.is(previous.current, dismissKey)) return;
|
|
904
|
+
previous.current = dismissKey;
|
|
905
|
+
closeAll();
|
|
906
|
+
}, [dismissKey]);
|
|
907
|
+
return /* @__PURE__ */ jsx(DialogStack, {
|
|
908
|
+
target: null,
|
|
909
|
+
host
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
var dialogs = {
|
|
913
|
+
alert,
|
|
914
|
+
confirm,
|
|
915
|
+
prompt,
|
|
916
|
+
openDialog,
|
|
917
|
+
closeAll
|
|
918
|
+
};
|
|
919
|
+
/** The same functions as the module exports, so a component reads as one that opens dialogs. */
|
|
920
|
+
function useDialog() {
|
|
921
|
+
return dialogs;
|
|
922
|
+
}
|
|
923
|
+
// src/Dialog.tsx
|
|
924
|
+
var subscribeNever = () => () => {};
|
|
925
|
+
function useHydrated() {
|
|
926
|
+
return useSyncExternalStore(subscribeNever, () => true, () => false);
|
|
927
|
+
}
|
|
928
|
+
function useScrollLock(open) {
|
|
929
|
+
useLayoutEffect(() => {
|
|
930
|
+
if (!open) return;
|
|
931
|
+
runtime().inPlace++;
|
|
932
|
+
syncPage();
|
|
933
|
+
return () => {
|
|
934
|
+
runtime().inPlace--;
|
|
935
|
+
syncPage();
|
|
936
|
+
};
|
|
937
|
+
}, [open]);
|
|
938
|
+
}
|
|
939
|
+
/** A dialog rendered where it is placed, so its children read the latest state of the component (#63 #66). */
|
|
940
|
+
function Dialog(props) {
|
|
941
|
+
var _ref, _props$modal, _ref2, _props$closeOnClickOu, _ref3, _props$closeOnEscape;
|
|
942
|
+
const id = useId();
|
|
943
|
+
const hydrated = useHydrated();
|
|
944
|
+
useScrollLock(props.open && hydrated);
|
|
945
|
+
if (!hydrated || !props.open) return null;
|
|
946
|
+
return /* @__PURE__ */ jsx(DialogFrame, {
|
|
947
|
+
id,
|
|
948
|
+
modal: (_ref = (_props$modal = props.modal) !== null && _props$modal !== void 0 ? _props$modal : runtime().defaults.modal) !== null && _ref !== void 0 ? _ref : true,
|
|
949
|
+
state: "open",
|
|
950
|
+
kind: "custom",
|
|
951
|
+
layers: styleLayers(props, false),
|
|
952
|
+
label: props["aria-label"],
|
|
953
|
+
labelledBy: props["aria-labelledby"],
|
|
954
|
+
describedBy: props["aria-describedby"],
|
|
955
|
+
role: "dialog",
|
|
956
|
+
closeOnClickOutside: (_ref2 = (_props$closeOnClickOu = props.closeOnClickOutside) !== null && _props$closeOnClickOu !== void 0 ? _props$closeOnClickOu : runtime().defaults.closeOnClickOutside) !== null && _ref2 !== void 0 ? _ref2 : true,
|
|
957
|
+
closeOnEscape: (_ref3 = (_props$closeOnEscape = props.closeOnEscape) !== null && _props$closeOnEscape !== void 0 ? _props$closeOnEscape : runtime().defaults.closeOnEscape) !== null && _ref3 !== void 0 ? _ref3 : true,
|
|
958
|
+
legacy: false,
|
|
959
|
+
onKeyDown: props.onKeyDown,
|
|
960
|
+
onDismiss: () => props.onOpenChange(false),
|
|
961
|
+
children: props.children
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
/** confirm, alert or prompt rendered in place and driven by `open`. */
|
|
965
|
+
function ConfirmDialog(props) {
|
|
966
|
+
var _ref7, _props$modal2, _ref8, _props$closeOnClickOu2, _ref9, _props$closeOnEscape2;
|
|
967
|
+
const id = useId();
|
|
968
|
+
const titleId = useId();
|
|
969
|
+
const descriptionId = useId();
|
|
970
|
+
const hydrated = useHydrated();
|
|
971
|
+
const controller = useRef(new AbortController());
|
|
972
|
+
useLayoutEffect(() => {
|
|
973
|
+
if (!props.open) return;
|
|
974
|
+
const current = new AbortController();
|
|
975
|
+
controller.current = current;
|
|
976
|
+
return () => current.abort();
|
|
977
|
+
}, [props.open]);
|
|
978
|
+
useScrollLock(props.open && hydrated);
|
|
979
|
+
if (!hydrated || !props.open) return null;
|
|
980
|
+
const hide = () => props.onOpenChange(false);
|
|
981
|
+
const base = {
|
|
982
|
+
id,
|
|
983
|
+
titleId,
|
|
984
|
+
descriptionId,
|
|
985
|
+
title: props.title,
|
|
986
|
+
message: props.message,
|
|
987
|
+
dismiss: hide,
|
|
988
|
+
onClose: hide
|
|
989
|
+
};
|
|
990
|
+
let body;
|
|
991
|
+
if (props.kind === "prompt") {
|
|
992
|
+
const { onSubmit } = props;
|
|
993
|
+
const close = async (value) => {
|
|
994
|
+
try {
|
|
995
|
+
await onSubmit(value);
|
|
996
|
+
hide();
|
|
997
|
+
} catch (error) {
|
|
998
|
+
console.error(error);
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
1001
|
+
body = /* @__PURE__ */ jsx(PromptBody, {
|
|
1002
|
+
props: {
|
|
1003
|
+
...base,
|
|
1004
|
+
buttons: [],
|
|
1005
|
+
close: (v) => {
|
|
1006
|
+
close(v);
|
|
1007
|
+
}
|
|
1008
|
+
},
|
|
1009
|
+
options: props
|
|
1010
|
+
});
|
|
1011
|
+
} else {
|
|
1012
|
+
var _ref4, _props$okLabel, _runtime$defaults$lab, _ref5, _props$cancelLabel, _runtime$defaults$lab2, _ref6, _props$confirmLabel, _runtime$defaults$lab3;
|
|
1013
|
+
const danger = props.kind === "confirm" && props.tone === "danger";
|
|
1014
|
+
const buttons = props.kind === "alert" ? [{
|
|
1015
|
+
label: (_ref4 = (_props$okLabel = props.okLabel) !== null && _props$okLabel !== void 0 ? _props$okLabel : (_runtime$defaults$lab = runtime().defaults.labels) === null || _runtime$defaults$lab === void 0 ? void 0 : _runtime$defaults$lab.ok) !== null && _ref4 !== void 0 ? _ref4 : "OK",
|
|
1016
|
+
variant: "primary"
|
|
1017
|
+
}] : [{
|
|
1018
|
+
label: (_ref5 = (_props$cancelLabel = props.cancelLabel) !== null && _props$cancelLabel !== void 0 ? _props$cancelLabel : (_runtime$defaults$lab2 = runtime().defaults.labels) === null || _runtime$defaults$lab2 === void 0 ? void 0 : _runtime$defaults$lab2.cancel) !== null && _ref5 !== void 0 ? _ref5 : "Cancel",
|
|
1019
|
+
value: false
|
|
1020
|
+
}, {
|
|
1021
|
+
label: (_ref6 = (_props$confirmLabel = props.confirmLabel) !== null && _props$confirmLabel !== void 0 ? _props$confirmLabel : (_runtime$defaults$lab3 = runtime().defaults.labels) === null || _runtime$defaults$lab3 === void 0 ? void 0 : _runtime$defaults$lab3.confirm) !== null && _ref6 !== void 0 ? _ref6 : "Confirm",
|
|
1022
|
+
value: true,
|
|
1023
|
+
variant: danger ? "danger" : "primary",
|
|
1024
|
+
onClick: props.onConfirm && ((ctx) => props.onConfirm({ signal: ctx.signal }))
|
|
1025
|
+
}];
|
|
1026
|
+
const renderProps = {
|
|
1027
|
+
...base,
|
|
1028
|
+
buttons,
|
|
1029
|
+
close: hide
|
|
1030
|
+
};
|
|
1031
|
+
body = /* @__PURE__ */ jsx(Body, {
|
|
1032
|
+
props: renderProps,
|
|
1033
|
+
children: /* @__PURE__ */ jsx(Buttons, {
|
|
1034
|
+
props: renderProps,
|
|
1035
|
+
signal: () => controller.current.signal,
|
|
1036
|
+
focus: props.kind === "alert" || danger ? 0 : 1
|
|
1037
|
+
})
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
1040
|
+
return /* @__PURE__ */ jsx(DialogFrame, {
|
|
1041
|
+
id,
|
|
1042
|
+
modal: (_ref7 = (_props$modal2 = props.modal) !== null && _props$modal2 !== void 0 ? _props$modal2 : runtime().defaults.modal) !== null && _ref7 !== void 0 ? _ref7 : true,
|
|
1043
|
+
state: "open",
|
|
1044
|
+
kind: props.kind,
|
|
1045
|
+
layers: styleLayers(props, false),
|
|
1046
|
+
tone: props.kind === "confirm" ? props.tone : void 0,
|
|
1047
|
+
labelledBy: props.title ? titleId : void 0,
|
|
1048
|
+
describedBy: props.message ? descriptionId : void 0,
|
|
1049
|
+
role: props.kind === "prompt" ? "dialog" : "alertdialog",
|
|
1050
|
+
closeOnClickOutside: (_ref8 = (_props$closeOnClickOu2 = props.closeOnClickOutside) !== null && _props$closeOnClickOu2 !== void 0 ? _props$closeOnClickOu2 : runtime().defaults.closeOnClickOutside) !== null && _ref8 !== void 0 ? _ref8 : true,
|
|
1051
|
+
closeOnEscape: (_ref9 = (_props$closeOnEscape2 = props.closeOnEscape) !== null && _props$closeOnEscape2 !== void 0 ? _props$closeOnEscape2 : runtime().defaults.closeOnEscape) !== null && _ref9 !== void 0 ? _ref9 : true,
|
|
1052
|
+
legacy: false,
|
|
1053
|
+
onKeyDown: props.onKeyDown,
|
|
1054
|
+
onDismiss: hide,
|
|
1055
|
+
children: body
|
|
1056
|
+
});
|
|
1057
|
+
}
|
|
1058
|
+
export { ConfirmDialog, Dialog, DialogHost, alert, closeAll, closeDialog, configureDialogs, confirm, confirmAlert, ReactConfirmAlert as default, openDialog, prompt, useDialog };
|