Skip to content

🎥 Make videos programmatically with React

feat: make volume responsive according to available space

Selmacdda17714fed
diff --git a/packages/player/src/MediaVolumeSlider.tsx b/packages/player/src/MediaVolumeSlider.tsx
index 5081300240..fb5fc94c83 100644
--- a/packages/player/src/MediaVolumeSlider.tsx
+++ b/packages/player/src/MediaVolumeSlider.tsx
@@ -1,13 +1,12 @@
import React, {useCallback, useLayoutEffect, useRef, useState} from 'react';
-import {Internals, useVideoConfig} from 'remotion';
+import {Internals} from 'remotion';
import {ICON_SIZE, VolumeOffIcon, VolumeOnIcon} from './icons';
import {VOLUME_SLIDER_INPUT_CSS_CLASSNAME} from './player-css-classname';
import {useHoverState} from './use-hover-state';
const BAR_HEIGHT = 5;
const KNOB_SIZE = 12;
-const VOLUME_SLIDER_WIDTH = 100;
-const MAX_MOBILE_WIDTH = 480;
+export const VOLUME_SLIDER_WIDTH = 100;
const scope = `.${VOLUME_SLIDER_INPUT_CSS_CLASSNAME}`;
const sliderStyle = `
@@ -49,7 +48,9 @@ ${scope} {
}
`;
-const parentDivStyle = (width: Number): React.CSSProperties => ({
+const parentDivStyle = (
+ displayVerticalVolumeSlider: Boolean
+): React.CSSProperties => ({
display: 'inline-flex',
background: 'none',
border: 'none',
@@ -57,10 +58,12 @@ const parentDivStyle = (width: Number): React.CSSProperties => ({
justifyContent: 'center',
alignItems: 'center',
touchAction: 'none',
- ...(width <= MAX_MOBILE_WIDTH && {position: 'relative'}),
+ ...(displayVerticalVolumeSlider && {position: 'relative'}),
});
-const volumeContainer = (width: Number): React.CSSProperties => ({
+const volumeContainer = (
+ displayVerticalVolumeSlider: Boolean
+): React.CSSProperties => ({
display: 'inline',
width: ICON_SIZE,
height: ICON_SIZE,
@@ -69,17 +72,14 @@ const volumeContainer = (width: Number): React.CSSProperties => ({
background: 'none',
border: 'none',
padding: 0,
- ...(width <= MAX_MOBILE_WIDTH && {position: 'absolute'}),
+ ...(displayVerticalVolumeSlider && {position: 'absolute'}),
});
export const MediaVolumeSlider: React.FC<{
- isFullscreen: Boolean;
-}> = ({isFullscreen}) => {
+ displayVerticalVolumeSlider: Boolean;
+}> = ({displayVerticalVolumeSlider}) => {
const [mediaMuted, setMediaMuted] = Internals.useMediaMutedState();
const [mediaVolume, setMediaVolume] = Internals.useMediaVolumeState();
- const {width} = useVideoConfig();
- const playerWidth = isFullscreen ? window.innerWidth : width;
- const isPlayerWidthSmall = playerWidth < MAX_MOBILE_WIDTH;
const [focused, setFocused] = useState<boolean>(false);
const parentDivRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
@@ -87,14 +87,14 @@ export const MediaVolumeSlider: React.FC<{
const isMutedOrZero = mediaMuted || mediaVolume === 0;
useLayoutEffect(() => {
- if (isPlayerWidthSmall) {
+ if (displayVerticalVolumeSlider) {
Internals.CSSUtils.removeCSS(sliderStyle);
Internals.CSSUtils.injectCSS(sliderStyleVertical);
} else {
Internals.CSSUtils.removeCSS(sliderStyleVertical);
Internals.CSSUtils.injectCSS(sliderStyle);
}
- }, [isFullscreen, width]);
+ }, [displayVerticalVolumeSlider]);
const onVolumeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setMediaVolume(parseFloat(e.target.value));
@@ -121,14 +121,14 @@ export const MediaVolumeSlider: React.FC<{
}, [mediaVolume, setMediaMuted, setMediaVolume]);
return (
- <div ref={parentDivRef} style={parentDivStyle(playerWidth)}>
+ <div ref={parentDivRef} style={parentDivStyle(displayVerticalVolumeSlider)}>
<button
aria-label={isMutedOrZero ? 'Unmute sound' : 'Mute sound'}
title={isMutedOrZero ? 'Unmute sound' : 'Mute sound'}
onClick={onClick}
onBlur={onBlur}
onFocus={() => setFocused(true)}
- style={volumeContainer(playerWidth)}
+ style={volumeContainer(displayVerticalVolumeSlider)}
type="button"
>
{isMutedOrZero ? <VolumeOffIcon /> : <VolumeOnIcon />}
diff --git a/packages/player/src/PlayerControls.tsx b/packages/player/src/PlayerControls.tsx
index 5402b7d585..78ff21ecd0 100644
--- a/packages/player/src/PlayerControls.tsx
+++ b/packages/player/src/PlayerControls.tsx
@@ -6,6 +6,9 @@ import {FullscreenIcon, PauseIcon, PlayIcon} from './icons';
import {MediaVolumeSlider} from './MediaVolumeSlider';
import {PlayerSeekBar} from './PlayerSeekBar';
import type {usePlayer} from './use-player';
+import {useVideoControlsResize} from './use-video-controls-resize';
+
+export const X_SPACER = 10;
const containerStyle: React.CSSProperties = {
boxSizing: 'border-box',
@@ -64,11 +67,14 @@ const flex1: React.CSSProperties = {
const fullscreen: React.CSSProperties = {};
-const timeLabel: React.CSSProperties = {
+const timeLabel = (maxWidth: number): React.CSSProperties => ({
color: 'white',
fontFamily: 'sans-serif',
fontSize: 14,
-};
+ maxWidth,
+ overflow: 'hidden',
+ textOverflow: 'ellipsis',
+});
declare global {
interface Document {
@@ -117,6 +123,9 @@ export const Controls: React.FC<{
const playButtonRef = useRef<HTMLButtonElement | null>(null);
const frame = Internals.Timeline.useTimelinePosition();
const [supportsFullscreen, setSupportsFullscreen] = useState(false);
+
+ const {maxTimeLabelWidth, displayVerticalVolumeSlider} =
+ useVideoControlsResize(isFullscreen, allowFullscreen);
const [shouldShowInitially, setInitiallyShowControls] = useState<
boolean | number
>(() => {
@@ -208,11 +217,13 @@ export const Controls: React.FC<{
{showVolumeControls ? (
<>
<div style={xSpacer} />
- <MediaVolumeSlider isFullscreen={isFullscreen} />
+ <MediaVolumeSlider
+ displayVerticalVolumeSlider={displayVerticalVolumeSlider}
+ />
</>
) : null}
<div style={xSpacer} />
- <div style={timeLabel}>
+ <div style={timeLabel(maxTimeLabelWidth)}>
{formatTime(frame / fps)} / {formatTime(durationInFrames / fps)}
</div>
<div style={xSpacer} />
diff --git a/packages/player/src/icons.tsx b/packages/player/src/icons.tsx
index 392545742c..4b8a2fbeaf 100644
--- a/packages/player/src/icons.tsx
+++ b/packages/player/src/icons.tsx
@@ -1,7 +1,7 @@
import React from 'react';
export const ICON_SIZE = 25;
-const fullscreenIconSize = 16;
+export const fullscreenIconSize = 16;
const rotate: React.CSSProperties = {
transform: `rotate(90deg)`,
diff --git a/packages/player/src/use-video-controls-resize.ts b/packages/player/src/use-video-controls-resize.ts
new file mode 100644
index 0000000000..859d6df5c7
--- /dev/null
+++ b/packages/player/src/use-video-controls-resize.ts
@@ -0,0 +1,67 @@
+import {useMemo} from 'react';
+import {useVideoConfig} from 'remotion';
+import {fullscreenIconSize, ICON_SIZE} from './icons';
+import {VOLUME_SLIDER_WIDTH} from './MediaVolumeSlider';
+import {X_SPACER} from './PlayerControls';
+
+export const useVideoControlsResize = (
+ isFullscreen: Boolean,
+ allowFullScreen: Boolean
+): {
+ maxTimeLabelWidth: number;
+ displayVerticalVolumeSlider: Boolean;
+} => {
+ const {width} = useVideoConfig();
+
+ const {maxTimeLabelWidth, displayVerticalVolumeSlider} = useMemo((): {
+ maxTimeLabelWidth: number;
+ displayVerticalVolumeSlider: Boolean;
+ } => {
+ const playerWidth = isFullscreen ? window.innerWidth : width;
+ const playPauseIconSize = ICON_SIZE;
+ const volumeIconSize = ICON_SIZE;
+ const _fullscreenIconSize = allowFullScreen ? fullscreenIconSize : 0;
+ const xPadding = 12 * 2;
+ const maxTimeLabelWidth =
+ playerWidth -
+ volumeIconSize -
+ playPauseIconSize -
+ xPadding -
+ _fullscreenIconSize -
+ X_SPACER * 2;
+
+ const maxTimeLabelWidthWithoutNegativeValue =
+ maxTimeLabelWidth > 0 ? maxTimeLabelWidth : 0;
+
+ const availableTimeLabelWidthIfVolumeOpen =
+ maxTimeLabelWidthWithoutNegativeValue - VOLUME_SLIDER_WIDTH;
+
+ // If max label width is lower than the volume width
+ // then it means we need to take it's width as the max label width
+ // otherwise we took the available width when volume open
+ const computedLabelWidth =
+ availableTimeLabelWidthIfVolumeOpen < VOLUME_SLIDER_WIDTH
+ ? maxTimeLabelWidthWithoutNegativeValue
+ : availableTimeLabelWidthIfVolumeOpen;
+ const minWidthForHorizontalDisplay =
+ computedLabelWidth +
+ volumeIconSize +
+ VOLUME_SLIDER_WIDTH +
+ playPauseIconSize +
+ _fullscreenIconSize +
+ xPadding +
+ X_SPACER * 2;
+
+ const displayVerticalVolumeSlider =
+ playerWidth < minWidthForHorizontalDisplay;
+ return {
+ maxTimeLabelWidth: maxTimeLabelWidthWithoutNegativeValue,
+ displayVerticalVolumeSlider,
+ };
+ }, [width, isFullscreen, allowFullScreen]);
+
+ return {
+ maxTimeLabelWidth,
+ displayVerticalVolumeSlider,
+ };
+};