Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(hud): clear the bar with tooltips, and balance their lines

EtienneLescot22d7bf43b5f2
diff --git a/src/components/launch/HudControls.tsx b/src/components/launch/HudControls.tsx
index 9310a2c74..6000d5ab7 100644
--- a/src/components/launch/HudControls.tsx
+++ b/src/components/launch/HudControls.tsx
@@ -1,8 +1,8 @@
import { Check, Languages, NotepadText, Settings } from "lucide-react";
-import { createContext, memo, type ReactElement, useContext } from "react";
+import { createContext, memo, type ReactElement, useContext, useRef, useState } from "react";
import { formatTimePadded } from "../../utils/timeUtils";
import { Button } from "../ui/button";
-import { Tooltip } from "../ui/tooltip";
+import { TOOLTIP_GAP_PX, Tooltip } from "../ui/tooltip";
import {
CameraIcon,
CursorIcon,
@@ -15,6 +15,7 @@ import {
SourceIcon,
VolumeIcon,
} from "./HudIcons";
+import { computeHudTooltipClearance } from "./hudGeometry";
import styles from "./LaunchWindow.module.css";
// Every control below is a `memo` boundary on purpose. The HUD's root re-renders
@@ -43,13 +44,42 @@ export const HudLayoutProvider = HudVerticalContext.Provider;
// Keeps a tooltip off the window's own edge, where its shadow would be cut.
const HUD_TOOLTIP_EDGE_PADDING = 8;
+// Radix places a tooltip against its trigger, but what the eye sees is the bar around it. The
+// bar pads the button, and a vertical bar widens to fit the timer while recording, so a gap
+// measured from the trigger left the tooltip overlapping the bar. The distance to the bar's
+// edge is measured when the tooltip opens (hence the controlled `open`: the offset has to be
+// right in the render that shows it, not one effect later) and added to the primitive's gap.
function HudTooltip({ content, children }: { content: string; children: ReactElement }) {
const vertical = useContext(HudVerticalContext);
+ const side = vertical ? "right" : "top";
+ const triggerRef = useRef<HTMLButtonElement | null>(null);
+ const [open, setOpen] = useState(false);
+ const [clearance, setClearance] = useState(0);
+
+ const handleOpenChange = (next: boolean) => {
+ const trigger = triggerRef.current;
+ const bar = trigger?.closest("[data-tray-layout]");
+ if (next && trigger && bar) {
+ setClearance(
+ computeHudTooltipClearance(
+ trigger.getBoundingClientRect(),
+ bar.getBoundingClientRect(),
+ side,
+ ),
+ );
+ }
+ setOpen(next);
+ };
+
return (
<Tooltip
+ ref={triggerRef}
content={content}
- side={vertical ? "right" : "top"}
+ side={side}
+ sideOffset={TOOLTIP_GAP_PX + clearance}
collisionPadding={HUD_TOOLTIP_EDGE_PADDING}
+ open={open}
+ onOpenChange={handleOpenChange}
>
{children}
</Tooltip>
diff --git a/src/components/launch/LaunchWindow.test.tsx b/src/components/launch/LaunchWindow.test.tsx
index 59b91cf93..06c83db94 100644
--- a/src/components/launch/LaunchWindow.test.tsx
+++ b/src/components/launch/LaunchWindow.test.tsx
@@ -333,6 +333,9 @@ function emitSourceSelectorClosed() {
function resetLaunchMocks() {
vi.stubGlobal("ResizeObserver", StubResizeObserver);
+ // The bar's orientation is a saved preference: a test that switches it must not leave the
+ // next one starting on a vertical bar.
+ localStorage.clear();
recorderState.value.toggleRecording = vi.fn();
recorderState.value.cursorCaptureMode = "editable-overlay";
recorderState.value.systemAudioEnabled = false;
@@ -674,6 +677,49 @@ describe("LaunchWindow record button", () => {
);
});
+ // Radix measures from the small trigger, but the bar is what the eye sees: while recording, a
+ // vertical bar widens to fit the timer and the Pause button sits well inside its edge. The
+ // tooltip has to clear the bar, or it overlaps it.
+ it("opens a tooltip clear of the bar's edge, not just the trigger's", async () => {
+ recorderState.value.recording = true;
+ recorderState.value.canPauseRecording = true;
+ renderLaunchWindow();
+ fireEvent.click(await screen.findByTestId("launch-tray-layout-button"));
+ const pause = await screen.findByTestId("launch-pause-button");
+ const bar = pause.closest("[data-tray-layout]") as HTMLElement;
+
+ const box = (x: number, width: number, height: number) => ({
+ x,
+ y: 0,
+ left: x,
+ top: 0,
+ right: x + width,
+ bottom: height,
+ width,
+ height,
+ toJSON: () => ({}),
+ });
+ // The bar is 100px wide; the button's right edge is 28px inside it.
+ vi.spyOn(bar, "getBoundingClientRect").mockReturnValue(box(0, 100, 300));
+ vi.spyOn(pause, "getBoundingClientRect").mockReturnValue(box(38, 34, 34));
+ // jsdom's viewport is zero-sized, which would make every placement overflow and flip.
+ const root = document.documentElement;
+ Object.defineProperty(root, "clientWidth", { value: 1000, configurable: true });
+ Object.defineProperty(root, "clientHeight", { value: 800, configurable: true });
+
+ try {
+ act(() => pause.focus());
+ await screen.findByRole("tooltip");
+
+ // Trigger's right edge 72 + the primitive's 8px gap + the 28px to the bar's edge.
+ const wrapper = document.querySelector<HTMLElement>("[data-radix-popper-content-wrapper]");
+ await waitFor(() => expect(wrapper?.style.transform).toBe("translate(108px, 17px)"));
+ } finally {
+ Reflect.deleteProperty(root, "clientWidth");
+ Reflect.deleteProperty(root, "clientHeight");
+ }
+ });
+
// Chromium records the pointer itself on these platforms once the native helper is missing,
// so the toggle would change nothing. An option that changes nothing is not shown.
it("hides the cursor toggle when capture falls back to the browser", async () => {
diff --git a/src/components/launch/hudGeometry.test.ts b/src/components/launch/hudGeometry.test.ts
index 167ad7f13..2ebb7c0ff 100644
--- a/src/components/launch/hudGeometry.test.ts
+++ b/src/components/launch/hudGeometry.test.ts
@@ -3,6 +3,7 @@ import {
computeHudBarMaxHeight,
computeHudModalMaxHeight,
computeHudPopoverMaxHeight,
+ computeHudTooltipClearance,
computeHudWindowSize,
HUD_BAR_BOTTOM,
HUD_EDGE_SLACK,
@@ -95,6 +96,33 @@ describe("computeHudPopoverMaxHeight", () => {
});
});
+describe("computeHudTooltipClearance", () => {
+ it("clears the padding above a button in a horizontal bar", () => {
+ expect(
+ computeHudTooltipClearance({ top: 508, right: 300 }, { top: 500, right: 900 }, "top"),
+ ).toBe(8);
+ });
+
+ it("clears a vertical bar that widened to show the timer, not just the trigger", () => {
+ // Idle: the bar hugs its 34px buttons. Recording: the timer makes it about 28px wider than
+ // the Pause, Restart and Cancel buttons, which stay where they were.
+ const idle = computeHudTooltipClearance({ top: 0, right: 92 }, { top: 0, right: 100 }, "right");
+ const recording = computeHudTooltipClearance(
+ { top: 0, right: 72 },
+ { top: 0, right: 100 },
+ "right",
+ );
+ expect(idle).toBe(8);
+ expect(recording).toBe(28);
+ });
+
+ it("never pulls a tooltip toward the trigger", () => {
+ expect(computeHudTooltipClearance({ top: 495, right: 0 }, { top: 500, right: 0 }, "top")).toBe(
+ 0,
+ );
+ });
+});
+
describe("computeHudBarMaxHeight", () => {
it("leaves room below the screen height for a popover", () => {
const max = computeHudBarMaxHeight(SCREEN);
diff --git a/src/components/launch/hudGeometry.ts b/src/components/launch/hudGeometry.ts
index 3800fef86..ae0787225 100644
--- a/src/components/launch/hudGeometry.ts
+++ b/src/components/launch/hudGeometry.ts
@@ -149,3 +149,21 @@ export function computeHudWindowSize(input: HudSizeInput): {
},
};
}
+
+/**
+ * How far past its trigger a tooltip has to sit to clear the BAR, on the side it opens.
+ *
+ * Radix places a tooltip against the small trigger, but the bar is the surface the eye sees. It
+ * pads the button (8px from the button's top edge to the bar's), and a vertical bar widens to
+ * fit the timer while recording (Pause, Restart and Cancel then sit about 28px inside its right
+ * edge). Both ate into the tooltip's gap, so it overlapped the bar. Measured at the moment the
+ * tooltip opens, it holds for either orientation and either state.
+ */
+export function computeHudTooltipClearance(
+ trigger: { top: number; right: number },
+ bar: { top: number; right: number },
+ side: "top" | "right",
+): number {
+ const inset = side === "top" ? trigger.top - bar.top : bar.right - trigger.right;
+ return Math.max(0, Math.round(inset));
+}
diff --git a/src/components/ui/tooltip.test.tsx b/src/components/ui/tooltip.test.tsx
index 912bbd1b1..e2dc233c0 100644
--- a/src/components/ui/tooltip.test.tsx
+++ b/src/components/ui/tooltip.test.tsx
@@ -1,8 +1,8 @@
// @vitest-environment jsdom
import "@testing-library/jest-dom";
-import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { Tooltip, TooltipProvider } from "./tooltip";
+import { TOOLTIP_GAP_PX, Tooltip, TooltipProvider } from "./tooltip";
class StubResizeObserver {
observe() {
@@ -43,6 +43,10 @@ function visibleTooltip() {
return document.querySelector<HTMLElement>('[data-slot="tooltip-content"]');
}
+function popperWrapper() {
+ return document.querySelector<HTMLElement>("[data-radix-popper-content-wrapper]");
+}
+
describe("Tooltip", () => {
it("wraps long text and follows the text's own direction", () => {
const button = renderTooltip();
@@ -51,6 +55,8 @@ describe("Tooltip", () => {
const content = visibleTooltip();
expect(content).toHaveAttribute("dir", "auto");
expect(content?.className).toContain("max-w-[260px]");
+ // Balanced lines, so a translation never ends on a single orphan word.
+ expect(content?.className).toContain("text-balance");
expect(content).toHaveTextContent("Add a zoom at the playhead");
});
@@ -72,6 +78,23 @@ describe("Tooltip", () => {
expect(visibleTooltip()?.querySelector("kbd")).toBeNull();
});
+ // The eye reads the gap against the trigger's edge. It was 6px, which two dark surfaces make
+ // look like overlap; the jsdom boxes are all zero-sized, so the offset is the whole translation.
+ it("keeps a real gap between the trigger and the tooltip", async () => {
+ expect(TOOLTIP_GAP_PX).toBe(8);
+ const button = renderTooltip();
+ act(() => button.focus());
+
+ await waitFor(() => expect(popperWrapper()?.style.transform).toBe("translate(0px, -8px)"));
+ });
+
+ it("lets a trigger that sits inside a padded surface ask for a larger gap", async () => {
+ const button = renderTooltip({ sideOffset: 8 + 28 });
+ act(() => button.focus());
+
+ await waitFor(() => expect(popperWrapper()?.style.transform).toBe("translate(0px, -36px)"));
+ });
+
it("opens 400 ms after the pointer arrives, not before", () => {
vi.useFakeTimers();
const button = renderTooltip();
diff --git a/src/components/ui/tooltip.tsx b/src/components/ui/tooltip.tsx
index 6d999230f..01b06c55e 100644
--- a/src/components/ui/tooltip.tsx
+++ b/src/components/ui/tooltip.tsx
@@ -7,6 +7,9 @@ import { cn } from "@/lib/utils";
// the pointer stops, and moving to a neighbouring control within 300 ms shows its tooltip at once.
const TOOLTIP_OPEN_DELAY_MS = 400;
const TOOLTIP_SKIP_DELAY_MS = 300;
+// The space between the trigger and the tooltip. 6px read as touching on a dark surface: a
+// tooltip is dark too, so it needs a gap the eye can see.
+export const TOOLTIP_GAP_PX = 8;
function TooltipProvider({
delayDuration = TOOLTIP_OPEN_DELAY_MS,
@@ -40,7 +43,7 @@ TooltipTrigger.displayName = "TooltipTrigger";
function TooltipContent({
className,
- sideOffset = 6,
+ sideOffset = TOOLTIP_GAP_PX,
...props
}: React.ComponentProps<typeof TooltipPrimitive.Content>) {
return (
@@ -52,8 +55,9 @@ function TooltipContent({
sideOffset={sideOffset}
className={cn(
// Popper sizes its wrapper to `max-content`, so without a cap a long tooltip is one
- // long line. 260px is about 40 characters a line at this size.
- "max-w-[260px] break-words px-2.5 py-1.5 text-xs leading-4 font-medium text-white/95 bg-black/85 border border-white/10 rounded-lg shadow-lg z-50",
+ // long line. 260px is about 40 characters a line at this size. `text-balance` evens the
+ // lines out, so a translation does not end on one orphan word.
+ "max-w-[260px] break-words text-balance px-2.5 py-1.5 text-xs leading-4 font-medium text-white/95 bg-black/85 border border-white/10 rounded-lg shadow-lg z-50",
"animate-in fade-in-0 zoom-in-95",
"data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95",
className,
@@ -79,28 +83,56 @@ const Tooltip = React.forwardRef<
*/
shortcut?: string;
side?: "top" | "right" | "bottom" | "left";
+ /**
+ * The distance from the trigger. Only for a trigger that is not the edge of what the eye
+ * sees, such as a button padded inside a bar: the tooltip then has to clear the bar.
+ */
+ sideOffset?: number;
/** Keeps the tooltip this far from the viewport's edges. */
collisionPadding?: number;
className?: string;
+ /** Controlled, for a caller that must measure something at the moment the tooltip opens. */
+ open?: boolean;
+ onOpenChange?: (open: boolean) => void;
}
->(({ children, content, shortcut, side, collisionPadding, className }, ref) => (
- <TooltipRoot>
- <TooltipTrigger ref={ref} asChild>
- {children}
- </TooltipTrigger>
- <TooltipContent side={side} collisionPadding={collisionPadding} className={className}>
- {content}
- {shortcut ? (
- <kbd
- dir="ltr"
- className="ms-2 inline-block rounded border border-white/15 bg-white/10 px-1 font-sans text-[11px] leading-4 text-white/80"
- >
- {shortcut}
- </kbd>
- ) : null}
- </TooltipContent>
- </TooltipRoot>
-));
+>(
+ (
+ {
+ children,
+ content,
+ shortcut,
+ side,
+ sideOffset,
+ collisionPadding,
+ className,
+ open,
+ onOpenChange,
+ },
+ ref,
+ ) => (
+ <TooltipRoot open={open} onOpenChange={onOpenChange}>
+ <TooltipTrigger ref={ref} asChild>
+ {children}
+ </TooltipTrigger>
+ <TooltipContent
+ side={side}
+ sideOffset={sideOffset}
+ collisionPadding={collisionPadding}
+ className={className}
+ >
+ {content}
+ {shortcut ? (
+ <kbd
+ dir="ltr"
+ className="ms-2 inline-block rounded border border-white/15 bg-white/10 px-1 font-sans text-[11px] leading-4 text-white/80"
+ >
+ {shortcut}
+ </kbd>
+ ) : null}
+ </TooltipContent>
+ </TooltipRoot>
+ ),
+);
Tooltip.displayName = "Tooltip";
export { Tooltip, TooltipContent, TooltipProvider, TooltipRoot, TooltipTrigger };
diff --git a/technical-documentation/engineering/tooltips.md b/technical-documentation/engineering/tooltips.md
index a95bf352e..48cd28c0a 100644
--- a/technical-documentation/engineering/tooltips.md
+++ b/technical-documentation/engineering/tooltips.md
@@ -60,7 +60,8 @@ If the icon has to swap and the wording has to follow (Play, Pause), it is an ac
|---|---|
| English text | 80 characters. 95 for the strings that define the two cursor modes |
| Sentences | 2, one idea each. Punctuation at the end only when there are two |
-| Layout | `max-w-[260px]`, `dir="auto"`. 3 lines in English, 4 in French, Italian and Spanish |
+| Layout | `max-w-[260px]`, `dir="auto"`, balanced lines. 3 lines in English, 4 in French, Italian and Spanish |
+| Gap | 8px from the trigger. Where the trigger sits inside a padded surface (a button in the HUD bar), the gap is measured from that surface's edge |
| Case | Sentence case. Form for an icon-only control: `Name: effect` |
| First appearance | 400 ms after the pointer stops, 300 ms to move between neighbours. One value, set on the shared provider |
| Disappearance | Never on a timer. Pointer leaves, focus leaves, or Escape |