@homecode/ui 4.26.5 → 4.26.6
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.
|
@@ -1,19 +1,33 @@
|
|
|
1
1
|
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
2
|
-
import { useState,
|
|
2
|
+
import { useState, useLayoutEffect, useMemo } from 'react';
|
|
3
3
|
import { createPortal } from 'react-dom';
|
|
4
4
|
import { config } from '../../tools/config.js';
|
|
5
|
+
import { isBrowser } from '../../tools/env.js';
|
|
5
6
|
|
|
6
7
|
const DEFAULT_SELECTOR = `#${config.appOverlayId}`;
|
|
7
8
|
function Portal(props) {
|
|
8
9
|
const { selector = DEFAULT_SELECTOR, children } = props;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
// Start with immediate rendering if we're in browser
|
|
11
|
+
const [isMounted, setMounted] = useState(isBrowser);
|
|
12
|
+
useLayoutEffect(() => {
|
|
13
|
+
// Ensure we're mounted on client side (handles SSR edge cases)
|
|
12
14
|
setMounted(true);
|
|
13
15
|
}, []);
|
|
16
|
+
// Cache target element to avoid DOM queries on every render
|
|
17
|
+
const targetElement = useMemo(() => {
|
|
18
|
+
if (!isMounted)
|
|
19
|
+
return null;
|
|
20
|
+
return document.querySelector(selector);
|
|
21
|
+
}, [selector, isMounted]);
|
|
22
|
+
// Don't render if not mounted (prevents SSR hydration mismatch)
|
|
14
23
|
if (!isMounted)
|
|
15
24
|
return null;
|
|
16
|
-
|
|
25
|
+
// Check if target element exists before rendering
|
|
26
|
+
if (!targetElement) {
|
|
27
|
+
console.warn(`Portal target element not found: ${selector}`);
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
return createPortal(jsx(Fragment, { children: children }), targetElement);
|
|
17
31
|
}
|
|
18
32
|
|
|
19
33
|
export { Portal };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|