Skip to content

Commit 1376895

Browse files
vanceingallsclaude
andcommitted
fix(studio): selecting a focused frame-wide audio clip keeps its focus
Arrow keys focus a clip without selecting it, and Enter then selects it. Under 24 px an audio clip only gained its peak tooltip once selected, so that selection swapped the focused button for a new one and focus fell to <body>, out of reach of the timeline's arrow keys. The tooltip now depends on the width tier alone; a frame-wide clip has no peak marks to describe. A zoom across the tier still swaps the button, so the clip hands focus from the old button to the new one. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent c713403 commit 1376895

3 files changed

Lines changed: 132 additions & 2 deletions

File tree

‎packages/studio/src/player/components/TimelineClip.test.tsx‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,85 @@ describe("TimelineClip", () => {
328328
expect(onClick).toHaveBeenCalledOnce();
329329
act(() => root.unmount());
330330
});
331+
332+
describe("keyboard focus on an audio clip", () => {
333+
const voice: TimelineElement = {
334+
id: "voice",
335+
label: "Voice",
336+
tag: "audio",
337+
start: 0,
338+
duration: 1,
339+
track: 0,
340+
};
341+
342+
function mountFocused(pps: number) {
343+
const host = document.createElement("div");
344+
document.body.append(host);
345+
const root = createRoot(host);
346+
const render = (next: { pps: number; isSelected: boolean }) =>
347+
act(() => {
348+
root.render(
349+
<TimelineClip
350+
el={voice}
351+
pps={next.pps}
352+
clipY={0}
353+
isSelected={next.isSelected}
354+
isHovered={false}
355+
hasCustomContent={false}
356+
capabilities={capabilities}
357+
isComposition={false}
358+
tabIndex={0}
359+
onHoverStart={vi.fn()}
360+
onHoverEnd={vi.fn()}
361+
onClick={vi.fn()}
362+
onDoubleClick={vi.fn()}
363+
/>,
364+
);
365+
});
366+
render({ pps, isSelected: false });
367+
const button = () => host.querySelector<HTMLButtonElement>(".timeline-clip")!;
368+
const focused = button();
369+
act(() => focused.focus());
370+
expect(document.activeElement).toBe(focused);
371+
return { root, render, button, focused };
372+
}
373+
374+
// Arrow keys focus without selecting, then Enter selects the focused clip.
375+
// Swapping the button there dropped focus to <body>, and arrows went dead.
376+
it("keeps the focused button when a frame-wide clip is selected", () => {
377+
const view = mountFocused(10);
378+
expect(view.focused.getAttribute("data-ladder")).toBe("frame");
379+
380+
view.render({ pps: 10, isSelected: true });
381+
382+
expect(view.button()).toBe(view.focused);
383+
expect(document.activeElement).toBe(view.focused);
384+
act(() => view.root.unmount());
385+
});
386+
387+
it("keeps focus on the clip when a zoom moves it across the frame tier", () => {
388+
const view = mountFocused(10);
389+
390+
view.render({ pps: 100, isSelected: false });
391+
expect(view.button().getAttribute("data-ladder")).toBe("labeled");
392+
expect(document.activeElement).toBe(view.button());
393+
394+
view.render({ pps: 10, isSelected: false });
395+
expect(view.button().getAttribute("data-ladder")).toBe("frame");
396+
expect(document.activeElement).toBe(view.button());
397+
act(() => view.root.unmount());
398+
});
399+
400+
it("leaves focus alone when the clip it moves across the tier was not focused", () => {
401+
const view = mountFocused(10);
402+
const elsewhere = document.createElement("button");
403+
document.body.append(elsewhere);
404+
act(() => elsewhere.focus());
405+
406+
view.render({ pps: 100, isSelected: false });
407+
408+
expect(document.activeElement).toBe(elsewhere);
409+
act(() => view.root.unmount());
410+
});
411+
});
331412
});

‎packages/studio/src/player/components/TimelineClip.tsx‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo, type CSSProperties, type ReactNode } from "react";
1+
import { memo, useCallback, useRef, type CSSProperties, type ReactNode } from "react";
22
import type { TimelineElement } from "../store/playerStore";
33
import {
44
CLIP_TRIM_HIT_PX,
@@ -60,6 +60,18 @@ export function clipZoomKey(el: TimelineElement, pps: number, interacting: boole
6060
return `${clipWidthLadder(widthPx)}${widthPx >= HANDLES_MIN_PX ? "+handles" : ""}`;
6161
}
6262

63+
function useFocusCarriedAcrossRemount() {
64+
const carry = useRef(false);
65+
return useCallback((node: HTMLButtonElement | null) => {
66+
if (!node) return;
67+
if (carry.current) node.focus({ preventScroll: true });
68+
carry.current = false;
69+
return () => {
70+
carry.current = node.ownerDocument.activeElement === node;
71+
};
72+
}, []);
73+
}
74+
6375
// fallow-ignore-next-line complexity
6476
export const TimelineClip = memo(function TimelineClip({
6577
el,
@@ -110,6 +122,7 @@ export const TimelineClip = memo(function TimelineClip({
110122
if (linkColor) Object.assign(themeVariables, { "--clip-link-color": linkColor });
111123
const hasFades = (isAudioClip || Boolean(el.hasAudio)) && !isGestureActor;
112124
const fade = useClipFadeDraft(el);
125+
const buttonRef = useFocusCarriedAcrossRemount();
113126
const badges =
114127
ladder === "labeled" && !isGestureActor ? (
115128
<ClipBadges el={el} onOpenMenu={onContextMenu} />
@@ -146,6 +159,7 @@ export const TimelineClip = memo(function TimelineClip({
146159

147160
const clip = (
148161
<button
162+
ref={buttonRef}
149163
type="button"
150164
data-clip={isGestureActor ? undefined : "true"}
151165
data-el-id={isGestureActor ? undefined : (el.key ?? el.id)}
@@ -273,6 +287,6 @@ export const TimelineClip = memo(function TimelineClip({
273287
)}
274288
</button>
275289
);
276-
const describesPeaks = (isAudioClip || el.hasAudio) && (ladder !== "frame" || isSelected);
290+
const describesPeaks = (isAudioClip || el.hasAudio) && ladder !== "frame";
277291
return describesPeaks ? <ClipPeakTooltip>{clip}</ClipPeakTooltip> : clip;
278292
});

‎packages/studio/src/player/components/TimelineLanes.test.tsx‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { defaultTimelineTheme } from "./timelineTheme";
1010
import { TRACK_H, getTimelineRowGeometry } from "./timelineLayout";
1111
import { createTimelineClipIndex } from "../lib/timelineClipIndex";
1212
import { buildTimelineLogicalRows } from "./timelineKeyboardNavigation";
13+
import { timelineClipFocusId } from "./timelineNavigationIdentity";
1314
import {
1415
usePlayerStore,
1516
type KeyframeCacheEntry,
@@ -545,6 +546,40 @@ describe("TimelineLanes selection", () => {
545546
});
546547
});
547548

549+
describe("TimelineLanes keyboard focus", () => {
550+
// Arrow keys focus a clip without selecting it; Enter then selects it. A
551+
// frame-wide audio clip used to swap its button on that selection, dropping
552+
// focus to <body> where the arrow keys no longer reach the timeline.
553+
it("keeps arrow navigation after Enter selects a focused frame-wide audio clip", () => {
554+
const voice = (id: string, start: number): TimelineElement => ({
555+
...element(id, TRACK_A),
556+
tag: "audio",
557+
start,
558+
});
559+
const elements = [voice("clip-a", 0), voice("clip-b", 3)];
560+
const view = renderLanes({ elements, pps: 5 });
561+
const clipA = view.host.querySelector<HTMLButtonElement>('[data-el-id="clip-a"]')!;
562+
expect(clipA.getAttribute("data-ladder")).toBe("frame");
563+
564+
act(() => clipA.focus());
565+
act(() => clipA.click()); // Enter on a focused native button
566+
expect(view.onSelectElement).toHaveBeenCalledWith(elements[0]);
567+
view.rerender({ elements, pps: 5, selectedElementIds: new Set(["clip-a"]) });
568+
569+
expect(view.host.querySelector('[data-el-id="clip-a"]')).toBe(clipA);
570+
expect(document.activeElement).toBe(clipA);
571+
const arrow = new KeyboardEvent("keydown", {
572+
bubbles: true,
573+
cancelable: true,
574+
key: "ArrowRight",
575+
});
576+
act(() => document.activeElement?.dispatchEvent(arrow));
577+
expect(arrow.defaultPrevented).toBe(true);
578+
expect(usePlayerStore.getState().timelineFocus?.id).toBe(timelineClipFocusId("clip-b"));
579+
act(() => view.root.unmount());
580+
});
581+
});
582+
548583
describe("TimelineLanes clip thumbnails", () => {
549584
it("keeps thumbnail content inside a selected clip", () => {
550585
const selected = element("clip-a", TRACK_A);

0 commit comments

Comments
 (0)