Skip to content

🎥 Make videos programmatically with React

`remotion`: Respect Tailwind classes added to AbsoluteFill

Jonny Burger4a406b6614c1
diff --git a/packages/core/src/AbsoluteFill.tsx b/packages/core/src/AbsoluteFill.tsx
index fdd5c191d9..18d7544ff4 100644
--- a/packages/core/src/AbsoluteFill.tsx
+++ b/packages/core/src/AbsoluteFill.tsx
@@ -1,24 +1,74 @@
import React, {forwardRef, useMemo} from 'react';
+const hasTailwindClassName = (
+ className: string | undefined,
+ classPrefix: string[],
+) => {
+ if (!className) {
+ return false;
+ }
+
+ return classPrefix.some((prefix) => {
+ return (
+ className.startsWith(prefix) ||
+ className.includes(` ${prefix}`) ||
+ className.includes(`:${prefix}`)
+ );
+ });
+};
+
const AbsoluteFillRefForwarding: React.ForwardRefRenderFunction<
HTMLDivElement,
React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
> = (props, ref) => {
const {style, ...other} = props;
+
const actualStyle = useMemo((): React.CSSProperties => {
+ // Make TailwindCSS classes get accepted
return {
position: 'absolute',
- top: 0,
- left: 0,
- right: 0,
- bottom: 0,
- width: '100%',
- height: '100%',
- display: 'flex',
- flexDirection: 'column',
+ top: hasTailwindClassName(other.className, ['top-', 'inset-'])
+ ? undefined
+ : 0,
+ left: hasTailwindClassName(other.className, ['left-', 'inset-'])
+ ? undefined
+ : 0,
+ right: hasTailwindClassName(other.className, ['right-', 'inset-'])
+ ? undefined
+ : 0,
+ bottom: hasTailwindClassName(other.className, ['bottom-', 'inset-'])
+ ? undefined
+ : 0,
+ width: hasTailwindClassName(other.className, ['w-']) ? undefined : '100%',
+ height: hasTailwindClassName(other.className, ['h-'])
+ ? undefined
+ : '100%',
+ display: hasTailwindClassName(other.className, [
+ 'block',
+ 'inline-block',
+ 'inline',
+ 'flex',
+ 'inline-flex',
+ 'flow-root',
+ 'grid',
+ 'inline-grid',
+ 'contents',
+ 'list-item',
+ 'hidden',
+ ])
+ ? undefined
+ : 'flex',
+ flexDirection: hasTailwindClassName(other.className, [
+ 'flex-row',
+ 'flex-col',
+ 'flex-row-reverse',
+ 'flex-col-reverse',
+ ])
+ ? undefined
+ : 'column',
...style,
};
- }, [style]);
+ }, [other.className, style]);
return <div ref={ref} style={actualStyle} {...other} />;
};
diff --git a/packages/docs/docs/absolute-fill.mdx b/packages/docs/docs/absolute-fill.mdx
index eb648f0351..c0dbba277f 100644
--- a/packages/docs/docs/absolute-fill.mdx
+++ b/packages/docs/docs/absolute-fill.mdx
@@ -2,31 +2,31 @@
image: /generated/articles-docs-absolute-fill.png
id: absolute-fill
title: <AbsoluteFill>
-crumb: "API"
+crumb: 'API'
---
A helper component - it is an absolutely positioned `<div>` with the following styles:
```ts twoslash title="Styles of AbsoluteFill"
-import React from "react";
+import React from 'react';
// ---cut---
const style: React.CSSProperties = {
- position: "absolute",
+ position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
- width: "100%",
- height: "100%",
- display: "flex",
- flexDirection: "column",
+ width: '100%',
+ height: '100%',
+ display: 'flex',
+ flexDirection: 'column',
};
```
This component is useful for layering content on top of each other. For example, you can use it to create a full-screen video background:
```tsx twoslash title="Layer example"
-import { AbsoluteFill, OffthreadVideo } from "remotion";
+import {AbsoluteFill, OffthreadVideo} from 'remotion';
const MyComp = () => {
return (
@@ -49,8 +49,8 @@ The layers that get rendered last appear on top - this is because of how HTML wo
You can add a [React ref](https://react.dev/learn/manipulating-the-dom-with-refs) to an `<AbsoluteFill>` from version `v3.2.13` on. If you use TypeScript, you need to type it with `HTMLDivElement`:
```tsx twoslash
-import { useRef } from "react";
-import { AbsoluteFill } from "remotion";
+import {useRef} from 'react';
+import {AbsoluteFill} from 'remotion';
const content = <div>Hello, World</div>;
// ---cut---
@@ -60,6 +60,19 @@ const MyComp = () => {
};
```
+## TailwindCSS class detection<AvailableFrom v="4.0.249" />
+
+This component has a `style` object, which has higher importance than `className`'s.
+
+In order to make this behave like you expect (row layout):
+
+```tsx twoslash
+<AbsoluteFill className="flex flex-row" />
+```
+
+We detect conflicting Tailwind classes and disable the corresponding inline styles if they are present from Remotion v4.0.249.
+Review the source code below to see how we detect Tailwind classes.
+
## See also
- [Source code for this component](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/AbsoluteFill.tsx)