Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiOverlayMask] Fix document is undefined #5422

Merged
merged 9 commits into from
Dec 3, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
**Bug fixes**

- Fixed scrollbars in `EuiRange` tick labels in Safari ([#5427](https://github.com/elastic/eui/pull/5427))
- Fixed an `EuiOverlayMask` bug where it calls window.document on server side([#5422](https://github.com/elastic/eui/pull/5422))

## [`42.0.0`](https://github.com/elastic/eui/tree/v42.0.0)

Expand Down
18 changes: 15 additions & 3 deletions src/components/overlay_mask/overlay_mask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const EuiOverlayMask: FunctionComponent<EuiOverlayMaskProps> = ({
headerZindexLocation = 'above',
...rest
}) => {
const overlayMaskNode = useRef<HTMLDivElement>(document.createElement('div'));
const overlayMaskNode = useRef<HTMLDivElement>();
const [isPortalTargetReady, setIsPortalTargetReady] = useState(false);

useEffect(() => {
Expand All @@ -63,9 +63,19 @@ export const EuiOverlayMask: FunctionComponent<EuiOverlayMaskProps> = ({
};
}, []);

useEffect(() => {
if (typeof document !== 'undefined') {
overlayMaskNode.current = document.createElement('div');
}
}, []);

useEffect(() => {
const portalTarget = overlayMaskNode.current;
document.body.appendChild(overlayMaskNode.current);

if (portalTarget) {
document.body.appendChild(portalTarget);
}

setIsPortalTargetReady(true);

return () => {
Expand All @@ -83,7 +93,9 @@ export const EuiOverlayMask: FunctionComponent<EuiOverlayMaskProps> = ({
`Unhandled property type. EuiOverlayMask property ${key} is not a string.`
);
}
overlayMaskNode.current.setAttribute(key, rest[key]!);
if (overlayMaskNode.current) {
overlayMaskNode.current.setAttribute(key, rest[key]!);
}
});
}, []); // eslint-disable-line react-hooks/exhaustive-deps

Expand Down