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