Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(frontend): a tooltip with an empty body says nothing

zchpeterc421cbabd259
diff --git a/frontend/src/components/ui/tooltip.test.tsx b/frontend/src/components/ui/tooltip.test.tsx
index 16dece1944..a897c851b3 100644
--- a/frontend/src/components/ui/tooltip.test.tsx
+++ b/frontend/src/components/ui/tooltip.test.tsx
@@ -1,4 +1,5 @@
import { Tooltip as BaseTooltip } from "@base-ui/react/tooltip";
+import type { ReactElement } from "react";
import { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, test, vi } from "vitest";
@@ -31,6 +32,14 @@ vi.mock("@base-ui/react/tooltip", async (importOriginal) => {
globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;
+const mount = (ui: ReactElement) => {
+ const container = document.createElement("div");
+ document.body.appendChild(container);
+ const root = createRoot(container);
+ act(() => root.render(ui));
+ return { container, root };
+};
+
describe("Tooltip", () => {
afterEach(() => {
recordTooltipProvider.mockClear();
@@ -44,15 +53,10 @@ describe("Tooltip", () => {
])("keeps the caller's trigger element %s", (_name, content) => {
// The element carries the caller's layout, so a row must not gain or lose
// it with the tooltip's content.
- const container = document.createElement("div");
- document.body.appendChild(container);
- const root = createRoot(container);
- act(() =>
- root.render(
+ const { container, root } = mount(
<Tooltip content={content} render={<span className="shrink-0" />}>
12:00
</Tooltip>
- )
);
const trigger = container.querySelector(".shrink-0");
@@ -61,15 +65,48 @@ describe("Tooltip", () => {
act(() => root.unmount());
});
- test("blocks keep their block trigger when there is nothing to say", () => {
+ test("blocks trigger a block, and add nothing when silent", () => {
+ // The block wrapper is this component's own default rather than layout a
+ // caller asked for, so a field with no tooltip is left as it was.
const container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
act(() =>
- root.render(<BlockTooltip content={undefined}>a field</BlockTooltip>)
+ root.render(<BlockTooltip content={<span>why</span>}>a field</BlockTooltip>)
);
-
expect(container.querySelector(".flex-1")).not.toBeNull();
+
+ act(() => root.render(<BlockTooltip content={undefined}>a field</BlockTooltip>));
+ expect(container.querySelector(".flex-1")).toBeNull();
+ expect(container.textContent).toBe("a field");
+ act(() => root.unmount());
+ });
+
+ test.each([
+ ["an empty string", ""],
+ ["nothing", undefined],
+ ])("opens nothing when its body is %s", async (_name, content) => {
+ // Callers pass strings that can be empty -- a disabled reason, an option's
+ // hint -- and an empty one is a tooltip with nothing to open.
+ vi.useFakeTimers();
+ const { container, root } = mount(
+ <Tooltip content={content}>
+ <button type="button">Trigger</button>
+ </Tooltip>
+ );
+
+ await act(async () => {
+ container
+ .querySelector("button")
+ ?.dispatchEvent(new FocusEvent("focusin", { bubbles: true }));
+ vi.advanceTimersByTime(100);
+ });
+
+ // Not the text -- an empty body renders an empty popup, which reads the
+ // same. What must not exist is the popup.
+ expect(
+ document.getElementById("bb-react-layer-overlay")?.childElementCount ?? 0
+ ).toBe(0);
act(() => root.unmount());
});
diff --git a/frontend/src/components/ui/tooltip.tsx b/frontend/src/components/ui/tooltip.tsx
index 4e3d383d92..4209b93b61 100644
--- a/frontend/src/components/ui/tooltip.tsx
+++ b/frontend/src/components/ui/tooltip.tsx
@@ -1,6 +1,6 @@
import { Tooltip as BaseTooltip } from "@base-ui/react/tooltip";
-import type { ComponentProps, ReactNode } from "react";
-import { cloneElement, isValidElement } from "react";
+import type { ComponentProps, ReactElement, ReactNode } from "react";
+import { cloneElement } from "react";
import { cn } from "@/lib/utils";
import { getLayerRoot, LAYER_SURFACE_CLASS } from "./layer";
@@ -14,8 +14,13 @@ interface TooltipProps {
readonly onOpenChange?: ComponentProps<
typeof BaseTooltip.Root
>["onOpenChange"];
- /** The trigger element; defaults to an inline-flex wrapper around `children`. */
- readonly render?: ComponentProps<typeof BaseTooltip.Trigger>["render"];
+ /**
+ * The trigger element; defaults to an inline-flex wrapper around `children`.
+ * An element rather than Base UI's wider render type, because this one is
+ * kept even when there is no tooltip, and a render *function* has nothing to
+ * be called with in that state.
+ */
+ readonly render?: ReactElement;
}
export function Tooltip({
@@ -33,11 +38,7 @@ export function Tooltip({
// dropping it when there is nothing to say would move their classes onto a
// grandchild in one state and not the other, which is a layout that breaks
// only sometimes.
- return isValidElement(render) ? (
- cloneElement(render, undefined, children)
- ) : (
- <>{children}</>
- );
+ return render ? cloneElement(render, undefined, children) : <>{children}</>;
}
return (
@@ -75,7 +76,10 @@ export function Tooltip({
* form sections.
*/
export function BlockTooltip({ render, ...props }: TooltipProps) {
- return (
- <Tooltip {...props} render={render ?? <div className="flex-1 min-w-0" />} />
- );
+ // A caller's own element is kept in both states; this default is the
+ // tooltip's, so it comes and goes with the tooltip.
+ const blockTrigger = props.content ? (
+ <div className="flex-1 min-w-0" />
+ ) : undefined;
+ return <Tooltip {...props} render={render ?? blockTrigger} />;
}