Skip to content

Commit ba0d640

Browse files
committed
fix(studio): scope lane ids per timeline and stop inventing a track row
Two latent defects in the announcement path this branch adds. - lanesId was keyed by render row alone, so a second TimelineLanes on the page (a mini-timeline beside the main one) would mint the same timeline-lanes-track-0 and every caret's aria-controls would resolve to whichever instance mounted first. The prefix now comes from useId, with the colons stripped so the id stays a legal CSS selector. - trackDisplayNumber returned trackOrder.length + 1 for a key it could not find, which is indistinguishable from a real row: the label announced a row the user can see is wrong and nothing upstream could tell it had guessed. It returns null now, and trackDisplaySuffix drops the number from the label rather than inventing one.
1 parent 477916c commit ba0d640

6 files changed

Lines changed: 106 additions & 14 deletions

File tree

‎packages/studio/src/hooks/timelineTrackVisibility.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { useCallback } from "react";
22
import { usePlayerStore, type TimelineElement } from "../player";
33
import { useExpandedTimelineElements } from "../player/hooks/useExpandedTimelineElements";
4-
import { timelineTrackOrder, trackDisplayNumber } from "../player/components/timelineTrackDisplay";
4+
import {
5+
timelineTrackOrder,
6+
trackDisplayNumber,
7+
trackDisplaySuffix,
8+
} from "../player/components/timelineTrackDisplay";
59
import { saveProjectFilesWithHistory } from "../utils/studioFileHistory";
610
import { readTagSnippetByTarget, type PatchOperation } from "../utils/sourcePatcher";
711
import {
@@ -211,13 +215,15 @@ export async function toggleTimelineTrackHidden({
211215
}: ToggleTimelineTrackHiddenInput): Promise<string[]> {
212216
// `track` is the fractional sort key the callback needs; the history entry is
213217
// read by a human, so it gets the display row instead.
214-
const displayNumber = trackDisplayNumber(timelineTrackOrder(timelineElements), track);
218+
const suffix = trackDisplaySuffix(
219+
trackDisplayNumber(timelineTrackOrder(timelineElements), track),
220+
);
215221
return setElementsHidden({
216222
projectId,
217223
activeCompPath,
218224
elements: timelineElements.filter((element) => element.track === track),
219225
hidden,
220-
label: hidden ? `Hide track ${displayNumber}` : `Show track ${displayNumber}`,
226+
label: hidden ? `Hide track${suffix}` : `Show track${suffix}`,
221227
previewIframe,
222228
writeProjectFile,
223229
recordEdit,

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,30 @@ describe("TimelineLanes disclosure target", () => {
215215
act(() => view.root.unmount());
216216
});
217217

218+
// Two timelines on one page (a mini-timeline in a modal beside the main one)
219+
// both minted `timeline-lanes-track-0`, so every caret's aria-controls
220+
// resolved to whichever instance mounted first.
221+
it("mints lane ids that do not collide with a second TimelineLanes on the page", () => {
222+
const first = renderLanes({ animations: ANIMATIONS, expandedClipIds: ["clip-a"] });
223+
const second = renderLanes({ animations: ANIMATIONS, expandedClipIds: ["clip-a"] });
224+
225+
const idsFor = (host: HTMLElement) =>
226+
Array.from(host.querySelectorAll("button[aria-controls]")).map((caret) =>
227+
caret.getAttribute("aria-controls"),
228+
);
229+
const firstIds = idsFor(first.host);
230+
const secondIds = idsFor(second.host);
231+
232+
expect(firstIds.length).toBeGreaterThan(0);
233+
expect(firstIds.some((id) => secondIds.includes(id))).toBe(false);
234+
// Still a legal CSS id selector: the aria-controls lookups above use `#id`.
235+
for (const id of [...firstIds, ...secondIds]) {
236+
expect(id).toMatch(/^[A-Za-z][\w-]*$/);
237+
}
238+
act(() => first.root.unmount());
239+
act(() => second.root.unmount());
240+
});
241+
218242
// The passenger branch wraps [clip, lanes] in a transformed div that re-renders
219243
// on every pointer move. An unstable key there remounts the lanes and drops the
220244
// in-flight drag.

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { useId } from "react";
12
import { BeatStrip, BeatBackgroundLines } from "./BeatStrip";
23
import { TimelineClip } from "./TimelineClip";
34
import { TimelineClipDiamonds } from "./TimelineClipDiamonds";
45
import { TimelinePropertyLanes } from "./TimelinePropertyLanes";
56
import { TimelineTrackHeader } from "./TimelineTrackHeader";
67
import { resolveTrackKeyframeClip } from "./useTimelineTrackLayout";
7-
import { trackDisplayNumber } from "./timelineTrackDisplay";
8+
import { trackDisplayNumber, trackDisplaySuffix } from "./timelineTrackDisplay";
89
import { clipTimingStart } from "../../hooks/gsapShared";
910
import { getTimelineEditCapabilities, resolveBlockedTimelineEditIntent } from "./timelineEditing";
1011
import { CLIP_Y, CLIP_HANDLE_W, TRACK_H, getTimelineRowHeight } from "./timelineLayout";
@@ -89,6 +90,12 @@ export function TimelineLanes({
8990
onRazorSplit,
9091
onRazorSplitAll,
9192
}: TimelineLanesProps) {
93+
// Per-INSTANCE, so two timelines on one page (a mini-timeline in a modal
94+
// beside the main one) cannot both mint `...-track-0` and have every caret's
95+
// aria-controls resolve to whichever mounted first. React's useId embeds
96+
// colons, which are legal in an id and in aria-controls but need escaping in
97+
// a CSS `#id` selector, so they come out here and the prefix stays plain.
98+
const lanesIdPrefix = `timeline-lanes${useId().replaceAll(":", "")}`;
9299
const expandedClipIds = usePlayerStore((s) => s.expandedClipIds);
93100
const toggleClipExpanded = usePlayerStore((s) => s.toggleClipExpanded);
94101
const toggleClipExpandedTracked = (key: string) => {
@@ -137,7 +144,7 @@ export function TimelineLanes({
137144
// the disclosure: the caret in the sticky header and the diamond lanes
138145
// on the canvas. Keyed by display row, not by `trackNum`, which is a
139146
// fractional sort key and would mint ids like `...-0.16666666666666666`.
140-
const lanesId = `timeline-lanes-track-${row}`;
147+
const lanesId = `${lanesIdPrefix}-track-${row}`;
141148
return (
142149
<div
143150
key={trackNum}
@@ -154,7 +161,10 @@ export function TimelineLanes({
154161
// key, so it stays out of every label and in every callback.
155162
trackDisplayNumber={displayNumber}
156163
trackLabel={
157-
els[0]?.label ?? els[0]?.domId ?? els[0]?.id ?? `Track ${displayNumber}`
164+
els[0]?.label ??
165+
els[0]?.domId ??
166+
els[0]?.id ??
167+
`Track${trackDisplaySuffix(displayNumber)}`
158168
}
159169
lanesId={lanesId}
160170
contentOrigin={contentOrigin}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ import {
1515
type TimelinePropertyLane,
1616
} from "./trackHeaderLaneState";
1717
import { valueReadout } from "./trackHeaderLaneValues";
18+
import { trackDisplaySuffix } from "./timelineTrackDisplay";
1819

1920
interface TimelineTrackHeaderProps {
2021
/** The track's real key: a FRACTIONAL z-order sort value. Routes callbacks;
2122
* never shown or announced. */
2223
trackNumber: number;
2324
/** The track's 1-based position in the rendered order: the only number safe
2425
* to put in a label. Announcing `trackNumber` read out "track
25-
* 0.16666666666666666". */
26-
trackDisplayNumber: number;
26+
* 0.16666666666666666". Null when the key has no row, which drops the number
27+
* from the label rather than inventing one (see trackDisplayNumber). */
28+
trackDisplayNumber: number | null;
2729
trackLabel: string;
2830
/** Id of the canvas-side lanes element the disclosure caret expands. Minted by
2931
* TimelineLanes, which is the one place that sees both subtrees. */
@@ -55,14 +57,15 @@ function VisibilityButton({
5557
}: {
5658
hidden: boolean;
5759
trackNumber: number;
58-
trackDisplayNumber: number;
60+
trackDisplayNumber: number | null;
5961
visible: boolean;
6062
onToggle: TimelineEditCallbacks["onToggleTrackHidden"];
6163
}) {
6264
if (!visible) return <span aria-hidden="true" className="h-6 w-6 shrink-0" />;
6365
// Display number in the text, real key in the callback. The two must not be
6466
// conflated in either direction.
65-
const label = hidden ? `Show track ${trackDisplayNumber}` : `Hide track ${trackDisplayNumber}`;
67+
const suffix = trackDisplaySuffix(trackDisplayNumber);
68+
const label = hidden ? `Show track${suffix}` : `Hide track${suffix}`;
6669
return (
6770
<button
6871
type="button"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, expect, it } from "vitest";
2+
import { timelineTrackOrder, trackDisplayNumber, trackDisplaySuffix } from "./timelineTrackDisplay";
3+
4+
describe("timelineTrackOrder", () => {
5+
it("is the ascending distinct key order, fractional sub-comp keys included", () => {
6+
const elements = [{ track: 1 }, { track: 0 }, { track: 1 / 6 }, { track: 1 }];
7+
8+
expect(timelineTrackOrder(elements)).toEqual([0, 1 / 6, 1]);
9+
});
10+
});
11+
12+
describe("trackDisplayNumber", () => {
13+
it("is the key's 1-based row, not the fractional key itself", () => {
14+
const order = timelineTrackOrder([{ track: 0 }, { track: 1 / 6 }, { track: 1 }]);
15+
16+
expect(trackDisplayNumber(order, 1 / 6)).toBe(2);
17+
expect(trackDisplayNumber(order, 1)).toBe(3);
18+
});
19+
20+
it("is null for a key with no row rather than a plausible-looking one", () => {
21+
// The old end-row fallback announced "track 4" for a track the user cannot
22+
// see at row 4, and nothing upstream could tell that apart from a real row.
23+
expect(trackDisplayNumber([0, 1, 2], 7)).toBeNull();
24+
});
25+
});
26+
27+
describe("trackDisplaySuffix", () => {
28+
it("names the row when there is one", () => {
29+
expect(`Hide track${trackDisplaySuffix(3)}`).toBe("Hide track 3");
30+
});
31+
32+
it("drops the number entirely when there is no row", () => {
33+
expect(`Hide track${trackDisplaySuffix(null)}`).toBe("Hide track");
34+
});
35+
});

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,24 @@ export function timelineTrackOrder(elements: readonly { track: number }[]): numb
1515
}
1616

1717
/**
18-
* A track key's 1-based display row. A key not in `trackOrder` (a drag preview
19-
* onto a brand-new track, say) reads as the row it would land on at the end.
18+
* A track key's 1-based display row, or null when the key is not in
19+
* `trackOrder` at all.
20+
*
21+
* Both callers build `trackOrder` from the same elements the key came from, so
22+
* null is unreachable by construction today. It is null rather than a number
23+
* because the only numbers available to return (the end row, the last row) are
24+
* indistinguishable from a real answer: a label would announce a row the user
25+
* can see is wrong, and nothing upstream would ever learn it had guessed.
2026
*/
21-
export function trackDisplayNumber(trackOrder: readonly number[], track: number): number {
27+
export function trackDisplayNumber(trackOrder: readonly number[], track: number): number | null {
2228
const row = trackOrder.indexOf(track);
23-
return row < 0 ? trackOrder.length + 1 : row + 1;
29+
return row < 0 ? null : row + 1;
30+
}
31+
32+
/**
33+
* The `" 3"` in `Hide track 3`, empty when there is no display row to name.
34+
* Announcing "Hide track" is thin; announcing an invented row is wrong.
35+
*/
36+
export function trackDisplaySuffix(displayNumber: number | null): string {
37+
return displayNumber === null ? "" : ` ${displayNumber}`;
2438
}

0 commit comments

Comments
 (0)