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.

110 lines
2.9 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. }
  35. componentDidUpdate (prevProps) {
  36. if (!this.props.children && !!prevProps.children) {
  37. this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
  38. this.activeElement.focus();
  39. this.activeElement = null;
  40. this.handleModalClose();
  41. }
  42. if (this.props.children) {
  43. requestAnimationFrame(() => {
  44. this.setState({ revealed: true });
  45. });
  46. if (!prevProps.children) this.handleModalOpen();
  47. }
  48. }
  49. componentWillUnmount () {
  50. window.removeEventListener('keyup', this.handleKeyUp);
  51. }
  52. handleModalClose () {
  53. this.unlistenHistory();
  54. const state = this.history.location.state;
  55. if (state && state.mastodonModalOpen) {
  56. this.history.goBack();
  57. }
  58. }
  59. handleModalOpen () {
  60. const history = this.history;
  61. const state = {...history.location.state, mastodonModalOpen: true};
  62. history.push(history.location.pathname, state);
  63. this.unlistenHistory = history.listen(() => {
  64. this.props.onClose();
  65. });
  66. }
  67. getSiblings = () => {
  68. return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
  69. }
  70. setRef = ref => {
  71. this.node = ref;
  72. }
  73. render () {
  74. const { children, onClose } = this.props;
  75. const { revealed } = this.state;
  76. const visible = !!children;
  77. if (!visible) {
  78. return (
  79. <div className='modal-root' ref={this.setRef} style={{ opacity: 0 }} />
  80. );
  81. }
  82. return (
  83. <div className='modal-root' ref={this.setRef} style={{ opacity: revealed ? 1 : 0 }}>
  84. <div style={{ pointerEvents: visible ? 'auto' : 'none' }}>
  85. <div role='presentation' className='modal-root__overlay' onClick={onClose} />
  86. <div role='dialog' className='modal-root__container'>{children}</div>
  87. </div>
  88. </div>
  89. );
  90. }
  91. }