forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPortal.tsx
83 lines (70 loc) · 2.11 KB
/
Portal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { css, cx } from '@emotion/css';
import { PropsWithChildren, useLayoutEffect, useRef } from 'react';
import * as React from 'react';
import ReactDOM from 'react-dom';
import { GrafanaTheme2 } from '@grafana/data';
import { useStyles2, useTheme2 } from '../../themes';
interface Props {
className?: string;
root?: HTMLElement;
forwardedRef?: React.ForwardedRef<HTMLDivElement>;
}
export const Portal: React.FC<PropsWithChildren<Props>> = (props) => {
const { children, className, root, forwardedRef } = props;
const theme = useTheme2();
const node = useRef<HTMLDivElement | null>(null);
const portalRoot = root ?? getPortalContainer();
if (!node.current) {
node.current = document.createElement('div');
if (className) {
node.current.className = className;
}
node.current.style.position = 'relative';
node.current.style.zIndex = `${theme.zIndex.portal}`;
}
useLayoutEffect(() => {
if (node.current) {
portalRoot.appendChild(node.current);
}
return () => {
if (node.current) {
portalRoot.removeChild(node.current);
}
};
}, [portalRoot]);
return ReactDOM.createPortal(
<div data-qiankun="grafana-full-app" ref={forwardedRef}>
{children}
</div>,
node.current
);
}
/** @internal */
export function getPortalContainer() {
return window.document.getElementById('grafana-portal-container') ?? document.body;
}
/** @internal */
export function PortalContainer() {
const styles = useStyles2(getStyles);
const isBodyScrolling = window.grafanaBootData?.settings.featureToggles.bodyScrolling;
return (
<div
id="grafana-portal-container"
className={cx({
[styles.grafanaPortalContainer]: isBodyScrolling,
})}
/>
);
}
const getStyles = (theme: GrafanaTheme2) => ({
grafanaPortalContainer: css({
position: 'fixed',
top: 0,
width: '100%',
zIndex: theme.zIndex.portal,
}),
});
export const RefForwardingPortal = React.forwardRef<HTMLDivElement, Props>((props, ref) => {
return <Portal {...props} forwardedRef={ref} />;
});
RefForwardingPortal.displayName = 'RefForwardingPortal';