Skip to content

🎥 Make videos programmatically with React

hide controls when leaving cursor

Jonny Burger7d521c396633
diff --git a/packages/player-example/src/App.tsx b/packages/player-example/src/App.tsx
index 9951234e87..210c19ff31 100644
--- a/packages/player-example/src/App.tsx
+++ b/packages/player-example/src/App.tsx
@@ -16,7 +16,7 @@ export default function App() {
fps={30}
durationInFrames={500}
component={CarSlideshow}
- controls={true}
+ controls
props={{
title: String(title),
}}
diff --git a/packages/player/src/PlayerControls.tsx b/packages/player/src/PlayerControls.tsx
index c2ab7c6961..dd097f62e7 100644
--- a/packages/player/src/PlayerControls.tsx
+++ b/packages/player/src/PlayerControls.tsx
@@ -6,11 +6,15 @@ import {usePlaybackTime} from './PlayPause';
export const Controls: React.FC<{
fps: number;
durationInFrames: number;
-}> = ({durationInFrames, fps}) => {
+ hovered: boolean;
+}> = ({durationInFrames, hovered, fps}) => {
const {toggle} = usePlaybackTime();
const [playing] = Internals.Timeline.usePlayingState();
const frame = Internals.Timeline.useTimelinePosition();
+ // Hide if playing and mouse outside
+ const shouldShow = hovered || !playing;
+
return (
<div
style={{
@@ -25,6 +29,8 @@ export const Controls: React.FC<{
paddingRight: 12,
paddingLeft: 12,
flexDirection: 'column',
+ transition: 'opacity 0.3s',
+ opacity: Number(shouldShow),
}}
>
<div
@@ -52,7 +58,7 @@ export const Controls: React.FC<{
<div style={{flex: 1}} />
</div>
<div style={{height: 8}} />
- <PlayerSeekBar durationInFrames={durationInFrames} fps={fps} />
+ <PlayerSeekBar durationInFrames={durationInFrames} />
</div>
);
};
diff --git a/packages/player/src/PlayerSeekBar.tsx b/packages/player/src/PlayerSeekBar.tsx
index b067862013..4f69f14739 100644
--- a/packages/player/src/PlayerSeekBar.tsx
+++ b/packages/player/src/PlayerSeekBar.tsx
@@ -17,6 +17,8 @@ const getFrameFromX = (
return frame;
};
+const BAR_HEIGHT = 5;
+const KNOB_SIZE = 12;
const VERTICAL_PADDING = 4;
const containerStyle: React.CSSProperties = {
@@ -28,14 +30,12 @@ const containerStyle: React.CSSProperties = {
};
const barBackground: React.CSSProperties = {
- height: 5,
+ height: BAR_HEIGHT,
backgroundColor: 'rgba(255, 255, 255, 0.5)',
width: '100%',
- borderRadius: 3,
+ borderRadius: BAR_HEIGHT / 2,
};
-const KNOB_SIZE = 12;
-
export const PlayerSeekBar: React.FC<{
durationInFrames: number;
}> = ({durationInFrames}) => {
@@ -133,10 +133,10 @@ export const PlayerSeekBar: React.FC<{
const fillStyle: React.CSSProperties = useMemo(() => {
return {
- height: 5,
+ height: BAR_HEIGHT,
backgroundColor: 'rgba(255, 255, 255, 1)',
width: (frame / durationInFrames) * 100 + '%',
- borderRadius: 3,
+ borderRadius: BAR_HEIGHT / 2,
};
}, [durationInFrames, frame]);
diff --git a/packages/player/src/RootComponent.tsx b/packages/player/src/RootComponent.tsx
index 0e89dda2fd..0eb444bf48 100644
--- a/packages/player/src/RootComponent.tsx
+++ b/packages/player/src/RootComponent.tsx
@@ -1,6 +1,7 @@
-import React, {Suspense, useMemo} from 'react';
+import React, {Suspense, useMemo, useRef} from 'react';
import {Internals} from 'remotion';
import {Controls} from './PlayerControls';
+import {useHoverState} from './use-hover-state';
const RootComponent: React.FC<{
controls: boolean;
@@ -8,6 +9,9 @@ const RootComponent: React.FC<{
}> = ({controls, style}) => {
const config = Internals.useUnsafeVideoConfig();
const video = Internals.useVideo();
+ const container = useRef<HTMLDivElement>(null);
+ const hovered = useHoverState(container);
+
const VideoComponent = video ? video.component : null;
const containerStyle: React.CSSProperties = useMemo(() => {
@@ -29,7 +33,7 @@ const RootComponent: React.FC<{
return (
<Suspense fallback={<h1>Loading...</h1>}>
- <div style={containerStyle}>
+ <div ref={container} style={containerStyle}>
{VideoComponent ? (
<VideoComponent {...(((video?.props as unknown) as {}) ?? {})} />
) : null}
@@ -37,6 +41,7 @@ const RootComponent: React.FC<{
<Controls
fps={config.fps}
durationInFrames={config.durationInFrames}
+ hovered={hovered}
/>
) : null}
</div>
diff --git a/packages/player/src/use-hover-state.ts b/packages/player/src/use-hover-state.ts
new file mode 100644
index 0000000000..aac302ed36
--- /dev/null
+++ b/packages/player/src/use-hover-state.ts
@@ -0,0 +1,27 @@
+import {useEffect, useState} from 'react';
+
+export const useHoverState = (ref: React.RefObject<HTMLDivElement>) => {
+ const [hovered, stetHovered] = useState(false);
+
+ useEffect(() => {
+ const {current} = ref;
+ if (!current) {
+ return;
+ }
+ const onHover = () => {
+ stetHovered(true);
+ };
+ const onLeave = () => {
+ stetHovered(false);
+ };
+
+ current.addEventListener('mouseenter', onHover);
+ current.addEventListener('mouseleave', onLeave);
+ return () => {
+ current.removeEventListener('mouseenter', onHover);
+ current.removeEventListener('mouseenter', onLeave);
+ };
+ }, [ref]);
+
+ return hovered;
+};