|
| 1 | +import {Component} from 'react'; |
| 2 | +import {render} from 'react-universal-interface'; |
| 3 | +import {h, isClient, on, off, noop} from '../util'; |
| 4 | +import {Overlay} from '../Overlay'; |
| 5 | + |
| 6 | +let cnt = 0; |
| 7 | +let id = 0; |
| 8 | + |
| 9 | +const ESC = 27; |
| 10 | + |
| 11 | +export interface IModalProps { |
| 12 | + onElement?: (el: HTMLDivElement) => void; |
| 13 | + onEsc?: (event) => void; |
| 14 | +} |
| 15 | + |
| 16 | +export interface IModalState { |
| 17 | +} |
| 18 | + |
| 19 | +class Modal extends Component<IModalProps, IModalState> { |
| 20 | + id: number; |
| 21 | + el: HTMLElement = null; |
| 22 | + activeEl: Element; // Previous active element; |
| 23 | + |
| 24 | + constructor (props, context) { |
| 25 | + super(props, context); |
| 26 | + |
| 27 | + cnt++; |
| 28 | + this.id = id++; |
| 29 | + |
| 30 | + this.state = { |
| 31 | + bindTitle: { |
| 32 | + id: 'dialog-title-' + this.id |
| 33 | + }, |
| 34 | + bindDescr: { |
| 35 | + id: 'dialog-descr-' + this.id |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + this.activeEl = isClient ? document.activeElement : null; |
| 40 | + } |
| 41 | + |
| 42 | + componentDidMount () { |
| 43 | + on(document, 'keydown', this.onKey); |
| 44 | + |
| 45 | + setTimeout(() => { |
| 46 | + const firstFocusableElement = this.el.querySelector('button, [href], input, select, textarea, [tabindex]:not(tabindex="-1"]') as HTMLElement; |
| 47 | + |
| 48 | + if (firstFocusableElement && firstFocusableElement.focus) { |
| 49 | + firstFocusableElement.focus(); |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + componentWillUnmount () { |
| 55 | + cnt--; |
| 56 | + |
| 57 | + off(document, 'keydown', this.onKey); |
| 58 | + |
| 59 | + const siblings = Array.from(document.body.children); |
| 60 | + |
| 61 | + for (let i = 0; i < siblings.length; i++) { |
| 62 | + const sibling = siblings[i] as HTMLElement; |
| 63 | + |
| 64 | + if (sibling === this.el) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + if ((sibling as any).__modal_lock !== this) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + delete (sibling as any).__modal_lock; |
| 73 | + (sibling as any).inert = false; |
| 74 | + sibling.style.removeProperty('pointer-events'); |
| 75 | + sibling.removeAttribute('aria-hidden'); |
| 76 | + } |
| 77 | + |
| 78 | + // Focus previously active element. |
| 79 | + if (this.activeEl && (this.activeEl as any).focus) { |
| 80 | + (this.activeEl as any).focus(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + onElement = (el) => { |
| 85 | + this.el = el; |
| 86 | + |
| 87 | + el.setAttribute('role', 'dialog'); |
| 88 | + el.classList.add('dialog'); |
| 89 | + |
| 90 | + el.setAttribute('aria-labelledby', 'dialog-title-' + this.id); |
| 91 | + el.setAttribute('aria-describedby', 'dialog-descr-' + this.id); |
| 92 | + |
| 93 | + const siblings = Array.from(document.body.children); |
| 94 | + |
| 95 | + for (let i = 0; i < siblings.length; i++) { |
| 96 | + const sibling = siblings[i] as HTMLElement; |
| 97 | + |
| 98 | + if (sibling === el) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + if ((sibling as any).__modal_lock) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + (sibling as any).__modal_lock = this; |
| 107 | + (sibling as any).inert = true; |
| 108 | + sibling.style.setProperty('pointer-events', 'none'); |
| 109 | + sibling.setAttribute('aria-hidden', 'true'); |
| 110 | + } |
| 111 | + |
| 112 | + (this.props.onElement || noop)(el); |
| 113 | + }; |
| 114 | + |
| 115 | + onKey = (event) => { |
| 116 | + if (event.keyCode === ESC) { |
| 117 | + (this.props.onEsc || noop)(event); |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + render () { |
| 122 | + return h(Overlay, { |
| 123 | + onElement: this.onElement |
| 124 | + }, render(this.props, this.state)); |
| 125 | + } |
| 126 | +} |
0 commit comments