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.

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