|
18 | 18 | import van from '../van.min.js'; |
19 | 19 | import { getValue } from '../utils.js'; |
20 | 20 |
|
| 21 | +const { div } = van.tags; |
| 22 | + |
21 | 23 | const STREAMLIT_DIALOG_ZINDEX = 1000060; |
22 | 24 | const STREAMLIT_DIALOG_CLASS = 'stDialog'; |
23 | 25 |
|
24 | 26 | const Portal = (/** @type Options */ options, ...args) => { |
25 | 27 | const { target, align = 'left', position = 'bottom' } = getValue(options); |
26 | 28 | const id = `${target}-portal`; |
27 | | - let portalEl = null; |
28 | 29 | let outsideClickHandler = null; |
29 | 30 |
|
30 | 31 | const close = () => { options.opened.val = false; }; |
31 | 32 |
|
32 | 33 | window.testgen.portals[id] = { domId: id, targetId: target, opened: options.opened, close }; |
33 | 34 |
|
| 35 | + // Side-effect derive: manages close loop and outside-click handler. |
| 36 | + // Kept free of van.add / DOM creation to avoid corrupting VanJS dependency tracking. |
34 | 37 | van.derive(() => { |
35 | 38 | const isOpen = getValue(options.opened); |
36 | 39 |
|
37 | 40 | if (!isOpen) { |
38 | | - portalEl?.remove(); |
39 | | - portalEl = null; |
40 | 41 | if (outsideClickHandler) { |
41 | 42 | document.removeEventListener('click', outsideClickHandler, true); |
42 | 43 | outsideClickHandler = null; |
43 | 44 | } |
44 | 45 | return; |
45 | 46 | } |
46 | 47 |
|
47 | | - // Close other open portals before opening this one |
| 48 | + const anchor = document.getElementById(target); |
| 49 | + if (!anchor) return; |
| 50 | + |
| 51 | + // Close other open portals — skip parent portals that contain our anchor. |
| 52 | + const toClose = []; |
48 | 53 | for (const p of Object.values(window.testgen.portals)) { |
49 | | - if (p.domId !== id && getValue(p.opened)) { |
50 | | - p.close(); |
| 54 | + if (p.domId !== id && p.opened?.rawVal) { |
| 55 | + const otherEl = document.getElementById(p.domId); |
| 56 | + if (otherEl?.contains(anchor)) continue; |
| 57 | + toClose.push(p); |
51 | 58 | } |
52 | 59 | } |
| 60 | + if (toClose.length) { |
| 61 | + queueMicrotask(() => toClose.forEach(p => { p.opened.val = false; })); |
| 62 | + } |
| 63 | + |
| 64 | + if (!outsideClickHandler) { |
| 65 | + outsideClickHandler = (event) => { |
| 66 | + const anchor = document.getElementById(target); |
| 67 | + const portalEl = document.getElementById(id); |
| 68 | + if (portalEl?.contains(event.target)) return; |
| 69 | + if (anchor?.contains(event.target)) return; |
| 70 | + if (isClickInsideChildPortal(event.target, id, portalEl)) return; |
| 71 | + close(); |
| 72 | + }; |
| 73 | + document.addEventListener('click', outsideClickHandler, true); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + // DOM rendering: a VanJS binding on document.body. |
| 78 | + // VanJS manages the element lifecycle natively — no manual createElement/remove. |
| 79 | + van.add(document.body, () => { |
| 80 | + if (!getValue(options.opened)) { |
| 81 | + return ''; |
| 82 | + } |
53 | 83 |
|
54 | 84 | const anchor = document.getElementById(target); |
55 | | - if (!anchor) return; |
| 85 | + if (!anchor) return ''; |
56 | 86 |
|
57 | 87 | const fixed = hasFixedAncestor(anchor); |
58 | 88 | const fromDialog = hasStreamlitDialogAncestor(anchor); |
59 | | - const zIndex = fromDialog ? (STREAMLIT_DIALOG_ZINDEX + 1) : 1001; |
| 89 | + const parentPortalEl = getParentPortalElement(anchor, id); |
| 90 | + const zIndex = parentPortalEl |
| 91 | + ? (parseInt(parentPortalEl.style.zIndex) || 1001) + 1 |
| 92 | + : fromDialog ? (STREAMLIT_DIALOG_ZINDEX + 1) : 1001; |
60 | 93 | const coords = position === 'bottom' |
61 | 94 | ? calculateBottomPosition(anchor, align, fixed) |
62 | 95 | : calculateTopPosition(anchor, align, fixed); |
63 | 96 |
|
64 | | - if (!portalEl) { |
65 | | - portalEl = document.createElement('div'); |
66 | | - document.body.appendChild(portalEl); |
67 | | - van.add(portalEl, ...args); |
68 | | - |
69 | | - outsideClickHandler = (event) => { |
70 | | - const anchor = document.getElementById(target); |
71 | | - if (!portalEl?.contains(event.target) && !anchor?.contains(event.target)) { |
72 | | - close(); |
73 | | - } |
74 | | - }; |
75 | | - document.addEventListener('click', outsideClickHandler, true); |
76 | | - } |
77 | | - |
78 | | - portalEl.id = id; |
79 | | - portalEl.className = getValue(options.class) ?? ''; |
80 | | - portalEl.style.cssText = `position: ${fixed ? 'fixed' : 'absolute'}; z-index: ${zIndex}; ${coords} ${getValue(options.style) ?? ''}`; |
| 97 | + return div( |
| 98 | + { |
| 99 | + id, |
| 100 | + class: getValue(options.class) ?? '', |
| 101 | + style: `position: ${fixed ? 'fixed' : 'absolute'}; z-index: ${zIndex}; ${coords} ${getValue(options.style) ?? ''}`, |
| 102 | + }, |
| 103 | + ...args, |
| 104 | + ); |
81 | 105 | }); |
82 | 106 |
|
83 | 107 | return ''; |
84 | 108 | }; |
85 | 109 |
|
| 110 | +function getParentPortalElement(anchor, selfId) { |
| 111 | + for (const p of Object.values(window.testgen.portals)) { |
| 112 | + if (p.domId === selfId) continue; |
| 113 | + const el = document.getElementById(p.domId); |
| 114 | + if (el?.contains(anchor)) return el; |
| 115 | + } |
| 116 | + return null; |
| 117 | +} |
| 118 | + |
| 119 | +function isClickInsideChildPortal(target, selfId, selfPortalEl) { |
| 120 | + for (const p of Object.values(window.testgen.portals)) { |
| 121 | + if (p.domId === selfId) continue; |
| 122 | + const childEl = document.getElementById(p.domId); |
| 123 | + if (childEl?.contains(target)) { |
| 124 | + const childAnchor = document.getElementById(p.targetId); |
| 125 | + if (selfPortalEl?.contains(childAnchor)) return true; |
| 126 | + } |
| 127 | + } |
| 128 | + return false; |
| 129 | +} |
| 130 | + |
86 | 131 | function hasFixedAncestor(el) { |
87 | 132 | let node = el.parentElement; |
88 | 133 | while (node && node !== document.body) { |
|
0 commit comments