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.

92 lines
2.4 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' || e.key === 'Esc' || e.keyCode === 27)
  22. && !!this.props.type) {
  23. this.props.onClose();
  24. }
  25. }
  26. componentDidMount () {
  27. window.addEventListener('keyup', this.handleKeyUp, false);
  28. }
  29. componentWillUnmount () {
  30. window.removeEventListener('keyup', this.handleKeyUp);
  31. }
  32. willEnter () {
  33. return { opacity: 0, scale: 0.98 };
  34. }
  35. willLeave () {
  36. return { opacity: spring(0), scale: spring(0.98) };
  37. }
  38. render () {
  39. const { type, props, onClose } = this.props;
  40. const items = [];
  41. if (!!type) {
  42. items.push({
  43. key: type,
  44. data: { type, props },
  45. style: { opacity: spring(1), scale: spring(1, { stiffness: 120, damping: 14 }) }
  46. });
  47. }
  48. return (
  49. <TransitionMotion
  50. styles={items}
  51. willEnter={this.willEnter}
  52. willLeave={this.willLeave}>
  53. {interpolatedStyles =>
  54. <div className='modal-root'>
  55. {interpolatedStyles.map(({ key, data: { type, props }, style }) => {
  56. const SpecificComponent = MODAL_COMPONENTS[type];
  57. return (
  58. <div key={key}>
  59. <div role='presentation' className='modal-root__overlay' style={{ opacity: style.opacity }} onClick={onClose} />
  60. <div className='modal-root__container' style={{ opacity: style.opacity, transform: `translateZ(0px) scale(${style.scale})` }}>
  61. <SpecificComponent {...props} onClose={onClose} />
  62. </div>
  63. </div>
  64. );
  65. })}
  66. </div>
  67. }
  68. </TransitionMotion>
  69. );
  70. }
  71. }
  72. ModalRoot.propTypes = {
  73. type: PropTypes.string,
  74. props: PropTypes.object,
  75. onClose: PropTypes.func.isRequired
  76. };
  77. export default ModalRoot;