Skip to content

πŸŽ₯ Make videos programmatically with React

validate at runtime

Jonny Burgera490c7ed0017
diff --git a/packages/cli/src/preview-server/error-overlay/remotion-overlay/Button.tsx b/packages/cli/src/preview-server/error-overlay/remotion-overlay/Button.tsx
index ed94915d11..16aaf45ea6 100644
--- a/packages/cli/src/preview-server/error-overlay/remotion-overlay/Button.tsx
+++ b/packages/cli/src/preview-server/error-overlay/remotion-overlay/Button.tsx
@@ -1,4 +1,4 @@
-import React from 'react';
+import React, {useMemo} from 'react';
import {
INPUT_BACKGROUND,
INPUT_BORDER_COLOR_UNHOVERED,
@@ -25,9 +25,21 @@ export const Button: React.FC<{
onClick: () => void;
disabled?: boolean;
children: React.ReactNode;
-}> = ({children, onClick, disabled}) => {
+ style?: React.CSSProperties;
+}> = ({children, onClick, disabled, style}) => {
+ const combined = useMemo(() => {
+ return {
+ ...button,
+ ...(style ?? {}),
+ };
+ }, [style]);
return (
- <button style={button} type="button" disabled={disabled} onClick={onClick}>
+ <button
+ style={combined}
+ type="button"
+ disabled={disabled}
+ onClick={onClick}
+ >
<div style={buttonContainer}>{children}</div>
</button>
);
diff --git a/packages/cli/src/preview-server/error-overlay/remotion-overlay/HelpLink.tsx b/packages/cli/src/preview-server/error-overlay/remotion-overlay/HelpLink.tsx
index ba168fbb91..43f7cd7da6 100644
--- a/packages/cli/src/preview-server/error-overlay/remotion-overlay/HelpLink.tsx
+++ b/packages/cli/src/preview-server/error-overlay/remotion-overlay/HelpLink.tsx
@@ -4,6 +4,11 @@ import {Button} from './Button';
import type {THelpLink} from './get-help-link';
import {ShortcutHint} from './ShortcutHint';
+const buttonStyle: React.CSSProperties = {
+ backgroundColor: 'var(--blue)',
+ color: 'white',
+};
+
export const HelpLink: React.FC<{
canHaveKeyboardShortcuts: boolean;
link: THelpLink;
@@ -32,7 +37,7 @@ export const HelpLink: React.FC<{
}, [canHaveKeyboardShortcuts, openLink, registerKeybinding]);
return (
- <Button onClick={openLink}>
+ <Button style={buttonStyle} onClick={openLink}>
Help: {'"'}
{link.title}
{'"'}
diff --git a/packages/cli/src/preview-server/error-overlay/remotion-overlay/get-help-link.ts b/packages/cli/src/preview-server/error-overlay/remotion-overlay/get-help-link.ts
index dd7bfdb3b7..4507b32079 100644
--- a/packages/cli/src/preview-server/error-overlay/remotion-overlay/get-help-link.ts
+++ b/packages/cli/src/preview-server/error-overlay/remotion-overlay/get-help-link.ts
@@ -22,5 +22,12 @@ export const getHelpLink = (message: string): THelpLink | null => {
};
}
+ if (message.includes('https://remotion.dev/docs/staticfile-relative-paths')) {
+ return {
+ title: 'staticFile() relative paths',
+ url: 'https://remotion.dev/docs/staticfile-relative-paths',
+ };
+ }
+
return null;
};
diff --git a/packages/core/src/static-file.ts b/packages/core/src/static-file.ts
index ac500d0bdf..d5ba0568a7 100644
--- a/packages/core/src/static-file.ts
+++ b/packages/core/src/static-file.ts
@@ -15,6 +15,12 @@ const inner = (path: string): string => {
};
export const staticFile = (path: string) => {
+ if (path.startsWith('..') || path.startsWith('./')) {
+ throw new TypeError(
+ `staticFile() does not support relative paths - got "${path}". Instead, pass the name of a file that is inside the public/ folder. See: https://remotion.dev/docs/staticfile-relative-paths`
+ );
+ }
+
const preparsed = inner(path);
if (!preparsed.startsWith('/')) {
return `/${preparsed}`;
diff --git a/packages/docs/docs/static-file-relative-paths.md b/packages/docs/docs/static-file-relative-paths.md
new file mode 100644
index 0000000000..2fd5440348
--- /dev/null
+++ b/packages/docs/docs/static-file-relative-paths.md
@@ -0,0 +1,34 @@
+---
+id: staticfile-relative-paths
+title: staticFile() does not support relative paths
+sidebar_label: staticFile() relative paths
+---
+
+If you got the following error message:
+
+```
+staticFile() does not support relative paths. Instead, pass the name of a file that is inside the public/ folder.
+```
+
+You have tried to pass a relative path to `staticFile()`:
+
+```tsx twoslash title="❌ Relative path"
+import { staticFile } from "remotion";
+staticFile("../public/image.png");
+```
+
+```tsx twoslash title="❌ File should not have ./ prefix"
+import { staticFile } from "remotion";
+staticFile("./image.png");
+```
+
+Instead, pass the name of the file that is inside the public folder directly:
+
+```tsx twoslash title="βœ… Filename"
+import { staticFile } from "remotion";
+staticFile("image.png");
+```
+
+## See also
+
+- [`staticFile()`](/docs/staticfile)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index dc1c285a9d..11bc639d91 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -272,6 +272,7 @@ module.exports = {
"enametoolong",
"slow-method-to-extract-frame",
"wrong-composition-mount",
+ "staticfile-relative-paths",
],
},
{
diff --git a/packages/example/src/Freeze/FreezeExample.tsx b/packages/example/src/Freeze/FreezeExample.tsx
index 40fdbd9d1a..49e66eec0a 100644
--- a/packages/example/src/Freeze/FreezeExample.tsx
+++ b/packages/example/src/Freeze/FreezeExample.tsx
@@ -2,7 +2,7 @@ import React from 'react';
import {Freeze, Series, staticFile, Video} from 'remotion';
export const FreezeExample: React.FC = () => {
- const video = staticFile('./framermp4withoutfileextension');
+ const video = staticFile('framermp4withoutfileextension');
return (
<Series>
diff --git a/packages/example/src/VideoTesting/index.tsx b/packages/example/src/VideoTesting/index.tsx
index 246dd9ed1d..f8842ce775 100644
--- a/packages/example/src/VideoTesting/index.tsx
+++ b/packages/example/src/VideoTesting/index.tsx
@@ -11,8 +11,8 @@ export const VideoTesting: React.FC<{
offthread: boolean;
}> = ({codec, offthread}) => {
const {durationInFrames} = useVideoConfig();
- const videoMp4 = staticFile('./framermp4withoutfileextension');
- const videoWebm = staticFile('./framer.webm');
+ const videoMp4 = staticFile('framermp4withoutfileextension');
+ const videoWebm = staticFile('framer.webm');
const Comp = offthread ? OffthreadVideo : Video;