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: Fix code selection in property tooltip #4709

Merged
merged 5 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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,6 +1,6 @@
import { atom } from "nanostores";
import { useStore } from "@nanostores/react";
import { useState, type ReactNode } from "react";
import { useRef, useState, type ReactNode } from "react";
import { AlertIcon, ResetIcon } from "@webstudio-is/icons";
import {
hyphenateProperty,
Expand Down Expand Up @@ -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) {
Expand Down
28 changes: 22 additions & 6 deletions packages/design-system/src/components/tooltip.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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 ? (
Expand All @@ -104,7 +111,7 @@ export const Tooltip = forwardRef(
<TooltipPrimitive.Root
open={open}
defaultOpen={defaultOpen}
onOpenChange={setOpen}
onOpenChange={handleOpenChange}
delayDuration={delayDuration}
disableHoverableContent={disableHoverableContent}
>
Expand Down Expand Up @@ -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" }}>
Expand Down
Loading