Skip to content

🎥 Make videos programmatically with React

feat(player): support volume control using keyboard

Arthur Dennerbda8c35528ef
diff --git a/packages/player-example/src/CarSlideshow.tsx b/packages/player-example/src/CarSlideshow.tsx
index fe6d70dcfe..c98aeea57d 100644
--- a/packages/player-example/src/CarSlideshow.tsx
+++ b/packages/player-example/src/CarSlideshow.tsx
@@ -2,15 +2,15 @@ import {interpolate, useCurrentFrame, useVideoConfig} from 'remotion';
type Props = {
title: string;
- bgColor: string,
- color: string
+ bgColor: string;
+ color: string;
};
const CarSlideshow = ({title, bgColor, color}: Props) => {
const frame = useCurrentFrame();
const {width, height, durationInFrames} = useVideoConfig();
const left = interpolate(frame, [0, durationInFrames], [width, width * -1]);
- console.log(bgColor, color)
+
return (
<div
style={{
diff --git a/packages/player/src/MediaVolumeSlider.tsx b/packages/player/src/MediaVolumeSlider.tsx
index 22df7b62b1..acc41041e9 100644
--- a/packages/player/src/MediaVolumeSlider.tsx
+++ b/packages/player/src/MediaVolumeSlider.tsx
@@ -1,12 +1,10 @@
-import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
-import {Internals, interpolate} from 'remotion';
+import React, {useCallback, useRef, useState} from 'react';
+import {Internals} from 'remotion';
import {ICON_SIZE, VolumeOffIcon, VolumeOnIcon} from './icons';
import {useHoverState} from './use-hover-state';
-import {useElementSize} from './utils/use-element-size';
const BAR_HEIGHT = 5;
-const KNOB_SIZE = 12;
-const VERTICAL_PADDING = 4;
+// const KNOB_SIZE = 12;
const VOLUME_SLIDER_WIDTH = 100;
const parentDivStyle: React.CSSProperties = {
@@ -19,36 +17,12 @@ const parentDivStyle: React.CSSProperties = {
touchAction: 'none',
};
-const containerStyle: React.CSSProperties = {
- userSelect: 'none',
- paddingTop: VERTICAL_PADDING,
- paddingBottom: VERTICAL_PADDING,
- boxSizing: 'border-box',
- cursor: 'pointer',
- position: 'relative',
-};
-
const barBackground: React.CSSProperties = {
height: BAR_HEIGHT,
backgroundColor: 'rgba(255, 255, 255, 0.5)',
width: VOLUME_SLIDER_WIDTH,
borderRadius: BAR_HEIGHT / 2,
-};
-
-const getVolumeFromX = (clientX: number, width: number) => {
- const pos = clientX;
- const volume =
- width === 0
- ? 0
- : interpolate(pos, [0, width], [0, 1], {
- extrapolateLeft: 'clamp',
- extrapolateRight: 'clamp',
- });
- return volume;
-};
-
-const xSpacer: React.CSSProperties = {
- width: 5,
+ marginLeft: 5,
};
const volumeContainer: React.CSSProperties = {
@@ -56,19 +30,34 @@ const volumeContainer: React.CSSProperties = {
width: ICON_SIZE,
height: ICON_SIZE,
cursor: 'pointer',
+ appearance: 'none',
+ background: 'none',
+ border: 'none',
+ padding: 0,
};
export const MediaVolumeSlider: React.FC = () => {
const [mediaMuted, setMediaMuted] = Internals.useMediaMutedState();
const [mediaVolume, setMediaVolume] = Internals.useMediaVolumeState();
- const [dragging, setDragging] = useState<boolean>(false);
- const currentRef = useRef<HTMLDivElement>(null);
- const iconDivRef = useRef<HTMLDivElement>(null);
+ const [focused, setFocused] = useState<boolean>(false);
const parentDivRef = useRef<HTMLDivElement>(null);
- const size = useElementSize(currentRef, {triggerOnWindowResize: true});
+ const inputRef = useRef<HTMLInputElement>(null);
const hover = useHoverState(parentDivRef);
+ const isMutedOrZero = mediaMuted || mediaVolume === 0;
+
+ const onVolumeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
+ setMediaVolume(parseFloat(e.target.value));
+ };
- const hoverOrDragging = hover || dragging;
+ const onBlur = () => {
+ setTimeout(() => {
+ // We need a small delay to check which element was focused next,
+ // and if it wasn't the volume slider, we hide it
+ if (document.activeElement !== inputRef.current) {
+ setFocused(false);
+ }
+ }, 10);
+ };
const onClick = useCallback(() => {
if (mediaVolume === 0) {
@@ -80,118 +69,34 @@ export const MediaVolumeSlider: React.FC = () => {
setMediaMuted((mute) => !mute);
}, [mediaVolume, setMediaMuted, setMediaVolume]);
- const onPointerDown = useCallback(
- (e: React.PointerEvent<HTMLDivElement>) => {
- if (!size) {
- throw new Error('Player has no size');
- }
-
- const _volume = getVolumeFromX(
- e.clientX - size.left - KNOB_SIZE / 2,
- size.width - KNOB_SIZE
- );
- setMediaVolume(_volume);
- if (_volume > 0) {
- setMediaMuted(false);
- }
-
- setDragging(true);
- },
- [setMediaMuted, setMediaVolume, size]
- );
-
- const onPointerMove = useCallback(
- (e: PointerEvent) => {
- if (!size) {
- throw new Error('Player has no size');
- }
-
- if (!dragging) return;
-
- const _volume = getVolumeFromX(
- e.clientX - size.left - KNOB_SIZE / 2,
- size.width - KNOB_SIZE
- );
- setMediaVolume(_volume);
- if (_volume > 0) {
- setMediaMuted(false);
- }
- },
- [dragging, setMediaMuted, setMediaVolume, size]
- );
-
- const onPointerUp = useCallback(() => {
- setDragging(false);
- }, []);
-
- useEffect(() => {
- if (!dragging) {
- return;
- }
-
- window.addEventListener('pointermove', onPointerMove);
- window.addEventListener('pointerup', onPointerUp);
- return () => {
- window.removeEventListener('pointermove', onPointerMove);
- window.removeEventListener('pointerup', onPointerUp);
- };
- }, [currentRef, dragging, onPointerMove, onPointerUp, onPointerDown]);
-
- const knobStyle: React.CSSProperties = useMemo(() => {
- return {
- height: KNOB_SIZE,
- width: KNOB_SIZE,
- borderRadius: KNOB_SIZE / 2,
- position: 'absolute',
- top: VERTICAL_PADDING - KNOB_SIZE / 2 + 5 / 2,
- backgroundColor: 'white',
- left: mediaMuted
- ? 0
- : Math.max(0, mediaVolume * ((size?.width ?? 0) - KNOB_SIZE)),
- boxShadow: '0 0 2px black',
- opacity: Number(hoverOrDragging),
- };
- }, [hoverOrDragging, mediaMuted, mediaVolume, size?.width]);
-
- const fillStyle: React.CSSProperties = useMemo(() => {
- return {
- height: BAR_HEIGHT,
- backgroundColor: 'rgba(255, 255, 255, 1)',
- width: mediaMuted ? 0 : (mediaVolume / 1) * 100 + '%',
- borderRadius: BAR_HEIGHT / 2,
- };
- }, [mediaMuted, mediaVolume]);
-
- const isMutedOrZero = mediaMuted || mediaVolume === 0;
-
return (
<div ref={parentDivRef} style={parentDivStyle}>
- <div
- ref={iconDivRef}
- role="button"
+ <button
aria-label={isMutedOrZero ? 'Unmute sound' : 'Mute sound'}
title={isMutedOrZero ? 'Unmute sound' : 'Mute sound'}
onClick={onClick}
+ onBlur={onBlur}
+ onFocus={() => setFocused(true)}
style={volumeContainer}
+ type="button"
>
{isMutedOrZero ? <VolumeOffIcon /> : <VolumeOnIcon />}
- </div>
- <div style={xSpacer} />
-
- <div
- ref={currentRef}
- onPointerDown={onPointerDown}
- style={containerStyle}
- >
- {hoverOrDragging ? (
- <>
- <div style={barBackground}>
- <div style={fillStyle} />
- </div>
- <div style={knobStyle} />
- </>
- ) : null}
- </div>
+ </button>
+
+ {(focused || hover) && !mediaMuted ? (
+ <input
+ ref={inputRef}
+ aria-label="Change volume"
+ max={1}
+ min={0}
+ onBlur={() => setFocused(false)}
+ onChange={onVolumeChange}
+ step={0.01}
+ style={barBackground}
+ type="range"
+ value={mediaVolume}
+ />
+ ) : null}
</div>
);
};