Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(ui): show project picker name tooltip only when truncated (MUL-8313) (#9617)

Multica Eved51665feb6a9
diff --git a/packages/ui/components/common/__tests__/truncated-tooltip.test.tsx b/packages/ui/components/common/__tests__/truncated-tooltip.test.tsx
new file mode 100644
index 0000000000..bf34e02985
--- /dev/null
+++ b/packages/ui/components/common/__tests__/truncated-tooltip.test.tsx
@@ -0,0 +1,99 @@
+import type { ReactNode } from "react";
+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, describe, expect, it } from "vitest";
+import { isContentTruncated, TruncatedTooltip } from "../truncated-tooltip";
+import { TooltipProvider } from "../../ui/tooltip";
+
+afterEach(() => {
+ cleanup();
+ document.body.innerHTML = "";
+});
+
+function renderWithTooltips(ui: ReactNode) {
+ return render(<TooltipProvider delay={0}>{ui}</TooltipProvider>);
+}
+
+async function hover(element: HTMLElement) {
+ await act(async () => {
+ fireEvent.pointerEnter(element, { pointerType: "mouse" });
+ fireEvent.mouseEnter(element);
+ fireEvent.pointerMove(element, { pointerType: "mouse" });
+ fireEvent.mouseMove(element);
+ });
+}
+
+// jsdom has no layout, so give an element the box sizes a browser would.
+function setBox(
+ el: Element,
+ box: { scrollWidth: number; clientWidth: number; scrollHeight?: number; clientHeight?: number },
+) {
+ const { scrollWidth, clientWidth, scrollHeight = 20, clientHeight = 20 } = box;
+ Object.defineProperties(el, {
+ scrollWidth: { configurable: true, value: scrollWidth },
+ clientWidth: { configurable: true, value: clientWidth },
+ scrollHeight: { configurable: true, value: scrollHeight },
+ clientHeight: { configurable: true, value: clientHeight },
+ });
+}
+
+const tooltipPopup = () => document.querySelector("[data-slot='tooltip-content']");
+
+function Row({ label }: { label: string }) {
+ return (
+ <TruncatedTooltip content={label}>
+ <button type="button">
+ <span data-testid="label" style={{ overflowX: "hidden", overflowY: "hidden" }}>
+ {label}
+ </span>
+ </button>
+ </TruncatedTooltip>
+ );
+}
+
+describe("TruncatedTooltip", () => {
+ it("shows the full text when the label is cut off", async () => {
+ renderWithTooltips(<Row label="A very long project name" />);
+ setBox(screen.getByTestId("label"), { scrollWidth: 240, clientWidth: 120 });
+
+ await hover(screen.getByRole("button"));
+ expect(tooltipPopup()).toHaveTextContent("A very long project name");
+ });
+
+ it("stays closed when the label fits", async () => {
+ renderWithTooltips(<Row label="Short" />);
+ setBox(screen.getByTestId("label"), { scrollWidth: 40, clientWidth: 120 });
+
+ await hover(screen.getByRole("button"));
+ expect(tooltipPopup()).toBeNull();
+ });
+
+ it("renders the child alone without content", () => {
+ renderWithTooltips(
+ <TruncatedTooltip content="">
+ <button type="button">Plain</button>
+ </TruncatedTooltip>,
+ );
+ expect(screen.getByRole("button", { name: "Plain" })).not.toHaveAttribute("data-popup-open");
+ });
+});
+
+describe("isContentTruncated", () => {
+ it("ignores overflow that is not clipped", () => {
+ const el = document.createElement("div");
+ document.body.appendChild(el);
+ const icon = document.createElement("span");
+ el.appendChild(icon);
+ setBox(icon, { scrollWidth: 18, clientWidth: 14 });
+ expect(isContentTruncated(el)).toBe(false);
+ });
+
+ it("detects vertical clipping from line-clamp", () => {
+ const el = document.createElement("div");
+ document.body.appendChild(el);
+ // jsdom does not expand the `overflow` shorthand into its longhands.
+ el.style.overflowX = "hidden";
+ el.style.overflowY = "hidden";
+ setBox(el, { scrollWidth: 100, clientWidth: 100, scrollHeight: 60, clientHeight: 40 });
+ expect(isContentTruncated(el)).toBe(true);
+ });
+});
diff --git a/packages/ui/components/common/truncated-tooltip.tsx b/packages/ui/components/common/truncated-tooltip.tsx
new file mode 100644
index 0000000000..9188195b69
--- /dev/null
+++ b/packages/ui/components/common/truncated-tooltip.tsx
@@ -0,0 +1,79 @@
+"use client";
+
+import { useRef, useState, type ComponentProps, type ReactElement, type ReactNode } from "react";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@multica/ui/components/ui/tooltip";
+
+/**
+ * Whether `node` hides part of its content. Overflow only counts on an axis
+ * that actually clips (`overflow` other than `visible`): an emoji icon that
+ * spills out of its fixed-size box is still fully visible, so it is not
+ * truncation. Covers `truncate` (horizontal) and `line-clamp-*` (vertical).
+ */
+const CLIPPING_OVERFLOW = new Set(["hidden", "clip", "auto", "scroll"]);
+
+function clipsContent(node: Element): boolean {
+ const overflowX = node.scrollWidth > node.clientWidth;
+ const overflowY = node.scrollHeight > node.clientHeight;
+ if (!overflowX && !overflowY) return false;
+ const style = getComputedStyle(node);
+ return (
+ (overflowX && CLIPPING_OVERFLOW.has(style.overflowX)) ||
+ (overflowY && CLIPPING_OVERFLOW.has(style.overflowY))
+ );
+}
+
+/** True when `el` or any element inside it cuts off part of its content. */
+export function isContentTruncated(el: HTMLElement | null): boolean {
+ if (!el) return false;
+ if (clipsContent(el)) return true;
+ for (const node of el.querySelectorAll("*")) {
+ if (clipsContent(node)) return true;
+ }
+ return false;
+}
+
+/**
+ * Tooltip that reveals the full text only when the trigger's text is actually
+ * cut off. Built on the shared `Tooltip` (same styling and open delay); right
+ * before it would open, it measures the trigger and its descendants and stays
+ * closed when nothing overflows. Measuring at open time keeps it correct
+ * across resizes and content changes without observers.
+ *
+ * Use it for tooltips whose content repeats the trigger's own text. Tooltips
+ * that add information (icon labels, disabled reasons) keep using `Tooltip`.
+ */
+function TruncatedTooltip({
+ content,
+ side = "top",
+ children,
+}: {
+ /** Full text shown when the trigger is truncated. Empty renders the child alone. */
+ content: ReactNode;
+ side?: ComponentProps<typeof TooltipContent>["side"];
+ /** The trigger element. Its own props and element type are preserved. */
+ children: ReactElement;
+}) {
+ const triggerRef = useRef<HTMLButtonElement | null>(null);
+ const [open, setOpen] = useState(false);
+
+ if (content == null || content === false || content === "") return children;
+
+ return (
+ <Tooltip
+ open={open}
+ onOpenChange={(next) => {
+ if (next && !isContentTruncated(triggerRef.current)) return;
+ setOpen(next);
+ }}
+ >
+ <TooltipTrigger ref={triggerRef} render={children} />
+ <TooltipContent side={side}>{content}</TooltipContent>
+ </Tooltip>
+ );
+}
+
+export { TruncatedTooltip };
diff --git a/packages/views/issues/components/pickers/property-picker.tsx b/packages/views/issues/components/pickers/property-picker.tsx
index 3bad8cf131..8f3d84ee49 100644
--- a/packages/views/issues/components/pickers/property-picker.tsx
+++ b/packages/views/issues/components/pickers/property-picker.tsx
@@ -12,6 +12,7 @@ import {
TooltipContent,
TooltipTrigger,
} from "@multica/ui/components/ui/tooltip";
+import { TruncatedTooltip } from "@multica/ui/components/common/truncated-tooltip";
import { isImeComposing } from "@multica/core/utils";
import { useT } from "../../../i18n";
@@ -235,6 +236,7 @@ export function PickerItem({
onClick,
hoverClassName,
tooltip,
+ truncationTooltip,
emptyValue = false,
children,
}: {
@@ -257,6 +259,10 @@ export function PickerItem({
* a single line. Wrapped in a real Tooltip component (200ms delay,
* styled), not a native `title` attribute. */
tooltip?: React.ReactNode;
+ /** Full text of a row label that may be cut off. Unlike `tooltip`, it only
+ * opens when the row's content is actually truncated, so short labels
+ * don't pop a bubble that repeats them. Ignored when `tooltip` is set. */
+ truncationTooltip?: React.ReactNode;
children: React.ReactNode;
}) {
const button = (
@@ -283,7 +289,10 @@ export function PickerItem({
</button>
);
- if (!tooltip) return button;
+ if (!tooltip) {
+ if (!truncationTooltip) return button;
+ return <TruncatedTooltip content={truncationTooltip}>{button}</TruncatedTooltip>;
+ }
return (
<Tooltip>
diff --git a/packages/views/projects/components/project-picker.tsx b/packages/views/projects/components/project-picker.tsx
index 31096c8657..815b0fb6b1 100644
--- a/packages/views/projects/components/project-picker.tsx
+++ b/packages/views/projects/components/project-picker.tsx
@@ -118,7 +118,7 @@ export function ProjectPicker({
<PickerItem
key={p.id}
selected={p.id === projectId}
- tooltip={p.title}
+ truncationTooltip={p.title}
onClick={() => {
onUpdate({ project_id: p.id });
setOpen(false);