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(ui): forward refs through the Popover and Tooltip wrappers

Ola Adebayo82bdfb6d86aa
diff --git a/src/components/ai-edition/v4/EditorShellV4.module.css b/src/components/ai-edition/v4/EditorShellV4.module.css
index cfe219f03..7962cfca0 100644
--- a/src/components/ai-edition/v4/EditorShellV4.module.css
+++ b/src/components/ai-edition/v4/EditorShellV4.module.css
@@ -966,6 +966,21 @@
cursor: pointer;
text-align: left;
}
+/* The key that does the same thing, parked at the end of a menu row. Teaches
+ the shortcut at the moment the user is reaching for the slow way to do it. */
+.recMenuKey {
+ margin-left: auto;
+ flex-shrink: 0;
+ padding: 2px 6px;
+ border-radius: 5px;
+ border: 1px solid var(--border-soft);
+ background: var(--surface-2);
+ color: var(--muted);
+ font-size: 10.5px;
+ font-weight: 600;
+ line-height: 1.4;
+}
+
.recMenuRow:hover:not(:disabled) {
background: var(--surface-2);
}
diff --git a/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx b/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
index 89f753b12..25c0f3a4e 100644
--- a/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.geometry.test.tsx
@@ -16,7 +16,9 @@ vi.mock("sonner", () => ({ toast: { error: vi.fn(), info: vi.fn(), success: vi.f
// The audio lane's pill renders a ClipWaveform; no decode in this geometry suite.
vi.mock("@/hooks/useAudioPeaks", () => ({ useAudioPeaks: () => null }));
+import { ShortcutsProvider } from "@/contexts/ShortcutsContext";
import type { useTimeline } from "@/lib/ai-edition/store/useTimeline";
+import { DEFAULT_SHORTCUTS, formatBinding } from "@/lib/shortcuts";
import { V4Timeline } from "./V4Timeline";
beforeAll(() => {
@@ -100,18 +102,20 @@ function renderTimeline(
}),
};
render(
- <V4Timeline
- // Only the members the lanes and the clip row read are mocked; the prop
- // stays typed as the real API rather than widened to `any` (AGENTS.md).
- tl={tl as unknown as ReturnType<typeof useTimeline>}
- setCurrentTime={vi.fn()}
- playing={false}
- onTogglePlay={vi.fn()}
- onPrevClip={vi.fn()}
- onNextClip={vi.fn()}
- onEditClip={vi.fn()}
- onAddVoiceover={vi.fn()}
- />,
+ <ShortcutsProvider>
+ <V4Timeline
+ // Only the members the lanes and the clip row read are mocked; the prop
+ // stays typed as the real API rather than widened to `any` (AGENTS.md).
+ tl={tl as unknown as ReturnType<typeof useTimeline>}
+ setCurrentTime={vi.fn()}
+ playing={false}
+ onTogglePlay={vi.fn()}
+ onPrevClip={vi.fn()}
+ onNextClip={vi.fn()}
+ onEditClip={vi.fn()}
+ onAddVoiceover={vi.fn()}
+ />
+ </ShortcutsProvider>,
);
return {
pill: screen.getByTitle("toolbar.newAnnotation"),
@@ -400,16 +404,18 @@ describe("V4Timeline audio lane drag", () => {
addZoom: vi.fn(async () => undefined),
};
render(
- <V4Timeline
- tl={tl as unknown as ReturnType<typeof useTimeline>}
- setCurrentTime={vi.fn()}
- playing={false}
- onTogglePlay={vi.fn()}
- onPrevClip={vi.fn()}
- onNextClip={vi.fn()}
- onEditClip={vi.fn()}
- onAddVoiceover={props.onAddVoiceover ?? vi.fn()}
- />,
+ <ShortcutsProvider>
+ <V4Timeline
+ tl={tl as unknown as ReturnType<typeof useTimeline>}
+ setCurrentTime={vi.fn()}
+ playing={false}
+ onTogglePlay={vi.fn()}
+ onPrevClip={vi.fn()}
+ onNextClip={vi.fn()}
+ onEditClip={vi.fn()}
+ onAddVoiceover={props.onAddVoiceover ?? vi.fn()}
+ />
+ </ShortcutsProvider>,
);
return { pill: screen.getByTitle("vo"), placeAudioTrack, selectAudioTrack };
}
@@ -427,6 +433,18 @@ describe("V4Timeline audio lane drag", () => {
expect(onAddVoiceover).toHaveBeenCalledTimes(1);
});
+ it("teaches the key that does the same thing", () => {
+ // Read off the live bindings rather than hardcoded here, so a rebind in the
+ // shortcuts dialog moves the menu with it instead of teaching a stale key.
+ renderAudio();
+ fireEvent.click(screen.getByLabelText("toolbar.addAudioTooltip"));
+ const keys = Array.from(document.querySelectorAll("kbd"), (k) => k.textContent);
+ expect(keys).toEqual([
+ formatBinding(DEFAULT_SHORTCUTS.addVoiceover, false),
+ formatBinding(DEFAULT_SHORTCUTS.addAudio, false),
+ ]);
+ });
+
it("marks where a looping track starts its file over", () => {
// A 60s file under a 180s span repeats twice more after the first pass, so
// there are two boundaries to show — at a third and two thirds.
diff --git a/src/components/ai-edition/v4/V4Timeline.tsx b/src/components/ai-edition/v4/V4Timeline.tsx
index 569db9828..de9235bca 100644
--- a/src/components/ai-edition/v4/V4Timeline.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.tsx
@@ -31,6 +31,7 @@ import { Tooltip, TooltipProvider } from "@/components/ui/tooltip";
import { fromFileUrl, toFileUrl } from "@/components/video-editor/projectPersistence";
import { ZOOM_DEPTH_SCALES } from "@/components/video-editor/types";
import { useScopedT } from "@/contexts/I18nContext";
+import { useShortcuts } from "@/contexts/ShortcutsContext";
import { useAudioPeaks } from "@/hooks/useAudioPeaks";
import { collapseTracksToPills } from "@/lib/ai-edition/document/audioTracks";
import { createId } from "@/lib/ai-edition/document/ids";
@@ -59,6 +60,7 @@ import {
type AutoZoomSuggestion,
buildAutoZoomSuggestionsForClips,
} from "@/lib/ai-edition/timeline/zoom-suggestions";
+import { formatBinding } from "@/lib/shortcuts";
import { nativeBridgeClient } from "@/native/client";
import { TransportBar } from "../TransportBar";
import type { VideoSource } from "../VirtualPreview";
@@ -485,6 +487,9 @@ export function V4Timeline({
onAddVoiceover: () => void;
}) {
const t = useScopedT("timeline");
+ // The live bindings, not the defaults: these keys are remappable, and a menu
+ // that taught the wrong one would be worse than teaching none.
+ const { shortcuts, isMac } = useShortcuts();
// The camera lane borrows the Layout pane's "No Webcam" wording when there is no
// camera to grow, so the two surfaces say the same thing about the same project.
const ts = useScopedT("settings");
@@ -1660,6 +1665,9 @@ export function V4Timeline({
{t("audio.addVoiceoverHint")}
</span>
</span>
+ <kbd className={styles.recMenuKey}>
+ {formatBinding(shortcuts.addVoiceover, isMac)}
+ </kbd>
</button>
<button
type="button"
@@ -1676,6 +1684,9 @@ export function V4Timeline({
{t("audio.importFileHint")}
</span>
</span>
+ <kbd className={styles.recMenuKey}>
+ {formatBinding(shortcuts.addAudio, isMac)}
+ </kbd>
</button>
</div>
</PopoverContent>
diff --git a/src/components/ai-edition/v4/V4Timeline.waveform.test.tsx b/src/components/ai-edition/v4/V4Timeline.waveform.test.tsx
index 64def94c6..c29f5f84d 100644
--- a/src/components/ai-edition/v4/V4Timeline.waveform.test.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.waveform.test.tsx
@@ -54,6 +54,7 @@ vi.mock("@/lib/ai-edition/store/useEditorSettings", () => ({
}),
}));
+import { ShortcutsProvider } from "@/contexts/ShortcutsContext";
import type { useTimeline } from "@/lib/ai-edition/store/useTimeline";
import { V4Timeline } from "./V4Timeline";
@@ -125,17 +126,19 @@ function renderBars(atGainDb: number): string[] {
addZoom: vi.fn(async () => undefined),
};
const view = render(
- <V4Timeline
- tl={tl as unknown as ReturnType<typeof useTimeline>}
- videoSources={[{ id: "a1", src: "file:///tmp/rec.mp4", label: "rec" }]}
- setCurrentTime={vi.fn()}
- playing={false}
- onTogglePlay={vi.fn()}
- onPrevClip={vi.fn()}
- onNextClip={vi.fn()}
- onEditClip={vi.fn()}
- onAddVoiceover={vi.fn()}
- />,
+ <ShortcutsProvider>
+ <V4Timeline
+ tl={tl as unknown as ReturnType<typeof useTimeline>}
+ videoSources={[{ id: "a1", src: "file:///tmp/rec.mp4", label: "rec" }]}
+ setCurrentTime={vi.fn()}
+ playing={false}
+ onTogglePlay={vi.fn()}
+ onPrevClip={vi.fn()}
+ onNextClip={vi.fn()}
+ onEditClip={vi.fn()}
+ onAddVoiceover={vi.fn()}
+ />
+ </ShortcutsProvider>,
);
const bars = Array.from(
document.querySelectorAll<HTMLElement>('[class*="tlWave"] span'),
diff --git a/src/components/ui/popover.tsx b/src/components/ui/popover.tsx
index 9341c8d3e..807481b6d 100644
--- a/src/components/ui/popover.tsx
+++ b/src/components/ui/popover.tsx
@@ -9,9 +9,18 @@ function Popover({ ...props }: React.ComponentProps<typeof PopoverPrimitive.Root
return <PopoverPrimitive.Root data-slot="popover" {...props} />;
}
-function PopoverTrigger({ ...props }: React.ComponentProps<typeof PopoverPrimitive.Trigger>) {
- return <PopoverPrimitive.Trigger data-slot="popover-trigger" {...props} />;
-}
+// forwardRef, like the Dialog parts: on React 18 a plain function component
+// cannot receive a ref, so anything that wraps this trigger with its own
+// `asChild` — a Tooltip around a popover button, say — fails to anchor and
+// warns. The primitive underneath has always forwarded; only this wrapper
+// swallowed it.
+const PopoverTrigger = React.forwardRef<
+ React.ComponentRef<typeof PopoverPrimitive.Trigger>,
+ React.ComponentProps<typeof PopoverPrimitive.Trigger>
+>(({ ...props }, ref) => (
+ <PopoverPrimitive.Trigger ref={ref} data-slot="popover-trigger" {...props} />
+));
+PopoverTrigger.displayName = "PopoverTrigger";
function PopoverContent({
className,
diff --git a/src/components/ui/tooltip.tsx b/src/components/ui/tooltip.tsx
index c5dfc12b5..823b3f9b5 100644
--- a/src/components/ui/tooltip.tsx
+++ b/src/components/ui/tooltip.tsx
@@ -46,25 +46,27 @@ function TooltipContent({
);
}
-function Tooltip({
- children,
- content,
- side,
- className,
-}: {
- children: React.ReactNode;
- content: React.ReactNode;
- side?: "top" | "right" | "bottom" | "left";
- className?: string;
-}) {
- return (
- <TooltipRoot>
- <TooltipTrigger asChild>{children}</TooltipTrigger>
- <TooltipContent side={side} className={className}>
- {content}
- </TooltipContent>
- </TooltipRoot>
- );
-}
+// forwardRef for the same reason as PopoverTrigger above: this is a convenience
+// wrapper people nest inside other `asChild` triggers, and on React 18 a plain
+// function component silently drops the ref it is handed.
+const Tooltip = React.forwardRef<
+ React.ComponentRef<typeof TooltipTrigger>,
+ {
+ children: React.ReactNode;
+ content: React.ReactNode;
+ side?: "top" | "right" | "bottom" | "left";
+ className?: string;
+ }
+>(({ children, content, side, className }, ref) => (
+ <TooltipRoot>
+ <TooltipTrigger ref={ref} asChild>
+ {children}
+ </TooltipTrigger>
+ <TooltipContent side={side} className={className}>
+ {content}
+ </TooltipContent>
+ </TooltipRoot>
+));
+Tooltip.displayName = "Tooltip";
export { Tooltip, TooltipContent, TooltipProvider, TooltipRoot, TooltipTrigger };
diff --git a/src/contexts/ShortcutsContext.tsx b/src/contexts/ShortcutsContext.tsx
index 91bd7f8d3..6f5e13731 100644
--- a/src/contexts/ShortcutsContext.tsx
+++ b/src/contexts/ShortcutsContext.tsx
@@ -39,9 +39,14 @@ export function ShortcutsProvider({ children }: { children: ReactNode }) {
useEffect(() => {
setIsMac(getIsMac());
+ // Guard `electronAPI` itself, not just the method on it — that is what the
+ // note above is after. Without preload (browser mode, and any test that
+ // renders a consumer) the bare property read threw and took the whole
+ // subtree with it, rather than falling back to the defaults already in
+ // state.
window.electronAPI
- .getShortcuts?.()
- .then((saved) => {
+ ?.getShortcuts?.()
+ ?.then((saved) => {
if (saved) {
setShortcuts(mergeWithDefaults(saved as Partial<ShortcutsConfig>));
}
@@ -54,9 +59,9 @@ export function ShortcutsProvider({ children }: { children: ReactNode }) {
const persistShortcuts = useCallback(
async (config?: ShortcutsConfig) => {
const configToSave = config ?? shortcuts;
- await window.electronAPI.saveShortcuts?.(configToSave);
+ await window.electronAPI?.saveShortcuts?.(configToSave);
- const result = await window.electronAPI.updateGlobalShortcut?.(configToSave.openApp);
+ const result = await window.electronAPI?.updateGlobalShortcut?.(configToSave.openApp);
return result ? result.success : true;
},
[shortcuts],