forked from remotion-dev/remotion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaVolumeSlider.tsx
197 lines (172 loc) · 4.8 KB
/
MediaVolumeSlider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {Internals, interpolate} 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 VOLUME_SLIDER_WIDTH = 100;
const parentDivStyle: React.CSSProperties = {
display: 'inline-flex',
background: 'none',
border: 'none',
padding: '6px',
justifyContent: 'center',
alignItems: 'center',
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,
};
const volumeContainer: React.CSSProperties = {
display: 'inline',
width: ICON_SIZE,
height: ICON_SIZE,
cursor: 'pointer',
};
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 parentDivRef = useRef<HTMLDivElement>(null);
const size = useElementSize(currentRef);
const hover = useHoverState(parentDivRef);
const hoverOrDragging = hover || dragging;
const onClick = useCallback(() => {
if (mediaVolume === 0) {
setMediaVolume(1);
setMediaMuted(false);
return;
}
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"
aria-label={isMutedOrZero ? 'Unmute sound' : 'Mute sound'}
title={isMutedOrZero ? 'Unmute sound' : 'Mute sound'}
onClick={onClick}
style={volumeContainer}
>
{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>
</div>
);
};