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.

114 lines
3.4 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import 'wicg-inert';
  4. import { multiply } from 'color-blend';
  5. export default class ModalRoot extends React.PureComponent {
  6. static propTypes = {
  7. children: PropTypes.node,
  8. onClose: PropTypes.func.isRequired,
  9. backgroundColor: PropTypes.shape({
  10. r: PropTypes.number,
  11. g: PropTypes.number,
  12. b: PropTypes.number,
  13. }),
  14. };
  15. activeElement = this.props.children ? document.activeElement : null;
  16. handleKeyUp = (e) => {
  17. if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27)
  18. && !!this.props.children) {
  19. this.props.onClose();
  20. }
  21. }
  22. handleKeyDown = (e) => {
  23. if (e.key === 'Tab') {
  24. 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');
  25. const index = focusable.indexOf(e.target);
  26. let element;
  27. if (e.shiftKey) {
  28. element = focusable[index - 1] || focusable[focusable.length - 1];
  29. } else {
  30. element = focusable[index + 1] || focusable[0];
  31. }
  32. if (element) {
  33. element.focus();
  34. e.stopPropagation();
  35. e.preventDefault();
  36. }
  37. }
  38. }
  39. componentDidMount () {
  40. window.addEventListener('keyup', this.handleKeyUp, false);
  41. window.addEventListener('keydown', this.handleKeyDown, false);
  42. }
  43. componentWillReceiveProps (nextProps) {
  44. if (!!nextProps.children && !this.props.children) {
  45. this.activeElement = document.activeElement;
  46. this.getSiblings().forEach(sibling => sibling.setAttribute('inert', true));
  47. }
  48. }
  49. componentDidUpdate (prevProps) {
  50. if (!this.props.children && !!prevProps.children) {
  51. this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
  52. // Because of the wicg-inert polyfill, the activeElement may not be
  53. // immediately selectable, we have to wait for observers to run, as
  54. // described in https://github.com/WICG/inert#performance-and-gotchas
  55. Promise.resolve().then(() => {
  56. this.activeElement.focus({ preventScroll: true });
  57. this.activeElement = null;
  58. }).catch(console.error);
  59. }
  60. }
  61. componentWillUnmount () {
  62. window.removeEventListener('keyup', this.handleKeyUp);
  63. window.removeEventListener('keydown', this.handleKeyDown);
  64. }
  65. getSiblings = () => {
  66. return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
  67. }
  68. setRef = ref => {
  69. this.node = ref;
  70. }
  71. render () {
  72. const { children, onClose } = this.props;
  73. const visible = !!children;
  74. if (!visible) {
  75. return (
  76. <div className='modal-root' ref={this.setRef} style={{ opacity: 0 }} />
  77. );
  78. }
  79. let backgroundColor = null;
  80. if (this.props.backgroundColor) {
  81. backgroundColor = multiply({ ...this.props.backgroundColor, a: 1 }, { r: 0, g: 0, b: 0, a: 0.7 });
  82. }
  83. return (
  84. <div className='modal-root' ref={this.setRef}>
  85. <div style={{ pointerEvents: visible ? 'auto' : 'none' }}>
  86. <div role='presentation' className='modal-root__overlay' onClick={onClose} style={{ backgroundColor: backgroundColor ? `rgba(${backgroundColor.r}, ${backgroundColor.g}, ${backgroundColor.b}, 0.7)` : null }} />
  87. <div role='dialog' className='modal-root__container'>{children}</div>
  88. </div>
  89. </div>
  90. );
  91. }
  92. }