Skip to content

React

import { useRef } from "react";
import { TICKS_PER_SECOND as hz, shallowEqual } from "@lumen-timeline/core";
import {
Timeline,
TimelineProvider,
useEditorSelector,
useTimelineContext,
useTransportSelector,
} from "@lumen-timeline/react";
import "@lumen-timeline/timeline/style.css";
function TakeInspector() {
const { editor } = useTimelineContext();
const clip = useEditorSelector(
editor,
({ snapshot, selection }) => {
const found = snapshot.clips.find((c) => c.id === selection.at(-1));
return found ? { id: found.id, name: found.name, take: found.activeTakeId } : null;
},
shallowEqual,
);
return <aside>{clip ? `${clip.name}: ${clip.take}` : "No clip selected"}</aside>;
}
function Clock() {
const { transport } = useTimelineContext();
const tenths = useTransportSelector(transport, (s) => Math.floor(s.presentedTick / (hz / 10)));
return <output>{(tenths / 10).toFixed(1)} s</output>;
}
export function Workspace({ editor, transport, tiles }) {
const view = useRef(null);
return (
<TimelineProvider editor={editor} transport={transport}>
<Clock />
<Timeline
editor={editor}
transport={transport}
waveforms={tiles}
snap={{ gridTicks: hz / 10 }}
ref={view}
/>
<TakeInspector />
</TimelineProvider>
);
}
  • <Timeline {...options}> renders a div and mounts the view in it from a layout effect. It takes every mountTimeline option as a prop, with className and style for the div. ref receives the TimelineView, for reveal, setViewport or hitTest, and null once it is disposed.
  • useEditorSelector(editor, select, isEqual) returns a field of the editor’s state through useSyncExternalStore. The selector may be written inline. A new selection equal to the one on screen keeps that one’s identity, so the component renders only when isEqual finds a change.
  • useTransportSelector(transport, select, isEqual) does the same for any source with snapshot() and subscribe(). A transport notifies on every frame during playback, so select no finer a field than the panel shows.
  • <TimelineProvider editor transport> and useTimelineContext() hand the host’s editor and transport to panels below it. useTimelineContext throws without a provider.
  • Under StrictMode in development, React mounts each new component’s effects twice. The first view is disposed at once, before anything draws.