Skip to content

🎥 Make videos programmatically with React

`@remotion/player`: Fix vertical slider not being accessible

Jonny Burgere1631bed64a9
diff --git a/packages/player/rollup.config.js b/packages/player/rollup.config.js
index ad9a31aa3b..4c61bf07af 100644
--- a/packages/player/rollup.config.js
+++ b/packages/player/rollup.config.js
@@ -12,7 +12,7 @@ export default [
sourcemap: false,
},
],
- external: ['react', 'remotion', 'react/jsx-runtime'],
+ external: ['react', 'remotion', 'remotion/no-react', 'react/jsx-runtime'],
plugins: [
typescript({
tsconfig: 'tsconfig-esm.json',
diff --git a/packages/player/src/MediaVolumeSlider.tsx b/packages/player/src/MediaVolumeSlider.tsx
index 721f39158c..345131f8bd 100644
--- a/packages/player/src/MediaVolumeSlider.tsx
+++ b/packages/player/src/MediaVolumeSlider.tsx
@@ -16,18 +16,23 @@ export const MediaVolumeSlider: React.FC<{
const parentDivRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
const hover = useHoverState(parentDivRef);
+
// Need to import it from React to fix React 17 ESM support.
const randomId =
// eslint-disable-next-line react-hooks/rules-of-hooks
typeof React.useId === 'undefined' ? 'volume-slider' : React.useId();
+
const [randomClass] = useState(() =>
`__remotion-volume-slider-${random(randomId)}`.replace('.', ''),
);
const isMutedOrZero = mediaMuted || mediaVolume === 0;
- const onVolumeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
- setMediaVolume(parseFloat(e.target.value));
- };
+ const onVolumeChange = useCallback(
+ (e: React.ChangeEvent<HTMLInputElement>) => {
+ setMediaVolume(parseFloat(e.target.value));
+ },
+ [setMediaVolume],
+ );
const onBlur = () => {
setTimeout(() => {
@@ -75,6 +80,29 @@ export const MediaVolumeSlider: React.FC<{
};
}, []);
+ const sliderContainer = useMemo((): React.CSSProperties => {
+ const paddingLeft = 5;
+ const common: React.CSSProperties = {
+ paddingLeft,
+ height: ICON_SIZE,
+ width: VOLUME_SLIDER_WIDTH,
+ };
+
+ if (displayVerticalVolumeSlider) {
+ return {
+ ...common,
+ position: 'absolute',
+ transform: `rotate(-90deg) translateX(${
+ VOLUME_SLIDER_WIDTH / 2 + ICON_SIZE / 2
+ }px)`,
+ };
+ }
+
+ return {
+ ...common,
+ };
+ }, [displayVerticalVolumeSlider]);
+
const inputStyle = useMemo((): React.CSSProperties => {
const commonStyle: React.CSSProperties = {
WebkitAppearance: 'none',
@@ -91,16 +119,11 @@ export const MediaVolumeSlider: React.FC<{
if (displayVerticalVolumeSlider) {
return {
...commonStyle,
- transform: `rotate(-90deg)`,
- position: 'absolute',
- bottom: ICON_SIZE + VOLUME_SLIDER_WIDTH / 2 + 5,
+ bottom: ICON_SIZE + VOLUME_SLIDER_WIDTH / 2,
};
}
- return {
- ...commonStyle,
- marginLeft: 5,
- };
+ return commonStyle;
}, [displayVerticalVolumeSlider, mediaVolume]);
const sliderStyle = `
@@ -143,19 +166,21 @@ export const MediaVolumeSlider: React.FC<{
{isMutedOrZero ? <VolumeOffIcon /> : <VolumeOnIcon />}
</button>
{(focused || hover) && !mediaMuted && !Internals.isIosSafari() ? (
- <input
- ref={inputRef}
- aria-label="Change volume"
- className={randomClass}
- max={1}
- min={0}
- onBlur={() => setFocused(false)}
- onChange={onVolumeChange}
- step={0.01}
- type="range"
- value={mediaVolume}
- style={inputStyle}
- />
+ <div style={sliderContainer}>
+ <input
+ ref={inputRef}
+ aria-label="Change volume"
+ className={randomClass}
+ max={1}
+ min={0}
+ onBlur={() => setFocused(false)}
+ onChange={onVolumeChange}
+ step={0.01}
+ type="range"
+ value={mediaVolume}
+ style={inputStyle}
+ />
+ </div>
) : null}
</div>
);