闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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