Skip to content

πŸŽ₯ Make videos programmatically with React

use ref, update docs

Jonny Burger7b6b6514102d
diff --git a/packages/docs/docs/player/api.mdx b/packages/docs/docs/player/api.mdx
index f9c229ef69..a367a34f4a 100644
--- a/packages/docs/docs/player/api.mdx
+++ b/packages/docs/docs/player/api.mdx
@@ -358,6 +358,70 @@ export const App: React.FC = () => {
};
```
+### `renderCustomControls?`<AvailableFrom v="4.0.418" />
+
+Allows you to render custom controls inside the player's control bar, between the main playback controls and the fullscreen button. Must return a valid React element or `null`.
+
+To access player state (playing, frame, volume, etc.) and control methods (play, pause, seekTo, etc.), use a [`PlayerRef`](#playerref).
+
+```tsx twoslash title="App.tsx"
+const MyVideo: React.FC = () => null;
+// ---cut---
+import {Player, PlayerRef, RenderCustomControls} from '@remotion/player';
+import {useCallback, useEffect, useRef, useState} from 'react';
+
+const CustomPlayIndicator: React.FC<{
+ playerRef: React.RefObject<PlayerRef | null>;
+}> = ({playerRef}) => {
+ const [isPlaying, setIsPlaying] = useState(false);
+
+ useEffect(() => {
+ const {current} = playerRef;
+ if (!current) return;
+
+ const onPlay = () => setIsPlaying(true);
+ const onPause = () => setIsPlaying(false);
+
+ current.addEventListener('play', onPlay);
+ current.addEventListener('pause', onPause);
+
+ return () => {
+ current.removeEventListener('play', onPlay);
+ current.removeEventListener('pause', onPause);
+ };
+ }, [playerRef]);
+
+ return (
+ <span style={{color: 'white', fontSize: '12px'}}>
+ {isPlaying ? 'Playing' : 'Paused'}
+ </span>
+ );
+};
+
+export const App: React.FC = () => {
+ const playerRef = useRef<PlayerRef>(null);
+
+ const renderCustomControls: RenderCustomControls = useCallback(() => {
+ return <CustomPlayIndicator playerRef={playerRef} />;
+ }, []);
+
+ return (
+ <Player
+ ref={playerRef}
+ component={MyVideo}
+ durationInFrames={120}
+ compositionWidth={1920}
+ compositionHeight={1080}
+ fps={30}
+ controls
+ renderCustomControls={renderCustomControls}
+ />
+ );
+};
+```
+
+See [Custom controls](/docs/player/custom-controls#controls-slot) for more examples.
+
### `alwaysShowControls?`<AvailableFrom v="3.3.55" />
If true, displays the player controls at all times even if the mouse is outside the player area. This settings override the default behavior, which automatically hides the controls after a period of mouse inactivity over the player area. Default `false`.
diff --git a/packages/docs/docs/player/custom-controls.mdx b/packages/docs/docs/player/custom-controls.mdx
index a7e363e0e1..f9d585b060 100644
--- a/packages/docs/docs/player/custom-controls.mdx
+++ b/packages/docs/docs/player/custom-controls.mdx
@@ -28,6 +28,81 @@ Use the following APIs to customize the individual controls:
- [`renderMuteButton()`](/docs/player/player#rendermutebutton)
- [`renderVolumeSlider()`](/docs/player/player#rendervolumeslider)
+## Controls slot
+
+<AvailableFrom v="4.0.418" />
+
+The Player provides a slot between the main playback controls and the fullscreen button where you can render custom controls using the [`renderCustomControls`](/docs/player/player#rendercustomcontrols) prop.
+
+Use this when you want to add custom buttons or indicators that blend in with the default controls bar.
+
+```tsx twoslash title="ControlsSlot.tsx"
+const MyVideo: React.FC = () => null;
+// ---cut---
+import {Player, PlayerRef, RenderCustomControls} from '@remotion/player';
+import React, {useCallback, useEffect, useRef, useState} from 'react';
+
+const DownloadButton: React.FC<{
+ playerRef: React.RefObject<PlayerRef | null>;
+}> = ({playerRef}) => {
+ const [frame, setFrame] = useState(0);
+
+ useEffect(() => {
+ const {current} = playerRef;
+ if (!current) return;
+
+ const onFrameUpdate = () => {
+ setFrame(current.getCurrentFrame());
+ };
+
+ current.addEventListener('frameupdate', onFrameUpdate);
+ return () => {
+ current.removeEventListener('frameupdate', onFrameUpdate);
+ };
+ }, [playerRef]);
+
+ return (
+ <button
+ type="button"
+ onClick={() => console.log('Download at frame', frame)}
+ style={{
+ background: 'transparent',
+ border: 'none',
+ color: 'white',
+ cursor: 'pointer',
+ }}
+ >
+ Download
+ </button>
+ );
+};
+
+export const App: React.FC = () => {
+ const playerRef = useRef<PlayerRef>(null);
+
+ const renderCustomControls: RenderCustomControls = useCallback(() => {
+ return <DownloadButton playerRef={playerRef} />;
+ }, []);
+
+ return (
+ <Player
+ ref={playerRef}
+ component={MyVideo}
+ durationInFrames={120}
+ compositionWidth={1920}
+ compositionHeight={1080}
+ fps={30}
+ controls
+ renderCustomControls={renderCustomControls}
+ />
+ );
+};
+```
+
+:::note
+Use a [`PlayerRef`](/docs/player/player#playerref) to access player state and methods from within your custom controls. The patterns shown in the [Controls outside the Player](#controls-outside-the-player) section below work the same way inside the controls slot.
+:::
+
## Controls outside the Player
Use this approach if you:
diff --git a/packages/docs/docs/player/rendercustomcontrols.mdx b/packages/docs/docs/player/rendercustomcontrols.mdx
deleted file mode 100644
index 85dcd59ea2..0000000000
--- a/packages/docs/docs/player/rendercustomcontrols.mdx
+++ /dev/null
@@ -1,300 +0,0 @@
----
-title: renderCustomControls
----
-
-_available from v4.1.0_
-
-Allows you to add custom controls to the player, must return a valid React element.
-
-## API
-
-```tsx title="MyApp.tsx"
-import {Player, RenderCustomControls} from '@remotion/player';
-import {useCallback} from 'react';
-
-const MyVideo = () => {
- return <div>My Video</div>;
-};
-// ---cut---
-export const App: React.FC = () => {
- const renderCustomControls: RenderCustomControls = useCallback(
- ({playing, toggle}) => {
- return (
- <button onClick={toggle}>
- {playing ? 'Pause' : 'Play'}
- </button>
- );
- },
- []
- );
-
- return (
- <Player
- component={MyVideo}
- durationInFrames={120}
- compositionWidth={1920}
- compositionHeight={1080}
- fps={30}
- renderCustomControls={renderCustomControls}
- />
- );
-};
-```
-
-The function you pass to `renderCustomControls` receives an object with the following properties:
-
-## Properties
-
-### `playing`
-
-Whether the video is currently playing.
-
-### `play`
-
-A function to start playing the video.
-
-### `pause`
-
-A function to pause the video.
-
-### `toggle`
-
-A function to toggle between play and pause.
-
-### `frame`
-
-The current frame number (0-indexed).
-
-### `durationInFrames`
-
-The total number of frames in the composition.
-
-### `seek`
-
-A function to seek to a specific frame.
-
-```tsx
-seek(100); // Seeks to frame 100
-```
-
-### `fps`
-
-The frames per second of the composition.
-
-### `isFullscreen`
-
-Whether the player is currently in fullscreen mode.
-
-### `volume`
-
-The current volume (between 0 and 1).
-
-### `setVolume`
-
-A function to set the volume.
-
-```tsx
-setVolume(0.5); // Set volume to 50%
-```
-
-## Examples
-
-### Adding a download button
-
-```tsx title="DownloadButton.tsx"
-import {Player, RenderCustomControls} from '@remotion/player';
-import {useCallback} from 'react';
-
-const MyVideo = () => {
- return <div>My Video</div>;
-};
-// ---cut---
-export const App: React.FC = () => {
- const handleDownload = () => {
- // Your download logic
- console.log('Download clicked');
- };
-
- const renderCustomControls: RenderCustomControls = useCallback(() => {
- return (
- <button onClick={handleDownload} style={{padding: '8px 12px'}}>
- πŸ“₯ Download
- </button>
- );
- }, []);
-
- return (
- <Player
- component={MyVideo}
- durationInFrames={120}
- compositionWidth={1920}
- compositionHeight={1080}
- fps={30}
- renderCustomControls={renderCustomControls}
- />
- );
-};
-```
-
-### Using player state
-
-Access the player's state to create interactive controls:
-
-```tsx title="StateAwareControls.tsx"
-import {Player, RenderCustomControls} from '@remotion/player';
-import {useCallback} from 'react';
-
-const MyVideo = () => {
- return <div>My Video</div>;
-};
-// ---cut---
-export const App: React.FC = () => {
- const renderCustomControls: RenderCustomControls = useCallback(
- ({playing, frame, durationInFrames, toggle}) => {
- const progress = (frame / durationInFrames) * 100;
-
- return (
- <div style={{display: 'flex', gap: '8px', alignItems: 'center'}}>
- <button onClick={toggle}>{playing ? '⏸️' : '▢️'}</button>
- <span style={{color: 'white', fontSize: '12px'}}>
- {progress.toFixed(1)}%
- </span>
- </div>
- );
- },
- []
- );
-
- return (
- <Player
- component={MyVideo}
- durationInFrames={120}
- compositionWidth={1920}
- compositionHeight={1080}
- fps={30}
- renderCustomControls={renderCustomControls}
- />
- );
-};
-```
-
-### Multiple custom controls
-
-Add several controls at once:
-
-```tsx title="MultipleControls.tsx"
-import {Player, RenderCustomControls} from '@remotion/player';
-import {useCallback} from 'react';
-
-const MyVideo = () => {
- return <div>My Video</div>;
-};
-// ---cut---
-export const App: React.FC = () => {
- const renderCustomControls: RenderCustomControls = useCallback(() => {
- return (
- <div style={{display: 'flex', gap: '4px'}}>
- <button onClick={() => console.log('Download')}>πŸ“₯</button>
- <button onClick={() => console.log('Share')}>πŸ”—</button>
- <button onClick={() => console.log('Settings')}>βš™οΈ</button>
- </div>
- );
- }, []);
-
- return (
- <Player
- component={MyVideo}
- durationInFrames={120}
- compositionWidth={1920}
- compositionHeight={1080}
- fps={30}
- renderCustomControls={renderCustomControls}
- />
- );
-};
-```
-
-### Dropdown menu
-
-Create a dropdown menu for additional options:
-
-```tsx title="DropdownMenu.tsx"
-import {Player, RenderCustomControls} from '@remotion/player';
-import {useCallback, useState} from 'react';
-
-const MyVideo = () => {
- return <div>My Video</div>;
-};
-// ---cut---
-export const App: React.FC = () => {
- const [menuOpen, setMenuOpen] = useState(false);
-
- const renderCustomControls: RenderCustomControls = useCallback(
- ({frame, fps, durationInFrames}) => {
- return (
- <div style={{position: 'relative'}}>
- <button
- onClick={() => setMenuOpen(!menuOpen)}
- aria-label="More options"
- >
- β‹―
- </button>
-
- {menuOpen && (
- <div
- style={{
- position: 'absolute',
- bottom: '100%',
- right: 0,
- background: 'rgba(0, 0, 0, 0.95)',
- padding: '8px 0',
- borderRadius: '4px',
- minWidth: '200px',
- }}
- >
- <button
- onClick={() => {
- console.log('Download');
- setMenuOpen(false);
- }}
- style={{width: '100%', padding: '8px 16px'}}
- >
- πŸ“₯ Download
- </button>
- <button
- onClick={() => {
- console.log('Settings');
- setMenuOpen(false);
- }}
- style={{width: '100%', padding: '8px 16px'}}
- >
- βš™οΈ Settings
- </button>
- </div>
- )}
- </div>
- );
- },
- [menuOpen]
- );
-
- return (
- <Player
- component={MyVideo}
- durationInFrames={120}
- compositionWidth={1920}
- compositionHeight={1080}
- fps={30}
- renderCustomControls={renderCustomControls}
- />
- );
-};
-```
-
-## Notes
-
-Custom controls are rendered between the main playback controls (play, seek, volume) and the fullscreen button.
-
-Wrap your render function in `useCallback` to avoid unnecessary re-renders.
-
-If your controls use local state, include that state in the `useCallback` dependency array.
diff --git a/packages/player-example/pages/index.tsx b/packages/player-example/pages/index.tsx
index 12787156ea..0c7e3d5497 100644
--- a/packages/player-example/pages/index.tsx
+++ b/packages/player-example/pages/index.tsx
@@ -2,6 +2,7 @@ import {Thumbnail} from '@remotion/player';
import React from 'react';
import App from '../src/App';
import CarSlideshow from '../src/CarSlideshow';
+import {CustomControlsExample} from '../src/CustomControlsExample';
import {FontPicker} from '../src/FontPicker';
import {ThumbnailDemo} from '../src/ThumbnailDemo';
@@ -19,6 +20,16 @@ function Index() {
>
<App component={CarSlideshow} durationInFrames={500} />
</div>
+ <h2>Custom Controls</h2>
+ <div
+ style={{
+ display: 'flex',
+ flexDirection: 'row',
+ justifyContent: 'center',
+ }}
+ >
+ <CustomControlsExample />
+ </div>
<h2>Thumbnail</h2>
<Thumbnail
component={CarSlideshow}
diff --git a/packages/player-example/src/CustomControlsExample.tsx b/packages/player-example/src/CustomControlsExample.tsx
new file mode 100644
index 0000000000..fa788ac591
--- /dev/null
+++ b/packages/player-example/src/CustomControlsExample.tsx
@@ -0,0 +1,87 @@
+import type {PlayerRef, RenderCustomControls} from '@remotion/player';
+import {Player} from '@remotion/player';
+import React, {useCallback, useEffect, useRef, useState} from 'react';
+import CarSlideshow from './CarSlideshow';
+
+const CustomButton: React.FC<{
+ playerRef: React.RefObject<PlayerRef | null>;
+}> = ({playerRef}) => {
+ const [isPlaying, setIsPlaying] = useState(false);
+
+ useEffect(() => {
+ const {current} = playerRef;
+ if (!current) {
+ return;
+ }
+
+ const onPlay = () => setIsPlaying(true);
+ const onPause = () => setIsPlaying(false);
+
+ current.addEventListener('play', onPlay);
+ current.addEventListener('pause', onPause);
+
+ return () => {
+ current.removeEventListener('play', onPlay);
+ current.removeEventListener('pause', onPause);
+ };
+ }, [playerRef]);
+
+ const handleClick = useCallback(() => {
+ playerRef.current?.toggle();
+ }, [playerRef]);
+
+ return (
+ <button
+ type="button"
+ onClick={handleClick}
+ style={{
+ background: 'rgba(255, 255, 255, 0.2)',
+ border: 'none',
+ borderRadius: '4px',
+ padding: '4px 8px',
+ color: 'white',
+ cursor: 'pointer',
+ fontSize: '12px',
+ }}
+ >
+ {isPlaying ? 'Custom Pause' : 'Custom Play'}
+ </button>
+ );
+};
+
+export const CustomControlsExample: React.FC = () => {
+ const playerRef = useRef<PlayerRef>(null);
+
+ const renderCustomControls: RenderCustomControls = useCallback(() => {
+ return <CustomButton playerRef={playerRef} />;
+ }, []);
+
+ return (
+ <div>
+ <h3>Custom Controls Example</h3>
+ <p style={{fontSize: '14px', color: '#666', marginBottom: '10px'}}>
+ The custom control is rendered between main controls and fullscreen
+ button
+ </p>
+ <Player
+ ref={playerRef}
+ component={CarSlideshow}
+ durationInFrames={500}
+ compositionWidth={1920}
+ compositionHeight={1080}
+ fps={30}
+ controls
+ acknowledgeRemotionLicense
+ renderCustomControls={renderCustomControls}
+ inputProps={{
+ title: 'Custom Controls Demo',
+ bgColor: '#000',
+ color: '#fff',
+ }}
+ style={{
+ width: 400,
+ }}
+ />
+ </div>
+ );
+};
diff --git a/packages/player/src/PlayerControls.tsx b/packages/player/src/PlayerControls.tsx
index 9048ffff48..41123fc8f8 100644
--- a/packages/player/src/PlayerControls.tsx
+++ b/packages/player/src/PlayerControls.tsx
@@ -7,10 +7,7 @@ import {PlaybackrateControl, playerButtonStyle} from './PlaybackrateControl.js';
import {PlayerSeekBar} from './PlayerSeekBar.js';
import {PlayerTimeLabel} from './PlayerTimeLabel.js';
import {FullscreenIcon} from './icons.js';
-import type {
- RenderCustomControls,
- RenderCustomControlsInfo,
-} from './player-methods.js';
+import type {RenderCustomControls} from './player-methods.js';
import type {RenderVolumeSlider} from './render-volume-slider.js';
import {useHoverState} from './use-hover-state.js';
import {
@@ -124,13 +121,6 @@ export const Controls: React.FC<{
readonly renderVolumeSlider: RenderVolumeSlider | null;
readonly playing: boolean;
readonly toggle: (e?: SyntheticEvent | PointerEvent) => void;
- readonly frame: number;
- readonly seekTo: (frame: number) => void;
- readonly volume: number;
- readonly setVolume: (volume: number) => void;
- readonly isMuted: boolean;
- readonly mute: () => void;
- readonly unmute: () => void;
readonly renderCustomControls: RenderCustomControls | null;
}> = ({
durationInFrames,
@@ -160,13 +150,6 @@ export const Controls: React.FC<{
renderVolumeSlider,
playing,
toggle,
- frame,
- seekTo,
- volume,
- setVolume,
- isMuted,
- mute,
- unmute,
renderCustomControls,
}) => {
const playButtonRef = useRef<HTMLButtonElement | null>(null);
@@ -284,44 +267,6 @@ export const Controls: React.FC<{
return null;
}, [showPlaybackRateControl]);
- const customControlsInfo: RenderCustomControlsInfo = useMemo(() => {
- return {
- playing,
- frame,
- durationInFrames,
- fps,
- volume,
- isFullscreen,
- isMuted,
- buffering,
- play: () => {
- toggle();
- },
- pause: () => {
- toggle();
- },
- toggle,
- seekTo,
- setVolume,
- mute,
- unmute,
- };
- }, [
- playing,
- frame,
- durationInFrames,
- fps,
- volume,
- isFullscreen,
- isMuted,
- buffering,
- toggle,
- seekTo,
- setVolume,
- mute,
- unmute,
- ]);
-
const customControlsElement = renderCustomControls ? (
<div
style={{
@@ -330,7 +275,7 @@ export const Controls: React.FC<{
gap: '4px',
}}
>
- {renderCustomControls(customControlsInfo)}
+ {renderCustomControls()}
</div>
) : null;
diff --git a/packages/player/src/PlayerUI.tsx b/packages/player/src/PlayerUI.tsx
index 95df2ad910..c21e18d52a 100644
--- a/packages/player/src/PlayerUI.tsx
+++ b/packages/player/src/PlayerUI.tsx
@@ -713,28 +713,6 @@ const PlayerUI: React.ForwardRefRenderFunction<
}
renderMuteButton={renderMuteButton}
renderVolumeSlider={renderVolumeSlider}
- frame={player.getCurrentFrame()}
- seekTo={(f: number) => {
- const lastFrame = durationInFrames - 1;
- const frameToSeekTo = Math.max(0, Math.min(lastFrame, f));
-
- if (player.isPlaying()) {
- const pauseToResume = frameToSeekTo !== lastFrame || loop;
- setHasPausedToResume(pauseToResume);
- player.pause();
- }
-
- if (frameToSeekTo === lastFrame && !loop) {
- player.emitter.dispatchEnded();
- }
-
- player.seek(frameToSeekTo);
- }}
- volume={mediaVolume}
- setVolume={setMediaVolume}
- isMuted={isMuted}
- mute={() => setMediaMuted(true)}
- unmute={() => setMediaMuted(false)}
renderCustomControls={renderCustomControls}
/>
) : null}
diff --git a/packages/player/src/index.ts b/packages/player/src/index.ts
index c788dcc3d0..28406a2631 100644
--- a/packages/player/src/index.ts
+++ b/packages/player/src/index.ts
@@ -19,7 +19,6 @@ export type {
PlayerMethods,
PlayerRef,
RenderCustomControls,
- RenderCustomControlsInfo,
ThumbnailMethods,
ThumbnailRef,
} from './player-methods.js';
diff --git a/packages/player/src/player-methods.ts b/packages/player/src/player-methods.ts
index 4e3d7a0fa4..a497273630 100644
--- a/packages/player/src/player-methods.ts
+++ b/packages/player/src/player-methods.ts
@@ -26,24 +26,4 @@ export type PlayerMethods = ThumbnailMethods & {
export type ThumbnailRef = ThumbnailEmitter & ThumbnailMethods;
export type PlayerRef = PlayerEmitter & PlayerMethods;
-export type RenderCustomControlsInfo = {
- playing: boolean;
- play: (e?: SyntheticEvent) => void;
- pause: () => void;
- toggle: (e?: SyntheticEvent) => void;
- frame: number;
- durationInFrames: number;
- seekTo: (frame: number) => void;
- fps: number;
- isFullscreen: boolean;
- volume: number;
- setVolume: (volume: number) => void;
- isMuted: boolean;
- mute: () => void;
- unmute: () => void;
- buffering: boolean;
-};
-
-export type RenderCustomControls = (
- info: RenderCustomControlsInfo,
-) => ReactElement | null;
+export type RenderCustomControls = () => ReactElement | null;