| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx } from "react/jsx-runtime"; |
| 3 |
import { Component } from 'react'; |
| 4 |
import * as ReactDOM from 'react-dom'; |
| 5 |
import { canUseDOM, KeyTypes } from '../../helpers'; |
| 6 |
import { css } from '@patternfly/react-styles'; |
| 7 |
import styles from '@patternfly/react-styles/css/components/Backdrop/backdrop.mjs'; |
| 8 |
import { ModalContent } from './ModalContent'; |
| 9 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 10 |
export var ModalVariant; |
| 11 |
(function (ModalVariant) { |
| 12 |
ModalVariant["small"] = "small"; |
| 13 |
ModalVariant["medium"] = "medium"; |
| 14 |
ModalVariant["large"] = "large"; |
| 15 |
ModalVariant["default"] = "default"; |
| 16 |
})(ModalVariant || (ModalVariant = {})); |
| 17 |
class Modal extends Component { |
| 18 |
constructor(props) { |
| 19 |
super(props); |
| 20 |
this.boxId = ''; |
| 21 |
this.backdropId = ''; |
| 22 |
this.handleEscKeyClick = (event) => { |
| 23 |
var _a, _b; |
| 24 |
const { onEscapePress } = this.props; |
| 25 |
if (event.key === KeyTypes.Escape && this.props.isOpen) { |
| 26 |
onEscapePress ? onEscapePress(event) : (_b = (_a = this.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a, event); |
| 27 |
} |
| 28 |
}; |
| 29 |
this.getElement = (appendTo) => { |
| 30 |
if (typeof appendTo === 'function') { |
| 31 |
return appendTo(); |
| 32 |
} |
| 33 |
return appendTo || document.body; |
| 34 |
}; |
| 35 |
this.toggleSiblingsFromScreenReaders = (hide) => { |
| 36 |
const { appendTo } = this.props; |
| 37 |
const target = this.getElement(appendTo); |
| 38 |
const bodyChildren = target.children; |
| 39 |
for (const child of Array.from(bodyChildren)) { |
| 40 |
if (child.id !== this.backdropId) { |
| 41 |
hide ? child.setAttribute('aria-hidden', '' + hide) : child.removeAttribute('aria-hidden'); |
| 42 |
} |
| 43 |
} |
| 44 |
}; |
| 45 |
this.isEmpty = (value) => value === null || value === undefined || value === ''; |
| 46 |
const boxIdNum = Modal.currentId++; |
| 47 |
const backdropId = boxIdNum + 1; |
| 48 |
this.boxId = props.id || `pf-modal-part-${boxIdNum}`; |
| 49 |
this.backdropId = `pf-modal-part-${backdropId}`; |
| 50 |
this.state = { |
| 51 |
mounted: false |
| 52 |
}; |
| 53 |
} |
| 54 |
componentDidMount() { |
| 55 |
this.setState({ mounted: true }); |
| 56 |
const { appendTo } = this.props; |
| 57 |
const target = this.getElement(appendTo); |
| 58 |
target.addEventListener('keydown', this.handleEscKeyClick, false); |
| 59 |
if (this.props.isOpen) { |
| 60 |
target.classList.add(css(styles.backdropOpen)); |
| 61 |
this.toggleSiblingsFromScreenReaders(true); |
| 62 |
} |
| 63 |
} |
| 64 |
componentDidUpdate(prevProps) { |
| 65 |
const { appendTo } = this.props; |
| 66 |
const target = this.getElement(appendTo); |
| 67 |
if (this.props.isOpen) { |
| 68 |
target.classList.add(css(styles.backdropOpen)); |
| 69 |
this.toggleSiblingsFromScreenReaders(true); |
| 70 |
} |
| 71 |
else { |
| 72 |
if (prevProps.isOpen !== this.props.isOpen) { |
| 73 |
target.classList.remove(css(styles.backdropOpen)); |
| 74 |
this.toggleSiblingsFromScreenReaders(false); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
componentWillUnmount() { |
| 79 |
const { appendTo } = this.props; |
| 80 |
const target = this.getElement(appendTo); |
| 81 |
target.removeEventListener('keydown', this.handleEscKeyClick, false); |
| 82 |
target.classList.remove(css(styles.backdropOpen)); |
| 83 |
this.toggleSiblingsFromScreenReaders(false); |
| 84 |
} |
| 85 |
render() { |
| 86 |
const _a = this.props, { appendTo, |
| 87 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 88 |
onEscapePress, 'aria-labelledby': ariaLabelledby, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedby, ouiaId, ouiaSafe, position, elementToFocus } = _a, props = __rest(_a, ["appendTo", "onEscapePress", 'aria-labelledby', 'aria-label', 'aria-describedby', "ouiaId", "ouiaSafe", "position", "elementToFocus"]); |
| 89 |
if (!this.state.mounted || !canUseDOM || !this.getElement(appendTo)) { |
| 90 |
return null; |
| 91 |
} |
| 92 |
return (_jsx(SSRSafeIds, { prefix: "pf-", ouiaComponentType: `Modal${this.props.variant ? `-${this.props.variant}` : ''}`, children: (_, generatedOuiaId) => ReactDOM.createPortal(_jsx(ModalContent, Object.assign({ boxId: this.boxId, backdropId: this.backdropId, "aria-label": ariaLabel, "aria-describedby": ariaDescribedby, "aria-labelledby": ariaLabelledby, ouiaId: ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe: ouiaSafe, position: position, elementToFocus: elementToFocus, backdropClassName: props.backdropClassName }, props)), this.getElement(appendTo)) })); |
| 93 |
} |
| 94 |
} |
| 95 |
Modal.displayName = 'Modal'; |
| 96 |
Modal.currentId = 0; |
| 97 |
Modal.defaultProps = { |
| 98 |
isOpen: false, |
| 99 |
variant: 'default', |
| 100 |
appendTo: () => document.body, |
| 101 |
ouiaSafe: true, |
| 102 |
position: 'default' |
| 103 |
}; |
| 104 |
export { Modal }; |
| 105 |
//# sourceMappingURL=Modal.js.map |