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

Add a delay to tooltip #15716

Merged
merged 1 commit into from
Jan 12, 2023
Merged
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
21 changes: 19 additions & 2 deletions components/dashboard/src/components/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See License.AGPL.txt in the project root for license information.
*/

import { ReactNode, useEffect, useState } from "react";
import { ReactNode, useCallback, useEffect, useState } from "react";
import { Portal } from "react-portal";
import { usePopper } from "react-popper";

Expand All @@ -16,6 +16,7 @@ export interface TooltipProps {

function Tooltip(props: TooltipProps) {
const [expanded, setExpanded] = useState(false);
const [showTooltipTimeout, setShowTooltipTimeout] = useState<ReturnType<typeof setTimeout> | null>(null);
const [triggerEl, setTriggerEl] = useState<HTMLElement | null>(null);
const [tooltipEl, setTooltipEl] = useState<HTMLElement | null>(null);

Expand All @@ -29,8 +30,24 @@ function Tooltip(props: TooltipProps) {
update && update();
}, [update, props.content]);

// Adds a 500ms delay to showing tooltip so we don't show them until user pauses a bit like native browser tooltips
const handleMouseEnter = useCallback(() => {
const timeout = setTimeout(() => {
setExpanded(true);
}, 500);
setShowTooltipTimeout(timeout);
}, []);

const handleMouseLeave = useCallback(() => {
if (showTooltipTimeout) {
clearTimeout(showTooltipTimeout);
}
setShowTooltipTimeout(null);
setExpanded(false);
}, [showTooltipTimeout]);

return (
<div onMouseLeave={() => setExpanded(false)} onMouseEnter={() => setExpanded(true)} className="relative">
<div onMouseLeave={handleMouseLeave} onMouseEnter={handleMouseEnter} className="relative">
<div ref={setTriggerEl}>{props.children}</div>
{expanded ? (
<Portal>
Expand Down