Skip to main content

Timeline usage

Component integration​

Import and use the timeline components in your React application.

For optimal performance and proper functionality, structure your components like this:

import type {PlayerRef} from '@remotion/player';
import {useRef} from 'react';
import {PreviewContainer} from './layout';
import {ActionRow} from './timeline/remotion-timeline/components/action-row';
import {Timeline, TimelineContainer} from './timeline/remotion-timeline/components/timeline';
import {TimelineProvider} from './timeline/remotion-timeline/context/provider';
import {TimelineSizeProvider} from './timeline/remotion-timeline/context/timeline-size-provider';
import {TimelineZoomProvider} from './timeline/remotion-timeline/context/timeline-zoom-provider';
import {useElementSize} from './timeline/remotion-timeline/hooks/use-element-size';
import {timelineContainerRef} from './timeline/remotion-timeline/utils/restore-scroll-after-zoom';
import {VideoPreview} from './timeline/video-preview';

export const App = () => {
  const playerRef = useRef<PlayerRef>(null);
  const timelineContainerSize = useElementSize(timelineContainerRef);
  const timelineContainerWidth = timelineContainerSize?.width;

  return (
    <TimelineProvider
      onChange={(newState) => {
        console.log('New timeline state:', newState);
      }}
      initialState={initialState}
    >
      <TimelineZoomProvider initialZoom={1}>
        <PreviewContainer>
          <VideoPreview loop playerRef={playerRef} />
          <ActionRow playerRef={playerRef} />
        </PreviewContainer>

        <TimelineContainer>
          {timelineContainerWidth ? (
            <TimelineSizeProvider containerWidth={timelineContainerWidth}>
              <Timeline playerRef={playerRef} />
            </TimelineSizeProvider>
          ) : null}
        </TimelineContainer>
      </TimelineZoomProvider>
    </TimelineProvider>
  );
};

This structure ensures:

  • Timeline state management through TimelineProvider
  • Zoom functionality with TimelineZoomProvider
  • Proper sizing calculations with TimelineSizeProvider
  • Responsive timeline container with TimelineContainer

Default state structure​

The project includes a predefined state structure for timeline tracks and items. You can check TimelineProvider component for the state structure.

Video preview​

This project includes a CanvasComposition, a Remotion composition that renders the timeline state in the Remotion Player.

You can:

  • Use the provided composition as-is for quick prototyping
  • Adapt it to your needs
  • Replace it with your own composition while keeping the timeline functionality

Check video-preview.tsx and App.tsx for implementation examples.

State persistence​

The TimelineProvider includes an onChange callback that fires when the tracks change (adding/removing tracks, moving items, etc.). Calls are debounced by 300 milliseconds. Use this to save the editor state to your backend:

function VideoEditor() {
  const saveToServer = async (state: {tracks: TrackType[]}) => {
    try {
      await fetch('/api/save-timeline', {
        method: 'POST',
        body: JSON.stringify(state),
      });
    } catch (error) {
      console.error('Failed to save timeline state:', error);
    }
  };

  return (
    <TimelineProvider onChange={saveToServer} initialState={initialState}>
      {/* Same children as in the example above */}
    </TimelineProvider>
  );
}

Example project structure​

In App.tsx, you can see the complete implementation of the timeline component.

The example project demonstrates:

  • Importing the timeline components
  • Using the ActionRow component to use timeline zoom and add clips
  • Using the Timeline component to render the timeline