Skip to content

🎥 Make videos programmatically with React

Elements: Add authoring lint rules to Element Library template

Igor Samokhovets7ba6d316390d
diff --git a/packages/template-element-library/README.md b/packages/template-element-library/README.md
index b3101f02b6..5f5938c1c0 100644
--- a/packages/template-element-library/README.md
+++ b/packages/template-element-library/README.md
@@ -37,6 +37,19 @@ The detail page, preview renderer, and Studio use the same source files. The gal
See the [Element guidelines](https://www.remotion.dev/docs/studio/creating-elements), [interactivity documentation](https://www.remotion.dev/docs/studio/interactivity), and [library integration guide](https://www.remotion.dev/docs/studio-protocol/component-library-integration) for layout, assets, dependencies, and installation requirements.
+## Element lint rules
+
+`npm run lint` includes local rules from `eslint/`, configured in `eslint.config.mjs`. They apply only to `elements/**/*.tsx`, not the website, registry, or preview compositions.
+
+- `elements/assets-as-props` (error): Pass media sources through props. Do not hardcode `src`, `srcSet`, or `poster` values, including constant aliases and fallback URLs, import media files, or call `staticFile()` inside an Element. The rule also checks SVG image references and static CSS asset URLs. Put example URLs in the registry's `initialProps`. Use `assets` and installation props with `staticFileRef()` for assets that Studio should download.
+- `elements/self-contained` (error): Keep each Element in one source file. Do not use relative imports, absolute paths, or project aliases such as `@/`, `~/`, and `#`. Package imports are allowed; declare their dependencies in the registry. Computed import paths that cannot be resolved statically are rejected.
+- `elements/no-global-styles` (error): Use styles on the Element's own nodes, not stylesheet imports, `<style>` tags, stylesheet links, or direct mutations of `document.body.style` and `document.documentElement.style`.
+- `elements/interactive-name` (warning): Give `Interactive.*` nodes and supported Remotion media and sequence layers an explicit, nonempty `name`. Dynamic names are allowed.
+
+For example, use `<Img name="Photo" src={src} />` in the Element and supply the photo URL in the registry. Do not use `<Img src="https://example.com/photo.jpg" />` inside the Element.
+
+These checks catch common static patterns, not every possible runtime dependency or side effect. They do not validate registry dependencies or prove that a dynamic asset comes from props. Check installation in a clean Remotion project and review the result in Studio.
+
## Render and upload previews
`remotion.media` hosts the example previews. The gallery and caption picker use `elements/previews.json` for posters and videos. The videos play on hover or focus.
diff --git a/packages/template-element-library/eslint.config.mjs b/packages/template-element-library/eslint.config.mjs
index 64cf76a65f..c9ce098e26 100644
--- a/packages/template-element-library/eslint.config.mjs
+++ b/packages/template-element-library/eslint.config.mjs
@@ -1,6 +1,31 @@
import { makeConfig } from "@remotion/eslint-config-flat";
+import assetsAsProps from "./eslint/assets-as-props.mjs";
+import interactiveName from "./eslint/interactive-name.mjs";
+import noGlobalStyles from "./eslint/no-global-styles.mjs";
+import selfContained from "./eslint/self-contained.mjs";
export default [
{ ignores: [".react-router/**", "build/**"] },
...makeConfig({ remotionDir: "{elements,remotion}/**" }),
+ {
+ files: ["elements/**/*.tsx"],
+ plugins: {
+ elements: {
+ rules: {
+ "assets-as-props": assetsAsProps,
+ "self-contained": selfContained,
+ "no-global-styles": noGlobalStyles,
+ "interactive-name": interactiveName,
+ },
+ },
+ },
+ rules: {
+ "elements/assets-as-props": "error",
+ "elements/self-contained": "error",
+ "elements/no-global-styles": "error",
+ "elements/interactive-name": "warn",
+ // The Element rule replaces the general advice to import local assets.
+ "@remotion/no-string-assets": "off",
+ },
+ },
];
diff --git a/packages/template-element-library/eslint/assets-as-props.mjs b/packages/template-element-library/eslint/assets-as-props.mjs
new file mode 100644
index 0000000000..3ef67c01da
--- /dev/null
+++ b/packages/template-element-library/eslint/assets-as-props.mjs
@@ -0,0 +1,110 @@
+import {
+ findVariable,
+ importedName,
+ importVisitors,
+ resolveExpression,
+ staticString,
+} from "./ast.mjs";
+
+const assetProperties = new Set(["src", "srcSet", "poster"]);
+const assetExtension =
+ /\.(?:avif|bmp|gif|ico|jpe?g|png|svg|webp|mp4|webm|mov|m4v|mp3|wav|ogg|m4a|aac|flac|woff2?|ttf|otf)(?:[?#]|$)/i;
+
+export default {
+ meta: {
+ type: "problem",
+ docs: {
+ description: "Pass Element assets through props instead of hardcoding them.",
+ url: "https://www.remotion.dev/docs/studio-protocol/component-library-integration#asset-handling",
+ },
+ schema: [],
+ messages: {
+ asset:
+ "Pass this asset through props. Put preview URLs in the registry's initialProps, and declare downloadable assets in its assets metadata.",
+ },
+ },
+ create(context) {
+ const { sourceCode } = context;
+ const report = (node) => context.report({ node, messageId: "asset" });
+
+ const hasAsset = (expression, seen = new Set()) => {
+ const node = resolveExpression(expression, sourceCode);
+ if (!node || seen.has(node)) return false;
+ seen.add(node);
+ const value = staticString(node, sourceCode);
+ if (value !== null) return value.trim() !== "" && !value.startsWith("#");
+ if (node.type === "Identifier") {
+ // A prop may have any name, including a destructuring alias. Its default
+ // is still a hardcoded asset when the binding is used as a media source.
+ return (findVariable(node, sourceCode)?.identifiers ?? []).some(
+ (binding) =>
+ binding.parent.type === "AssignmentPattern" &&
+ binding.parent.left === binding &&
+ hasAsset(binding.parent.right, new Set(seen)),
+ );
+ }
+
+ if (node.type === "LogicalExpression") {
+ return hasAsset(node.left, new Set(seen)) || hasAsset(node.right, new Set(seen));
+ }
+
+ if (node.type === "ConditionalExpression") {
+ return hasAsset(node.consequent, new Set(seen)) || hasAsset(node.alternate, new Set(seen));
+ }
+
+ // A dynamic filename does not make a hardcoded host or directory portable.
+ if (node.type === "TemplateLiteral") {
+ if (node.quasis[0].value.raw.startsWith("#")) return false;
+ return (
+ node.quasis[0].value.raw.trim() !== "" || hasAsset(node.expressions[0], new Set(seen))
+ );
+ }
+
+ if (node.type === "BinaryExpression" && node.operator === "+") {
+ return hasAsset(node.left, new Set(seen));
+ }
+
+ return false;
+ };
+
+ const imports = importVisitors(sourceCode, (node) => {
+ const value = staticString(node, sourceCode);
+ if (value !== null && assetExtension.test(value)) report(node);
+ });
+
+ return {
+ ...imports,
+ CallExpression(node) {
+ imports.CallExpression(node);
+ const imported = importedName(node.callee, sourceCode);
+ if (imported?.source === "remotion" && imported.name.join(".") === "staticFile") {
+ report(node);
+ }
+ },
+ JSXAttribute(node) {
+ const name = node.name.name;
+ const tag = node.parent.name;
+ const isSvgAsset =
+ tag.type === "JSXIdentifier" &&
+ ["image", "use"].includes(tag.name) &&
+ ["href", "xlinkHref"].includes(name);
+ if (!assetProperties.has(name) && !isSvgAsset) return;
+ const value =
+ node.value?.type === "JSXExpressionContainer" ? node.value.expression : node.value;
+ if (hasAsset(value)) report(node);
+ },
+ Property(node) {
+ const key = node.computed
+ ? staticString(node.key, sourceCode)
+ : (node.key.name ?? node.key.value);
+ if (assetProperties.has(key) && hasAsset(node.value)) report(node.value);
+ if (["background", "backgroundImage", "mask", "maskImage"].includes(key)) {
+ const value = staticString(node.value, sourceCode);
+ if (value !== null && /url\(\s*["']?(?!["']?#)[^\s)'"#]/i.test(value)) {
+ report(node.value);
+ }
+ }
+ },
+ };
+ },
+};
diff --git a/packages/template-element-library/eslint/ast.mjs b/packages/template-element-library/eslint/ast.mjs
new file mode 100644
index 0000000000..e70b50442d
--- /dev/null
+++ b/packages/template-element-library/eslint/ast.mjs
@@ -0,0 +1,107 @@
+// Resolve bindings instead of matching local names, so aliases and shadowing work.
+export const findVariable = (node, sourceCode) => {
+ for (let scope = sourceCode.getScope(node); scope; scope = scope.upper) {
+ const variable = scope.set.get(node.name);
+ if (variable) return variable;
+ }
+
+ return null;
+};
+
+export const resolveExpression = (node, sourceCode, seen = new Set()) => {
+ if (!node || seen.has(node)) return node;
+ seen.add(node);
+
+ if (["TSAsExpression", "TSSatisfiesExpression", "TSNonNullExpression"].includes(node.type)) {
+ return resolveExpression(node.expression, sourceCode, seen);
+ }
+
+ if (node.type === "Identifier") {
+ const definition = findVariable(node, sourceCode)?.defs[0];
+ if (
+ definition?.type === "Variable" &&
+ definition.parent.kind === "const" &&
+ definition.node.id.type === "Identifier" &&
+ definition.node.init
+ ) {
+ return resolveExpression(definition.node.init, sourceCode, seen);
+ }
+ }
+
+ return node;
+};
+
+export const staticString = (expression, sourceCode, seen = new Set()) => {
+ const node = resolveExpression(expression, sourceCode);
+ if (!node || seen.has(node)) return null;
+ seen.add(node);
+
+ if (node.type === "Literal" && typeof node.value === "string") {
+ return node.value;
+ }
+
+ if (node.type === "TemplateLiteral") {
+ let value = node.quasis[0].value.cooked ?? node.quasis[0].value.raw;
+ for (let i = 0; i < node.expressions.length; i++) {
+ const part = staticString(node.expressions[i], sourceCode, new Set(seen));
+ if (part === null) return null;
+ value += part + (node.quasis[i + 1].value.cooked ?? node.quasis[i + 1].value.raw);
+ }
+
+ return value;
+ }
+
+ if (node.type === "BinaryExpression" && node.operator === "+") {
+ const left = staticString(node.left, sourceCode, new Set(seen));
+ const right = staticString(node.right, sourceCode, new Set(seen));
+ return left === null || right === null ? null : left + right;
+ }
+
+ return null;
+};
+
+export const importedName = (node, sourceCode) => {
+ if (!node) return null;
+ if (node.type === "MemberExpression" || node.type === "JSXMemberExpression") {
+ const object = importedName(node.object, sourceCode);
+ const property = node.computed ? staticString(node.property, sourceCode) : node.property.name;
+ return object && property ? { source: object.source, name: [...object.name, property] } : null;
+ }
+
+ if (node.type !== "Identifier" && node.type !== "JSXIdentifier") return null;
+ const definition = findVariable(node, sourceCode)?.defs[0];
+ if (definition?.type !== "ImportBinding") return null;
+ const specifier = definition.node;
+ return {
+ source: definition.parent.source.value,
+ name:
+ specifier.type === "ImportNamespaceSpecifier"
+ ? []
+ : [specifier.imported?.name ?? specifier.imported?.value ?? "default"],
+ };
+};
+
+// Imports, re-exports and static dynamic imports all affect portability.
+export const importVisitors = (sourceCode, check) => ({
+ ImportDeclaration: (node) => check(node.source),
+ ExportNamedDeclaration: (node) => {
+ if (node.source) check(node.source);
+ },
+ ExportAllDeclaration: (node) => check(node.source),
+ ImportExpression: (node) => check(node.source),
+ TSImportEqualsDeclaration: (node) => {
+ if (node.moduleReference.type === "TSExternalModuleReference") {
+ check(node.moduleReference.expression);
+ }
+ },
+ CallExpression: (node) => {
+ if (
+ node.callee.type === "Identifier" &&
+ node.callee.name === "require" &&
+ !findVariable(node.callee, sourceCode)?.defs.length &&
+ node.arguments[0]
+ ) {
+ check(node.arguments[0]);
+ }
+ },
+});
diff --git a/packages/template-element-library/eslint/interactive-name.mjs b/packages/template-element-library/eslint/interactive-name.mjs
new file mode 100644
index 0000000000..0f7877a286
--- /dev/null
+++ b/packages/template-element-library/eslint/interactive-name.mjs
@@ -0,0 +1,62 @@
+import { importedName, resolveExpression, staticString } from "./ast.mjs";
+
+const remotionLayers = new Set([
+ "Img",
+ "Audio",
+ "Video",
+ "Html5Audio",
+ "Html5Video",
+ "OffthreadVideo",
+ "Sequence",
+ "Series.Sequence",
+]);
+
+export default {
+ meta: {
+ type: "suggestion",
+ docs: {
+ description: "Give editable Element layers a name in Studio.",
+ url: "https://www.remotion.dev/docs/studio/interactivity-best-practices#give-interactive-elements-a-descriptive-name",
+ },
+ schema: [],
+ messages: {
+ name: "Give this editable layer an explicit, nonempty name so users can identify it in Studio.",
+ },
+ },
+ create(context) {
+ const { sourceCode } = context;
+ return {
+ JSXOpeningElement(node) {
+ const imported = importedName(node.name, sourceCode);
+ if (!imported) return;
+ const name = imported.name.join(".");
+ const interactive =
+ (imported.source === "remotion" &&
+ ((imported.name.length === 2 && imported.name[0] === "Interactive") ||
+ remotionLayers.has(name))) ||
+ (imported.source === "@remotion/media" && ["Video", "Audio"].includes(name)) ||
+ (imported.source === "@remotion/gif" && name === "Gif");
+ if (!interactive) return;
+
+ const attribute = node.attributes.find(
+ (candidate) => candidate.type === "JSXAttribute" && candidate.name.name === "name",
+ );
+ const value =
+ attribute?.value?.type === "JSXExpressionContainer"
+ ? attribute.value.expression
+ : attribute?.value;
+ const expression = resolveExpression(value, sourceCode);
+ const text = staticString(value, sourceCode);
+ // Dynamic names are allowed; only reject missing or statically invalid names.
+ if (
+ !value ||
+ (text !== null && text.trim() === "") ||
+ (expression?.type === "Literal" && typeof expression.value !== "string") ||
+ (expression?.type === "Identifier" && expression.name === "undefined")
+ ) {
+ context.report({ node: attribute ?? node, messageId: "name" });
+ }
+ },
+ };
+ },
+};
diff --git a/packages/template-element-library/eslint/no-global-styles.mjs b/packages/template-element-library/eslint/no-global-styles.mjs
new file mode 100644
index 0000000000..b0e719c0a0
--- /dev/null
+++ b/packages/template-element-library/eslint/no-global-styles.mjs
@@ -0,0 +1,93 @@
+import { findVariable, importVisitors, resolveExpression, staticString } from "./ast.mjs";
+
+export default {
+ meta: {
+ type: "problem",
+ docs: {
+ description: "Keep Element styles local to the Element.",
+ url: "https://www.remotion.dev/docs/studio/creating-elements#make-it-portable",
+ },
+ schema: [],
+ messages: {
+ global:
+ "Keep styles on the Element's own nodes. Stylesheet imports, global style tags and document-level style mutations affect the host project.",
+ },
+ },
+ create(context) {
+ const { sourceCode } = context;
+ const report = (node) => context.report({ node, messageId: "global" });
+ const globalPath = (expression, seen = new Set()) => {
+ const node = resolveExpression(expression, sourceCode);
+ if (!node || seen.has(node)) return "";
+ seen.add(node);
+ if (node.type === "Identifier") {
+ return findVariable(node, sourceCode)?.defs.length ? "" : node.name;
+ }
+
+ if (node.type === "MemberExpression") {
+ const object = globalPath(node.object, seen);
+ const property = node.computed
+ ? staticString(node.property, sourceCode)
+ : node.property.name;
+ return object && property ? `${object}.${property}` : "";
+ }
+
+ return "";
+ };
+ const globalStyle =
+ /^(?:(?:window|globalThis)\.)?document\.(?:body|documentElement)\.style(?:\.|$)/;
+ const globalAttribute =
+ /^(?:(?:window|globalThis)\.)?document\.(?:body|documentElement)\.(?:setAttribute|removeAttribute)$/;
+ const imports = importVisitors(sourceCode, (node) => {
+ const value = staticString(node, sourceCode);
+ if (value !== null && /\.(?:css|scss|sass|less|styl)(?:[?#]|$)/i.test(value)) report(node);
+ });
+
+ return {
+ ...imports,
+ JSXOpeningElement(node) {
+ if (node.name.type !== "JSXIdentifier") return;
+ if (node.name.name === "style") report(node);
+ if (
+ node.name.name === "link" &&
+ node.attributes.some(
+ (attribute) =>
+ attribute.type === "JSXAttribute" &&
+ attribute.name.name === "rel" &&
+ staticString(
+ attribute.value?.type === "JSXExpressionContainer"
+ ? attribute.value.expression
+ : attribute.value,
+ sourceCode,
+ )
+ ?.toLowerCase()
+ .split(/\s+/)
+ .includes("stylesheet"),
+ )
+ ) {
+ report(node);
+ }
+ },
+ AssignmentExpression(node) {
+ if (globalStyle.test(globalPath(node.left))) report(node);
+ },
+ UpdateExpression(node) {
+ if (globalStyle.test(globalPath(node.argument))) report(node);
+ },
+ CallExpression(node) {
+ imports.CallExpression(node);
+ const path = globalPath(node.callee);
+ if (/\.(?:setProperty|removeProperty)$/.test(path) && globalStyle.test(path)) {
+ report(node);
+ } else if (path === "Object.assign" && globalStyle.test(globalPath(node.arguments[0]))) {
+ report(node);
+ } else if (
+ globalAttribute.test(path) &&
+ staticString(node.arguments[0], sourceCode) === "style"
+ ) {
+ report(node);
+ }
+ },
+ };
+ },
+};
diff --git a/packages/template-element-library/eslint/self-contained.mjs b/packages/template-element-library/eslint/self-contained.mjs
new file mode 100644
index 0000000000..7df0851c48
--- /dev/null
+++ b/packages/template-element-library/eslint/self-contained.mjs
@@ -0,0 +1,26 @@
+import { importVisitors, staticString } from "./ast.mjs";
+
+export default {
+ meta: {
+ type: "problem",
+ docs: {
+ description: "Keep copied Element source independent of project files.",
+ url: "https://www.remotion.dev/docs/studio-protocol/component-library-integration#reuse-component-code",
+ },
+ schema: [],
+ messages: {
+ import:
+ "Elements are installed as one source file. Inline local code and pass assets through props instead of importing project files or aliases. Package imports must also be declared in the Element's dependencies.",
+ },
+ },
+ create(context) {
+ return importVisitors(context.sourceCode, (node) => {
+ const value = staticString(node, context.sourceCode);
+ // Accept package specifiers, including scoped packages and subpaths.
+ // Relative/absolute paths, @/, ~/, # aliases and URL schemes are not portable.
+ if (value === null || !/^(?:@[\w.-]+\/)?[\w-]+(?:[./][\w.-]+)*(?:\/[^\s]*)?$/.test(value)) {
+ context.report({ node, messageId: "import" });
+ }
+ });
+ },
+};