The code powering m.abunchtell.com https://m.abunchtell.com
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

110 righe
3.0 KiB

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