| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.Modal = exports.ModalVariant = void 0; |
| 4 |
const tslib_1 = require("tslib"); |
| 5 |
const jsx_runtime_1 = require("react/jsx-runtime"); |
| 6 |
const react_1 = require("react"); |
| 7 |
const ReactDOM = tslib_1.__importStar(require("react-dom")); |
| 8 |
const helpers_1 = require("../../../helpers"); |
| 9 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 10 |
const backdrop_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Backdrop/backdrop")); |
| 11 |
const ModalContent_1 = require("./ModalContent"); |
| 12 |
const SSRSafeIds_1 = require("../../../helpers/SSRSafeIds/SSRSafeIds"); |
| 13 |
var ModalVariant; |
| 14 |
(function (ModalVariant) { |
| 15 |
ModalVariant["small"] = "small"; |
| 16 |
ModalVariant["medium"] = "medium"; |
| 17 |
ModalVariant["large"] = "large"; |
| 18 |
ModalVariant["default"] = "default"; |
| 19 |
})(ModalVariant || (exports.ModalVariant = ModalVariant = {})); |
| 20 |
class Modal extends react_1.Component { |
| 21 |
constructor(props) { |
| 22 |
super(props); |
| 23 |
this.boxId = ''; |
| 24 |
this.labelId = ''; |
| 25 |
this.descriptorId = ''; |
| 26 |
this.backdropId = ''; |
| 27 |
this.handleEscKeyClick = (event) => { |
| 28 |
var _a, _b; |
| 29 |
const { onEscapePress } = this.props; |
| 30 |
if (event.key === helpers_1.KeyTypes.Escape && this.props.isOpen) { |
| 31 |
onEscapePress ? onEscapePress(event) : (_b = (_a = this.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a, event); |
| 32 |
} |
| 33 |
}; |
| 34 |
this.getElement = (appendTo) => { |
| 35 |
if (typeof appendTo === 'function') { |
| 36 |
return appendTo(); |
| 37 |
} |
| 38 |
return appendTo || document.body; |
| 39 |
}; |
| 40 |
this.toggleSiblingsFromScreenReaders = (hide) => { |
| 41 |
const { appendTo } = this.props; |
| 42 |
const target = this.getElement(appendTo); |
| 43 |
const bodyChildren = target.children; |
| 44 |
for (const child of Array.from(bodyChildren)) { |
| 45 |
if (child.id !== this.backdropId) { |
| 46 |
hide ? child.setAttribute('aria-hidden', '' + hide) : child.removeAttribute('aria-hidden'); |
| 47 |
} |
| 48 |
} |
| 49 |
}; |
| 50 |
this.isEmpty = (value) => value === null || value === undefined || value === ''; |
| 51 |
const boxIdNum = Modal.currentId++; |
| 52 |
const labelIdNum = boxIdNum + 1; |
| 53 |
const descriptorIdNum = boxIdNum + 2; |
| 54 |
const backdropIdNum = boxIdNum + 3; |
| 55 |
this.boxId = props.id || `pf-modal-part-${boxIdNum}`; |
| 56 |
this.labelId = `pf-modal-part-${labelIdNum}`; |
| 57 |
this.descriptorId = `pf-modal-part-${descriptorIdNum}`; |
| 58 |
this.backdropId = `pf-modal-part-${backdropIdNum}`; |
| 59 |
this.state = { |
| 60 |
mounted: false |
| 61 |
}; |
| 62 |
} |
| 63 |
componentDidMount() { |
| 64 |
this.setState({ mounted: true }); |
| 65 |
const { appendTo, title, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, hasNoBodyWrapper, header } = this.props; |
| 66 |
const target = this.getElement(appendTo); |
| 67 |
target.addEventListener('keydown', this.handleEscKeyClick, false); |
| 68 |
if (this.props.isOpen) { |
| 69 |
target.classList.add((0, react_styles_1.css)(backdrop_1.default.backdropOpen)); |
| 70 |
this.toggleSiblingsFromScreenReaders(true); |
| 71 |
} |
| 72 |
if (!title && this.isEmpty(ariaLabel) && this.isEmpty(ariaLabelledby)) { |
| 73 |
// eslint-disable-next-line no-console |
| 74 |
console.error('Modal: Specify at least one of: title, aria-label, aria-labelledby.'); |
| 75 |
} |
| 76 |
if (this.isEmpty(ariaLabel) && this.isEmpty(ariaLabelledby) && (hasNoBodyWrapper || header)) { |
| 77 |
// eslint-disable-next-line no-console |
| 78 |
console.error('Modal: When using hasNoBodyWrapper or setting a custom header, ensure you assign an accessible name to the the modal container with aria-label or aria-labelledby.'); |
| 79 |
} |
| 80 |
} |
| 81 |
componentDidUpdate(prevProps) { |
| 82 |
const { appendTo } = this.props; |
| 83 |
const target = this.getElement(appendTo); |
| 84 |
if (this.props.isOpen) { |
| 85 |
target.classList.add((0, react_styles_1.css)(backdrop_1.default.backdropOpen)); |
| 86 |
this.toggleSiblingsFromScreenReaders(true); |
| 87 |
} |
| 88 |
else { |
| 89 |
if (prevProps.isOpen !== this.props.isOpen) { |
| 90 |
target.classList.remove((0, react_styles_1.css)(backdrop_1.default.backdropOpen)); |
| 91 |
this.toggleSiblingsFromScreenReaders(false); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
componentWillUnmount() { |
| 96 |
const { appendTo } = this.props; |
| 97 |
const target = this.getElement(appendTo); |
| 98 |
target.removeEventListener('keydown', this.handleEscKeyClick, false); |
| 99 |
target.classList.remove((0, react_styles_1.css)(backdrop_1.default.backdropOpen)); |
| 100 |
this.toggleSiblingsFromScreenReaders(false); |
| 101 |
} |
| 102 |
render() { |
| 103 |
const _a = this.props, { appendTo, |
| 104 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 105 |
onEscapePress, 'aria-labelledby': ariaLabelledby, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedby, bodyAriaLabel, bodyAriaRole, title, titleIconVariant, titleLabel, ouiaId, ouiaSafe, position, elementToFocus } = _a, props = tslib_1.__rest(_a, ["appendTo", "onEscapePress", 'aria-labelledby', 'aria-label', 'aria-describedby', "bodyAriaLabel", "bodyAriaRole", "title", "titleIconVariant", "titleLabel", "ouiaId", "ouiaSafe", "position", "elementToFocus"]); |
| 106 |
if (!this.state.mounted || !helpers_1.canUseDOM || !this.getElement(appendTo)) { |
| 107 |
return null; |
| 108 |
} |
| 109 |
return ((0, jsx_runtime_1.jsx)(SSRSafeIds_1.SSRSafeIds, { prefix: "pf-", ouiaComponentType: `Modal${this.props.variant ? `-${this.props.variant}` : ''}`, children: (_, generatedOuiaId) => ReactDOM.createPortal((0, jsx_runtime_1.jsx)(ModalContent_1.ModalContent, Object.assign({}, props, { boxId: this.boxId, labelId: this.labelId, descriptorId: this.descriptorId, backdropId: this.backdropId, title: title, titleIconVariant: titleIconVariant, titleLabel: titleLabel, "aria-label": ariaLabel, "aria-describedby": ariaDescribedby, "aria-labelledby": ariaLabelledby, bodyAriaLabel: bodyAriaLabel, bodyAriaRole: bodyAriaRole, ouiaId: ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe: ouiaSafe, position: position, elementToFocus: elementToFocus })), this.getElement(appendTo)) })); |
| 110 |
} |
| 111 |
} |
| 112 |
exports.Modal = Modal; |
| 113 |
Modal.displayName = 'Modal'; |
| 114 |
Modal.currentId = 0; |
| 115 |
Modal.defaultProps = { |
| 116 |
className: '', |
| 117 |
isOpen: false, |
| 118 |
title: '', |
| 119 |
titleIconVariant: null, |
| 120 |
titleLabel: '', |
| 121 |
'aria-label': '', |
| 122 |
showClose: true, |
| 123 |
'aria-describedby': '', |
| 124 |
'aria-labelledby': '', |
| 125 |
id: undefined, |
| 126 |
actions: [], |
| 127 |
onClose: () => undefined, |
| 128 |
variant: 'default', |
| 129 |
hasNoBodyWrapper: false, |
| 130 |
appendTo: () => document.body, |
| 131 |
ouiaSafe: true, |
| 132 |
position: 'default' |
| 133 |
}; |
| 134 |
//# sourceMappingURL=Modal.js.map |