Skip to content

🎥 Make videos programmatically with React

document initiallyShowControls

Jonny Burgerf0d1cbe92e88
diff --git a/packages/docs/docs/player/api.md b/packages/docs/docs/player/api.md
index 907f876a40..6963c41b47 100644
--- a/packages/docs/docs/player/api.md
+++ b/packages/docs/docs/player/api.md
@@ -281,6 +281,12 @@ _optional, available from v3.2.15_
Limit playback to only play before a certain frame. The video will end at this frame and move to the beginning once it has ended. Must be an integer, not smaller than `1`, not smaller than [`inFrame`](#inframe) and not bigger than `durationInFrames - 1`. Default `null`, which means the end of the video.
+### `initiallyShowControls`
+
+_optional, available from v3.2.24_
+
+If true, the controls flash when the player enters the scene. After 2 seconds without hover, the controls fade out. This is similar to how YouTube does it, and signals to the user that the player is in fact controllable. You can also pass a `number`, with which you can customize the duration in milliseconds. Default `true` since `v3.2.24`, before that unsupported.
+
## `PlayerRef`
You may attach a ref to the player and control it in an imperative manner.
diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsx
index 386b23bd58..eda60e1807 100644
--- a/packages/player/src/Player.tsx
+++ b/packages/player/src/Player.tsx
@@ -67,6 +67,7 @@ export type PlayerProps<T> = {
showPosterWhenUnplayed?: boolean;
inFrame?: number | null;
outFrame?: number | null;
+ initiallyShowControls?: number | boolean;
} & PropsIfHasProps<T> &
CompProps<T>;
@@ -109,6 +110,7 @@ export const PlayerFn = <T,>(
renderPoster,
inFrame,
outFrame,
+ initiallyShowControls,
...componentProps
}: PlayerProps<T>,
ref: MutableRefObject<PlayerRef>
@@ -428,6 +430,7 @@ export const PlayerFn = <T,>(
renderPoster={renderPoster}
inFrame={inFrame ?? null}
outFrame={outFrame ?? null}
+ initiallyShowControls={initiallyShowControls ?? true}
/>
</Internals.PrefetchProvider>
</PlayerEventEmitterContext.Provider>
diff --git a/packages/player/src/PlayerControls.tsx b/packages/player/src/PlayerControls.tsx
index 62a849d91f..53738f746d 100644
--- a/packages/player/src/PlayerControls.tsx
+++ b/packages/player/src/PlayerControls.tsx
@@ -96,6 +96,7 @@ export const Controls: React.FC<{
onSeekStart: () => void;
inFrame: number | null;
outFrame: number | null;
+ initiallyShowControls: number | boolean;
}> = ({
durationInFrames,
hovered,
@@ -111,20 +112,51 @@ export const Controls: React.FC<{
onSeekStart,
inFrame,
outFrame,
+ initiallyShowControls,
}) => {
const playButtonRef = useRef<HTMLButtonElement | null>(null);
const frame = Internals.Timeline.useTimelinePosition();
const [supportsFullscreen, setSupportsFullscreen] = useState(false);
- const [initiallyShowControls, setInitiallyShowControls] = useState(true);
+ const [shouldShowInitially, setInitiallyShowControls] = useState<
+ boolean | number
+ >(() => {
+ if (typeof initiallyShowControls === 'boolean') {
+ return initiallyShowControls;
+ }
+
+ if (typeof initiallyShowControls === 'number') {
+ if (initiallyShowControls % 1 !== 0) {
+ throw new Error(
+ 'initiallyShowControls must be an integer or a boolean'
+ );
+ }
+
+ if (Number.isNaN(initiallyShowControls)) {
+ throw new Error('initiallyShowControls must not be NaN');
+ }
+
+ if (!Number.isFinite(initiallyShowControls)) {
+ throw new Error('initiallyShowControls must be finite');
+ }
+
+ if (initiallyShowControls <= 0) {
+ throw new Error('initiallyShowControls must be a positive integer');
+ }
+
+ return initiallyShowControls;
+ }
+
+ return true;
+ });
const containerCss: React.CSSProperties = useMemo(() => {
// Hide if playing and mouse outside
- const shouldShow = hovered || !player.playing || initiallyShowControls;
+ const shouldShow = hovered || !player.playing || shouldShowInitially;
return {
...containerStyle,
opacity: Number(shouldShow),
};
- }, [hovered, initiallyShowControls, player.playing]);
+ }, [hovered, shouldShowInitially, player.playing]);
useEffect(() => {
if (playButtonRef.current && spaceKeyToPlayOrPause) {
@@ -145,14 +177,19 @@ export const Controls: React.FC<{
}, []);
useEffect(() => {
+ if (shouldShowInitially === false) {
+ return;
+ }
+
+ const time = shouldShowInitially === true ? 2000 : shouldShowInitially;
const timeout = setTimeout(() => {
setInitiallyShowControls(false);
- }, 2000);
+ }, time);
return () => {
clearInterval(timeout);
};
- }, []);
+ }, [shouldShowInitially]);
return (
<div style={containerCss}>
diff --git a/packages/player/src/PlayerUI.tsx b/packages/player/src/PlayerUI.tsx
index 406660120a..d7c2009b16 100644
--- a/packages/player/src/PlayerUI.tsx
+++ b/packages/player/src/PlayerUI.tsx
@@ -67,6 +67,7 @@ const PlayerUI: React.ForwardRefRenderFunction<
showPosterWhenUnplayed: boolean;
inFrame: number | null;
outFrame: number | null;
+ initiallyShowControls: number | boolean;
}
> = (
{
@@ -95,6 +96,7 @@ const PlayerUI: React.ForwardRefRenderFunction<
showPosterWhenPaused,
inFrame,
outFrame,
+ initiallyShowControls,
},
ref
) => {
@@ -514,6 +516,7 @@ const PlayerUI: React.ForwardRefRenderFunction<
onSeekStart={onSeekStart}
inFrame={inFrame}
outFrame={outFrame}
+ initiallyShowControls={initiallyShowControls}
/>
) : null}
</>