-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDecoupledOverlay.tsx
More file actions
97 lines (91 loc) · 3.37 KB
/
DecoupledOverlay.tsx
File metadata and controls
97 lines (91 loc) · 3.37 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import React from "react";
import { createPortal } from "react-dom";
import { Classes as BlueprintClasses } from "@blueprintjs/core";
import { createPopper } from "@popperjs/core";
import { CLASSPREFIX as eccgui, ContextOverlayProps, TestableComponent, TooltipSize, WhiteSpaceContainer } from "../../index";
export interface DecoupledOverlayProps
extends React.HTMLAttributes<HTMLDivElement>,
TestableComponent,
Pick<ContextOverlayProps, "usePortal" | "portalContainer" | "placement" | "minimal" | "paddingSize"> {
/**
* Element that should be used. The step content is displayed as a tooltip instead of a modal.
* In case of an array, the first match is highlighted. */
targetSelectorOrElement: string | Element;
/**
* The size of the overlay.
* */
size?: TooltipSize;
}
/**
* Use an overlay popover without the necessity to use a target that need to be rendered in place.
* The target is referenced by a selector string or element object.
* It can exist somewhere in the DOM, but it must exist when the overlay is rendered.
* It is always displayed, close it by removement.
*/
export const DecoupledOverlay = ({
targetSelectorOrElement,
usePortal = true,
portalContainer = document.body,
minimal = false,
placement = "auto",
size = "large",
paddingSize,
children,
}: DecoupledOverlayProps) => {
const overlayRef = React.useCallback(
(overlay: HTMLDivElement | null) => {
const target =
typeof targetSelectorOrElement === "string"
? document.querySelector(targetSelectorOrElement)
: targetSelectorOrElement;
if (overlay && target) {
createPopper(target, overlay, {
placement: placement,
modifiers: [
{
name: "offset",
options: {
offset: [0, 15],
},
},
],
});
}
},
[targetSelectorOrElement]
);
const overlay = (
<div
className={
`${eccgui}-decoupled-overlay` +
` ${eccgui}-decoupled-overlay--${size}` +
` ${BlueprintClasses.POPOVER}` +
(minimal ? ` ${BlueprintClasses.MINIMAL}` : "")
}
role="tooltip"
ref={overlayRef}
>
{!minimal && (
<div
className={`${eccgui}-decoupled-overlay__arrow ${BlueprintClasses.POPOVER_ARROW}`}
data-popper-arrow
aria-hidden
/>
)}
<div className={`${BlueprintClasses.POPOVER_CONTENT} ${eccgui}-decoupled-overlay__content`}>
{paddingSize ? (
<WhiteSpaceContainer
paddingTop={paddingSize}
paddingRight={paddingSize}
paddingBottom={paddingSize}
paddingLeft={paddingSize}
>
{children}
</WhiteSpaceContainer>
) : children}
</div>
</div>
);
return usePortal ? createPortal(overlay, portalContainer) : overlay;
};
export default DecoupledOverlay;