闭社主体 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.

132 lines
3.7 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 ActionsModal from '../components/actions_modal';
  9. import {
  10. MediaModal,
  11. OnboardingModal,
  12. VideoModal,
  13. BoostModal,
  14. ConfirmationModal,
  15. ReportModal,
  16. EmbedModal,
  17. } from '../../../features/ui/util/async-components';
  18. const MODAL_COMPONENTS = {
  19. 'MEDIA': MediaModal,
  20. 'ONBOARDING': OnboardingModal,
  21. 'VIDEO': VideoModal,
  22. 'BOOST': BoostModal,
  23. 'CONFIRM': ConfirmationModal,
  24. 'REPORT': ReportModal,
  25. 'ACTIONS': () => Promise.resolve({ default: ActionsModal }),
  26. 'EMBED': EmbedModal,
  27. };
  28. export default class ModalRoot extends React.PureComponent {
  29. static propTypes = {
  30. type: PropTypes.string,
  31. props: PropTypes.object,
  32. onClose: PropTypes.func.isRequired,
  33. };
  34. handleKeyUp = (e) => {
  35. if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27)
  36. && !!this.props.type) {
  37. this.props.onClose();
  38. }
  39. }
  40. componentDidMount () {
  41. window.addEventListener('keyup', this.handleKeyUp, false);
  42. }
  43. componentWillReceiveProps (nextProps) {
  44. if (!!nextProps.type && !this.props.type) {
  45. this.activeElement = document.activeElement;
  46. this.getSiblings().forEach(sibling => sibling.setAttribute('inert', true));
  47. }
  48. }
  49. componentDidUpdate (prevProps) {
  50. if (!this.props.type && !!prevProps.type) {
  51. this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
  52. this.activeElement.focus();
  53. this.activeElement = null;
  54. }
  55. }
  56. componentWillUnmount () {
  57. window.removeEventListener('keyup', this.handleKeyUp);
  58. }
  59. getSiblings = () => {
  60. return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
  61. }
  62. setRef = ref => {
  63. this.node = ref;
  64. }
  65. willEnter () {
  66. return { opacity: 0, scale: 0.98 };
  67. }
  68. willLeave () {
  69. return { opacity: spring(0), scale: spring(0.98) };
  70. }
  71. renderLoading = () => {
  72. return <ModalLoading />;
  73. }
  74. renderError = (props) => {
  75. const { onClose } = this.props;
  76. return <BundleModalError {...props} onClose={onClose} />;
  77. }
  78. render () {
  79. const { type, props, onClose } = this.props;
  80. const visible = !!type;
  81. const items = [];
  82. if (visible) {
  83. items.push({
  84. key: type,
  85. data: { type, props },
  86. style: { opacity: spring(1), scale: spring(1, { stiffness: 120, damping: 14 }) },
  87. });
  88. }
  89. return (
  90. <TransitionMotion
  91. styles={items}
  92. willEnter={this.willEnter}
  93. willLeave={this.willLeave}
  94. >
  95. {interpolatedStyles =>
  96. <div className='modal-root' ref={this.setRef}>
  97. {interpolatedStyles.map(({ key, data: { type, props }, style }) => (
  98. <div key={key} style={{ pointerEvents: visible ? 'auto' : 'none' }}>
  99. <div role='presentation' className='modal-root__overlay' style={{ opacity: style.opacity }} onClick={onClose} />
  100. <div role='dialog' className='modal-root__container' style={{ opacity: style.opacity, transform: `translateZ(0px) scale(${style.scale})` }}>
  101. <BundleContainer fetchComponent={MODAL_COMPONENTS[type]} loading={this.renderLoading} error={this.renderError} renderDelay={200}>
  102. {(SpecificComponent) => <SpecificComponent {...props} onClose={onClose} />}
  103. </BundleContainer>
  104. </div>
  105. </div>
  106. ))}
  107. </div>
  108. }
  109. </TransitionMotion>
  110. );
  111. }
  112. }