Skip to content

🎥 Make videos programmatically with React

player: don't scroll page when adding initial focus to the play button

Jonny Burgera12ffde30ee4
diff --git a/packages/docs/components/LandingPage/Programmatic.tsx b/packages/docs/components/LandingPage/Programmatic.tsx
index 8aeca6cd88..7cb3cccdca 100644
--- a/packages/docs/components/LandingPage/Programmatic.tsx
+++ b/packages/docs/components/LandingPage/Programmatic.tsx
@@ -1,6 +1,6 @@
import type { PlayerRef } from "@remotion/player";
import { Player } from "@remotion/player";
-import React, { useEffect, useRef, useState } from "react";
+import React, { useCallback, useEffect, useRef, useState } from "react";
import type { GithubResponse } from "./GithubDemo";
import { GithubDemo } from "./GithubDemo";
import styles from "./mobileplayer.module.css";
@@ -11,38 +11,38 @@ export const ProgrammaticContent: React.FC<{ data: GithubResponse | null }> = ({
const containerRef = useRef<HTMLDivElement>(null);
const playerRef = useRef<PlayerRef>(null);
- const [isIntersecting] = useState(false);
+ const [isIntersecting, setIsIntersecting] = useState(false);
- // const callback: IntersectionObserverCallback = useCallback(
- // (newData) => {
- // setIsIntersecting(newData[0].isIntersecting);
- // if (isIntersecting) {
- // playerRef.current?.play();
- // } else {
- // playerRef.current?.pause();
- // }
- // },
- // [isIntersecting]
- // );
+ const callback: IntersectionObserverCallback = useCallback(
+ (newData) => {
+ setIsIntersecting(newData[0].isIntersecting);
+ if (isIntersecting) {
+ playerRef.current?.play();
+ } else {
+ playerRef.current?.pause();
+ }
+ },
+ [isIntersecting]
+ );
- // useEffect(() => {
- // const { current } = containerRef;
- // if (!current) {
- // return;
- // }
+ useEffect(() => {
+ const { current } = containerRef;
+ if (!current) {
+ return;
+ }
- // const observer = new IntersectionObserver(callback, {
- // root: null,
- // threshold: 1,
- // });
- // // Docusaurus sometimes has a layout shift that immediately focuses
- // // the video on page load
- // setTimeout(() => {
- // observer.observe(current);
- // }, 2000);
+ const observer = new IntersectionObserver(callback, {
+ root: null,
+ threshold: 1,
+ });
+ // Docusaurus sometimes has a layout shift that immediately focuses
+ // the video on page load
+ setTimeout(() => {
+ observer.observe(current);
+ }, 2000);
- // return () => observer.unobserve(current);
- // }, [callback]);
+ return () => observer.unobserve(current);
+ }, [callback]);
useEffect(() => {
if (isIntersecting) {
diff --git a/packages/player/src/PlayerControls.tsx b/packages/player/src/PlayerControls.tsx
index f035927252..cde1c289ba 100644
--- a/packages/player/src/PlayerControls.tsx
+++ b/packages/player/src/PlayerControls.tsx
@@ -1,11 +1,5 @@
-import type {
- MouseEventHandler} from 'react';
-import React, {
- useEffect,
- useMemo,
- useRef,
- useState,
-} from 'react';
+import type {MouseEventHandler} from 'react';
+import React, {useEffect, useMemo, useRef, useState} from 'react';
import {Internals} from 'remotion';
import {formatTime} from './format-time';
import {FullscreenIcon, PauseIcon, PlayIcon} from './icons';
@@ -126,7 +120,9 @@ export const Controls: React.FC<{
useEffect(() => {
if (playButtonRef.current && spaceKeyToPlayOrPause) {
// This switches focus to play button when player.playing flag changes
- playButtonRef.current.focus();
+ playButtonRef.current.focus({
+ preventScroll: true,
+ });
}
}, [player.playing, spaceKeyToPlayOrPause]);