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.

125 lines
3.6 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import BundleContainer from '../containers/bundle_container';
  4. import BundleModalError from './bundle_modal_error';
  5. import ModalLoading from './modal_loading';
  6. import ActionsModal from './actions_modal';
  7. import MediaModal from './media_modal';
  8. import VideoModal from './video_modal';
  9. import BoostModal from './boost_modal';
  10. import ConfirmationModal from './confirmation_modal';
  11. import {
  12. OnboardingModal,
  13. ReportModal,
  14. EmbedModal,
  15. } from '../../../features/ui/util/async-components';
  16. const MODAL_COMPONENTS = {
  17. 'MEDIA': () => Promise.resolve({ default: MediaModal }),
  18. 'ONBOARDING': OnboardingModal,
  19. 'VIDEO': () => Promise.resolve({ default: VideoModal }),
  20. 'BOOST': () => Promise.resolve({ default: BoostModal }),
  21. 'CONFIRM': () => Promise.resolve({ default: ConfirmationModal }),
  22. 'REPORT': ReportModal,
  23. 'ACTIONS': () => Promise.resolve({ default: ActionsModal }),
  24. 'EMBED': EmbedModal,
  25. };
  26. export default class ModalRoot extends React.PureComponent {
  27. static propTypes = {
  28. type: PropTypes.string,
  29. props: PropTypes.object,
  30. onClose: PropTypes.func.isRequired,
  31. };
  32. state = {
  33. revealed: false,
  34. };
  35. handleKeyUp = (e) => {
  36. if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27)
  37. && !!this.props.type) {
  38. this.props.onClose();
  39. }
  40. }
  41. componentDidMount () {
  42. window.addEventListener('keyup', this.handleKeyUp, false);
  43. }
  44. componentWillReceiveProps (nextProps) {
  45. if (!!nextProps.type && !this.props.type) {
  46. this.activeElement = document.activeElement;
  47. this.getSiblings().forEach(sibling => sibling.setAttribute('inert', true));
  48. } else if (!nextProps.type) {
  49. this.setState({ revealed: false });
  50. }
  51. }
  52. componentDidUpdate (prevProps) {
  53. if (!this.props.type && !!prevProps.type) {
  54. this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
  55. this.activeElement.focus();
  56. this.activeElement = null;
  57. }
  58. if (this.props.type) {
  59. requestAnimationFrame(() => {
  60. this.setState({ revealed: true });
  61. });
  62. }
  63. }
  64. componentWillUnmount () {
  65. window.removeEventListener('keyup', this.handleKeyUp);
  66. }
  67. getSiblings = () => {
  68. return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
  69. }
  70. setRef = ref => {
  71. this.node = ref;
  72. }
  73. renderLoading = modalId => () => {
  74. return ['MEDIA', 'VIDEO', 'BOOST', 'CONFIRM', 'ACTIONS'].indexOf(modalId) === -1 ? <ModalLoading /> : null;
  75. }
  76. renderError = (props) => {
  77. const { onClose } = this.props;
  78. return <BundleModalError {...props} onClose={onClose} />;
  79. }
  80. render () {
  81. const { type, props, onClose } = this.props;
  82. const { revealed } = this.state;
  83. const visible = !!type;
  84. if (!visible) {
  85. return (
  86. <div className='modal-root' ref={this.setRef} style={{ opacity: 0 }} />
  87. );
  88. }
  89. return (
  90. <div className='modal-root' ref={this.setRef} style={{ opacity: revealed ? 1 : 0 }}>
  91. <div style={{ pointerEvents: visible ? 'auto' : 'none' }}>
  92. <div role='presentation' className='modal-root__overlay' onClick={onClose} />
  93. <div role='dialog' className='modal-root__container'>
  94. {
  95. visible ?
  96. (<BundleContainer fetchComponent={MODAL_COMPONENTS[type]} loading={this.renderLoading(type)} error={this.renderError} renderDelay={200}>
  97. {(SpecificComponent) => <SpecificComponent {...props} onClose={onClose} />}
  98. </BundleContainer>) :
  99. null
  100. }
  101. </div>
  102. </div>
  103. </div>
  104. );
  105. }
  106. }