Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Modal/Modal.js

raw
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.backdropId = '';
25 this.handleEscKeyClick = (event) => {
26 var _a, _b;
27 const { onEscapePress } = this.props;
28 if (event.key === helpers_1.KeyTypes.Escape && this.props.isOpen) {
29 onEscapePress ? onEscapePress(event) : (_b = (_a = this.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a, event);
30 }
31 };
32 this.getElement = (appendTo) => {
33 if (typeof appendTo === 'function') {
34 return appendTo();
35 }
36 return appendTo || document.body;
37 };
38 this.toggleSiblingsFromScreenReaders = (hide) => {
39 const { appendTo } = this.props;
40 const target = this.getElement(appendTo);
41 const bodyChildren = target.children;
42 for (const child of Array.from(bodyChildren)) {
43 if (child.id !== this.backdropId) {
44 hide ? child.setAttribute('aria-hidden', '' + hide) : child.removeAttribute('aria-hidden');
45 }
46 }
47 };
48 this.isEmpty = (value) => value === null || value === undefined || value === '';
49 const boxIdNum = Modal.currentId++;
50 const backdropId = boxIdNum + 1;
51 this.boxId = props.id || `pf-modal-part-${boxIdNum}`;
52 this.backdropId = `pf-modal-part-${backdropId}`;
53 this.state = {
54 mounted: false
55 };
56 }
57 componentDidMount() {
58 this.setState({ mounted: true });
59 const { appendTo } = this.props;
60 const target = this.getElement(appendTo);
61 target.addEventListener('keydown', this.handleEscKeyClick, false);
62 if (this.props.isOpen) {
63 target.classList.add((0, react_styles_1.css)(backdrop_1.default.backdropOpen));
64 this.toggleSiblingsFromScreenReaders(true);
65 }
66 }
67 componentDidUpdate(prevProps) {
68 const { appendTo } = this.props;
69 const target = this.getElement(appendTo);
70 if (this.props.isOpen) {
71 target.classList.add((0, react_styles_1.css)(backdrop_1.default.backdropOpen));
72 this.toggleSiblingsFromScreenReaders(true);
73 }
74 else {
75 if (prevProps.isOpen !== this.props.isOpen) {
76 target.classList.remove((0, react_styles_1.css)(backdrop_1.default.backdropOpen));
77 this.toggleSiblingsFromScreenReaders(false);
78 }
79 }
80 }
81 componentWillUnmount() {
82 const { appendTo } = this.props;
83 const target = this.getElement(appendTo);
84 target.removeEventListener('keydown', this.handleEscKeyClick, false);
85 target.classList.remove((0, react_styles_1.css)(backdrop_1.default.backdropOpen));
86 this.toggleSiblingsFromScreenReaders(false);
87 }
88 render() {
89 const _a = this.props, { appendTo,
90 // eslint-disable-next-line @typescript-eslint/no-unused-vars
91 onEscapePress, 'aria-labelledby': ariaLabelledby, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedby, ouiaId, ouiaSafe, position, elementToFocus } = _a, props = tslib_1.__rest(_a, ["appendTo", "onEscapePress", 'aria-labelledby', 'aria-label', 'aria-describedby', "ouiaId", "ouiaSafe", "position", "elementToFocus"]);
92 if (!this.state.mounted || !helpers_1.canUseDOM || !this.getElement(appendTo)) {
93 return null;
94 }
95 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({ 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)) }));
96 }
97 }
98 exports.Modal = Modal;
99 Modal.displayName = 'Modal';
100 Modal.currentId = 0;
101 Modal.defaultProps = {
102 isOpen: false,
103 variant: 'default',
104 appendTo: () => document.body,
105 ouiaSafe: true,
106 position: 'default'
107 };
108 //# sourceMappingURL=Modal.js.map