bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/external-video-player/volume-slider/component.jsx

92 lines
2.1 KiB
React
Raw Normal View History

2021-08-06 04:03:43 +08:00
import React, { Component } from 'react';
import Styled from './styles';
2021-08-06 04:03:43 +08:00
class VolumeSlider extends Component {
constructor(props) {
super(props);
this.state = {
volume: props.volume,
muted: props.muted,
};
this.handleOnChange = this.handleOnChange.bind(this);
this.getVolumeIcon = this.getVolumeIcon.bind(this);
this.setMuted = this.setMuted.bind(this);
}
componentDidUpdate(prevProp) {
const { volume, muted } = this.props;
const { volume: prevVolume, muted: prevMuted } = prevProp;
if (prevVolume !== volume) {
this.handleOnChange(volume);
}
if (prevMuted !== muted) {
this.setMuted(muted);
}
}
handleOnChange(volume) {
const { onVolumeChanged } = this.props;
onVolumeChanged(volume);
this.setState({ volume }, () => {
const { volume: stateVolume, muted } = this.state;
if (muted && stateVolume > 0) { // unmute if volume was raised during mute
this.setMuted(false);
} else if (stateVolume <= 0) { // mute if volume is turned to 0
this.setMuted(true);
}
});
}
setMuted(muted) {
const { onMuted } = this.props;
this.setState({ muted }, () => onMuted(muted));
}
getVolumeIcon() {
const { muted, volume } = this.state;
if (muted || volume <= 0) return 'volume_off';
if (volume <= 0.25) return 'volume_mute';
if (volume <= 0.75) return 'volume_down';
return 'volume_up';
}
render() {
const { muted, volume } = this.state;
const { hideVolume } = this.props;
if (hideVolume) {
return null;
}
2021-08-06 04:03:43 +08:00
return (
<Styled.Slider>
<Styled.Volume onClick={() => this.setMuted(!muted)}>
2021-08-06 04:03:43 +08:00
<i
tabIndex="-1"
className={`icon-bbb-${this.getVolumeIcon()}`}
/>
</Styled.Volume>
<Styled.VolumeSlider
2021-08-06 04:03:43 +08:00
type="range"
min={0}
max={1}
step={0.02}
value={muted ? 0 : volume}
onChange={(e) => this.handleOnChange(e.target.valueAsNumber)}
2021-08-06 04:03:43 +08:00
/>
</Styled.Slider>
2021-08-06 04:03:43 +08:00
);
}
}
export default VolumeSlider;