Debian Codegraph @

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

raw
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.labelId = '';
22 this.descriptorId = '';
23 this.backdropId = '';
24 this.handleEscKeyClick = (event) => {
25 var _a, _b;
26 const { onEscapePress } = this.props;
27 if (event.key === KeyTypes.Escape && this.props.isOpen) {
28 onEscapePress ? onEscapePress(event) : (_b = (_a = this.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a, event);
29 }
30 };
31 this.getElement = (appendTo) => {
32 if (typeof appendTo === 'function') {
33 return appendTo();
34 }
35 return appendTo || document.body;
36 };
37 this.toggleSiblingsFromScreenReaders = (hide) => {
38 const { appendTo } = this.props;
39 const target = this.getElement(appendTo);
40 const bodyChildren = target.children;
41 for (const child of Array.from(bodyChildren)) {
42 if (child.id !== this.backdropId) {
43 hide ? child.setAttribute('aria-hidden', '' + hide) : child.removeAttribute('aria-hidden');
44 }
45 }
46 };
47 this.isEmpty = (value) => value === null || value === undefined || value === '';
48 const boxIdNum = Modal.currentId++;
49 const labelIdNum = boxIdNum + 1;
50 const descriptorIdNum = boxIdNum + 2;
51 const backdropIdNum = boxIdNum + 3;
52 this.boxId = props.id || `pf-modal-part-${boxIdNum}`;
53 this.labelId = `pf-modal-part-${labelIdNum}`;
54 this.descriptorId = `pf-modal-part-${descriptorIdNum}`;
55 this.backdropId = `pf-modal-part-${backdropIdNum}`;
56 this.state = {
57 mounted: false
58 };
59 }
60 componentDidMount() {
61 this.setState({ mounted: true });
62 const { appendTo, title, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, hasNoBodyWrapper, header } = this.props;
63 const target = this.getElement(appendTo);
64 target.addEventListener('keydown', this.handleEscKeyClick, false);
65 if (this.props.isOpen) {
66 target.classList.add(css(styles.backdropOpen));
67 this.toggleSiblingsFromScreenReaders(true);
68 }
69 if (!title && this.isEmpty(ariaLabel) && this.isEmpty(ariaLabelledby)) {
70 // eslint-disable-next-line no-console
71 console.error('Modal: Specify at least one of: title, aria-label, aria-labelledby.');
72 }
73 if (this.isEmpty(ariaLabel) && this.isEmpty(ariaLabelledby) && (hasNoBodyWrapper || header)) {
74 // eslint-disable-next-line no-console
75 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.');
76 }
77 }
78 componentDidUpdate(prevProps) {
79 const { appendTo } = this.props;
80 const target = this.getElement(appendTo);
81 if (this.props.isOpen) {
82 target.classList.add(css(styles.backdropOpen));
83 this.toggleSiblingsFromScreenReaders(true);
84 }
85 else {
86 if (prevProps.isOpen !== this.props.isOpen) {
87 target.classList.remove(css(styles.backdropOpen));
88 this.toggleSiblingsFromScreenReaders(false);
89 }
90 }
91 }
92 componentWillUnmount() {
93 const { appendTo } = this.props;
94 const target = this.getElement(appendTo);
95 target.removeEventListener('keydown', this.handleEscKeyClick, false);
96 target.classList.remove(css(styles.backdropOpen));
97 this.toggleSiblingsFromScreenReaders(false);
98 }
99 render() {
100 const _a = this.props, { appendTo,
101 // eslint-disable-next-line @typescript-eslint/no-unused-vars
102 onEscapePress, 'aria-labelledby': ariaLabelledby, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedby, bodyAriaLabel, bodyAriaRole, title, titleIconVariant, titleLabel, ouiaId, ouiaSafe, position, elementToFocus } = _a, props = __rest(_a, ["appendTo", "onEscapePress", 'aria-labelledby', 'aria-label', 'aria-describedby', "bodyAriaLabel", "bodyAriaRole", "title", "titleIconVariant", "titleLabel", "ouiaId", "ouiaSafe", "position", "elementToFocus"]);
103 if (!this.state.mounted || !canUseDOM || !this.getElement(appendTo)) {
104 return null;
105 }
106 return (_jsx(SSRSafeIds, { prefix: "pf-", ouiaComponentType: `Modal${this.props.variant ? `-${this.props.variant}` : ''}`, children: (_, generatedOuiaId) => ReactDOM.createPortal(_jsx(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)) }));
107 }
108 }
109 Modal.displayName = 'Modal';
110 Modal.currentId = 0;
111 Modal.defaultProps = {
112 className: '',
113 isOpen: false,
114 title: '',
115 titleIconVariant: null,
116 titleLabel: '',
117 'aria-label': '',
118 showClose: true,
119 'aria-describedby': '',
120 'aria-labelledby': '',
121 id: undefined,
122 actions: [],
123 onClose: () => undefined,
124 variant: 'default',
125 hasNoBodyWrapper: false,
126 appendTo: () => document.body,
127 ouiaSafe: true,
128 position: 'default'
129 };
130 export { Modal };
131 //# sourceMappingURL=Modal.js.map