-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathContextOverlay.tsx
More file actions
201 lines (186 loc) · 7.5 KB
/
Copy pathContextOverlay.tsx
File metadata and controls
201 lines (186 loc) · 7.5 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React from "react";
import {
Classes as BlueprintClasses,
Popover as BlueprintPopover,
PopoverInteractionKind as InteractionKind,
PopoverProps as BlueprintPopoverProps,
Utils as BlueprintUtils,
} from "@blueprintjs/core";
import { CLASSPREFIX as eccgui, WhiteSpaceContainer, WhiteSpaceContainerProps } from "../../index";
export interface ContextOverlayProps extends Omit<BlueprintPopoverProps, "position"> {
/**
* `target` element to use as toggler for the overlay display.
*/
children?: JSX.Element;
/**
* Type of counter property to `Modal.forceTopPosition`.
* Use it when you need to display modal dialogs out of the context overlay.
*/
preventTopPosition?: boolean;
/**
* Use the overlay target as placeholder before the real `<ContextOverlay /` is rendered on first hover or focus event.
* Currently experimental.
*/
usePlaceholder?: boolean;
/**
* Adds white space to each side of the overlay content.
* For more control use `WhiteSpaceContainer` directly as wrapper for the content children.
*/
paddingSize?: WhiteSpaceContainerProps["paddingTop"];
}
/**
* Element displays connected content by interacting with a target element.
* Full list of available option can be seen at https://blueprintjs.com/docs/#popover2-package/popover2
*/
export const ContextOverlay = ({
children,
portalClassName,
preventTopPosition,
className = "",
usePlaceholder = false,
paddingSize,
content,
...otherPopoverProps
}: ContextOverlayProps) => {
const placeholderRef = React.useRef<HTMLElement>(null);
const eventMemory = React.useRef<undefined | "mouseenter" | "focusin" | "click">(undefined);
const swapDelay = React.useRef<null | NodeJS.Timeout>(null);
const interactionKind = React.useRef<InteractionKind>(otherPopoverProps.interactionKind ?? InteractionKind.CLICK);
const swapDelayTime = 15;
const [placeholder, setPlaceholder] = React.useState<boolean>(
// use placeholder only for "simple" overlays without special states
!otherPopoverProps.disabled &&
!otherPopoverProps.defaultIsOpen &&
!otherPopoverProps.isOpen &&
otherPopoverProps.renderTarget === undefined &&
usePlaceholder
);
const swap = (ev: MouseEvent | globalThis.FocusEvent) => {
const waitForClick =
interactionKind.current === InteractionKind.CLICK ||
interactionKind.current === InteractionKind.CLICK_TARGET_ONLY;
if (swapDelay.current) {
clearTimeout(swapDelay.current);
}
const replacePlaceholder = () => {
eventMemory.current = ev.type as "mouseenter" | "focusin" | "click";
setPlaceholder(false);
};
if (waitForClick) {
ev.stopImmediatePropagation();
replacePlaceholder();
return;
}
swapDelay.current = setTimeout(
replacePlaceholder,
// we delay the swap for hover/focus to prevent unwanted effects
// (e.g. event hickup after replacing elements when it is not really necessary)
swapDelayTime
);
};
React.useEffect(() => {
interactionKind.current = otherPopoverProps.interactionKind ?? InteractionKind.CLICK;
const waitForClick =
interactionKind.current === InteractionKind.CLICK ||
interactionKind.current === InteractionKind.CLICK_TARGET_ONLY;
const removeEvents = () => {
if (placeholderRef.current) {
placeholderRef.current.removeEventListener("click", swap);
placeholderRef.current.removeEventListener("mouseenter", swap);
placeholderRef.current.removeEventListener("focusin", swap);
}
return;
};
if (placeholderRef.current) {
removeEvents(); // remove events in case of interaction kind changed during existence
if (waitForClick) {
placeholderRef.current.addEventListener("click", swap);
} else {
placeholderRef.current.addEventListener("mouseenter", swap);
placeholderRef.current.addEventListener("focusin", swap);
}
return () => {
removeEvents();
};
}
return () => {};
}, [!!placeholderRef.current, otherPopoverProps.interactionKind]);
const refocus = React.useCallback((node) => {
const target = node?.targetRef.current.children[0];
if (!eventMemory.current || !target) {
return;
}
switch (eventMemory.current) {
case "focusin":
target.focus();
break;
case "click":
target.click();
break;
case "mouseenter":
// re-check if the cursor is still over the element after swapping the placeholder before triggering the event to bubble up
(target as HTMLElement).addEventListener(
"mouseover",
() => (target as HTMLElement).dispatchEvent(new MouseEvent("mouseover", { bubbles: true })),
{
capture: true,
once: true,
}
);
break;
}
}, []);
const targetClassName = `${eccgui}-contextoverlay` + (className ? ` ${className}` : "");
const displayPlaceholder = () => {
const PlaceholderElement = otherPopoverProps?.targetTagName ?? (otherPopoverProps?.fill ? "div" : "span");
const childTarget = BlueprintUtils.ensureElement(React.Children.toArray(children)[0]);
if (!childTarget) {
return null;
}
return React.createElement(
PlaceholderElement,
{
...otherPopoverProps?.targetProps,
className: `${BlueprintClasses.POPOVER_TARGET} ${targetClassName} ${eccgui}-contextoverlay__wrapper--placeholder`,
ref: placeholderRef,
},
React.cloneElement(childTarget, {
...childTarget.props,
className:
childTarget.props.className ?? "" + (otherPopoverProps.fill ? ` ${BlueprintClasses.FILL}` : ""),
tabIndex:
childTarget.props.tabIndex ??
(!otherPopoverProps?.disabled && otherPopoverProps?.openOnTargetFocus ? 0 : undefined),
})
);
};
const portalClassNameFinal =
(preventTopPosition ? `${eccgui}-contextoverlay__portal--lowertop` : "") +
(portalClassName ? ` ${portalClassName}` : "");
return placeholder ? (
displayPlaceholder()
) : (
<BlueprintPopover
placement="bottom"
content={content ? (
paddingSize ? (
<WhiteSpaceContainer
paddingTop={paddingSize}
paddingRight={paddingSize}
paddingBottom={paddingSize}
paddingLeft={paddingSize}
>
{content}
</WhiteSpaceContainer>
) : content
) : undefined}
{...otherPopoverProps}
className={targetClassName}
portalClassName={portalClassNameFinal.trim() ?? undefined}
ref={refocus}
>
{children}
</BlueprintPopover>
);
};
export default ContextOverlay;