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.

104 lines
2.8 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import TransitionMotion from 'react-motion/lib/TransitionMotion';
  4. import spring from 'react-motion/lib/spring';
  5. import BundleContainer from '../containers/bundle_container';
  6. import BundleModalError from './bundle_modal_error';
  7. import ModalLoading from './modal_loading';
  8. import {
  9. MediaModal,
  10. OnboardingModal,
  11. VideoModal,
  12. BoostModal,
  13. ConfirmationModal,
  14. ReportModal,
  15. } from '../../../features/ui/util/async-components';
  16. const MODAL_COMPONENTS = {
  17. 'MEDIA': MediaModal,
  18. 'ONBOARDING': OnboardingModal,
  19. 'VIDEO': VideoModal,
  20. 'BOOST': BoostModal,
  21. 'CONFIRM': ConfirmationModal,
  22. 'REPORT': ReportModal,
  23. };
  24. export default class ModalRoot extends React.PureComponent {
  25. static propTypes = {
  26. type: PropTypes.string,
  27. props: PropTypes.object,
  28. onClose: PropTypes.func.isRequired,
  29. };
  30. handleKeyUp = (e) => {
  31. if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27)
  32. && !!this.props.type) {
  33. this.props.onClose();
  34. }
  35. }
  36. componentDidMount () {
  37. window.addEventListener('keyup', this.handleKeyUp, false);
  38. }
  39. componentWillUnmount () {
  40. window.removeEventListener('keyup', this.handleKeyUp);
  41. }
  42. willEnter () {
  43. return { opacity: 0, scale: 0.98 };
  44. }
  45. willLeave () {
  46. return { opacity: spring(0), scale: spring(0.98) };
  47. }
  48. renderLoading = () => {
  49. return <ModalLoading />;
  50. }
  51. renderError = (props) => {
  52. const { onClose } = this.props;
  53. return <BundleModalError {...props} onClose={onClose} />;
  54. }
  55. render () {
  56. const { type, props, onClose } = this.props;
  57. const visible = !!type;
  58. const items = [];
  59. if (visible) {
  60. items.push({
  61. key: type,
  62. data: { type, props },
  63. style: { opacity: spring(1), scale: spring(1, { stiffness: 120, damping: 14 }) },
  64. });
  65. }
  66. return (
  67. <TransitionMotion
  68. styles={items}
  69. willEnter={this.willEnter}
  70. willLeave={this.willLeave}
  71. >
  72. {interpolatedStyles =>
  73. <div className='modal-root'>
  74. {interpolatedStyles.map(({ key, data: { type, props }, style }) => (
  75. <div key={key} style={{ pointerEvents: visible ? 'auto' : 'none' }}>
  76. <div role='presentation' className='modal-root__overlay' style={{ opacity: style.opacity }} onClick={onClose} />
  77. <div className='modal-root__container' style={{ opacity: style.opacity, transform: `translateZ(0px) scale(${style.scale})` }}>
  78. <BundleContainer fetchComponent={MODAL_COMPONENTS[type]} loading={this.renderLoading} error={this.renderError} renderDelay={200}>
  79. {(SpecificComponent) => <SpecificComponent {...props} onClose={onClose} />}
  80. </BundleContainer>
  81. </div>
  82. </div>
  83. ))}
  84. </div>
  85. }
  86. </TransitionMotion>
  87. );
  88. }
  89. }