Skip to content

🎥 Make videos programmatically with React

fix allowFullscreen hydration mismatch

Jonny Burgerfc2a398b15ad
diff --git a/packages/player/src/PlayerControls.tsx b/packages/player/src/PlayerControls.tsx
index 8249afdc0a..b3bd507ab5 100644
--- a/packages/player/src/PlayerControls.tsx
+++ b/packages/player/src/PlayerControls.tsx
@@ -1,11 +1,16 @@
-import React, {MouseEventHandler, useEffect, useMemo, useRef} from 'react';
+import React, {
+ MouseEventHandler,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from 'react';
import {Internals} from 'remotion';
import {formatTime} from './format-time';
import {FullscreenIcon, PauseIcon, PlayIcon} from './icons';
import {MediaVolumeSlider} from './MediaVolumeSlider';
import {PlayerSeekBar} from './PlayerSeekBar';
import {usePlayer} from './use-player';
-import {browserSupportsFullscreen} from './utils/browser-supports-fullscreen';
const containerStyle: React.CSSProperties = {
boxSizing: 'border-box',
@@ -70,6 +75,17 @@ const timeLabel: React.CSSProperties = {
fontSize: 14,
};
+declare global {
+ interface Document {
+ webkitFullscreenEnabled?: boolean;
+ webkitFullscreenElement?: Element;
+ webkitExitFullscreen?: Document['exitFullscreen'];
+ }
+ interface HTMLDivElement {
+ webkitRequestFullScreen: HTMLDivElement['requestFullscreen'];
+ }
+}
+
export const Controls: React.FC<{
fps: number;
durationInFrames: number;
@@ -95,6 +111,7 @@ export const Controls: React.FC<{
}) => {
const playButtonRef = useRef<HTMLButtonElement | null>(null);
const frame = Internals.Timeline.useTimelinePosition();
+ const [supportsFullscreen, setSupportsFullscreen] = useState(false);
const containerCss: React.CSSProperties = useMemo(() => {
// Hide if playing and mouse outside
@@ -112,6 +129,15 @@ export const Controls: React.FC<{
}
}, [player.playing, spaceKeyToPlayOrPause]);
+ useEffect(() => {
+ // Must be handled client-side to avoid SSR hydration mismatch
+ setSupportsFullscreen(
+ (typeof document !== 'undefined' &&
+ (document.fullscreenEnabled || document.webkitFullscreenEnabled)) ??
+ false
+ );
+ }, []);
+
return (
<div style={containerCss}>
<div style={controlsRow}>
@@ -139,8 +165,8 @@ export const Controls: React.FC<{
<div style={xSpacer} />
</div>
<div style={flex1} />
- <div style={fullscreen} suppressHydrationWarning>
- {browserSupportsFullscreen && allowFullscreen ? (
+ <div style={fullscreen}>
+ {supportsFullscreen && allowFullscreen ? (
<button
type="button"
aria-label={isFullscreen ? 'Exit fullscreen' : 'Enter Fullscreen'}
diff --git a/packages/player/src/utils/browser-supports-fullscreen.ts b/packages/player/src/utils/browser-supports-fullscreen.ts
deleted file mode 100644
index 4a639667dd..0000000000
--- a/packages/player/src/utils/browser-supports-fullscreen.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-declare global {
- interface Document {
- webkitFullscreenEnabled?: boolean;
- webkitFullscreenElement?: Element;
- webkitExitFullscreen?: Document['exitFullscreen'];
- }
- interface HTMLDivElement {
- webkitRequestFullScreen: HTMLDivElement['requestFullscreen'];
- }
-}
-
-export const browserSupportsFullscreen =
- typeof document !== 'undefined' &&
- (document.fullscreenEnabled || document.webkitFullscreenEnabled);