You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VolumeSlider.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. /**
  4. * Used to modify initialValue, which is expected to be a decimal value between
  5. * 0 and 1, and converts it to a number representable by an input slider, which
  6. * recognizes whole numbers.
  7. */
  8. const VOLUME_SLIDER_SCALE = 100;
  9. /**
  10. * Implements a React {@link Component} which displays an input slider for
  11. * adjusting the local volume of a remote participant.
  12. *
  13. * @extends Component
  14. */
  15. class VolumeSlider extends Component {
  16. /**
  17. * {@code VolumeSlider} component's property types.
  18. *
  19. * @static
  20. */
  21. static propTypes = {
  22. /**
  23. * The value of the audio slider should display at when the component
  24. * first mounts. Changes will be stored in state. The value should be a
  25. * number between 0 and 1.
  26. */
  27. initialValue: PropTypes.number,
  28. /**
  29. * The callback to invoke when the audio slider value changes.
  30. */
  31. onChange: PropTypes.func
  32. };
  33. /**
  34. * Initializes a new {@code VolumeSlider} instance.
  35. *
  36. * @param {Object} props - The read-only properties with which the new
  37. * instance is to be initialized.
  38. */
  39. constructor(props) {
  40. super(props);
  41. this.state = {
  42. /**
  43. * The volume of the participant's audio element. The value will
  44. * be represented by a slider.
  45. *
  46. * @type {Number}
  47. */
  48. volumeLevel: (props.initialValue || 0) * VOLUME_SLIDER_SCALE
  49. };
  50. // Bind event handlers so they are only bound once for every instance.
  51. this._onVolumeChange = this._onVolumeChange.bind(this);
  52. }
  53. /**
  54. * Implements React's {@link Component#render()}.
  55. *
  56. * @inheritdoc
  57. * @returns {ReactElement}
  58. */
  59. render() {
  60. return (
  61. <li className = 'popupmenu__item'>
  62. <div className = 'popupmenu__contents'>
  63. <span className = 'popupmenu__icon'>
  64. <i className = 'icon-volume' />
  65. </span>
  66. <div className = 'popupmenu__slider_container'>
  67. <input
  68. className = 'popupmenu__slider'
  69. max = { VOLUME_SLIDER_SCALE }
  70. min = { 0 }
  71. onChange = { this._onVolumeChange }
  72. type = 'range'
  73. value = { this.state.volumeLevel } />
  74. </div>
  75. </div>
  76. </li>
  77. );
  78. }
  79. /**
  80. * Sets the internal state of the volume level for the volume slider.
  81. * Invokes the prop onVolumeChange to notify of volume changes.
  82. *
  83. * @param {Object} event - DOM Event for slider change.
  84. * @private
  85. * @returns {void}
  86. */
  87. _onVolumeChange(event) {
  88. const volumeLevel = event.currentTarget.value;
  89. this.props.onChange(volumeLevel / VOLUME_SLIDER_SCALE);
  90. this.setState({ volumeLevel });
  91. }
  92. }
  93. export default VolumeSlider;