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.6KB

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