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.

70 lines
1.6 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Icon from 'mastodon/components/icon';
  4. import { removePictureInPicture } from 'mastodon/actions/picture_in_picture';
  5. import { connect } from 'react-redux';
  6. import { debounce } from 'lodash';
  7. import { FormattedMessage } from 'react-intl';
  8. class PictureInPicturePlaceholder extends React.PureComponent {
  9. static propTypes = {
  10. width: PropTypes.number,
  11. dispatch: PropTypes.func.isRequired,
  12. };
  13. state = {
  14. width: this.props.width,
  15. height: this.props.width && (this.props.width / (16/9)),
  16. };
  17. handleClick = () => {
  18. const { dispatch } = this.props;
  19. dispatch(removePictureInPicture());
  20. };
  21. setRef = c => {
  22. this.node = c;
  23. if (this.node) {
  24. this._setDimensions();
  25. }
  26. };
  27. _setDimensions () {
  28. const width = this.node.offsetWidth;
  29. const height = width / (16/9);
  30. this.setState({ width, height });
  31. }
  32. componentDidMount () {
  33. window.addEventListener('resize', this.handleResize, { passive: true });
  34. }
  35. componentWillUnmount () {
  36. window.removeEventListener('resize', this.handleResize);
  37. }
  38. handleResize = debounce(() => {
  39. if (this.node) {
  40. this._setDimensions();
  41. }
  42. }, 250, {
  43. trailing: true,
  44. });
  45. render () {
  46. const { height } = this.state;
  47. return (
  48. <div ref={this.setRef} className='picture-in-picture-placeholder' style={{ height }} role='button' tabIndex='0' onClick={this.handleClick}>
  49. <Icon id='window-restore' />
  50. <FormattedMessage id='picture_in_picture.restore' defaultMessage='Put it back' />
  51. </div>
  52. );
  53. }
  54. }
  55. export default connect()(PictureInPicturePlaceholder);