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
3.0 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import 'wicg-inert';
  4. export default class ModalRoot extends React.PureComponent {
  5. static propTypes = {
  6. children: PropTypes.node,
  7. onClose: PropTypes.func.isRequired,
  8. };
  9. state = {
  10. revealed: !!this.props.children,
  11. };
  12. activeElement = this.state.revealed ? document.activeElement : null;
  13. handleKeyUp = (e) => {
  14. if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27)
  15. && !!this.props.children) {
  16. this.props.onClose();
  17. }
  18. }
  19. handleKeyDown = (e) => {
  20. if (e.key === 'Tab') {
  21. const focusable = Array.from(this.node.querySelectorAll('button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])')).filter((x) => window.getComputedStyle(x).display !== 'none');
  22. const index = focusable.indexOf(e.target);
  23. let element;
  24. if (e.shiftKey) {
  25. element = focusable[index - 1] || focusable[focusable.length - 1];
  26. } else {
  27. element = focusable[index + 1] || focusable[0];
  28. }
  29. if (element) {
  30. element.focus();
  31. e.stopPropagation();
  32. e.preventDefault();
  33. }
  34. }
  35. }
  36. componentDidMount () {
  37. window.addEventListener('keyup', this.handleKeyUp, false);
  38. window.addEventListener('keydown', this.handleKeyDown, false);
  39. }
  40. componentWillReceiveProps (nextProps) {
  41. if (!!nextProps.children && !this.props.children) {
  42. this.activeElement = document.activeElement;
  43. this.getSiblings().forEach(sibling => sibling.setAttribute('inert', true));
  44. } else if (!nextProps.children) {
  45. this.setState({ revealed: false });
  46. }
  47. if (!nextProps.children && !!this.props.children) {
  48. this.activeElement.focus();
  49. this.activeElement = null;
  50. }
  51. }
  52. componentDidUpdate (prevProps) {
  53. if (!this.props.children && !!prevProps.children) {
  54. this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
  55. }
  56. if (this.props.children) {
  57. requestAnimationFrame(() => {
  58. this.setState({ revealed: true });
  59. });
  60. }
  61. }
  62. componentWillUnmount () {
  63. window.removeEventListener('keyup', this.handleKeyUp);
  64. window.removeEventListener('keydown', this.handleKeyDown);
  65. }
  66. getSiblings = () => {
  67. return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
  68. }
  69. setRef = ref => {
  70. this.node = ref;
  71. }
  72. render () {
  73. const { children, onClose } = this.props;
  74. const { revealed } = this.state;
  75. const visible = !!children;
  76. if (!visible) {
  77. return (
  78. <div className='modal-root' ref={this.setRef} style={{ opacity: 0 }} />
  79. );
  80. }
  81. return (
  82. <div className='modal-root' ref={this.setRef} style={{ opacity: revealed ? 1 : 0 }}>
  83. <div style={{ pointerEvents: visible ? 'auto' : 'none' }}>
  84. <div role='presentation' className='modal-root__overlay' onClick={onClose} />
  85. <div role='dialog' className='modal-root__container'>{children}</div>
  86. </div>
  87. </div>
  88. );
  89. }
  90. }