fix: Fix code selection in property tooltip (#4709)

## Description

In property tooltip I want to be able to select and copy the code.

## 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:
0000)
- [ ] 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
This commit is contained in:
Oleg Isonen authored and GitHub committed 2025-01-06 20:16:39 +01:00
1 parent 8d5b69844a
commit 72dbd80ef9
2 files changed
+22 -8

No files matched your search

@@ -257,8 +257,6 @@ export const PropertyLabel = ({
<Tooltip
open={isOpen}
onOpenChange={setIsOpen}
// prevent closing tooltip on content click
onPointerDown={(event) => event.preventDefault()}
triggerProps={{
onClick: (event) => {
if (event.altKey) {
@@ -1,4 +1,4 @@
import type { Ref, ComponentProps, ReactNode } from "react";
import type { Ref, ComponentProps, ReactNode, MouseEventHandler } from "react";
import { forwardRef, useEffect, useRef, useState } from "react";
import {
autoUpdate,
@@ -75,6 +75,7 @@ export const Tooltip = forwardRef(
onOpenChange?.(open);
},
});
const preventCloseRef = useRef(false);
/**
* When the mouse leaves Tooltip.Content and hovers over an iframe, the Radix Tooltip stays open.
@@ -89,13 +90,19 @@ export const Tooltip = forwardRef(
*
* The simpler solution with fewer side effects is to close the tooltip on mouse leave.
*/
const handleMouseEnterComposed: React.MouseEventHandler<HTMLDivElement> = (
event
) => {
const handleMouseLeave: MouseEventHandler<HTMLDivElement> = (event) => {
setOpen(false);
props.onMouseLeave?.(event);
};
const handleOpenChange = (open: boolean) => {
if (open === false && preventCloseRef.current) {
return;
}
setOpen(open);
};
// 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 ? (
@@ -104,7 +111,7 @@ export const Tooltip = forwardRef(
<TooltipPrimitive.Root
open={open}
defaultOpen={defaultOpen}
onOpenChange={setOpen}
onOpenChange={handleOpenChange}
delayDuration={delayDuration}
disableHoverableContent={disableHoverableContent}
>
@@ -149,7 +156,16 @@ export const Tooltip = forwardRef(
collisionPadding={8}
arrowPadding={8}
{...props}
onMouseLeave={handleMouseEnterComposed}
onMouseLeave={handleMouseLeave}
onPointerDown={() => {
// Allows clicking on links or selecting text inside the tooltip.
// Prevent closing tooltip on content click.
// Can't use preventDefault() because it will prevent selecting code for copy/paste.
preventCloseRef.current = true;
requestAnimationFrame(() => {
preventCloseRef.current = false;
});
}}
>
{typeof content === "string" ? <Text>{content}</Text> : content}
<Box css={{ color: "transparent" }}>