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.

ToolbarButtonWithDialog.web.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import InlineDialog from '@atlaskit/inline-dialog';
  2. import PropTypes from 'prop-types';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import { TOOLTIP_TO_POPUP_POSITION } from '../constants';
  6. import ToolbarButton from './ToolbarButton';
  7. /**
  8. * React {@code Component} for displaying a button which will open an inline
  9. * dialog.
  10. *
  11. * @extends Component
  12. */
  13. class ToolbarButtonWithDialog extends Component {
  14. /**
  15. * {@code ToolbarButtonWithDialog}'s property types.
  16. *
  17. * @static
  18. */
  19. static propTypes = {
  20. /**
  21. * Whether or not the button is visible, based on the visibility of the
  22. * toolbar. Used to automatically hide {@code InlineDialog} if not
  23. * visible.
  24. */
  25. _visible: PropTypes.bool,
  26. /**
  27. * A configuration object to describe how {@code ToolbarButton} should
  28. * render.
  29. *
  30. */
  31. button: PropTypes.object,
  32. /**
  33. * The React Component to show within {@code InlineDialog}.
  34. */
  35. content: PropTypes.func,
  36. /**
  37. * From which side tooltips should display. Will be re-used for
  38. * displaying the inline dialog for video quality adjustment.
  39. */
  40. tooltipPosition: PropTypes.string
  41. };
  42. /**
  43. * Initializes new {@code ToolbarButtonWithDialog} instance.
  44. *
  45. * @param {Object} props - The read-only properties with which the new
  46. * instance is to be initialized.
  47. */
  48. constructor(props) {
  49. super(props);
  50. this.state = {
  51. /**
  52. * Whether or not the inline dialog should be displayed.
  53. */
  54. showDialog: false
  55. };
  56. // Bind event handlers so they are only bound once for every instance.
  57. this._onDialogClose = this._onDialogClose.bind(this);
  58. this._onDialogToggle = this._onDialogToggle.bind(this);
  59. }
  60. /**
  61. * Automatically close the inline dialog if the button will not be visible.
  62. *
  63. * @inheritdoc
  64. * @returns {void}
  65. */
  66. componentWillReceiveProps(nextProps) {
  67. if (!nextProps._visible) {
  68. this._onDialogClose();
  69. }
  70. }
  71. /**
  72. * Implements React's {@link Component#render()}.
  73. *
  74. * @inheritdoc
  75. * @returns {ReactElement}
  76. */
  77. render() {
  78. const { _visible, content, tooltipPosition } = this.props;
  79. const buttonConfiguration = {
  80. ...this.props.button,
  81. classNames: [
  82. ...this.props.button.classNames,
  83. this.state.showDialog ? 'toggled button-active' : ''
  84. ]
  85. };
  86. const Content = content;
  87. return (
  88. <InlineDialog
  89. content = { <Content onClose = { this._onDialogClose } /> }
  90. isOpen = { _visible && this.state.showDialog }
  91. onClose = { this._onDialogClose }
  92. position = { TOOLTIP_TO_POPUP_POSITION[tooltipPosition] }>
  93. <ToolbarButton
  94. button = { buttonConfiguration }
  95. onClick = { this._onDialogToggle }
  96. tooltipPosition = { tooltipPosition } />
  97. </InlineDialog>
  98. );
  99. }
  100. /**
  101. * Hides the attached inline dialog.
  102. *
  103. * @private
  104. * @returns {void}
  105. */
  106. _onDialogClose() {
  107. this.setState({ showDialog: false });
  108. }
  109. /**
  110. * Toggles the display of the dialog.
  111. *
  112. * @private
  113. * @returns {void}
  114. */
  115. _onDialogToggle() {
  116. this.setState({
  117. showDialog: !this.state.showDialog
  118. });
  119. }
  120. }
  121. /**
  122. * Maps (parts of) the Redux state to the associated
  123. * {@code ToolbarButtonWithDialog} component's props.
  124. *
  125. * @param {Object} state - The Redux state.
  126. * @private
  127. * @returns {{
  128. * _visible: boolean
  129. * }}
  130. */
  131. function _mapStateToProps(state) {
  132. return {
  133. _visible: state['features/toolbox'].visible
  134. };
  135. }
  136. export default connect(_mapStateToProps)(ToolbarButtonWithDialog);