Skip to content

🎥 Make videos programmatically with React

Fix useColorMode hydration issues by using CSS-based theme switching

copilot-swe-agent[bot]0aff241fcc36
diff --git a/packages/docs/src/components/VideoPlayerHeader.tsx b/packages/docs/src/components/VideoPlayerHeader.tsx
index db8d6c602b..b8627681bb 100644
--- a/packages/docs/src/components/VideoPlayerHeader.tsx
+++ b/packages/docs/src/components/VideoPlayerHeader.tsx
@@ -1,4 +1,3 @@
-import {useColorMode} from '@docusaurus/theme-common';
import React, {forwardRef} from 'react';
import {CancelIcon, IconLeft, IconRight} from '../icons/arrows';
@@ -50,24 +49,14 @@ const VideoPlayerHeader: React.ForwardRefRenderFunction<
readonly hasPrevious: boolean;
}
> = ({onDismiss, onNext, onPrevious, hasNext, hasPrevious}, ref) => {
- let colorMode = 'light';
- try {
- const colorModeHook = useColorMode();
- colorMode = colorModeHook.colorMode;
- } catch (error) {
- // Fallback to light mode if context is not available
- console.warn('useColorMode context not available, falling back to light mode');
- }
-
- const iconColor = colorMode === 'dark' ? '#ddd' : '#222';
- const borderBottom =
- colorMode === 'dark'
- ? '1px solid rgba(255, 255, 255, 0.1)'
- : '1px solid rgba(0, 0, 0, 0.1)';
return (
- <div ref={ref} style={{...container, borderBottom}}>
+ <div
+ ref={ref}
+ style={container}
+ className="video-player-header"
+ >
<button onClick={onDismiss} type="button" style={button}>
- <CancelIcon color={iconColor} style={iconStyle} />
+ <CancelIcon style={iconStyle} className="video-player-icon" />
</button>
<div style={flex} />
<button onClick={onPrevious} type="button" style={button}>
@@ -75,21 +64,20 @@ const VideoPlayerHeader: React.ForwardRefRenderFunction<
style={{
...iconStyle,
opacity: hasPrevious ? 1 : 0.6,
- color: iconColor,
cursor: hasPrevious ? 'pointer' : 'default',
}}
+ className="video-player-icon"
/>
</button>
<div style={space} />
<button onClick={onNext} type="button" style={button}>
<IconRight
- color={iconColor}
style={{
...iconStyle,
opacity: hasNext ? 1 : 0.6,
- color: iconColor,
cursor: hasNext ? 'pointer' : 'default',
}}
+ className="video-player-icon"
/>
</button>
</div>
diff --git a/packages/docs/src/css/custom.css b/packages/docs/src/css/custom.css
index 50f25a8b87..119efdb365 100644
--- a/packages/docs/src/css/custom.css
+++ b/packages/docs/src/css/custom.css
@@ -239,3 +239,29 @@ code {
.header-x-link:hover::before {
background-color: var(--ifm-navbar-link-hover-color);
}
+
+/* Video Player Header theme-specific styles */
+[data-theme='light'] .video-player-header {
+ border-bottom: 1px solid rgba(0, 0, 0, 0.1);
+}
+
+[data-theme='dark'] .video-player-header {
+ border-bottom: 1px solid rgba(255, 255, 255, 0.1);
+}
+
+[data-theme='light'] .video-player-icon {
+ color: #222;
+}
+
+[data-theme='dark'] .video-player-icon {
+ color: #ddd;
+}
+
+/* Footer logo theme-specific styles */
+[data-theme='light'] .footer-logo-dark {
+ display: none;
+}
+
+[data-theme='dark'] .footer-logo-light {
+ display: none;
+}
diff --git a/packages/docs/src/pages/index.tsx b/packages/docs/src/pages/index.tsx
index 1c2b198d25..e2f81a6cdb 100644
--- a/packages/docs/src/pages/index.tsx
+++ b/packages/docs/src/pages/index.tsx
@@ -1,9 +1,8 @@
-import {useColorMode} from '@docusaurus/theme-common';
import '@remotion/promo-pages/dist/Homepage.css';
import {NewLanding} from '@remotion/promo-pages/dist/Homepage.js';
import '@remotion/promo-pages/dist/tailwind.css';
import Layout from '@theme/Layout';
-import React from 'react';
+import React, {useEffect, useState} from 'react';
if (
typeof window !== 'undefined' &&
@@ -13,19 +12,52 @@ if (
}
const Inner: React.FC = () => {
- let colorMode = 'light';
- let setColorMode = () => {};
-
- try {
- const colorModeHook = useColorMode();
- colorMode = colorModeHook.colorMode;
- setColorMode = colorModeHook.setColorMode;
- } catch (error) {
- // Fallback to light mode if context is not available
- console.warn('useColorMode context not available, falling back to light mode');
- }
-
- return <NewLanding colorMode={colorMode} setColorMode={setColorMode} />;
+ // Initialize with default values to avoid hydration mismatch
+ const [colorMode, setColorModeState] = useState<'light' | 'dark'>('light');
+
+ useEffect(() => {
+ // After hydration, determine the actual color mode from DOM attributes
+ const htmlElement = document.documentElement;
+ const currentTheme = htmlElement.getAttribute('data-theme') as 'light' | 'dark' | null;
+ if (currentTheme) {
+ setColorModeState(currentTheme);
+ }
+
+ // Set up a MutationObserver to watch for theme changes
+ const observer = new MutationObserver((mutations) => {
+ mutations.forEach((mutation) => {
+ if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {
+ const newTheme = (mutation.target as HTMLElement).getAttribute('data-theme') as 'light' | 'dark' | null;
+ if (newTheme) {
+ setColorModeState(newTheme);
+ }
+ }
+ });
+ });
+
+ observer.observe(htmlElement, {
+ attributes: true,
+ attributeFilter: ['data-theme']
+ });
+
+ return () => observer.disconnect();
+ }, []);
+
+ const handleSetColorMode = (newColorMode: 'light' | 'dark') => {
+ // Dispatch a custom event to trigger Docusaurus theme change
+ const event = new CustomEvent('docusaurus-theme-change', {
+ detail: { theme: newColorMode }
+ });
+ window.dispatchEvent(event);
+
+ // Also try to find and click the actual theme toggle button
+ const themeToggle = document.querySelector('[title="Switch between dark and light mode"]') as HTMLButtonElement;
+ if (themeToggle && colorMode !== newColorMode) {
+ themeToggle.click();
+ }
+ };
+
+ return <NewLanding colorMode={colorMode} setColorMode={handleSetColorMode} />;
};
const Homepage: React.FC = () => {
diff --git a/packages/docs/src/theme/Footer/Links/MultiColumn/index.tsx b/packages/docs/src/theme/Footer/Links/MultiColumn/index.tsx
index d836d09c8e..45047cb7c8 100644
--- a/packages/docs/src/theme/Footer/Links/MultiColumn/index.tsx
+++ b/packages/docs/src/theme/Footer/Links/MultiColumn/index.tsx
@@ -1,7 +1,6 @@
-import {useColorMode} from '@docusaurus/theme-common';
+import React from 'react';
// @ts-expect-error
import LinkItem from '@theme/Footer/LinkItem';
-import React, {useEffect, useState} from 'react';
const footerTitle: React.CSSProperties = {
fontFamily: 'GTPlanar',
@@ -47,25 +46,6 @@ const copyright: React.CSSProperties = {
};
export default ({columns}) => {
- let colorMode = 'light';
- try {
- const colorModeHook = useColorMode();
- colorMode = colorModeHook.colorMode;
- } catch (error) {
- // Fallback to light mode if context is not available
- console.warn('useColorMode context not available, falling back to light mode');
- }
-
- const [src, setSrc] = useState('/img/new-logo.png');
-
- useEffect(() => {
- if (colorMode === 'dark') {
- setSrc('/img/remotion-white.png');
- } else {
- setSrc('/img/new-logo.png');
- }
- }, [colorMode]);
-
return (
<div className="row footer__links">
<div
@@ -74,14 +54,26 @@ export default ({columns}) => {
marginBottom: 20,
}}
>
- <img
- key={colorMode}
- src={src}
- style={{
- height: 32,
- marginRight: 80,
- }}
- />
+ <div className="footer-logo-container">
+ <img
+ src="/img/new-logo.png"
+ className="footer-logo footer-logo-light"
+ style={{
+ height: 32,
+ marginRight: 80,
+ }}
+ alt="Remotion Logo"
+ />
+ <img
+ src="/img/remotion-white.png"
+ className="footer-logo footer-logo-dark"
+ style={{
+ height: 32,
+ marginRight: 80,
+ }}
+ alt="Remotion Logo"
+ />
+ </div>
<p style={copyright}>
© Copyright {new Date().getFullYear()} Remotion AG. <br /> Website
created with Docusaurus.