/
ConditionalPortal.tsx
72 lines (63 loc) · 1.58 KB
/
ConditionalPortal.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
import React, { ReactElement } from "react";
import { PortalInto } from "./getContainer";
import { Portal } from "./Portal";
/**
* If any of these props are defined on a component, the component will render
* in a portal instead of the current tree.
*/
export interface RenderConditionalPortalProps {
/**
* Boolean if the portal should be used.
*/
portal?: boolean;
/**
* @see {@link PortalProps.into}
*/
portalInto?: PortalInto;
/**
* @see {@link PortalProps.intoId}
*/
portalIntoId?: string;
}
export interface ConditionalPortalProps extends RenderConditionalPortalProps {
/**
* This children to render.
*/
children: ReactElement | null;
}
/**
* This is a very simple component that is used in other places within react-md
* to conditionally render the children within a portal or not based on general
* portal config props.
*/
export function ConditionalPortal({
portal,
portalInto,
portalIntoId,
children,
}: ConditionalPortalProps): ReactElement | null {
if (!portal && !portalInto && !portalIntoId) {
return children;
}
return (
<Portal into={portalInto} intoId={portalIntoId}>
{children}
</Portal>
);
}
/* istanbul ignore next */
if (process.env.NODE_ENV !== "production") {
try {
const PropTypes = require("prop-types");
ConditionalPortal.propTypes = {
portal: PropTypes.bool,
portalInto: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.object,
]),
portalIntoId: PropTypes.string,
children: PropTypes.node,
};
} catch (e) {}
}