-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathModal.tsx
More file actions
201 lines (182 loc) · 6.94 KB
/
Copy pathModal.tsx
File metadata and controls
201 lines (182 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { Component } from 'react';
import * as ReactDOM from 'react-dom';
import { canUseDOM, KeyTypes, PickOptional } from '../../helpers';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/Backdrop/backdrop';
import { ModalContent } from './ModalContent';
import { OUIAProps } from '../../helpers';
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds';
export interface ModalProps extends React.HTMLProps<HTMLDivElement>, OUIAProps {
/** The parent container to append the modal to. Defaults to "document.body". */
appendTo?: HTMLElement | (() => HTMLElement);
/** Id to use for the modal box description. This should match the ModalHeader labelId or descriptorId. */
'aria-describedby'?: string;
/** Adds an accessible name to the modal when there is no title in the ModalHeader. */
'aria-label'?: string;
/** Id to use for the modal box label. This should include the ModalHeader labelId. */
'aria-labelledby'?: string;
/** Content rendered inside the modal. */
children: React.ReactNode;
/** Additional classes added to the modal. */
className?: string;
/** Additional classes added to the modal backdrop. */
backdropClassName?: string;
/** Flag to disable focus trap. */
disableFocusTrap?: boolean;
/** The element to focus when the modal opens. By default the first
* focusable element will receive focus.
*/
elementToFocus?: HTMLElement | SVGElement | string;
/** Id of the focus trap in the ModalContent component */
focusTrapId?: string;
/** An id to use for the modal box container. */
id?: string;
/** Flag to show the modal. */
isOpen?: boolean;
/** Add callback for when the close button is clicked. This prop needs to be passed to render the close button */
onClose?: (event: KeyboardEvent | React.MouseEvent) => void;
/** Modal handles pressing of the escape key and closes the modal. If you want to handle
* this yourself you can use this callback function. */
onEscapePress?: (event: KeyboardEvent) => void;
/** Position of the modal. By default a modal will be positioned vertically and horizontally centered. */
position?: 'default' | 'top';
/** Offset from alternate position. Can be any valid CSS length/percentage. */
positionOffset?: string;
/** Variant of the modal. */
variant?: 'small' | 'medium' | 'large' | 'default';
/** Default width of the modal. */
width?: number | string;
/** Maximum width of the modal. */
maxWidth?: number | string;
/** Value to overwrite the randomly generated data-ouia-component-id.*/
ouiaId?: number | string;
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
ouiaSafe?: boolean;
}
export enum ModalVariant {
small = 'small',
medium = 'medium',
large = 'large',
default = 'default'
}
interface ModalState {
mounted: boolean;
}
class Modal extends Component<ModalProps, ModalState> {
static displayName = 'Modal';
static currentId = 0;
boxId = '';
backdropId = '';
static defaultProps: PickOptional<ModalProps> = {
isOpen: false,
variant: 'default',
appendTo: () => document.body,
ouiaSafe: true,
position: 'default'
};
constructor(props: ModalProps) {
super(props);
const boxIdNum = Modal.currentId++;
const backdropId = boxIdNum + 1;
this.boxId = props.id || `pf-modal-part-${boxIdNum}`;
this.backdropId = `pf-modal-part-${backdropId}`;
this.state = {
mounted: false
};
}
handleEscKeyClick = (event: KeyboardEvent): void => {
const { onEscapePress } = this.props;
if (event.key === KeyTypes.Escape && this.props.isOpen) {
onEscapePress ? onEscapePress(event) : this.props.onClose?.(event);
}
};
getElement = (appendTo: HTMLElement | (() => HTMLElement)) => {
if (typeof appendTo === 'function') {
return appendTo();
}
return appendTo || document.body;
};
toggleSiblingsFromScreenReaders = (hide: boolean) => {
const { appendTo } = this.props;
const target: HTMLElement = this.getElement(appendTo);
const bodyChildren = target.children;
for (const child of Array.from(bodyChildren)) {
const isPopperElement = child.hasAttribute('data-popper-placement');
if (child.id !== this.backdropId && !isPopperElement) {
hide ? child.setAttribute('aria-hidden', '' + hide) : child.removeAttribute('aria-hidden');
}
}
};
isEmpty = (value: string | null | undefined) => value === null || value === undefined || value === '';
componentDidMount() {
this.setState({ mounted: true });
const { appendTo } = this.props;
const target: HTMLElement = this.getElement(appendTo);
target.addEventListener('keydown', this.handleEscKeyClick, false);
if (this.props.isOpen) {
target.classList.add(css(styles.backdropOpen));
this.toggleSiblingsFromScreenReaders(true);
}
}
componentDidUpdate(prevProps: ModalProps) {
const { appendTo } = this.props;
const target: HTMLElement = this.getElement(appendTo);
if (this.props.isOpen) {
target.classList.add(css(styles.backdropOpen));
this.toggleSiblingsFromScreenReaders(true);
} else {
if (prevProps.isOpen !== this.props.isOpen) {
target.classList.remove(css(styles.backdropOpen));
this.toggleSiblingsFromScreenReaders(false);
}
}
}
componentWillUnmount() {
const { appendTo } = this.props;
const target: HTMLElement = this.getElement(appendTo);
target.removeEventListener('keydown', this.handleEscKeyClick, false);
target.classList.remove(css(styles.backdropOpen));
this.toggleSiblingsFromScreenReaders(false);
}
render() {
const {
appendTo,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onEscapePress,
'aria-labelledby': ariaLabelledby,
'aria-label': ariaLabel,
'aria-describedby': ariaDescribedby,
ouiaId,
ouiaSafe,
position,
elementToFocus,
...props
} = this.props;
if (!this.state.mounted || !canUseDOM || !this.getElement(appendTo)) {
return null;
}
return (
<SSRSafeIds prefix="pf-" ouiaComponentType={`Modal${this.props.variant ? `-${this.props.variant}` : ''}`}>
{(_, generatedOuiaId) =>
ReactDOM.createPortal(
<ModalContent
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)
) as React.ReactElement<any>
}
</SSRSafeIds>
);
}
}
export { Modal };