@votodigital-onpeui/react 0.1.14 → 0.1.16

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/dist/hooks.js ADDED
@@ -0,0 +1,774 @@
1
+ 'use strict';
2
+
3
+ var zustand = require('zustand');
4
+ var react = require('react');
5
+ var io = require('socket.io-client');
6
+ var reactDom = require('react-dom');
7
+ var jsxRuntime = require('react/jsx-runtime');
8
+
9
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
+
11
+ var io__default = /*#__PURE__*/_interopDefault(io);
12
+
13
+ // src/hooks/onpeId/store/modalIframePreload.ts
14
+ var INITIAL_STATE = {
15
+ modalUrl: "",
16
+ isOpenModal: false,
17
+ isIframeReady: false
18
+ };
19
+ var useModalIframePreload = zustand.create()(
20
+ (set) => ({
21
+ ...INITIAL_STATE,
22
+ setModalUrl: (url) => set({ modalUrl: url }),
23
+ openModal: () => set({ isOpenModal: true }),
24
+ closeModal: () => {
25
+ set({
26
+ isOpenModal: false,
27
+ isIframeReady: false,
28
+ modalUrl: ""
29
+ });
30
+ },
31
+ setIsIframeReady: (ready) => set({ isIframeReady: ready }),
32
+ reset: () => set(INITIAL_STATE)
33
+ })
34
+ );
35
+ var useIframeCommunication = ({
36
+ onExpiredApp,
37
+ onOpenApp,
38
+ onLaunchApp,
39
+ enabled = true,
40
+ allowedOrigin
41
+ } = {}) => {
42
+ const handlersRef = react.useRef({
43
+ onExpiredApp,
44
+ onOpenApp,
45
+ onLaunchApp
46
+ });
47
+ react.useEffect(() => {
48
+ handlersRef.current = {
49
+ onExpiredApp,
50
+ onOpenApp,
51
+ onLaunchApp
52
+ };
53
+ }, [onExpiredApp, onOpenApp, onLaunchApp]);
54
+ react.useEffect(() => {
55
+ if (!enabled) return;
56
+ const handler = (event) => {
57
+ if (allowedOrigin && event.origin !== allowedOrigin) {
58
+ console.warn(
59
+ `Mensaje rechazado de origen no permitido: ${event.origin}`
60
+ );
61
+ return;
62
+ }
63
+ const data = event.data;
64
+ if (!data?.status) return;
65
+ const handlers = handlersRef.current;
66
+ switch (data.status) {
67
+ case "EXPIRED_APP":
68
+ handlers.onExpiredApp?.(data);
69
+ break;
70
+ case "OPEN_APP":
71
+ handlers.onOpenApp?.(data);
72
+ break;
73
+ case "LAUNCH_APP":
74
+ handlers.onLaunchApp?.(data);
75
+ break;
76
+ }
77
+ };
78
+ globalThis.addEventListener("message", handler);
79
+ return () => globalThis.removeEventListener("message", handler);
80
+ }, [enabled, allowedOrigin]);
81
+ };
82
+ var useSendMessageToIframe = ({
83
+ iframeRef,
84
+ modalUrl
85
+ }) => {
86
+ const sendMessageToIframe = react.useCallback(
87
+ (message) => {
88
+ if (!iframeRef.current || !modalUrl) return;
89
+ try {
90
+ const iframeOrigin = new URL(modalUrl).origin;
91
+ iframeRef.current.contentWindow?.postMessage(message, iframeOrigin);
92
+ } catch (error) {
93
+ console.error("Error al enviar mensaje al iframe:", error);
94
+ }
95
+ },
96
+ [iframeRef, modalUrl]
97
+ );
98
+ return { sendMessageToIframe };
99
+ };
100
+ var useIframePreload = () => {
101
+ const {
102
+ modalUrl,
103
+ setModalUrl,
104
+ isOpenModal,
105
+ closeModal,
106
+ setIsIframeReady,
107
+ openModal,
108
+ reset
109
+ } = useModalIframePreload();
110
+ const preloadIframeRef = react.useRef(null);
111
+ const iframeRef = react.useRef(null);
112
+ const isPreloading = !!modalUrl && !isOpenModal;
113
+ const handlePreloadIframeReady = () => {
114
+ setIsIframeReady(true);
115
+ openModal();
116
+ };
117
+ const handleModalIframeReady = () => {
118
+ if (!useModalIframePreload.getState().isIframeReady) {
119
+ setIsIframeReady(true);
120
+ }
121
+ };
122
+ return {
123
+ modalUrl,
124
+ setModalUrl,
125
+ isOpenModal,
126
+ closeModal,
127
+ isPreloading,
128
+ preloadIframeRef,
129
+ iframeRef,
130
+ handlePreloadIframeReady,
131
+ handleModalIframeReady,
132
+ reset
133
+ };
134
+ };
135
+ var RECONNECTION_ATTEMPTS = 14;
136
+ var useSocketConnection = ({
137
+ socketUrl,
138
+ secure = false,
139
+ isOpenLaunchApp,
140
+ dataOpenLaunchApp,
141
+ closeLaunchApp,
142
+ closeModal,
143
+ onComplete,
144
+ onDisconnectClient,
145
+ onDisconnect,
146
+ onMaxReconnects,
147
+ onConnectionChange,
148
+ enabled = true
149
+ }) => {
150
+ const socketRef = react.useRef(null);
151
+ const [attempts, setAttempts] = react.useState(0);
152
+ const dataOpenLaunchAppRef = react.useRef(dataOpenLaunchApp);
153
+ const closeLaunchAppRef = react.useRef(closeLaunchApp);
154
+ const closeModalRef = react.useRef(closeModal);
155
+ const enabledRef = react.useRef(enabled);
156
+ const onConnectionChangeRef = react.useRef(onConnectionChange);
157
+ const onCompleteRef = react.useRef(onComplete);
158
+ const onDisconnectClientRef = react.useRef(onDisconnectClient);
159
+ const onDisconnectRef = react.useRef(onDisconnect);
160
+ const onMaxReconnectsRef = react.useRef(onMaxReconnects);
161
+ react.useEffect(() => {
162
+ dataOpenLaunchAppRef.current = dataOpenLaunchApp;
163
+ closeLaunchAppRef.current = closeLaunchApp;
164
+ closeModalRef.current = closeModal;
165
+ enabledRef.current = enabled;
166
+ onConnectionChangeRef.current = onConnectionChange;
167
+ onCompleteRef.current = onComplete;
168
+ onDisconnectClientRef.current = onDisconnectClient;
169
+ onDisconnectRef.current = onDisconnect;
170
+ onMaxReconnectsRef.current = onMaxReconnects;
171
+ }, [
172
+ dataOpenLaunchApp,
173
+ closeLaunchApp,
174
+ closeModal,
175
+ enabled,
176
+ onConnectionChange,
177
+ onComplete,
178
+ onDisconnectClient,
179
+ onDisconnect,
180
+ onMaxReconnects
181
+ ]);
182
+ react.useEffect(() => {
183
+ if (attempts === RECONNECTION_ATTEMPTS) {
184
+ if (socketRef.current) {
185
+ socketRef.current.disconnect();
186
+ }
187
+ closeLaunchAppRef.current();
188
+ onConnectionChangeRef.current?.(false);
189
+ closeModalRef.current();
190
+ onMaxReconnectsRef.current?.();
191
+ setTimeout(() => setAttempts(0), 0);
192
+ }
193
+ }, [attempts]);
194
+ react.useEffect(() => {
195
+ if (!enabled) {
196
+ if (socketRef.current) {
197
+ socketRef.current.removeAllListeners();
198
+ socketRef.current.disconnect();
199
+ socketRef.current = null;
200
+ onConnectionChangeRef.current?.(false);
201
+ setTimeout(() => setAttempts(0), 0);
202
+ }
203
+ return;
204
+ }
205
+ if (!isOpenLaunchApp) {
206
+ if (socketRef.current) {
207
+ socketRef.current.removeAllListeners();
208
+ socketRef.current.disconnect();
209
+ socketRef.current = null;
210
+ }
211
+ return;
212
+ }
213
+ if (!socketRef.current) {
214
+ socketRef.current = io__default.default(socketUrl, {
215
+ transports: ["websocket"],
216
+ secure,
217
+ upgrade: false,
218
+ forceNew: true,
219
+ reconnection: true,
220
+ reconnectionAttempts: RECONNECTION_ATTEMPTS,
221
+ reconnectionDelay: 2e3,
222
+ reconnectionDelayMax: 1e3
223
+ });
224
+ const socket = socketRef.current;
225
+ const handleConnect = () => {
226
+ if (!enabledRef.current) return;
227
+ onConnectionChangeRef.current?.(true);
228
+ if (socketRef.current) {
229
+ const jsonObject = dataOpenLaunchAppRef.current;
230
+ if (jsonObject && Object.keys(jsonObject).length > 0) {
231
+ socketRef.current.emit("setToken", jsonObject);
232
+ } else {
233
+ setTimeout(() => {
234
+ const delayedData = dataOpenLaunchAppRef.current;
235
+ if (delayedData && Object.keys(delayedData).length > 0 && socketRef.current && enabledRef.current) {
236
+ socketRef.current.emit("setToken", delayedData);
237
+ }
238
+ }, 100);
239
+ }
240
+ } else {
241
+ console.error("Socket no est\xE1 disponible para enviar token");
242
+ }
243
+ };
244
+ const handleDisconnect = () => {
245
+ if (!enabledRef.current) return;
246
+ onConnectionChangeRef.current?.(false);
247
+ closeLaunchAppRef.current();
248
+ closeModalRef.current();
249
+ onDisconnectRef.current?.();
250
+ };
251
+ const handleReconnectAttempt = (attemptNumber) => {
252
+ setAttempts(attemptNumber);
253
+ };
254
+ const handleConnectError = (error) => {
255
+ console.error("Error de conexi\xF3n socket:", error);
256
+ };
257
+ const handleComplete = (data) => {
258
+ if (!enabledRef.current) return;
259
+ closeLaunchAppRef.current();
260
+ onConnectionChangeRef.current?.(false);
261
+ closeModalRef.current();
262
+ onCompleteRef.current?.(data);
263
+ };
264
+ const handleServerTimeout = () => {
265
+ if (!enabledRef.current) return;
266
+ closeLaunchAppRef.current();
267
+ onConnectionChangeRef.current?.(false);
268
+ closeModalRef.current();
269
+ };
270
+ const handleDisconnectClient = () => {
271
+ if (!enabledRef.current) return;
272
+ closeLaunchAppRef.current();
273
+ onConnectionChangeRef.current?.(false);
274
+ closeModalRef.current();
275
+ onDisconnectClientRef.current?.();
276
+ };
277
+ socket.on("connect", handleConnect);
278
+ socket.on("disconnect", handleDisconnect);
279
+ socket.on("connect_error", handleConnectError);
280
+ socket.io.on("reconnect_attempt", handleReconnectAttempt);
281
+ socket.on("onProcessComplete", handleComplete);
282
+ socket.on("onValidationComplete", handleComplete);
283
+ socket.on("onProcessCompleteFirma", handleComplete);
284
+ socket.on("onServerTimeout", handleServerTimeout);
285
+ socket.on("onDisconnectClient", handleDisconnectClient);
286
+ }
287
+ return () => {
288
+ if (socketRef.current && (!enabled || !isOpenLaunchApp)) {
289
+ socketRef.current.removeAllListeners();
290
+ socketRef.current.disconnect();
291
+ socketRef.current = null;
292
+ }
293
+ };
294
+ }, [enabled, isOpenLaunchApp, socketUrl, secure]);
295
+ react.useEffect(() => {
296
+ return () => {
297
+ if (socketRef.current) {
298
+ socketRef.current.removeAllListeners();
299
+ socketRef.current.disconnect();
300
+ socketRef.current = null;
301
+ }
302
+ };
303
+ }, []);
304
+ return { attempts };
305
+ };
306
+ var useOnpeIdAuth = ({
307
+ socketUrl,
308
+ secure = false,
309
+ navigate,
310
+ onConnectionChange,
311
+ onComplete,
312
+ onDisconnectClient,
313
+ onExpiredApp,
314
+ onDisconnect,
315
+ onMaxReconnects
316
+ }) => {
317
+ const [isOpenLaunchApp, setIsOpenLaunchApp] = react.useState(false);
318
+ const [dataOpenLaunchApp, setDataOpenLaunchApp] = react.useState({});
319
+ const openLaunchApp = react.useCallback(() => setIsOpenLaunchApp(true), []);
320
+ const closeLaunchApp = react.useCallback(() => setIsOpenLaunchApp(false), []);
321
+ const {
322
+ modalUrl,
323
+ setModalUrl,
324
+ isOpenModal,
325
+ isPreloading,
326
+ closeModal,
327
+ preloadIframeRef,
328
+ iframeRef,
329
+ handlePreloadIframeReady,
330
+ handleModalIframeReady,
331
+ reset: resetIframeState
332
+ } = useIframePreload();
333
+ const handleClose = react.useCallback(() => {
334
+ onConnectionChange?.(false);
335
+ closeModal();
336
+ closeLaunchApp();
337
+ resetIframeState();
338
+ }, [closeModal, closeLaunchApp, resetIframeState, onConnectionChange]);
339
+ const { attempts } = useSocketConnection({
340
+ socketUrl,
341
+ secure,
342
+ isOpenLaunchApp,
343
+ dataOpenLaunchApp,
344
+ closeLaunchApp,
345
+ closeModal: handleClose,
346
+ onComplete,
347
+ onDisconnectClient,
348
+ onDisconnect,
349
+ onMaxReconnects,
350
+ onConnectionChange,
351
+ enabled: isOpenModal && !!modalUrl
352
+ });
353
+ useIframeCommunication({
354
+ onExpiredApp: () => {
355
+ handleClose();
356
+ onExpiredApp?.();
357
+ },
358
+ onOpenApp: (data) => {
359
+ navigate(data.url);
360
+ },
361
+ onLaunchApp: (data) => {
362
+ openLaunchApp();
363
+ setDataOpenLaunchApp(data.data ?? {});
364
+ }
365
+ });
366
+ return {
367
+ modalUrl,
368
+ setModalUrl,
369
+ isOpenModal,
370
+ isPreloading,
371
+ isOpenLaunchApp,
372
+ handleClose,
373
+ iframeRef,
374
+ preloadIframeRef,
375
+ handlePreloadIframeReady,
376
+ handleModalIframeReady,
377
+ attempts,
378
+ reset: resetIframeState
379
+ };
380
+ };
381
+ var Portal = ({ children, container }) => {
382
+ const [mounted, setMounted] = react.useState(false);
383
+ react.useEffect(() => {
384
+ setMounted(true);
385
+ return () => setMounted(false);
386
+ }, []);
387
+ if (!mounted) return null;
388
+ let portalElement = container || document.querySelector("#portal");
389
+ if (!portalElement) {
390
+ portalElement = document.body;
391
+ }
392
+ return reactDom.createPortal(children, portalElement);
393
+ };
394
+ var IconCloseRadius = (props) => /* @__PURE__ */ jsxRuntime.jsx("svg", { xmlns: "http://www.w3.org/2000/svg", width: 28, height: 28, viewBox: "0 0 28 28", fill: "none", ...props, children: /* @__PURE__ */ jsxRuntime.jsx(
395
+ "path",
396
+ {
397
+ d: "M14 0C6.2 0 0 6.2 0 14C0 21.8 6.2 28 14 28C21.8 28 28 21.8 28 14C28 6.2 21.8 0 14 0ZM19.4 21L14 15.6L8.6 21L7 19.4L12.4 14L7 8.6L8.6 7L14 12.4L19.4 7L21 8.6L15.6 14L21 19.4L19.4 21Z",
398
+ fill: "currentColor"
399
+ }
400
+ ) });
401
+ var Modal = ({
402
+ isOpen,
403
+ onClose,
404
+ children,
405
+ whitoutBackground = false,
406
+ closeButton = false,
407
+ closeDisabled = false,
408
+ escapeToClose = true,
409
+ disableFocus = false,
410
+ disableFocusRestore = false,
411
+ existTabIndex = true,
412
+ zIndexLevel = 100,
413
+ onCloseComplete,
414
+ overlayColor: _overlayColor = "blue",
415
+ ...props
416
+ }) => {
417
+ const modalRef = react.useRef(null);
418
+ const contentRef = react.useRef(null);
419
+ const previousActiveElement = react.useRef(null);
420
+ const [mounted, setMounted] = react.useState(false);
421
+ const [visible, setVisible] = react.useState(false);
422
+ const [cachedChildren, setCachedChildren] = react.useState(children);
423
+ react.useEffect(() => {
424
+ if (isOpen) {
425
+ setCachedChildren(children);
426
+ }
427
+ }, [isOpen, children]);
428
+ react.useEffect(() => {
429
+ if (isOpen) {
430
+ setMounted(true);
431
+ const raf = requestAnimationFrame(() => {
432
+ requestAnimationFrame(() => setVisible(true));
433
+ });
434
+ return () => cancelAnimationFrame(raf);
435
+ } else {
436
+ setVisible(false);
437
+ const timer = setTimeout(() => {
438
+ setMounted(false);
439
+ onCloseComplete?.();
440
+ }, 200);
441
+ return () => clearTimeout(timer);
442
+ }
443
+ }, [isOpen, onCloseComplete]);
444
+ react.useEffect(() => {
445
+ if (isOpen) {
446
+ document.body.style.overflow = "hidden";
447
+ } else {
448
+ document.body.style.overflow = "";
449
+ }
450
+ return () => {
451
+ document.body.style.overflow = "";
452
+ };
453
+ }, [isOpen]);
454
+ react.useEffect(() => {
455
+ if (!isOpen) return;
456
+ const resetScroll = () => {
457
+ const el = contentRef.current;
458
+ if (!el) return;
459
+ el.style.scrollBehavior = "auto";
460
+ el.scrollTop = 0;
461
+ requestAnimationFrame(() => {
462
+ el.scrollTop = 0;
463
+ setTimeout(() => {
464
+ el.style.scrollBehavior = "smooth";
465
+ }, 10);
466
+ });
467
+ };
468
+ resetScroll();
469
+ [10, 50, 100, 200].forEach((d) => setTimeout(resetScroll, d));
470
+ }, [isOpen]);
471
+ react.useEffect(() => {
472
+ let focusOutWrapper = null;
473
+ const pendingTasks = [];
474
+ const isElementVisible = (element) => {
475
+ const style = window.getComputedStyle(element);
476
+ return style.visibility !== "hidden" && style.display !== "none" && element.offsetParent !== null;
477
+ };
478
+ const getFocusableElements = (wrapper) => {
479
+ const selector = [
480
+ "a[href]",
481
+ "area[href]",
482
+ "button:not([disabled])",
483
+ 'input:not([disabled]):not([type="hidden"])',
484
+ "select:not([disabled])",
485
+ "textarea:not([disabled])",
486
+ "iframe",
487
+ "object",
488
+ "embed",
489
+ '[tabindex]:not([tabindex="-1"])',
490
+ '[contenteditable="true"]'
491
+ ].join(",");
492
+ let focusable = Array.from(
493
+ wrapper.querySelectorAll(selector)
494
+ ).filter((el) => isElementVisible(el) && el.tabIndex !== -1);
495
+ if (wrapper.tabIndex >= 0) {
496
+ focusable = [wrapper, ...focusable];
497
+ }
498
+ return focusable;
499
+ };
500
+ const handleFocusOut = (e) => {
501
+ if (!isOpen || disableFocus) return;
502
+ const wrapper = modalRef.current;
503
+ if (!wrapper) return;
504
+ const relatedTarget = e.relatedTarget;
505
+ if (relatedTarget && !wrapper.contains(relatedTarget)) {
506
+ setTimeout(() => {
507
+ const currentActive = document.activeElement;
508
+ if (!currentActive || !wrapper.contains(currentActive)) {
509
+ const focusable = getFocusableElements(wrapper);
510
+ if (focusable.length > 0) {
511
+ focusable[focusable.length - 1].focus();
512
+ } else {
513
+ wrapper.focus();
514
+ }
515
+ }
516
+ }, 0);
517
+ }
518
+ };
519
+ const handleKeyDown = (e) => {
520
+ if (e.key === "Escape" && escapeToClose && !closeDisabled) {
521
+ onClose();
522
+ return;
523
+ }
524
+ if (!isOpen || disableFocus) return;
525
+ const wrapper = modalRef.current;
526
+ if (!wrapper) return;
527
+ const focusable = getFocusableElements(wrapper);
528
+ const active = document.activeElement || null;
529
+ const arrowKeys = ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"];
530
+ if (arrowKeys.includes(e.key)) {
531
+ if (active && wrapper.contains(active)) {
532
+ const activeIndex2 = focusable.indexOf(active);
533
+ if ((e.key === "ArrowUp" || e.key === "ArrowLeft") && activeIndex2 === 0) {
534
+ e.preventDefault();
535
+ e.stopPropagation();
536
+ if (focusable.length > 1) focusable[focusable.length - 1].focus();
537
+ else active.focus();
538
+ return;
539
+ }
540
+ if ((e.key === "ArrowDown" || e.key === "ArrowRight") && activeIndex2 === focusable.length - 1) {
541
+ e.preventDefault();
542
+ e.stopPropagation();
543
+ if (focusable.length > 1) focusable[0].focus();
544
+ else active.focus();
545
+ return;
546
+ }
547
+ requestAnimationFrame(() => {
548
+ const currentActive = document.activeElement;
549
+ if (!currentActive || !wrapper.contains(currentActive)) {
550
+ if (activeIndex2 !== -1 && focusable[activeIndex2])
551
+ focusable[activeIndex2].focus();
552
+ else if (focusable.length > 0) focusable[0].focus();
553
+ else wrapper.focus();
554
+ }
555
+ });
556
+ } else {
557
+ e.preventDefault();
558
+ if (focusable.length > 0) {
559
+ if (e.key === "ArrowUp" || e.key === "ArrowLeft")
560
+ focusable[focusable.length - 1].focus();
561
+ else focusable[0].focus();
562
+ } else {
563
+ wrapper.focus();
564
+ }
565
+ }
566
+ return;
567
+ }
568
+ if (e.key !== "Tab") return;
569
+ if (focusable.length === 0) {
570
+ e.preventDefault();
571
+ wrapper.focus();
572
+ return;
573
+ }
574
+ const first = focusable[0];
575
+ const last = focusable[focusable.length - 1];
576
+ const isShift = e.shiftKey;
577
+ if (!active || !wrapper.contains(active)) {
578
+ e.preventDefault();
579
+ (isShift ? last : first).focus();
580
+ return;
581
+ }
582
+ const activeIndex = focusable.indexOf(active);
583
+ if (!isShift && (active === last || activeIndex === focusable.length - 1)) {
584
+ e.preventDefault();
585
+ first.focus();
586
+ return;
587
+ }
588
+ if (isShift) {
589
+ e.preventDefault();
590
+ if (active === first || active === wrapper || activeIndex === 0)
591
+ last.focus();
592
+ else if (activeIndex > 0) focusable[activeIndex - 1].focus();
593
+ else last.focus();
594
+ }
595
+ };
596
+ if (isOpen && !disableFocus) {
597
+ previousActiveElement.current = document.activeElement;
598
+ const labelledById = props["aria-labelledby"];
599
+ const focusInitial = (wrapper) => {
600
+ if (labelledById) {
601
+ const labelEl = document.getElementById(labelledById);
602
+ if (labelEl instanceof HTMLElement) {
603
+ if (!labelEl.hasAttribute("tabindex")) labelEl.setAttribute("tabindex", "-1");
604
+ labelEl.focus({ preventScroll: true });
605
+ return;
606
+ }
607
+ }
608
+ const focusable = getFocusableElements(wrapper);
609
+ const first = focusable[0];
610
+ if (first) first.focus({ preventScroll: true });
611
+ else wrapper.focus();
612
+ };
613
+ const bindFocusManagement = (attempt = 0) => {
614
+ const wrapper = modalRef.current;
615
+ if (!wrapper) {
616
+ if (attempt < 10) {
617
+ pendingTasks.push(
618
+ globalThis.setTimeout(() => bindFocusManagement(attempt + 1), 25)
619
+ );
620
+ }
621
+ return;
622
+ }
623
+ if (focusOutWrapper !== wrapper) {
624
+ focusOutWrapper?.removeEventListener("focusout", handleFocusOut);
625
+ wrapper.addEventListener("focusout", handleFocusOut);
626
+ focusOutWrapper = wrapper;
627
+ }
628
+ focusInitial(wrapper);
629
+ };
630
+ document.addEventListener("keydown", handleKeyDown);
631
+ pendingTasks.push(globalThis.setTimeout(() => bindFocusManagement(), 0));
632
+ } else if (isOpen && disableFocus) {
633
+ document.addEventListener("keydown", handleKeyDown);
634
+ }
635
+ return () => {
636
+ pendingTasks.forEach((task) => globalThis.clearTimeout(task));
637
+ document.removeEventListener("keydown", handleKeyDown);
638
+ focusOutWrapper?.removeEventListener("focusout", handleFocusOut);
639
+ if (!disableFocus && !disableFocusRestore && previousActiveElement.current) {
640
+ previousActiveElement.current.focus();
641
+ }
642
+ };
643
+ }, [
644
+ isOpen,
645
+ onClose,
646
+ closeDisabled,
647
+ escapeToClose,
648
+ disableFocus,
649
+ disableFocusRestore,
650
+ props["aria-labelledby"]
651
+ ]);
652
+ if (!mounted) return null;
653
+ const contentClass = [
654
+ "relative flex flex-col items-center justify-start",
655
+ whitoutBackground ? "bg-transparent" : [
656
+ "bg-white",
657
+ "pt-[25px] px-4 pb-[50px]",
658
+ "min-w-[320px] w-[95vw] max-w-[95vw] max-h-[90vh]",
659
+ "overflow-y-auto scroll-smooth",
660
+ "md:pt-[35px] md:px-8 md:pb-[54px] md:max-w-[1000px]"
661
+ ].join(" "),
662
+ props.className || ""
663
+ ].filter(Boolean).join(" ");
664
+ return /* @__PURE__ */ jsxRuntime.jsxs(Portal, { children: [
665
+ /* @__PURE__ */ jsxRuntime.jsx(
666
+ "div",
667
+ {
668
+ style: { zIndex: zIndexLevel },
669
+ className: [
670
+ "fixed inset-0 bg-onpe-blue transition-opacity duration-200",
671
+ visible ? "opacity-80" : "opacity-0"
672
+ ].join(" "),
673
+ onClick: onClose
674
+ }
675
+ ),
676
+ /* @__PURE__ */ jsxRuntime.jsx(
677
+ "div",
678
+ {
679
+ style: { zIndex: zIndexLevel + 10 },
680
+ className: [
681
+ "fixed top-0 w-full h-screen grid place-items-center",
682
+ "transition-all duration-200",
683
+ visible ? "opacity-100 scale-100 translate-y-0" : "opacity-[0.2] scale-95 -translate-y-5"
684
+ ].join(" "),
685
+ children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "relative grid place-items-center", children: /* @__PURE__ */ jsxRuntime.jsxs(
686
+ "div",
687
+ {
688
+ ref: modalRef,
689
+ onClick: (e) => e.stopPropagation(),
690
+ ...existTabIndex && { tabIndex: disableFocus ? -1 : 0 },
691
+ role: "dialog",
692
+ "aria-modal": "true",
693
+ "aria-labelledby": props["aria-labelledby"],
694
+ "aria-describedby": props["aria-describedby"],
695
+ "aria-label": props["aria-label"],
696
+ children: [
697
+ /* @__PURE__ */ jsxRuntime.jsx("div", { ref: contentRef, className: contentClass, children: cachedChildren }),
698
+ closeButton && /* @__PURE__ */ jsxRuntime.jsx(
699
+ "button",
700
+ {
701
+ onClick: onClose,
702
+ className: "absolute top-2.5 right-2.5 text-onpe-red cursor-pointer w-4 h-4 border-none bg-transparent p-0 md:w-6 md:h-6",
703
+ "aria-label": "Cerrar",
704
+ type: "button",
705
+ children: /* @__PURE__ */ jsxRuntime.jsx(IconCloseRadius, { "aria-hidden": "true", className: "w-full h-full" })
706
+ }
707
+ )
708
+ ]
709
+ }
710
+ ) })
711
+ }
712
+ )
713
+ ] });
714
+ };
715
+ var OnpeIdModal = ({
716
+ modalUrl,
717
+ isOpenModal,
718
+ isOpenLaunchApp,
719
+ isOnline,
720
+ onClose,
721
+ iframeRef,
722
+ preloadIframeRef,
723
+ handlePreloadIframeReady,
724
+ handleModalIframeReady
725
+ }) => {
726
+ const [isIframeLoaded, setIsIframeLoaded] = react.useState(false);
727
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
728
+ modalUrl && !isOpenModal && isOnline && // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions -- iframe onLoad es necesario para detección de precarga
729
+ /* @__PURE__ */ jsxRuntime.jsx(
730
+ "iframe",
731
+ {
732
+ ref: preloadIframeRef,
733
+ src: modalUrl,
734
+ className: "hidden",
735
+ onLoad: handlePreloadIframeReady,
736
+ title: "Precarga ONPE ID"
737
+ }
738
+ ),
739
+ /* @__PURE__ */ jsxRuntime.jsx(
740
+ Modal,
741
+ {
742
+ isOpen: isOpenModal && isOnline,
743
+ onClose,
744
+ className: "max-w-custom-673 p-10 relative",
745
+ closeButton: !isOpenLaunchApp && isIframeLoaded,
746
+ escapeToClose: false,
747
+ children: !!modalUrl && isOnline && // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions -- iframe onLoad es necesario para el flujo de autenticación
748
+ /* @__PURE__ */ jsxRuntime.jsx(
749
+ "iframe",
750
+ {
751
+ ref: iframeRef,
752
+ src: modalUrl,
753
+ onLoad: () => {
754
+ handleModalIframeReady();
755
+ setIsIframeLoaded(true);
756
+ },
757
+ className: "w-full h-[380px] md:h-[315px]",
758
+ title: "Aplicativo ONPEID para autenticaci\xF3n y registro"
759
+ }
760
+ )
761
+ }
762
+ )
763
+ ] });
764
+ };
765
+
766
+ exports.OnpeIdModal = OnpeIdModal;
767
+ exports.useIframeCommunication = useIframeCommunication;
768
+ exports.useIframePreload = useIframePreload;
769
+ exports.useModalIframePreload = useModalIframePreload;
770
+ exports.useOnpeIdAuth = useOnpeIdAuth;
771
+ exports.useSendMessageToIframe = useSendMessageToIframe;
772
+ exports.useSocketConnection = useSocketConnection;
773
+ //# sourceMappingURL=hooks.js.map
774
+ //# sourceMappingURL=hooks.js.map