Skip to content

Commit

Permalink
fix: Prevent tooltip on focus (#4318)
Browse files Browse the repository at this point in the history
## Description

closes #4293

One of solution also
```
        onCloseAutoFocus={(event) => {
          event.preventDefault();
        }}
```

on dialog and popover content, but it's lost element focus.

This solution doesn't lost focus. Just prevent tooltip from being shown
on focus.


## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
  • Loading branch information
istarkov authored Oct 21, 2024
1 parent e5a4b0e commit 73328b3
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions packages/design-system/src/components/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { Box } from "./box";
import { Text } from "./text";
import type { CSS } from "../stitches.config";
import { theme } from "../stitches.config";
import { disableCanvasPointerEvents } from "../utilities";

export const TooltipProvider = TooltipPrimitive.TooltipProvider;

Expand Down Expand Up @@ -78,18 +77,24 @@ export const Tooltip = forwardRef(
});

/**
* When the mouse leaves Tooltip.Content and moves over an iframe, the Radix Tooltip stays open.
* This happens because Radix's internal grace area relies on the pointermove event, which isn't triggered over iframes.
* The current workaround is to set pointer-events: none on the canvas when the tooltip is open.
**/
useEffect(() => {
if (open) {
const enableCanvasPointerEvents = disableCanvasPointerEvents();
return () => {
enableCanvasPointerEvents?.();
};
}
}, [open]);
* When the mouse leaves Tooltip.Content and hovers over an iframe, the Radix Tooltip stays open.
* This occurs because Radix's grace area depends on the pointermove event, which iframes don't trigger.
*
* Two possible workarounds:
* 1. Set pointer-events: none on the canvas when the tooltip is open and content is hovered.
* (This doesn't work well in Chrome, as scrolling stops working on elements hovered with pointer-events: none,
* even after removing pointer-events.)
* 2. Close the tooltip on onMouseLeave.
* (This breaks some grace area behavior, such as closing the tooltip when moving the mouse from the content to the trigger.)
*
* The simpler solution with fewer side effects is to close the tooltip on mouse leave.
*/
const handleMouseEnterComposed: React.MouseEventHandler<HTMLDivElement> = (
event
) => {
setOpen(false);
props.onMouseLeave?.(event);
};

return (
<TooltipPrimitive.Root
Expand All @@ -112,6 +117,7 @@ export const Tooltip = forwardRef(
collisionPadding={8}
arrowPadding={8}
{...props}
onMouseLeave={handleMouseEnterComposed}
>
{typeof content === "string" ? <Text>{content}</Text> : content}
<Box css={{ color: theme.colors.transparentExtreme }}>
Expand Down

0 comments on commit 73328b3

Please sign in to comment.