Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Tooltips trigger without a delay #4419

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useCallback, useEffect, useRef, useState } from "react";
import type { Breakpoint, Breakpoints } from "@webstudio-is/sdk";
import {
EnhancedTooltip,
Flex,
Text,
Toolbar,
Expand Down Expand Up @@ -156,10 +155,11 @@ export const BreakpointsSelector = ({
{groupBreakpoints(Array.from(breakpoints.values())).map(
(breakpoint) => {
return (
<EnhancedTooltip
<Tooltip
key={breakpoint.id}
content={getTooltipContent(breakpoint)}
variant="wrapped"
disableHoverableContent
>
<ToolbarToggleItem
variant="subtle"
Expand All @@ -180,7 +180,7 @@ export const BreakpointsSelector = ({
<BpStarOffIcon size={22} />
))}
</ToolbarToggleItem>
</EnhancedTooltip>
</Tooltip>
);
}
)}
Expand Down
37 changes: 35 additions & 2 deletions packages/design-system/src/components/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const Tooltip = forwardRef(
},
ref: Ref<HTMLDivElement>
) => {
const triggerRef = useRef<HTMLButtonElement>(null);
// We need to intercept tooltip open
const [open = false, setOpen] = useControllableState({
prop: openProp,
Expand Down Expand Up @@ -95,15 +96,47 @@ export const Tooltip = forwardRef(
props.onMouseLeave?.(event);
};

return (
// There's no way to prevent a rendered trigger from opening.
// This causes delay issues when an invisible tooltip forces other tooltips to show immediately.
return content == null ? (
children
) : (
<TooltipPrimitive.Root
open={open}
defaultOpen={defaultOpen}
onOpenChange={setOpen}
delayDuration={delayDuration}
disableHoverableContent={disableHoverableContent}
>
<TooltipPrimitive.Trigger asChild {...triggerProps}>
<TooltipPrimitive.Trigger
asChild
ref={triggerRef}
{...triggerProps}
onFocus={(event) => {
// Prevent the tooltip from opening on focus
// The main issue is that after dialogs or selects, the tooltip button is autofocused and causes the tooltip to open
event.preventDefault();
}}
onPointerMove={(event) => {
// The tooltip captures pointer events, which can be an issue when the tooltip trigger is also the popover trigger.
// This is related to Popover.Anchor, but sometimes it can't be placed above the tooltip trigger.
// To prevent pointer movements from affecting the tooltip, we check if there’s an element between the target and the current dialog.
let currentElement =
event.target instanceof Element ? event.target : null;

while (
currentElement !== null &&
currentElement !== event.currentTarget
) {
if (currentElement.getAttribute("role") === "dialog") {
event.preventDefault();
break;
}

currentElement = currentElement.parentElement;
}
}}
>
{children}
</TooltipPrimitive.Trigger>
{content != null && (
Expand Down
Loading