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.

90 lines
2.5 KiB

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