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.

112 lines
3.0 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import createHistory from 'history/createBrowserHistory';
  4. export default class ModalRoot extends React.PureComponent {
  5. static contextTypes = {
  6. router: PropTypes.object,
  7. };
  8. static propTypes = {
  9. children: PropTypes.node,
  10. onClose: PropTypes.func.isRequired,
  11. noEsc: PropTypes.bool,
  12. };
  13. state = {
  14. revealed: !!this.props.children,
  15. };
  16. activeElement = this.state.revealed ? document.activeElement : null;
  17. handleKeyUp = (e) => {
  18. if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27)
  19. && !!this.props.children && !this.props.noEsc) {
  20. this.props.onClose();
  21. }
  22. }
  23. componentDidMount () {
  24. window.addEventListener('keyup', this.handleKeyUp, false);
  25. this.history = this.context.router ? this.context.router.history : createHistory();
  26. }
  27. componentWillReceiveProps (nextProps) {
  28. if (!!nextProps.children && !this.props.children) {
  29. this.activeElement = document.activeElement;
  30. this.getSiblings().forEach(sibling => sibling.setAttribute('inert', true));
  31. } else if (!nextProps.children) {
  32. this.setState({ revealed: false });
  33. }
  34. if (!nextProps.children && !!this.props.children) {
  35. this.activeElement.focus();
  36. this.activeElement = null;
  37. }
  38. }
  39. componentDidUpdate (prevProps) {
  40. if (!this.props.children && !!prevProps.children) {
  41. this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
  42. this.handleModalClose();
  43. }
  44. if (this.props.children) {
  45. requestAnimationFrame(() => {
  46. this.setState({ revealed: true });
  47. });
  48. if (!prevProps.children) this.handleModalOpen();
  49. }
  50. }
  51. componentWillUnmount () {
  52. window.removeEventListener('keyup', this.handleKeyUp);
  53. }
  54. handleModalClose () {
  55. this.unlistenHistory();
  56. const state = this.history.location.state;
  57. if (state && state.mastodonModalOpen) {
  58. this.history.goBack();
  59. }
  60. }
  61. handleModalOpen () {
  62. const history = this.history;
  63. const state = {...history.location.state, mastodonModalOpen: true};
  64. history.push(history.location.pathname, state);
  65. this.unlistenHistory = history.listen(() => {
  66. this.props.onClose();
  67. });
  68. }
  69. getSiblings = () => {
  70. return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
  71. }
  72. setRef = ref => {
  73. this.node = ref;
  74. }
  75. render () {
  76. const { children, onClose } = this.props;
  77. const { revealed } = this.state;
  78. const visible = !!children;
  79. if (!visible) {
  80. return (
  81. <div className='modal-root' ref={this.setRef} style={{ opacity: 0 }} />
  82. );
  83. }
  84. return (
  85. <div className='modal-root' ref={this.setRef} style={{ opacity: revealed ? 1 : 0 }}>
  86. <div style={{ pointerEvents: visible ? 'auto' : 'none' }}>
  87. <div role='presentation' className='modal-root__overlay' onClick={onClose} />
  88. <div role='dialog' className='modal-root__container'>{children}</div>
  89. </div>
  90. </div>
  91. );
  92. }
  93. }