Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

GDGT-3417: Only show one tooltip at a time (#85230)

Will Fry49f4439dd5a1
diff --git a/e2e/test/scenarios/models/reproductions-3.cy.spec.ts b/e2e/test/scenarios/models/reproductions-3.cy.spec.ts
index dfbe8e19752..1e32d367654 100644
--- a/e2e/test/scenarios/models/reproductions-3.cy.spec.ts
+++ b/e2e/test/scenarios/models/reproductions-3.cy.spec.ts
@@ -475,6 +475,9 @@ describe("issue 37009", () => {
);
cy.findByTestId("native-query-editor-container").icon("play").click();
cy.wait("@dataset");
+ // Clicking "Run query" hovers it with a synthetic hover that never ends,
+ // which keeps its own tooltip open.
+ cy.realPress("Escape");
cy.findByRole("tooltip").should("not.exist");
cy.findByTestId("dataset-edit-bar")
.button("Save")
@@ -505,6 +508,7 @@ describe("issue 37009", () => {
);
cy.findByTestId("native-query-editor-container").icon("play").click();
cy.wait("@dataset");
+ cy.realPress("Escape");
cy.findByRole("tooltip").should("not.exist");
cy.findByTestId("dataset-edit-bar")
.button("Save changes")
diff --git a/frontend/src/metabase/nav/containers/MainNavbar/SidebarItems/SidebarItemName.tsx b/frontend/src/metabase/nav/containers/MainNavbar/SidebarItems/SidebarItemName.tsx
index 00a9f451b12..011480906fa 100644
--- a/frontend/src/metabase/nav/containers/MainNavbar/SidebarItems/SidebarItemName.tsx
+++ b/frontend/src/metabase/nav/containers/MainNavbar/SidebarItems/SidebarItemName.tsx
@@ -17,7 +17,7 @@ export function SidebarItemName({
}: SidebarItemNameProps) {
if (itemName.length >= ITEM_NAME_LENGTH_TOOLTIP_THRESHOLD) {
return (
- <Tooltip label={itemName} withArrow maw="none">
+ <Tooltip label={itemName} withArrow maw="65ch">
<TreeNode.NameContainer
className={cx(
CS.textNoWrap,
diff --git a/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.module.css b/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.module.css
index 8f470cb30e0..95242823bd7 100644
--- a/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.module.css
+++ b/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.module.css
@@ -7,6 +7,7 @@
/* The 6px vertical padding from Figma has no matching spacing token. */
padding: 6px var(--mantine-spacing-sm);
white-space: unset;
+ width: max-content;
max-width: 320px;
/* Break long unbreakable strings so the tooltip can't overflow its max-width (metabase#74433). */
overflow-wrap: anywhere;
@@ -17,6 +18,15 @@
pointer-events: auto;
}
+.tooltip[data-entering-instantly] {
+ opacity: 1 !important; /* !important to override inline styles from Mantine */
+ transition: none !important;
+}
+
+.tooltip[data-replaced] {
+ display: none;
+}
+
.arrow {
/*
* Disabling the clip-path property to prevent the arrow from being cut off
diff --git a/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.tsx b/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.tsx
index 99b47ff722b..da5d658454c 100644
--- a/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.tsx
+++ b/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.tsx
@@ -11,8 +11,8 @@ import {
useMemo,
} from "react";
-import { useIsTransitionOpen } from "./use-is-transition-open";
import { useTooltipEventIsolation } from "./use-tooltip-event-isolation";
+import { useTooltipVisibility } from "./use-tooltip-visibility";
const TooltipPortalTargetContext = createContext<HTMLElement | null>(null);
@@ -44,11 +44,14 @@ const TooltipImpl = forwardRef<HTMLDivElement, TooltipProps>(
[contextTarget, callerPortalTarget, portalProps],
);
- const isControlled = rest.opened !== undefined;
- const { isOpen, transitionProps: resolvedTransitionProps } =
- useIsTransitionOpen(transitionProps);
-
- const isHoverable = isOpen && !isControlled;
+ const {
+ isHoverable,
+ isReplaced,
+ isEnteringInstantly,
+ transitionProps: resolvedTransitionProps,
+ } = useTooltipVisibility(transitionProps, {
+ isControlled: rest.opened !== undefined,
+ });
const { tooltipProps, labelProps } = useTooltipEventIsolation(rest);
@@ -61,7 +64,14 @@ const TooltipImpl = forwardRef<HTMLDivElement, TooltipProps>(
// Escape key, so it shouldn't linger.
closeDelay={rest.disabled ? 0 : rest.closeDelay}
label={<div {...labelProps}>{label}</div>}
- mod={[{ hoverable: isHoverable }, mod]}
+ mod={[
+ {
+ hoverable: isHoverable,
+ replaced: isReplaced,
+ "entering-instantly": isEnteringInstantly,
+ },
+ mod,
+ ]}
portalProps={resolvedPortalProps}
transitionProps={resolvedTransitionProps}
ref={ref}
diff --git a/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.unit.spec.tsx b/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.unit.spec.tsx
index d751700272d..47907a1bd7b 100644
--- a/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.unit.spec.tsx
+++ b/frontend/src/metabase/ui/components/overlays/Tooltip/Tooltip.unit.spec.tsx
@@ -27,6 +27,48 @@ const setup = () => {
return { user };
};
+type NeighbouringTargetsProps = {
+ isPinnedOpened: boolean;
+ hasFirstTarget: boolean;
+};
+
+const NeighbouringTargets = ({
+ isPinnedOpened,
+ hasFirstTarget,
+}: NeighbouringTargetsProps) => (
+ <div>
+ {(hasFirstTarget ? ["First", "Second"] : ["Second"]).map((name) => (
+ <Tooltip
+ key={name}
+ label={`${name} tooltip`}
+ transitionProps={{ duration: FADE_DURATION_MS }}
+ >
+ <button>{name}</button>
+ </Tooltip>
+ ))}
+ <Tooltip label="Pinned tooltip" opened={isPinnedOpened}>
+ <button>Pinned</button>
+ </Tooltip>
+ <span>Elsewhere</span>
+ </div>
+);
+
+const setupNeighbouringTargets = () => {
+ const user = setupUser();
+ let props: NeighbouringTargetsProps = {
+ isPinnedOpened: false,
+ hasFirstTarget: true,
+ };
+ const { rerender } = render(<NeighbouringTargets {...props} />);
+ const updateProps = (changes: Partial<NeighbouringTargetsProps>) => {
+ props = { ...props, ...changes };
+ rerender(<NeighbouringTargets {...props} />);
+ };
+ const openPinnedTooltip = () => updateProps({ isPinnedOpened: true });
+ const removeFirstTarget = () => updateProps({ hasFirstTarget: false });
+ return { user, openPinnedTooltip, removeFirstTarget };
+};
+
const ISOLATED_EVENTS = [
{ type: "click", fire: fireEvent.click },
{ type: "dblclick", fire: fireEvent.dblClick },
@@ -108,6 +150,138 @@ describe("Tooltip", () => {
});
});
+ it("hides the previous tooltip straight away when the pointer moves to another target", async () => {
+ const { user } = setupNeighbouringTargets();
+
+ await user.hover(screen.getByRole("button", { name: "First" }));
+ const firstTooltip = await screen.findByRole("tooltip", {
+ name: "First tooltip",
+ });
+ // With nothing to replace, it fades in as usual.
+ expect(firstTooltip).not.toHaveAttribute("data-entering-instantly");
+
+ await user.hover(screen.getByRole("button", { name: "Second" }));
+ const secondTooltip = await screen.findByRole("tooltip", {
+ name: "Second tooltip",
+ });
+ expect(firstTooltip).toHaveAttribute("data-replaced");
+ expect(secondTooltip).toHaveAttribute("data-entering-instantly");
+
+ await waitFor(() => {
+ expect(secondTooltip).not.toHaveAttribute("data-entering-instantly");
+ });
+ });
+
+ it("brings back a hidden tooltip that's still open as soon as the one that hid it closes", async () => {
+ const { user } = setupNeighbouringTargets();
+
+ await user.tab();
+ const firstTooltip = await screen.findByRole("tooltip", {
+ name: "First tooltip",
+ });
+
+ const secondTarget = screen.getByRole("button", { name: "Second" });
+ await user.hover(secondTarget);
+ const secondTooltip = await screen.findByRole("tooltip", {
+ name: "Second tooltip",
+ });
+ expect(firstTooltip).toHaveAttribute("data-replaced");
+
+ await user.unhover(secondTarget);
+ // Hidden rather than fading out, so the first tooltip shows at once.
+ await waitFor(() => {
+ expect(secondTooltip).toHaveAttribute("data-replaced");
+ });
+ expect(secondTooltip).toBeInTheDocument();
+ expect(firstTooltip).not.toHaveAttribute("data-replaced");
+ });
+
+ it("keeps showing a brought-back tooltip while it fades out", async () => {
+ const { user } = setupNeighbouringTargets();
+
+ await user.tab();
+ const firstTooltip = await screen.findByRole("tooltip", {
+ name: "First tooltip",
+ });
+
+ const secondTarget = screen.getByRole("button", { name: "Second" });
+ await user.hover(secondTarget);
+ const secondTooltip = await screen.findByRole("tooltip", {
+ name: "Second tooltip",
+ });
+ await user.unhover(secondTarget);
+ await waitFor(() => {
+ expect(firstTooltip).not.toHaveAttribute("data-replaced");
+ });
+
+ // Close the first tooltip while the second is still fading out.
+ await user.click(screen.getByText("Elsewhere"));
+ await waitFor(() => {
+ expect(firstTooltip).not.toHaveAttribute("data-hoverable");
+ });
+ expect(secondTooltip).toBeInTheDocument();
+ expect(secondTooltip).toHaveAttribute("data-replaced");
+ expect(firstTooltip).not.toHaveAttribute("data-replaced");
+ });
+
+ it("fades in the next tooltip as usual after a shown one is removed", async () => {
+ const { user, removeFirstTarget } = setupNeighbouringTargets();
+
+ await user.hover(screen.getByRole("button", { name: "First" }));
+ await screen.findByRole("tooltip", { name: "First tooltip" });
+
+ removeFirstTarget();
+ expect(
+ screen.queryByRole("tooltip", { name: "First tooltip" }),
+ ).not.toBeInTheDocument();
+
+ await user.hover(screen.getByRole("button", { name: "Second" }));
+ const secondTooltip = await screen.findByRole("tooltip", {
+ name: "Second tooltip",
+ });
+ expect(secondTooltip).not.toHaveAttribute("data-entering-instantly");
+ });
+
+ it("neither hides nor is hidden by a tooltip whose owner controls whether it's open", async () => {
+ const { user, openPinnedTooltip } = setupNeighbouringTargets();
+
+ await user.hover(screen.getByRole("button", { name: "First" }));
+ const firstTooltip = await screen.findByRole("tooltip", {
+ name: "First tooltip",
+ });
+
+ openPinnedTooltip();
+ const pinnedTooltip = await screen.findByRole("tooltip", {
+ name: "Pinned tooltip",
+ });
+ expect(firstTooltip).not.toHaveAttribute("data-replaced");
+ expect(pinnedTooltip).not.toHaveAttribute("data-entering-instantly");
+
+ await user.hover(screen.getByRole("button", { name: "Second" }));
+ await screen.findByRole("tooltip", { name: "Second tooltip" });
+ expect(pinnedTooltip).not.toHaveAttribute("data-replaced");
+ });
+
+ it("fades out a replacing tooltip that closes before it has finished appearing", async () => {
+ const { user } = setupNeighbouringTargets();
+
+ await user.hover(screen.getByRole("button", { name: "First" }));
+ await screen.findByRole("tooltip", { name: "First tooltip" });
+ await user.hover(screen.getByRole("button", { name: "Second" }));
+ const secondTooltip = await screen.findByRole("tooltip", {
+ name: "Second tooltip",
+ });
+ expect(secondTooltip).toHaveAttribute("data-entering-instantly");
+
+ await user.hover(screen.getByText("Elsewhere"));
+ await waitFor(() => {
+ expect(secondTooltip).not.toHaveAttribute("data-entering-instantly");
+ });
+ // With nothing replacing it, it stays visible while it fades.
+ expect(secondTooltip).not.toHaveAttribute("data-replaced");
+ expect(secondTooltip).toBeInTheDocument();
+ });
+
it.each(ISOLATED_EVENTS)(
"lets $type on the target reach its parents",
async ({ type, fire }) => {
diff --git a/frontend/src/metabase/ui/components/overlays/Tooltip/use-is-transition-open.ts b/frontend/src/metabase/ui/components/overlays/Tooltip/use-is-transition-open.ts
deleted file mode 100644
index 03bf157fe57..00000000000
--- a/frontend/src/metabase/ui/components/overlays/Tooltip/use-is-transition-open.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import type { TransitionOverride } from "@mantine/core";
-import { useMemo, useState } from "react";
-
-/**
- * Tracks whether the tooltip is shown or fading in. Returns transition props
- * to pass on to Mantine, which still call any `onEnter`/`onExit` given.
- * Stays `false` for a tooltip that mounts already open.
- */
-export function useIsTransitionOpen(
- transitionProps: TransitionOverride | undefined,
-) {
- const [isOpen, setIsOpen] = useState(false);
- const resolvedTransitionProps = useMemo(
- () => ({
- ...transitionProps,
- onEnter: () => {
- setIsOpen(true);
- transitionProps?.onEnter?.();
- },
- onExit: () => {
- setIsOpen(false);
- transitionProps?.onExit?.();
- },
- }),
- [transitionProps],
- );
- return { isOpen, transitionProps: resolvedTransitionProps };
-}
diff --git a/frontend/src/metabase/ui/components/overlays/Tooltip/use-tooltip-visibility.ts b/frontend/src/metabase/ui/components/overlays/Tooltip/use-tooltip-visibility.ts
new file mode 100644
index 00000000000..8bbd8ba2853
--- /dev/null
+++ b/frontend/src/metabase/ui/components/overlays/Tooltip/use-tooltip-visibility.ts
@@ -0,0 +1,139 @@
+import type { TransitionOverride } from "@mantine/core";
+import {
+ useEffect,
+ useId,
+ useMemo,
+ useState,
+ useSyncExternalStore,
+} from "react";
+
+type TooltipPhase = "closed" | "entering" | "open" | "exiting";
+
+/**
+ * Uncontrolled tooltips that are open, oldest first, so closing one reveals
+ * the one it replaced. Several can be open at once if e.g. one is hovered and
+ * another is focused.
+ */
+let openTooltipIds: string[] = [];
+/** The tooltip fading out, which shows only while no other tooltip is open. */
+let fadingOutTooltipId: string | null = null;
+const listeners = new Set<() => void>();
+
+/**
+ * Lets only one uncontrolled tooltip show at a time: a new one hides any other
+ * and skips its fade-in. Pass the returned `transitionProps` to Mantine, or the
+ * flags for styling the tooltip never change.
+ */
+export function useTooltipVisibility(
+ transitionProps: TransitionOverride | undefined,
+ { isControlled }: { isControlled: boolean },
+) {
+ const id = useId();
+ const [phase, setPhase] = useState<TooltipPhase>("closed");
+ const [isReplacing, setIsReplacing] = useState(false);
+ const isShown = phase !== "closed";
+ const isReplaced = useSyncExternalStore(
+ subscribe,
+ () => !isControlled && isShown && isOtherTooltipVisible(id),
+ );
+
+ // A tooltip removed while it's showing never reaches `onExited`.
+ useEffect(() => () => releaseTooltip(id), [id]);
+
+ const resolvedTransitionProps = useMemo(
+ () => ({
+ ...transitionProps,
+ onEnter: () => {
+ setPhase("entering");
+ if (!isControlled) {
+ setIsReplacing(isOtherTooltipVisible(id));
+ showTooltip(id);
+ }
+ transitionProps?.onEnter?.();
+ },
+ onEntered: () => {
+ setPhase("open");
+ transitionProps?.onEntered?.();
+ },
+ onExit: () => {
+ setPhase("exiting");
+ exitTooltip(id);
+ transitionProps?.onExit?.();
+ },
+ onExited: () => {
+ setPhase("closed");
+ releaseTooltip(id);
+ transitionProps?.onExited?.();
+ },
+ }),
+ [transitionProps, id, isControlled],
+ );
+
+ return {
+ isHoverable: !isControlled && (phase === "entering" || phase === "open"),
+ isReplaced,
+ isEnteringInstantly: isReplacing && phase === "entering",
+ transitionProps: resolvedTransitionProps,
+ };
+}
+
+function isOtherTooltipVisible(id: string) {
+ const visibleTooltipId = openTooltipIds.at(-1) ?? fadingOutTooltipId;
+ return visibleTooltipId !== null && visibleTooltipId !== id;
+}
+
+function setTooltips(
+ nextOpenTooltipIds: string[],
+ nextFadingOutTooltipId: string | null,
+) {
+ openTooltipIds = nextOpenTooltipIds;
+ fadingOutTooltipId = nextFadingOutTooltipId;
+ listeners.forEach((listener) => listener());
+}
+
+/**
+ * Runs as a tooltip starts fading in. It becomes the visible tooltip, hiding
+ * any other until it closes.
+ */
+function showTooltip(id: string) {
+ setTooltips(
+ [...withoutTooltip(id), id],
+ fadingOutTooltipId === id ? null : fadingOutTooltipId,
+ );
+}
+
+/**
+ * Runs as a tooltip starts fading out. It stays visible for the fade only if
+ * no other tooltip is open; otherwise the newest open one shows again at once.
+ */
+function exitTooltip(id: string) {
+ if (openTooltipIds.includes(id)) {
+ const remainingTooltipIds = withoutTooltip(id);
+ const isLastOpenTooltip = remainingTooltipIds.length === 0;
+ setTooltips(
+ remainingTooltipIds,
+ isLastOpenTooltip ? id : fadingOutTooltipId,
+ );
+ }
+}
+
+/** Runs once a tooltip has finished fading out, or is unmounted mid-fade. */
+function releaseTooltip(id: string) {
+ if (openTooltipIds.includes(id) || fadingOutTooltipId === id) {
+ setTooltips(
+ withoutTooltip(id),
+ fadingOutTooltipId === id ? null : fadingOutTooltipId,
+ );
+ }
+}
+
+function withoutTooltip(id: string) {
+ return openTooltipIds.filter((openTooltipId) => openTooltipId !== id);
+}
+
+function subscribe(listener: () => void) {
+ listeners.add(listener);
+ return () => {
+ listeners.delete(listener);
+ };
+}