Skip to content

Commit 66cbabc

Browse files
fix(studio): pressing Shift no longer re-renders the timeline (heygen-com#7298)
* fix(studio): pressing Shift no longer re-renders the timeline * fix(studio): format the Shift test and drop the hook's comment
1 parent e2bb7da commit 66cbabc

6 files changed

Lines changed: 52 additions & 19 deletions

‎packages/studio/src/player/components/timelineProviderStateBuilders.test.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ function inputs(overrides: Partial<TimelineMetaBuilderInputs> = {}): TimelineMet
1717
"data-timeline-element-count": 2,
1818
isDragOver: false,
1919
activeTool: "select",
20-
shiftHeld: false,
2120
accentClass: "ring",
2221
onMouseMove: vi.fn(),
2322
onMouseLeave: vi.fn(),
@@ -61,11 +60,9 @@ describe("buildTimelineMeta", () => {
6160
expect(meta.containerProps.className).toContain("cursor-crosshair");
6261
});
6362

64-
it("uses the shift branch for the container class", () => {
65-
const meta = buildTimelineMeta(
66-
inputs({ container: { ...inputs().container, shiftHeld: true } }),
67-
);
68-
expect(meta.containerProps.className).toContain("cursor-crosshair");
63+
it("leaves Shift's cursor to the container's data attribute", () => {
64+
const meta = buildTimelineMeta(inputs());
65+
expect(meta.containerProps.className).toContain("data-[shift-held]:cursor-crosshair");
6966
});
7067

7168
it("uses the default container class without an accent", () => {

‎packages/studio/src/player/components/timelineProviderStateBuilders.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export function shouldIgnoreTimelinePointerDown(target: EventTarget | null): boo
4343
type ContainerInputs = Omit<TimelineContainerProps, "className"> & {
4444
isDragOver: boolean;
4545
activeTool: string;
46-
shiftHeld: boolean;
4746
accentClass: string;
4847
};
4948

@@ -77,7 +76,7 @@ export function buildTimelineMeta({
7776
razorGuide,
7877
}: TimelineMetaBuilderInputs): TimelineMeta {
7978
const { labelMode, zoomMode, contentOrigin, ...viewportProps } = viewport;
80-
const containerClassName = `relative border-t select-none h-full overflow-hidden ${container.isDragOver ? container.accentClass : ""} ${container.activeTool === "razor" ? "cursor-crosshair" : container.shiftHeld ? "cursor-crosshair" : "cursor-default"}`;
79+
const containerClassName = `relative border-t select-none h-full overflow-hidden ${container.isDragOver ? container.accentClass : ""} ${container.activeTool === "razor" ? "cursor-crosshair" : "cursor-default data-[shift-held]:cursor-crosshair"}`;
8180
const viewportClassName = `${zoomMode === "fit" ? "overflow-x-hidden" : "overflow-x-auto"} overflow-y-auto h-full outline-hidden`;
8281
return {
8382
emptyState,

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export interface TimelineShellMetaInputs {
2929
focusProps: Pick<TimelineViewportProps, "onFocus" | "onBlur">;
3030
elementCount: number;
3131
activeTool: string;
32-
shiftHeld: boolean;
3332
labelMode: boolean;
3433
contentOrigin: number;
3534
zoomMode: "fit" | "manual";
@@ -56,7 +55,6 @@ export function buildTimelineShellMeta(input: TimelineShellMetaInputs): Timeline
5655
"data-timeline-element-count": input.elementCount,
5756
isDragOver: input.isDragOver,
5857
activeTool,
59-
shiftHeld: input.shiftHeld,
6058
accentClass: "ring-1 ring-inset ring-studio-accent/60",
6159
onMouseMove: razor.updateRazorGuide,
6260
onMouseLeave: razor.clearRazorGuide,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function useTimelineProviderState({
138138
const activeTool = usePlayerStore((s) => s.activeTool);
139139
const [hoveredClip, setHoveredClip] = useState<string | null>(null);
140140
const isDragging = useRef(false);
141-
const shiftHeld = useTimelineShiftModifier();
141+
useTimelineShiftModifier(containerRef);
142142
const [showPopover, setShowPopover] = useState(false);
143143
const [kfContextMenu, setKfContextMenu] = useState<KeyframeDiamondContextMenuState | null>(null);
144144
const [clipContextMenu, setClipContextMenu] = useState<ClipContextMenuState | null>(null);
@@ -564,7 +564,6 @@ export function useTimelineProviderState({
564564
focusProps: timelineFocus.timelineFocusProps,
565565
elementCount: timelineElements.length,
566566
activeTool,
567-
shiftHeld,
568567
labelMode,
569568
contentOrigin,
570569
zoomMode,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// @vitest-environment jsdom
2+
import { act, createElement, Profiler, useRef } from "react";
3+
import { createRoot } from "react-dom/client";
4+
import { expect, it } from "vitest";
5+
import { useTimelineShiftModifier } from "./useTimelineShiftModifier";
6+
7+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
8+
9+
function Timeline() {
10+
const container = useRef<HTMLDivElement>(null);
11+
useTimelineShiftModifier(container);
12+
return createElement("div", { ref: container, "data-testid": "timeline" });
13+
}
14+
15+
const key = (type: string, name: string) =>
16+
act(() => void window.dispatchEvent(new KeyboardEvent(type, { key: name })));
17+
18+
it("Shift marks the timeline for its cursor without rendering it", () => {
19+
const host = document.createElement("div");
20+
const root = createRoot(host);
21+
let commits = 0;
22+
act(() =>
23+
root.render(
24+
createElement(Profiler, { id: "t", onRender: () => (commits += 1) }, createElement(Timeline)),
25+
),
26+
);
27+
const timeline = host.querySelector("[data-testid=timeline]")!;
28+
const start = commits;
29+
key("keydown", "Shift");
30+
expect(timeline.hasAttribute("data-shift-held")).toBe(true);
31+
key("keydown", "a");
32+
expect(timeline.hasAttribute("data-shift-held")).toBe(true);
33+
key("keyup", "Shift");
34+
expect(timeline.hasAttribute("data-shift-held")).toBe(false);
35+
key("keydown", "Shift");
36+
act(() => void window.dispatchEvent(new Event("blur")));
37+
expect(timeline.hasAttribute("data-shift-held")).toBe(false);
38+
expect(commits).toBe(start);
39+
act(() => root.unmount());
40+
});
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { useState } from "react";
1+
import type { RefObject } from "react";
22
import { useMountEffect } from "../../hooks/useMountEffect";
33

4-
export function useTimelineShiftModifier(): boolean {
5-
const [shiftHeld, setShiftHeld] = useState(false);
4+
export function useTimelineShiftModifier(container: RefObject<HTMLElement | null>): void {
65
useMountEffect(() => {
7-
const handleKey = (event: KeyboardEvent) =>
8-
event.key === "Shift" && setShiftHeld(event.type === "keydown");
9-
const handleBlur = () => setShiftHeld(false);
6+
const hold = (held: boolean) => container.current?.toggleAttribute("data-shift-held", held);
7+
const handleKey = (event: KeyboardEvent) => {
8+
if (event.key === "Shift") hold(event.type === "keydown");
9+
};
10+
const handleBlur = () => hold(false);
1011
window.addEventListener("keydown", handleKey);
1112
window.addEventListener("keyup", handleKey);
1213
window.addEventListener("blur", handleBlur);
@@ -16,5 +17,4 @@ export function useTimelineShiftModifier(): boolean {
1617
window.removeEventListener("blur", handleBlur);
1718
};
1819
});
19-
return shiftHeld;
2020
}

0 commit comments

Comments
 (0)