fix(studio): pressing Shift no longer re-renders the timeline (#7298)

* fix(studio): pressing Shift no longer re-renders the timeline

* fix(studio): format the Shift test and drop the hook's comment
This commit is contained in:
Miguel Ángel
2026-11-06 06:26:11 +00:00
committed by GitHub
parent e2bb7da25d
commit 66cbabc39a
6 changed files with 52 additions and 19 deletions
@@ -17,7 +17,6 @@ function inputs(overrides: Partial<TimelineMetaBuilderInputs> = {}): TimelineMet
"data-timeline-element-count": 2,
isDragOver: false,
activeTool: "select",
shiftHeld: false,
accentClass: "ring",
onMouseMove: vi.fn(),
onMouseLeave: vi.fn(),
@@ -61,11 +60,9 @@ describe("buildTimelineMeta", () => {
expect(meta.containerProps.className).toContain("cursor-crosshair");
});
it("uses the shift branch for the container class", () => {
const meta = buildTimelineMeta(
inputs({ container: { ...inputs().container, shiftHeld: true } }),
);
expect(meta.containerProps.className).toContain("cursor-crosshair");
it("leaves Shift's cursor to the container's data attribute", () => {
const meta = buildTimelineMeta(inputs());
expect(meta.containerProps.className).toContain("data-[shift-held]:cursor-crosshair");
});
it("uses the default container class without an accent", () => {
@@ -43,7 +43,6 @@ export function shouldIgnoreTimelinePointerDown(target: EventTarget | null): boo
type ContainerInputs = Omit<TimelineContainerProps, "className"> & {
isDragOver: boolean;
activeTool: string;
shiftHeld: boolean;
accentClass: string;
};
@@ -77,7 +76,7 @@ export function buildTimelineMeta({
razorGuide,
}: TimelineMetaBuilderInputs): TimelineMeta {
const { labelMode, zoomMode, contentOrigin, ...viewportProps } = viewport;
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"}`;
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"}`;
const viewportClassName = `${zoomMode === "fit" ? "overflow-x-hidden" : "overflow-x-auto"} overflow-y-auto h-full outline-hidden`;
return {
emptyState,
@@ -29,7 +29,6 @@ export interface TimelineShellMetaInputs {
focusProps: Pick<TimelineViewportProps, "onFocus" | "onBlur">;
elementCount: number;
activeTool: string;
shiftHeld: boolean;
labelMode: boolean;
contentOrigin: number;
zoomMode: "fit" | "manual";
@@ -56,7 +55,6 @@ export function buildTimelineShellMeta(input: TimelineShellMetaInputs): Timeline
"data-timeline-element-count": input.elementCount,
isDragOver: input.isDragOver,
activeTool,
shiftHeld: input.shiftHeld,
accentClass: "ring-1 ring-inset ring-studio-accent/60",
onMouseMove: razor.updateRazorGuide,
onMouseLeave: razor.clearRazorGuide,
@@ -138,7 +138,7 @@ export function useTimelineProviderState({
const activeTool = usePlayerStore((s) => s.activeTool);
const [hoveredClip, setHoveredClip] = useState<string | null>(null);
const isDragging = useRef(false);
const shiftHeld = useTimelineShiftModifier();
useTimelineShiftModifier(containerRef);
const [showPopover, setShowPopover] = useState(false);
const [kfContextMenu, setKfContextMenu] = useState<KeyframeDiamondContextMenuState | null>(null);
const [clipContextMenu, setClipContextMenu] = useState<ClipContextMenuState | null>(null);
@@ -564,7 +564,6 @@ export function useTimelineProviderState({
focusProps: timelineFocus.timelineFocusProps,
elementCount: timelineElements.length,
activeTool,
shiftHeld,
labelMode,
contentOrigin,
zoomMode,
@@ -0,0 +1,40 @@
// @vitest-environment jsdom
import { act, createElement, Profiler, useRef } from "react";
import { createRoot } from "react-dom/client";
import { expect, it } from "vitest";
import { useTimelineShiftModifier } from "./useTimelineShiftModifier";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
function Timeline() {
const container = useRef<HTMLDivElement>(null);
useTimelineShiftModifier(container);
return createElement("div", { ref: container, "data-testid": "timeline" });
}
const key = (type: string, name: string) =>
act(() => void window.dispatchEvent(new KeyboardEvent(type, { key: name })));
it("Shift marks the timeline for its cursor without rendering it", () => {
const host = document.createElement("div");
const root = createRoot(host);
let commits = 0;
act(() =>
root.render(
createElement(Profiler, { id: "t", onRender: () => (commits += 1) }, createElement(Timeline)),
),
);
const timeline = host.querySelector("[data-testid=timeline]")!;
const start = commits;
key("keydown", "Shift");
expect(timeline.hasAttribute("data-shift-held")).toBe(true);
key("keydown", "a");
expect(timeline.hasAttribute("data-shift-held")).toBe(true);
key("keyup", "Shift");
expect(timeline.hasAttribute("data-shift-held")).toBe(false);
key("keydown", "Shift");
act(() => void window.dispatchEvent(new Event("blur")));
expect(timeline.hasAttribute("data-shift-held")).toBe(false);
expect(commits).toBe(start);
act(() => root.unmount());
});
@@ -1,12 +1,13 @@
import { useState } from "react";
import type { RefObject } from "react";
import { useMountEffect } from "../../hooks/useMountEffect";
export function useTimelineShiftModifier(): boolean {
const [shiftHeld, setShiftHeld] = useState(false);
export function useTimelineShiftModifier(container: RefObject<HTMLElement | null>): void {
useMountEffect(() => {
const handleKey = (event: KeyboardEvent) =>
event.key === "Shift" && setShiftHeld(event.type === "keydown");
const handleBlur = () => setShiftHeld(false);
const hold = (held: boolean) => container.current?.toggleAttribute("data-shift-held", held);
const handleKey = (event: KeyboardEvent) => {
if (event.key === "Shift") hold(event.type === "keydown");
};
const handleBlur = () => hold(false);
window.addEventListener("keydown", handleKey);
window.addEventListener("keyup", handleKey);
window.addEventListener("blur", handleBlur);
@@ -16,5 +17,4 @@ export function useTimelineShiftModifier(): boolean {
window.removeEventListener("blur", handleBlur);
};
});
return shiftHeld;
}