Skip to content

Commit 4ae4cb3

Browse files
committed
use a delay for the tooltip placeholder swap to prevent unwanted effects like displaying tooltips without hovering elements
1 parent ddc24cb commit 4ae4cb3

2 files changed

Lines changed: 27 additions & 14 deletions

File tree

src/components/ContextOverlay/ContextOverlay.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ export const ContextOverlay = ({
4141
const eventmemory = React.useRef<undefined | "afterhover" | "afterfocus">(undefined);
4242
const [placeholder, setPlaceholder] = React.useState<boolean>(
4343
// use placeholder only for "simple" overlays without special states
44-
(!otherPopoverProps?.disabled ||
45-
!!otherPopoverProps?.defaultIsOpen ||
46-
!!otherPopoverProps?.isOpen ||
47-
!otherPopoverProps?.renderTarget) &&
44+
otherPopoverProps.disabled !== true &&
45+
otherPopoverProps.defaultIsOpen !== true &&
46+
otherPopoverProps.isOpen !== true &&
47+
typeof otherPopoverProps.renderTarget === "undefined" &&
4848
usePlaceholder
4949
);
5050

src/components/Tooltip/Tooltip.tsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,42 @@ export const Tooltip = ({
5353
markdownEnabler = "\n\n",
5454
markdownProps,
5555
usePlaceholder,
56+
hoverOpenDelay = 500,
5657
...otherTooltipProps
5758
}: TooltipProps) => {
5859
const placeholderRef = React.useRef(null);
5960
const eventmemory = React.useRef<null | "afterhover" | "afterfocus">(null);
6061
const searchId = React.useRef<null | string>(null);
62+
const swapdelayTime = 100;
6163
const [placeholder, setPlaceholder] = React.useState<boolean>(
62-
(!otherTooltipProps?.disabled ||
63-
!!otherTooltipProps?.defaultIsOpen ||
64-
!!otherTooltipProps?.isOpen ||
65-
!otherTooltipProps?.renderTarget) &&
64+
otherTooltipProps.disabled !== true &&
65+
otherTooltipProps.defaultIsOpen !== true &&
66+
otherTooltipProps.isOpen !== true &&
67+
typeof otherTooltipProps.renderTarget === "undefined" &&
68+
hoverOpenDelay > swapdelayTime &&
6669
(usePlaceholder === true || (typeof content === "string" && usePlaceholder !== false))
6770
);
6871

6972
const targetClassName =
7073
`${eccgui}-tooltip__wrapper` +
7174
(className ? " " + className : "") +
7275
(addIndicator === true ? " " + BlueprintClasses.TOOLTIP_INDICATOR : "");
76+
7377
React.useEffect(() => {
74-
if (placeholderRef.current) {
78+
if (placeholderRef.current !== null) {
7579
const swap = (ev: MouseEvent | globalThis.FocusEvent) => {
76-
eventmemory.current = ev.type === "focusin" ? "afterfocus" : "afterhover";
77-
searchId.current = Date.now().toString(16) + Math.random().toString(16).slice(2);
78-
setPlaceholder(false);
80+
const swapdelay = setTimeout(() => {
81+
// we delay the swap to prevent unwanted effects
82+
// (e.g. forced mouseover after the swap but the curser is already somewhere else)
83+
eventmemory.current = ev.type === "focusin" ? "afterfocus" : "afterhover";
84+
searchId.current = Date.now().toString(16) + Math.random().toString(16).slice(2);
85+
setPlaceholder(false);
86+
}, swapdelayTime);
87+
if (placeholderRef.current !== null)
88+
(placeholderRef.current as HTMLElement).addEventListener(
89+
ev.type === "focusin" ? "focusout" : "mouseleave",
90+
() => clearTimeout(swapdelay)
91+
);
7992
};
8093
(placeholderRef.current as HTMLElement).addEventListener("mouseenter", swap);
8194
(placeholderRef.current as HTMLElement).addEventListener("focusin", swap);
@@ -112,7 +125,7 @@ export const Tooltip = ({
112125
PlaceholderElement,
113126
{
114127
...otherTooltipProps?.targetProps,
115-
className: `${BlueprintClasses.POPOVER_TARGET} ${targetClassName}`,
128+
className: `${BlueprintClasses.POPOVER_TARGET} ${targetClassName} ${eccgui}-tooltip__wrapper--placeholder`,
116129
ref: placeholderRef,
117130
},
118131
React.cloneElement(childTarget, {
@@ -139,7 +152,7 @@ export const Tooltip = ({
139152
) : (
140153
<BlueprintTooltip
141154
lazy={true}
142-
hoverOpenDelay={500}
155+
hoverOpenDelay={hoverOpenDelay - swapdelayTime}
143156
{...otherTooltipProps}
144157
content={tooltipContent}
145158
className={targetClassName}

0 commit comments

Comments
 (0)