Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
feat(ui): cap tooltip width, follow text direction, add a shortcut chip
EtienneLescot046e6babd506
diff --git a/src/components/ui/tooltip.test.tsx b/src/components/ui/tooltip.test.tsxnew file mode 100644index 000000000..912bbd1b1--- /dev/null+++ b/src/components/ui/tooltip.test.tsx@@ -0,0 +1,98 @@+// @vitest-environment jsdom+import "@testing-library/jest-dom";+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { Tooltip, TooltipProvider } from "./tooltip";++class StubResizeObserver {+ observe() {+ return undefined;+ }+ unobserve() {+ return undefined;+ }+ disconnect() {+ return undefined;+ }+}++beforeEach(() => {+ vi.stubGlobal("ResizeObserver", StubResizeObserver);+});++afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+ vi.useRealTimers();+});++function renderTooltip(props: Partial<React.ComponentProps<typeof Tooltip>> = {}) {+ render(+ <TooltipProvider>+ <Tooltip content="Add a zoom at the playhead" {...props}>+ <button type="button">Zoom</button>+ </Tooltip>+ </TooltipProvider>,+ );+ return screen.getByRole("button", { name: "Zoom" });+}++// Radix draws the visible tooltip and a visually hidden `role="tooltip"` copy for assistive+// technology, so the styled node is found by its slot.+function visibleTooltip() {+ return document.querySelector<HTMLElement>('[data-slot="tooltip-content"]');+}++describe("Tooltip", () => {+ it("wraps long text and follows the text's own direction", () => {+ const button = renderTooltip();+ act(() => button.focus());++ const content = visibleTooltip();+ expect(content).toHaveAttribute("dir", "auto");+ expect(content?.className).toContain("max-w-[260px]");+ expect(content).toHaveTextContent("Add a zoom at the playhead");+ });++ it("renders a shortcut as its own left-to-right chip after the text", () => {+ const button = renderTooltip({ shortcut: "Ctrl + Z" });+ act(() => button.focus());++ const chip = visibleTooltip()?.querySelector("kbd");+ expect(chip).toHaveTextContent("Ctrl + Z");+ expect(chip).toHaveAttribute("dir", "ltr");+ // The chip is a sibling of the text, not part of the translated string.+ expect(chip?.previousSibling?.textContent).toBe("Add a zoom at the playhead");+ });++ it("draws no chip when there is no shortcut", () => {+ const button = renderTooltip();+ act(() => button.focus());++ expect(visibleTooltip()?.querySelector("kbd")).toBeNull();+ });++ it("opens 400 ms after the pointer arrives, not before", () => {+ vi.useFakeTimers();+ const button = renderTooltip();++ fireEvent.pointerMove(button);+ act(() => {+ vi.advanceTimersByTime(399);+ });+ expect(visibleTooltip()).toBeNull();++ act(() => {+ vi.advanceTimersByTime(1);+ });+ expect(visibleTooltip()).not.toBeNull();+ });++ it("opens at once on keyboard focus and describes the trigger", () => {+ const button = renderTooltip();+ act(() => button.focus());++ expect(visibleTooltip()).not.toBeNull();+ expect(button).toHaveAccessibleDescription("Add a zoom at the playhead");+ });+});diff --git a/src/components/ui/tooltip.tsx b/src/components/ui/tooltip.tsxindex 718f09f2d..6d999230f 100644--- a/src/components/ui/tooltip.tsx+++ b/src/components/ui/tooltip.tsx@@ -3,14 +3,21 @@ import * as React from "react";import { cn } from "@/lib/utils";+// Timing from technical-documentation/engineering/tooltips.md: a tooltip appears 400 ms after+// 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;+function TooltipProvider({- delayDuration = 200,+ delayDuration = TOOLTIP_OPEN_DELAY_MS,+ skipDelayDuration = TOOLTIP_SKIP_DELAY_MS,...props}: React.ComponentProps<typeof TooltipPrimitive.Provider>) {return (<TooltipPrimitive.Providerdata-slot="tooltip-provider"delayDuration={delayDuration}+ skipDelayDuration={skipDelayDuration}{...props}/>);@@ -40,9 +47,13 @@ function TooltipContent({<TooltipPrimitive.Portal><TooltipPrimitive.Contentdata-slot="tooltip-content"+ // Follows the text's own direction, so Arabic starts at its own edge.+ dir="auto"sideOffset={sideOffset}className={cn(- "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",+ // 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","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,@@ -61,16 +72,32 @@ const Tooltip = React.forwardRef<{children: React.ReactNode;content: React.ReactNode;+ /**+ * The current key binding, already formatted (`formatBinding`). Shown as a chip after the+ * text, never inside the translated string: a remapped key would make that string lie, and+ * a Latin key inside Arabic text breaks the order of the words.+ */+ shortcut?: string;side?: "top" | "right" | "bottom" | "left";+ /** Keeps the tooltip this far from the viewport's edges. */+ collisionPadding?: number;className?: string;}->(({ children, content, side, className }, ref) => (+>(({ children, content, shortcut, side, collisionPadding, className }, ref) => (<TooltipRoot><TooltipTrigger ref={ref} asChild>{children}</TooltipTrigger>- <TooltipContent side={side} className={className}>+ <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>));