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.

108 lines
2.9 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. renderModal = (SpecificComponent) => {
  49. const { props, onClose } = this.props;
  50. return <SpecificComponent {...props} onClose={onClose} />;
  51. }
  52. renderLoading = () => {
  53. return <ModalLoading />;
  54. }
  55. renderError = (props) => {
  56. const { onClose } = this.props;
  57. return <BundleModalError {...props} onClose={onClose} />;
  58. }
  59. render () {
  60. const { type, props, onClose } = this.props;
  61. const visible = !!type;
  62. const items = [];
  63. if (visible) {
  64. items.push({
  65. key: type,
  66. data: { type, props },
  67. style: { opacity: spring(1), scale: spring(1, { stiffness: 120, damping: 14 }) },
  68. });
  69. }
  70. return (
  71. <TransitionMotion
  72. styles={items}
  73. willEnter={this.willEnter}
  74. willLeave={this.willLeave}
  75. >
  76. {interpolatedStyles =>
  77. <div className='modal-root'>
  78. {interpolatedStyles.map(({ key, data: { type }, style }) => (
  79. <div key={key} style={{ pointerEvents: visible ? 'auto' : 'none' }}>
  80. <div role='presentation' className='modal-root__overlay' style={{ opacity: style.opacity }} onClick={onClose} />
  81. <div className='modal-root__container' style={{ opacity: style.opacity, transform: `translateZ(0px) scale(${style.scale})` }}>
  82. <BundleContainer fetchComponent={MODAL_COMPONENTS[type]} loading={this.renderLoading} error={this.renderError} renderDelay={200}>{this.renderModal}</BundleContainer>
  83. </div>
  84. </div>
  85. ))}
  86. </div>
  87. }
  88. </TransitionMotion>
  89. );
  90. }
  91. }