Skip to content

🎥 Make videos programmatically with React

Finish demo

Jonny Burgerd8b0c1341817
diff --git a/packages/docs/components/InlineStep.tsx b/packages/docs/components/InlineStep.tsx
index 02777cbe1e..088458c4b9 100644
--- a/packages/docs/components/InlineStep.tsx
+++ b/packages/docs/components/InlineStep.tsx
@@ -1,50 +1,80 @@
import React from 'react';
import {RED} from './layout/colors';
+export const TitleStep: React.FC<{
+ readonly children: React.ReactNode;
+ readonly error?: boolean;
+}> = ({children, error}) => {
+ return (
+ <div
+ style={{
+ backgroundColor: error ? RED : 'var(--ifm-color-primary)',
+ height: '1em',
+ width: '1em',
+ borderRadius: '50%',
+ padding: 6,
+ fontSize: '0.7em',
+ lineHeight: '1',
+ display: 'inline-flex',
+ color: 'white',
+ justifyContent: 'center',
+ alignItems: 'center',
+ fontWeight: 'bold',
+ fontFeatureSettings: '"ss03" on',
+ boxSizing: 'content-box',
+ marginRight: 8,
+ }}
+ >
+ {children}
+ </div>
+ );
+};
+
export const InlineStep: React.FC<{
- readonly children: React.ReactNode;
- readonly error?: boolean;
+ readonly children: React.ReactNode;
+ readonly error?: boolean;
}> = ({children, error}) => {
- return (
- <div
- style={{
- backgroundColor: error ? RED : 'var(--ifm-color-primary)',
- height: 24,
- width: 24,
- display: 'inline-flex',
- color: 'white',
- justifyContent: 'center',
- alignItems: 'center',
- borderRadius: 12,
- fontSize: 13,
- fontWeight: 'bold',
- verticalAlign: 'top',
- }}
- >
- {children}
- </div>
- );
+ return (
+ <div
+ style={{
+ backgroundColor: error ? RED : 'var(--ifm-color-primary)',
+ height: 24,
+ width: 24,
+ display: 'inline-flex',
+ color: 'white',
+ justifyContent: 'center',
+ alignItems: 'center',
+ borderRadius: 12,
+ fontSize: 13,
+ fontWeight: 'bold',
+ verticalAlign: 'top',
+ fontFeatureSettings: '"ss03" on',
+ }}
+ >
+ {children}
+ </div>
+ );
};
export const Step: React.FC<{
- readonly children: React.ReactNode;
- readonly error?: boolean;
+ readonly children: React.ReactNode;
+ readonly error?: boolean;
}> = ({children, error}) => {
- return (
- <a
- id={children.toString()}
- style={{
- marginRight: 7,
- display: 'inline-block',
- position: 'relative',
- marginTop: 4,
- marginBottom: 4,
- cursor: 'pointer',
- textDecoration: 'none',
- }}
- href={`#${children.toString()}`}
- >
- <InlineStep error={error}>{children}</InlineStep>{' '}
- </a>
- );
+ return (
+ <a
+ id={children.toString()}
+ style={{
+ marginRight: 7,
+ display: 'inline-block',
+ position: 'relative',
+ marginTop: 4,
+ marginBottom: 4,
+ cursor: 'pointer',
+ textDecoration: 'none',
+ }}
+ href={`#${children.toString()}`}
+ >
+ <InlineStep error={error}>{children}</InlineStep>{' '}
+ </a>
+ );
};
diff --git a/packages/docs/docs/player/drag-and-drop/Main.tsx b/packages/docs/docs/player/drag-and-drop/Main.tsx
index fc34bf1b66..c9d1b8473b 100644
--- a/packages/docs/docs/player/drag-and-drop/Main.tsx
+++ b/packages/docs/docs/player/drag-and-drop/Main.tsx
@@ -2,13 +2,13 @@ import React, {useCallback} from 'react';
import {AbsoluteFill} from 'remotion';
import type {Item} from './item';
import {Layer} from './Layer';
-import {SelectionOutlines} from './SelectionOutlines';
+import {SortedOutlines} from './SortedOutlines';
export type MainProps = {
- items: Item[];
- setSelectedItem: React.Dispatch<React.SetStateAction<number | null>>;
- selectedItem: number | null;
- changeItem: (itemId: number, updater: (item: Item) => Item) => void;
+ readonly items: Item[];
+ readonly setSelectedItem: React.Dispatch<React.SetStateAction<number | null>>;
+ readonly selectedItem: number | null;
+ readonly changeItem: (itemId: number, updater: (item: Item) => Item) => void;
};
const outer: React.CSSProperties = {
@@ -43,7 +43,7 @@ export const Main: React.FC<MainProps> = ({
return <Layer key={item.id} item={item} />;
})}
</AbsoluteFill>
- <SelectionOutlines
+ <SortedOutlines
selectedItem={selectedItem}
items={items}
setSelectedItem={setSelectedItem}
diff --git a/packages/docs/docs/player/drag-and-drop/ResizeHandle.tsx b/packages/docs/docs/player/drag-and-drop/ResizeHandle.tsx
index 176046c179..b31d9c1e01 100644
--- a/packages/docs/docs/player/drag-and-drop/ResizeHandle.tsx
+++ b/packages/docs/docs/player/drag-and-drop/ResizeHandle.tsx
@@ -5,12 +5,13 @@ import type {Item} from './item';
const HANDLE_SIZE = 8;
export const ResizeHandle: React.FC<{
- type: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
- setItem: (itemId: number, updater: (item: Item) => Item) => void;
- item: Item;
+ readonly type: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
+ readonly setItem: (itemId: number, updater: (item: Item) => Item) => void;
+ readonly item: Item;
}> = ({type, setItem, item}) => {
const scale = useCurrentScale();
const size = Math.round(HANDLE_SIZE / scale);
+ const borderSize = 1 / scale;
const sizeStyle: React.CSSProperties = useMemo(() => {
return {
@@ -18,11 +19,11 @@ export const ResizeHandle: React.FC<{
height: size,
width: size,
backgroundColor: 'white',
- border: '1px solid #0B84F3',
+ border: `${borderSize}px solid #0B84F3`,
};
- }, [size]);
+ }, [borderSize, size]);
- const margin = -size / 2 - 1 / scale;
+ const margin = -size / 2 - borderSize;
const style: React.CSSProperties = useMemo(() => {
if (type === 'top-left') {
@@ -71,6 +72,9 @@ export const ResizeHandle: React.FC<{
const onPointerDown = useCallback(
(e: React.MouseEvent) => {
e.stopPropagation();
+ if (e.button !== 0) {
+ return;
+ }
const initialX = e.clientX;
const initialY = e.clientY;
@@ -79,49 +83,22 @@ export const ResizeHandle: React.FC<{
const offsetX = (pointerMoveEvent.clientX - initialX) / scale;
const offsetY = (pointerMoveEvent.clientY - initialY) / scale;
+ const isLeft = type === 'top-left' || type === 'bottom-left';
+ const isTop = type === 'top-left' || type === 'top-right';
+
setItem(item.id, (i) => {
- const newWidth = Math.max(
- 1,
- Math.round(
- item.width +
- (type === 'bottom-left' || type === 'top-left'
- ? -offsetX
- : offsetX),
- ),
- );
- const newHeight = Math.max(
- 1,
- Math.round(
- item.height +
- (type === 'top-left' || type === 'top-right'
- ? -offsetY
- : offsetY),
- ),
- );
- const newLeft = Math.min(
- item.left + item.width - 1,
- Math.round(
- item.left +
- (type === 'bottom-left' || type === 'top-left' ? offsetX : 0),
- ),
- );
-
- const newTop = Math.min(
- item.top + item.height - 1,
- Math.round(
- item.top +
- (type === 'top-left' || type === 'top-right' ? offsetY : 0),
- ),
- );
-
- const updatedItem: Item = {
+ const newWidth = item.width + (isLeft ? -offsetX : offsetX);
+ const newHeight = item.height + (isTop ? -offsetY : offsetY);
+ const newLeft = item.left + (isLeft ? offsetX : 0);
+ const newTop = item.top + (isTop ? offsetY : 0);
+
+ return {
...i,
- width: newWidth,
- height: newHeight,
- left: newLeft,
- top: newTop,
+ width: Math.max(1, Math.round(newWidth)),
+ height: Math.max(1, Math.round(newHeight)),
+ left: Math.min(item.left + item.width - 1, Math.round(newLeft)),
+ top: Math.min(item.top + item.height - 1, Math.round(newTop)),
};
- return updatedItem;
});
};
diff --git a/packages/docs/docs/player/drag-and-drop/SelectionOutline.tsx b/packages/docs/docs/player/drag-and-drop/SelectionOutline.tsx
index f06b9839c2..9b97ce14ae 100644
--- a/packages/docs/docs/player/drag-and-drop/SelectionOutline.tsx
+++ b/packages/docs/docs/player/drag-and-drop/SelectionOutline.tsx
@@ -5,10 +5,10 @@ import {ResizeHandle} from './ResizeHandle';
import type {Item} from './item';
export const SelectionOutline: React.FC<{
- item: Item;
- changeItem: (itemId: number, updater: (item: Item) => Item) => void;
- setSelectedItem: React.Dispatch<React.SetStateAction<number | null>>;
- selectedItem: number | null;
+ readonly item: Item;
+ readonly changeItem: (itemId: number, updater: (item: Item) => Item) => void;
+ readonly setSelectedItem: React.Dispatch<React.SetStateAction<number | null>>;
+ readonly selectedItem: number | null;
}> = ({item, changeItem, setSelectedItem, selectedItem}) => {
const scale = useCurrentScale();
const scaledBorder = Math.ceil(2 / scale);
@@ -23,7 +23,7 @@ export const SelectionOutline: React.FC<{
setHovered(false);
}, []);
- const selected = item.id === selectedItem;
+ const isSelected = item.id === selectedItem;
const style: React.CSSProperties = useMemo(() => {
return {
@@ -33,10 +33,10 @@ export const SelectionOutline: React.FC<{
top: item.top,
position: 'absolute',
outline:
- hovered || selected ? `${scaledBorder}px solid #0B84F3` : undefined,
+ hovered || isSelected ? `${scaledBorder}px solid #0B84F3` : undefined,
userSelect: 'none',
};
- }, [hovered, item, scaledBorder, selected]);
+ }, [hovered, item, scaledBorder, isSelected]);
const startDragging = useCallback(
(e: PointerEvent | React.MouseEvent) => {
@@ -47,12 +47,11 @@ export const SelectionOutline: React.FC<{
const offsetX = (pointerMoveEvent.clientX - initialX) / scale;
const offsetY = (pointerMoveEvent.clientY - initialY) / scale;
changeItem(item.id, (i) => {
- const updatedItem: Item = {
- ...(i as Item),
+ return {
+ ...i,
left: Math.round(item.left + offsetX),
top: Math.round(item.top + offsetY),
};
- return updatedItem as Item;
});
};
@@ -89,7 +88,7 @@ export const SelectionOutline: React.FC<{
onPointerLeave={onMouseLeave}
style={style}
>
- {selected ? (
+ {isSelected ? (
<>
<ResizeHandle item={item} setItem={changeItem} type="top-left" />
<ResizeHandle item={item} setItem={changeItem} type="top-right" />
diff --git a/packages/docs/docs/player/drag-and-drop/SelectionOutlines.tsx b/packages/docs/docs/player/drag-and-drop/SelectionOutlines.tsx
deleted file mode 100644
index db1a686d97..0000000000
--- a/packages/docs/docs/player/drag-and-drop/SelectionOutlines.tsx
+++ /dev/null
@@ -1,44 +0,0 @@
-import React from 'react';
-import {Sequence} from 'remotion';
-import {SelectionOutline} from './SelectionOutline';
-import type {Item} from './item';
-
-const displaySelectedItemOnTop = (
- items: Item[],
- selectedItem: number | null,
-): Item[] => {
- const selectedItems = items.filter((item) => item.id === selectedItem);
- const unselectedItems = items.filter((item) => item.id !== selectedItem);
-
- return [...unselectedItems, ...selectedItems];
-};
-
-export const SelectionOutlines: React.FC<{
- items: Item[];
- selectedItem: number | null;
- changeItem: (itemId: number, updater: (item: Item) => Item) => void;
- setSelectedItem: React.Dispatch<React.SetStateAction<number | null>>;
-}> = ({items, selectedItem, changeItem, setSelectedItem}) => {
- const itemsToDisplay = React.useMemo(
- () => displaySelectedItemOnTop(items, selectedItem),
- [items, selectedItem],
- );
-
- return itemsToDisplay.map((item) => {
- return (
- <Sequence
- key={item.id}
- from={item.from}
- durationInFrames={item.durationInFrames}
- layout="none"
- >
- <SelectionOutline
- changeItem={changeItem}
- item={item}
- setSelectedItem={setSelectedItem}
- selectedItem={selectedItem}
- />
- </Sequence>
- );
- });
-};
diff --git a/packages/docs/docs/player/drag-and-drop/SortedOutlines.tsx b/packages/docs/docs/player/drag-and-drop/SortedOutlines.tsx
new file mode 100644
index 0000000000..3b26bd82d8
--- /dev/null
+++ b/packages/docs/docs/player/drag-and-drop/SortedOutlines.tsx
@@ -0,0 +1,44 @@
+import React from 'react';
+import {Sequence} from 'remotion';
+import {SelectionOutline} from './SelectionOutline';
+import type {Item} from './item';
+
+const displaySelectedItemOnTop = (
+ items: Item[],
+ selectedItem: number | null,
+): Item[] => {
+ const selectedItems = items.filter((item) => item.id === selectedItem);
+ const unselectedItems = items.filter((item) => item.id !== selectedItem);
+
+ return [...unselectedItems, ...selectedItems];
+};
+
+export const SortedOutlines: React.FC<{
+ items: Item[];
+ selectedItem: number | null;
+ changeItem: (itemId: number, updater: (item: Item) => Item) => void;
+ setSelectedItem: React.Dispatch<React.SetStateAction<number | null>>;
+}> = ({items, selectedItem, changeItem, setSelectedItem}) => {
+ const itemsToDisplay = React.useMemo(
+ () => displaySelectedItemOnTop(items, selectedItem),
+ [items, selectedItem],
+ );
+
+ return itemsToDisplay.map((item) => {
+ return (
+ <Sequence
+ key={item.id}
+ from={item.from}
+ durationInFrames={item.durationInFrames}
+ layout="none"
+ >
+ <SelectionOutline
+ changeItem={changeItem}
+ item={item}
+ setSelectedItem={setSelectedItem}
+ selectedItem={selectedItem}
+ />
+ </Sequence>
+ );
+ });
+};
diff --git a/packages/docs/docs/player/drag-and-drop/index.mdx b/packages/docs/docs/player/drag-and-drop/index.mdx
index 8160a254db..d02af1d609 100644
--- a/packages/docs/docs/player/drag-and-drop/index.mdx
+++ b/packages/docs/docs/player/drag-and-drop/index.mdx
@@ -12,31 +12,22 @@ Try to drag and resize the elements below.
<DragAndDropDemo />
<br />
-Pointer events work just like in regular React. Make these Remotion-specific
-considerations:
-- Disable the [`controls`](/docs/player/player#controls) prop to disable any obstructing elements. You can render Playback controls [outside of the Player](/docs/player/custom-controls).
-- The Player might have CSS `scale()` applied to it.
- [If you measure elements](/docs/measuring), you need to divide by the scale obtained by [`useCurrentScale()`](/docs/use-current-scale).
-- You can pass state update functions via [`inputProps`](/docs/player/player#inputprops) to the Player. Alternatively, using React Context also works.
+## General consideration
-## Example
-
-Let's build the demo above.
+Pointer events work mostly just like in regular React.
-The following features have been considered:
+Disable the [`controls`](/docs/player/player#controls) prop to disable any obstructing elements. You can render Playback controls [outside of the Player](/docs/player/custom-controls).
-- Freely position and resizing the squares with the mouse.
-- One item can be selected at a time. Clicking on an empty space will deselect the item.
-- Items can not overflow the container, however the blue outlines can overflow the container.
+The Player might have CSS `scale()` applied to it.
+[If you measure elements](/docs/measuring), you need to divide by the scale obtained by [`useCurrentScale()`](/docs/use-current-scale).
-<Step>1</Step> Create a TypeScript type that describes the shape of your item.
+You can pass state update functions via [`inputProps`](/docs/player/player#inputprops) to the Player.
+Alternatively, wrapping the Player in React Context also works.
-In this example we store an identifier `id`, start and end frame `from` and
-`durationInFrames`, the position `left`, `top`, `width` and `height` and a color
-`color`.
+## Example
-```twoslash include Item
+```twoslash include item
export type Item = {
id: number;
durationInFrames: number;
@@ -63,6 +54,7 @@ export const ResizeHandle: React.FC<{
}> = ({type, setItem, item}) => {
const scale = useCurrentScale();
const size = Math.round(HANDLE_SIZE / scale);
+ const borderSize = 1 / scale;
const sizeStyle: React.CSSProperties = useMemo(() => {
return {
@@ -70,11 +62,11 @@ export const ResizeHandle: React.FC<{
height: size,
width: size,
backgroundColor: 'white',
- border: '1px solid #0B84F3',
+ border: `${borderSize}px solid #0B84F3`,
};
- }, [size]);
+ }, [borderSize, size]);
- const margin = -size / 2 - 1 / scale;
+ const margin = -size / 2 - borderSize;
const style: React.CSSProperties = useMemo(() => {
if (type === 'top-left') {
@@ -131,49 +123,22 @@ export const ResizeHandle: React.FC<{
const offsetX = (pointerMoveEvent.clientX - initialX) / scale;
const offsetY = (pointerMoveEvent.clientY - initialY) / scale;
+ const isLeft = type === 'top-left' || type === 'bottom-left';
+ const isTop = type === 'top-left' || type === 'top-right';
+
setItem(item.id, (i) => {
- const newWidth = Math.max(
- 1,
- Math.round(
- item.width +
- (type === 'bottom-left' || type === 'top-left'
- ? -offsetX
- : offsetX),
- ),
- );
- const newHeight = Math.max(
- 1,
- Math.round(
- item.height +
- (type === 'top-left' || type === 'top-right'
- ? -offsetY
- : offsetY),
- ),
- );
- const newLeft = Math.min(
- item.left + item.width - 1,
- Math.round(
- item.left +
- (type === 'bottom-left' || type === 'top-left' ? offsetX : 0),
- ),
- );
-
- const newTop = Math.min(
- item.top + item.height - 1,
- Math.round(
- item.top +
- (type === 'top-left' || type === 'top-right' ? offsetY : 0),
- ),
- );
-
- const updatedItem: Item = {
+ const newWidth = item.width + (isLeft ? -offsetX : offsetX);
+ const newHeight = item.height + (isTop ? -offsetY : offsetY);
+ const newLeft = item.left + (isLeft ? offsetX : 0);
+ const newTop = item.top + (isTop ? offsetY : 0);
+
+ return {
...i,
- width: newWidth,
- height: newHeight,
- left: newLeft,
- top: newTop,
+ width: Math.max(1, Math.round(newWidth)),
+ height: Math.max(1, Math.round(newHeight)),
+ left: Math.min(item.left + item.width - 1, Math.round(newLeft)),
+ top: Math.min(item.top + item.height - 1, Math.round(newTop)),
};
- return updatedItem;
});
};
@@ -194,22 +159,7 @@ export const ResizeHandle: React.FC<{
};
```
-```tsx twoslash title="item.ts"
-// @filename: item.ts
-// ---cut---
-// @include: Item
-```
-
-If you like to support different types of items (solid, video, image),
-see [here](/docs/building-a-timeline).
-
-<Step>2</Step> Declare a React component that renders the item.
-
-```tsx twoslash title="Layer.tsx"
-// @filename: item.ts
-// @include: Item
-// @filename: Layer.tsx
-// ---cut---
+```twoslash include Layer
import React, {useMemo} from 'react';
import {Sequence} from 'remotion';
import type {Item} from './item';
@@ -241,19 +191,7 @@ export const Layer: React.FC<{
};
```
-By using a [`<Sequence>`](/docs/sequence) component, the item is only displayed from `from` until `from + durationInFrames`.
-
-By adding [`layout="none"`](/docs/sequence#layout), the `<div>` is mounted as a direct child to the DOM.
-
-<Step>3</Step> Create a React component that renders the items.
-
-```tsx twoslash title="SelectionOutline.tsx"
-// @filename: ResizeHandle.tsx
-// @include: ResizeHandle
-// @filename: item.ts
-// @include: Item
-// @filename: SelectionOutline.tsx
-// ---cut---
+```twoslash include SelectionOutline
import React, {useCallback, useMemo} from 'react';
import {useCurrentScale} from 'remotion';
@@ -279,7 +217,7 @@ export const SelectionOutline: React.FC<{
setHovered(false);
}, []);
- const selected = item.id === selectedItem;
+ const isSelected = item.id === selectedItem;
const style: React.CSSProperties = useMemo(() => {
return {
@@ -289,10 +227,10 @@ export const SelectionOutline: React.FC<{
top: item.top,
position: 'absolute',
outline:
- hovered || selected ? `${scaledBorder}px solid #0B84F3` : undefined,
+ hovered || isSelected ? `${scaledBorder}px solid #0B84F3` : undefined,
userSelect: 'none',
};
- }, [hovered, item, scaledBorder, selected]);
+ }, [hovered, item, scaledBorder, isSelected]);
const startDragging = useCallback(
(e: PointerEvent | React.MouseEvent) => {
@@ -303,12 +241,11 @@ export const SelectionOutline: React.FC<{
const offsetX = (pointerMoveEvent.clientX - initialX) / scale;
const offsetY = (pointerMoveEvent.clientY - initialY) / scale;
changeItem(item.id, (i) => {
- const updatedItem: Item = {
- ...(i as Item),
+ return {
+ ...i,
left: Math.round(item.left + offsetX),
top: Math.round(item.top + offsetY),
};
- return updatedItem as Item;
});
};
@@ -345,7 +282,7 @@ export const SelectionOutline: React.FC<{
onPointerLeave={onMouseLeave}
style={style}
>
- {selected ? (
+ {isSelected ? (
<>
<ResizeHandle item={item} setItem={changeItem} type="top-left" />
<ResizeHandle item={item} setItem={changeItem} type="top-right" />
@@ -358,10 +295,343 @@ export const SelectionOutline: React.FC<{
};
```
+```twoslash include SortedOutlines
+import React from 'react';
+import {Sequence} from 'remotion';
+import {SelectionOutline} from './SelectionOutline';
+import type {Item} from './item';
+
+const displaySelectedItemOnTop = (
+ items: Item[],
+ selectedItem: number | null,
+): Item[] => {
+ const selectedItems = items.filter((item) => item.id === selectedItem);
+ const unselectedItems = items.filter((item) => item.id !== selectedItem);
+
+ return [...unselectedItems, ...selectedItems];
+};
+
+export const SortedOutlines: React.FC<{
+ items: Item[];
+ selectedItem: number | null;
+ changeItem: (itemId: number, updater: (item: Item) => Item) => void;
+ setSelectedItem: React.Dispatch<React.SetStateAction<number | null>>;
+}> = ({items, selectedItem, changeItem, setSelectedItem}) => {
+ const itemsToDisplay = React.useMemo(
+ () => displaySelectedItemOnTop(items, selectedItem),
+ [items, selectedItem],
+ );
+
+ return itemsToDisplay.map((item) => {
+ return (
+ <Sequence
+ key={item.id}
+ from={item.from}
+ durationInFrames={item.durationInFrames}
+ layout="none"
+ >
+ <SelectionOutline
+ changeItem={changeItem}
+ item={item}
+ setSelectedItem={setSelectedItem}
+ selectedItem={selectedItem}
+ />
+ </Sequence>
+ );
+ });
+};
+```
+
+```twoslash include Main
+import React, {useCallback} from 'react';
+import {AbsoluteFill} from 'remotion';
+import type {Item} from './item';
+import {Layer} from './Layer';
+import {SortedOutlines} from './SortedOutlines';
+
+export type MainProps = {
+ readonly items: Item[];
+ readonly setSelectedItem: React.Dispatch<React.SetStateAction<number | null>>;
+ readonly selectedItem: number | null;
+ readonly changeItem: (itemId: number, updater: (item: Item) => Item) => void;
+};
+
+const outer: React.CSSProperties = {
+ backgroundColor: '#eee',
+};
+
+const layerContainer: React.CSSProperties = {
+ overflow: 'hidden',
+};
+
+export const Main: React.FC<MainProps> = ({
+ items,
+ setSelectedItem,
+ selectedItem,
+ changeItem,
+}) => {
+ const onPointerDown = useCallback(
+ (e: React.PointerEvent) => {
+ if (e.button !== 0) {
+ return;
+ }
+
+ setSelectedItem(null);
+ },
+ [setSelectedItem],
+ );
+
+ return (
+ <AbsoluteFill style={outer} onPointerDown={onPointerDown}>
+ <AbsoluteFill style={layerContainer}>
+ {items.map((item) => {
+ return <Layer key={item.id} item={item} />;
+ })}
+ </AbsoluteFill>
+ <SortedOutlines
+ selectedItem={selectedItem}
+ items={items}
+ setSelectedItem={setSelectedItem}
+ changeItem={changeItem}
+ />
+ </AbsoluteFill>
+ );
+};
+```
+
+```twoslash include Demo
+import {Player} from '@remotion/player';
+import React, {useCallback, useMemo, useState} from 'react';
+import type {MainProps} from './Main';
+import {Main} from './Main';
+import type {Item} from './item';
+
+export const DragAndDropDemo: React.FC = () => {
+ const [items, setItems] = useState<Item[]>([
+ {
+ left: 395,
+ top: 270,
+ width: 540,
+ durationInFrames: 100,
+ from: 0,
+ height: 540,
+ id: 0,
+ color: '#ccc',
+ },
+ {
+ left: 985,
+ top: 270,
+ width: 540,
+ durationInFrames: 100,
+ from: 0,
+ height: 540,
+ id: 1,
+ color: '#ccc',
+ },
+ ]);
+ const [selectedItem, setSelectedItem] = useState<number | null>(null);
+
+ const changeItem = useCallback(
+ (itemId: number, updater: (item: Item) => Item) => {
+ setItems((oldItems) => {
+ return oldItems.map((item) => {
+ if (item.id === itemId) {
+ return updater(item);
+ }
+
+ return item;
+ });
+ });
+ },
+ [],
+ );
+
+ const inputProps: MainProps = useMemo(() => {
+ return {
+ items,
+ setSelectedItem,
+ changeItem,
+ selectedItem,
+ };
+ }, [changeItem, items, selectedItem]);
+
+ return (
+ <Player
+ style={{
+ width: '100%',
+ }}
+ component={Main}
+ compositionHeight={1080}
+ compositionWidth={1920}
+ durationInFrames={300}
+ fps={30}
+ inputProps={inputProps}
+ overflowVisible
+ />
+ );
+};
+```
+
+Let's build the demo above.
+
+The following features have been defined as goals:
+
+- Ability to freely position and resizing the squares with the mouse.
+- One item can be selected at a time. Clicking on an empty space will deselect the item.
+- Items can not overflow the container, however the blue outlines can overflow the container.
+
+### <TitleStep>1</TitleStep> Data structure
+
+Create a TypeScript type that describes the shape of your item.
+
+In this example we store an identifier `id`, start and end frame `from` and
+`durationInFrames`, the position `left`, `top`, `width` and `height` and a
+`color`.
+
+```tsx twoslash title="item.ts"
+// @filename: item.ts
+// ---cut---
+// @include: item
+```
+
+If you like to support different types of items (solid, video, image),
+see [here](/docs/building-a-timeline).
+
+### <TitleStep>2</TitleStep> Item rendering
+
+Declare a React component that renders the item.
+
+```tsx twoslash title="Layer.tsx"
+// @filename: item.ts
+// @include: item
+// @filename: Layer.tsx
+// ---cut---
+// @include: Layer
+```
+
+By using a [`<Sequence>`](/docs/sequence) component, the item is only displayed from `from` until `from + durationInFrames`.
+
+By adding [`layout="none"`](/docs/sequence#layout), the `<div>` is mounted as a direct child to the DOM.
+
+### <TitleStep>3</TitleStep> Outline rendering
+
+Create a React component that renders an outline when an item isselected or hovered.
+
+```tsx twoslash title="SelectionOutline.tsx"
+// @filename: ResizeHandle.tsx
+// @include: ResizeHandle
+// @filename: item.ts
+// @include: item
+// @filename: SelectionOutline.tsx
+// ---cut---
+// @include: SelectionOutline
+```
+
```tsx twoslash title="ResizeHandle.tsx"
// @filename: item.ts
-// @include: Item
+// @include: item
// @filename: ResizeHandle.tsx
// ---cut---
// @include: ResizeHandle
```
+
+**Z-indexing**: These elements will later in this tutorial be rendered on top of all layers so that they are able to accept mouse events.
+Clicking on one of these elements will select the underlying item.
+
+**Scaling**: The [`<Player />`](/docs/player/player) has a CSS `scale` transform applied to it.
+Since we consume the coordinates of the pointer events, we need to divide them by [`useCurrentScale()`](/docs/use-current-scale) to get the correct position.
+
+The thickness of the borders will also be affected by the scale, so we need to divide them by the scale too.
+
+**Pointer event hadling**: On any `pointerdown` event, we call `e.stopPropagation()` prevent the event from bubbling up.
+Later, we will treat any event that bubbles up as an click on an empty space and deselect the item.
+
+We also don't do any action if `e.button !== 0`. This prevents a bug where the item moves after a right click.
+
+### <TitleStep>4</TitleStep> Sorting outlines
+
+Let's create a component that renders all outlines.
+The item which gets rendered as last element [will be on top of all other items](/docs/layers).
+
+The item that is selected should be rendered on top of all other items because it is the item that is responsive to mouse events.
+
+We render the selected outlines last. This ensures that the resize handles are always on top.
+This logic only applies to the outlines, not the actual layers.
+
+```tsx twoslash title="SortedOutlines.tsx"
+// @filename: item.ts
+// @include: item
+// @filename: ResizeHandle.tsx
+// @include: ResizeHandle
+// @filename: SelectionOutline.tsx
+// @include: SelectionOutline
+// @filename: SortedOutlines.tsx
+// ---cut---
+// @include: SortedOutlines
+```
+
+### <TitleStep>5</TitleStep> Putting it together
+
+Next, we create a main component that renders the items and the outlines.
+
+```tsx twoslash title="SortedOutlines.tsx"
+// @filename: item.ts
+// @include: item
+// @filename: ResizeHandle.tsx
+// @include: ResizeHandle
+// @filename: SelectionOutline.tsx
+// @include: SelectionOutline
+// @filename: SortedOutlines.tsx
+// @include: SortedOutlines
+// @filename: Layer.tsx
+// @include: Layer
+// @filename: Main.tsx
+// ---cut---
+// @include: Main
+```
+
+**Z-indexing:** We render the layers first and then the outlines. This means the outlines get rendered on top the items.
+
+**Overflow behavior**: Note that we apply a different `overflow` behavior to the layers and the outlines.
+We hide the overflow of the layers, but show the overflow of the outlines.
+This is a UI behavior that is similar to Figma frames.
+
+**Click handling**: We also listen to `pointerdown` events on the canvas.
+Any event that was not stopped using `e.stopPropagation()` before will be treated as a click on an empty space and results in deselecting the item.
+
+### <TitleStep>6</TitleStep> Rendering the Player
+
+We can now render the Main component that we created in the Remotion Player.
+
+```tsx twoslash title="Demo.tsx"
+// @filename: item.ts
+// @include: item
+// @filename: ResizeHandle.tsx
+// @include: ResizeHandle
+// @filename: SelectionOutline.tsx
+// @include: SelectionOutline
+// @filename: SortedOutlines.tsx
+// @include: SortedOutlines
+// @filename: Layer.tsx
+// @include: Layer
+// @filename: Main.tsx
+// @include: Main
+// @filename: Demo.tsx
+// ---cut---
+// @include: Demo
+```
+
+We omit the [`controls`](/docs/player/player#controls) prop to disable the built-in controls.
+You can add [custom controls outside the Player](/docs/player/custom-controls).
+
+We add the [`overflowVisible`](/docs/player/player#overflowvisible) prop to make the outlines visible if they go outside the canvas.
+Remember that we've hidden the overflow of the layers itself because it would also not be visible in the final video.
+
+We've now implemented thoughtful drag and drop interactions!
+
+## See also
+
+- [Source code for this demo](https://github.com/remotion-dev/remotion/blob/main/packages/docs/docs/player/drag-and-drop/Demo.tsx)
+- [Custom controls](/docs/player/custom-controls)
+- [`useCurrentScale()`](/docs/use-current-scale)
+- [`<Player />`](/docs/player/player)
diff --git a/packages/docs/src/theme/MDXComponents.js b/packages/docs/src/theme/MDXComponents.js
index ee131e954b..564c35c264 100644
--- a/packages/docs/src/theme/MDXComponents.js
+++ b/packages/docs/src/theme/MDXComponents.js
@@ -1,7 +1,7 @@
// Import the original mapper
import MDXComponents from '@theme-original/MDXComponents';
import {ExperimentalBadge} from '../../components/Experimental';
-import {InlineStep, Step} from '../../components/InlineStep';
+import {InlineStep, Step, TitleStep} from '../../components/InlineStep';
import {Installation} from '../../components/Installation';
import {YouTube} from '../../components/YouTube';
import {Demo} from '../../components/demos';
@@ -26,4 +26,5 @@ export default {
Credits,
YouTube,
Installation,
+ TitleStep,
};