Skip to content

🎥 Make videos programmatically with React

Gif: Add ref forwarding

Rob Jacksone6bee0eff5e2
diff --git a/packages/docs/docs/gif/gif.md b/packages/docs/docs/gif/gif.md
index 0a488b5b40..59a1a477e1 100644
--- a/packages/docs/docs/gif/gif.md
+++ b/packages/docs/docs/gif/gif.md
@@ -71,18 +71,25 @@ The looping behavior of the GIF. Can be one of these values:
- `'pause-after-finish'`: The GIF will play once and then show the last frame.
- `'unmount-after-finish'`: The GIF will play once and then unmount. Note that if you attach a `ref`, it will become `null` after the GIF has finished playing.
+### `ref` <AvailableFrom v="3.4.88" />
+
+You can add a [React ref](https://reactjs.org/docs/refs-and-the-dom.html) to `<Gif>`. If you use TypeScript, you need to type it with `HTMLCanvasElement`.
+
## Example
```tsx twoslash
+import { useRef } from "react";
import { useVideoConfig } from "remotion";
// ---cut---
import { Gif } from "@remotion/gif";
export const MyComponent: React.FC = () => {
const { width, height } = useVideoConfig();
+ const ref = useRef<HTMLCanvasElement>(null);
return (
<Gif
+ ref={ref}
src="https://media.giphy.com/media/3o72F7YT6s0EMFI0Za/giphy.gif"
width={width}
height={height}
diff --git a/packages/example/src/GifTest/index.tsx b/packages/example/src/GifTest/index.tsx
index a1e6ac875b..2d006ae7e0 100644
--- a/packages/example/src/GifTest/index.tsx
+++ b/packages/example/src/GifTest/index.tsx
@@ -1,18 +1,35 @@
import {Gif} from '@remotion/gif';
-import {Sequence, staticFile, useVideoConfig} from 'remotion';
+import {useEffect, useRef} from 'react';
+import {Sequence, staticFile, useCurrentFrame, useVideoConfig} from 'remotion';
const GifTest: React.FC = () => {
const {width, height} = useVideoConfig();
const giphy = staticFile('giphy.gif');
+ const ref1 = useRef<HTMLCanvasElement>(null);
+ const ref2 = useRef<HTMLCanvasElement>(null);
+ const ref3 = useRef<HTMLCanvasElement>(null);
+ const ref4 = useRef<HTMLCanvasElement>(null);
+ const frame = useCurrentFrame();
+
+ useEffect(() => {
+ console.log(
+ frame,
+ ref1.current?.tagName ?? 'Not rendered',
+ ref2.current?.tagName ?? 'Not rendered',
+ ref3.current?.tagName ?? 'Not rendered',
+ ref4.current?.tagName ?? 'Not rendered'
+ );
+ }, [frame]);
return (
<div style={{flex: 1, backgroundColor: 'black'}}>
<Sequence durationInFrames={50}>
- <Gif src={giphy} width={width} height={height} fit="fill" />
+ <Gif ref={ref1} src={giphy} width={width} height={height} fit="fill" />
</Sequence>
<Sequence from={50} durationInFrames={50}>
<Gif
+ ref={ref2}
src="https://media.giphy.com/media/xT0GqH01ZyKwd3aT3G/giphy.gif"
width={width}
height={height}
@@ -22,6 +39,7 @@ const GifTest: React.FC = () => {
<Sequence from={100} durationInFrames={50}>
<Gif
+ ref={ref3}
src="https://media.giphy.com/media/3o72F7YT6s0EMFI0Za/giphy.gif"
width={width}
height={height}
@@ -36,6 +54,7 @@ const GifTest: React.FC = () => {
}}
>
<Gif
+ ref={ref4}
src={staticFile('disposal-type-3.gif')}
width={width}
height={height}
diff --git a/packages/gif/src/Gif.tsx b/packages/gif/src/Gif.tsx
index f78079ea79..01d7f92d0f 100644
--- a/packages/gif/src/Gif.tsx
+++ b/packages/gif/src/Gif.tsx
@@ -1,3 +1,4 @@
+import {forwardRef} from 'react';
import {Internals} from 'remotion';
import {GifForDevelopment} from './GifForDevelopment';
import {GifForRendering} from './GifForRendering';
@@ -7,11 +8,13 @@ import type {RemotionGifProps} from './props';
* @description Displays a GIF that synchronizes with Remotions useCurrentFrame().
* @see [Documentation](https://www.remotion.dev/docs/gif/gif)
*/
-export const Gif = (props: RemotionGifProps) => {
- const env = Internals.useRemotionEnvironment();
- if (env === 'rendering') {
- return <GifForRendering {...props} />;
- }
+export const Gif = forwardRef<HTMLCanvasElement, RemotionGifProps>(
+ (props, ref) => {
+ const env = Internals.useRemotionEnvironment();
+ if (env === 'rendering') {
+ return <GifForRendering {...props} ref={ref} />;
+ }
- return <GifForDevelopment {...props} />;
-};
+ return <GifForDevelopment {...props} ref={ref} />;
+ }
+);