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.

91 lines
2.3 KiB

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