Debian Codegraph @

node/@patternfly/react-core/src/deprecated/components/Modal/ModalContent.tsx

raw
1 import { FocusTrap } from '../../../helpers';
2 import modalStyles from '@patternfly/react-styles/css/components/ModalBox/modal-box';
3 import bullsEyeStyles from '@patternfly/react-styles/css/layouts/Bullseye/bullseye';
4 import { css } from '@patternfly/react-styles';
5 import { getOUIAProps, OUIAProps } from '../../../helpers';
6
7 import { Backdrop } from '../../../components/Backdrop/Backdrop';
8 import { ModalBoxBody } from './ModalBoxBody';
9 import { ModalBoxCloseButton } from './ModalBoxCloseButton';
10 import { ModalBox } from './ModalBox';
11 import { ModalBoxFooter } from './ModalBoxFooter';
12 import { ModalBoxDescription } from './ModalBoxDescription';
13 import { ModalBoxHeader } from './ModalBoxHeader';
14 import { ModalBoxTitle, isVariantIcon } from './ModalBoxTitle';
15
16 export interface ModalContentProps extends OUIAProps {
17 /** Action buttons to add to the standard modal footer. Ignored if the footer property
18 * is passed in.
19 */
20 actions?: any;
21 /** Id to use for the modal box descriptor. */
22 'aria-describedby'?: string;
23 /** Accessible descriptor of the modal. */
24 'aria-label'?: string;
25 /** Id to use for the modal box label. */
26 'aria-labelledby'?: string | null;
27 /** Id of the backdrop. */
28 backdropId?: string;
29 /** Accessible label applied to the modal box body. This should be used to communicate
30 * important information about the modal box body div element if needed, such as that it
31 * is scrollable.
32 */
33 bodyAriaLabel?: string;
34 /** Accessible role applied to the modal box body. This will default to "region" if the
35 * bodyAriaLabel property is passed in. Set to a more appropriate role as applicable
36 * based on the modal content and context.
37 */
38 bodyAriaRole?: string;
39 /** Id of the modal box container. */
40 boxId: string;
41 /** Content rendered inside the modal. */
42 children: React.ReactNode;
43 /** Additional classes added to the modal box. */
44 className?: string;
45 /** Description of the modal. */
46 description?: React.ReactNode;
47 /** Id of the modal box description. */
48 descriptorId: string;
49 /** Flag to disable focus trap. */
50 disableFocusTrap?: boolean;
51 /** The element to focus when the modal opens. By default the first
52 * focusable element will receive focus.
53 */
54 elementToFocus?: HTMLElement | SVGElement | string;
55 /** Custom footer. */
56 footer?: React.ReactNode;
57 /** Flag indicating if modal content should be placed in a modal box body wrapper. */
58 hasNoBodyWrapper?: boolean;
59 /** Complex header (more than just text), supersedes the title property for header content. */
60 header?: React.ReactNode;
61 /** Optional help section for the modal header. */
62 help?: React.ReactNode;
63 /** Flag to show the modal. */
64 isOpen?: boolean;
65 /** Id of the modal box title. */
66 labelId: string;
67 /** A callback for when the close button is clicked. */
68 onClose?: (event: KeyboardEvent | React.MouseEvent) => void;
69 /** Position of the modal. By default a modal will be positioned vertically and horizontally centered. */
70 position?: 'default' | 'top';
71 /** Offset from alternate position. Can be any valid CSS length/percentage. */
72 positionOffset?: string;
73 /** Flag to show the close button in the header area of the modal. */
74 showClose?: boolean;
75 /** Text content of the modal header. */
76 title?: React.ReactNode;
77 /** Optional alert icon (or other) to show before the title of the modal header. When the
78 * predefined alert types are used the default styling will be automatically applied.
79 */
80 titleIconVariant?: 'success' | 'danger' | 'warning' | 'info' | 'custom' | React.ComponentType<any>;
81 /** Optional title label text for screen readers. */
82 titleLabel?: string;
83 /** Variant of the modal. */
84 variant?: 'small' | 'medium' | 'large' | 'default';
85 /** Default width of the modal. */
86 width?: number | string;
87 /** Maximum width of the modal. */
88 maxWidth?: number | string;
89 /** Value to overwrite the randomly generated data-ouia-component-id.*/
90 ouiaId?: number | string;
91 /** 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. */
92 ouiaSafe?: boolean;
93 }
94
95 export const ModalContent: React.FunctionComponent<ModalContentProps> = ({
96 children,
97 className = '',
98 isOpen = false,
99 header = null,
100 help = null,
101 description = null,
102 title = '',
103 titleIconVariant = null,
104 titleLabel = '',
105 'aria-label': ariaLabel = '',
106 'aria-describedby': ariaDescribedby,
107 'aria-labelledby': ariaLabelledby,
108 bodyAriaLabel,
109 bodyAriaRole,
110 showClose = true,
111 footer = null,
112 actions = [],
113 onClose = () => undefined as any,
114 variant = 'default',
115 position,
116 positionOffset,
117 width,
118 maxWidth,
119 boxId,
120 labelId,
121 backdropId,
122 descriptorId,
123 disableFocusTrap = false,
124 hasNoBodyWrapper = false,
125 ouiaId,
126 ouiaSafe = true,
127 elementToFocus,
128 ...props
129 }: ModalContentProps) => {
130 if (!isOpen) {
131 return null;
132 }
133
134 const modalBoxHeader = header ? (
135 <ModalBoxHeader help={help}>{header}</ModalBoxHeader>
136 ) : (
137 title && (
138 <ModalBoxHeader help={help}>
139 <ModalBoxTitle title={title} titleIconVariant={titleIconVariant} titleLabel={titleLabel} id={labelId} />
140 {description && <ModalBoxDescription id={descriptorId}>{description}</ModalBoxDescription>}
141 </ModalBoxHeader>
142 )
143 );
144
145 const modalBoxFooter = footer ? (
146 <ModalBoxFooter>{footer}</ModalBoxFooter>
147 ) : (
148 actions.length > 0 && <ModalBoxFooter>{actions}</ModalBoxFooter>
149 );
150
151 const defaultModalBodyAriaRole = bodyAriaLabel ? 'region' : undefined;
152
153 const hasNoDescription = !description && !ariaDescribedby;
154 const id = hasNoDescription ? descriptorId : undefined;
155
156 const modalBody = hasNoBodyWrapper ? (
157 children
158 ) : (
159 <ModalBoxBody aria-label={bodyAriaLabel} role={bodyAriaRole || defaultModalBodyAriaRole} {...props} id={id}>
160 {children}
161 </ModalBoxBody>
162 );
163 const ariaLabelledbyFormatted = (): null | string => {
164 if (ariaLabelledby === null) {
165 return null;
166 }
167 const idRefList: string[] = [];
168 if ((ariaLabel && boxId) !== '') {
169 idRefList.push(ariaLabel && boxId);
170 }
171 if (ariaLabelledby) {
172 idRefList.push(ariaLabelledby);
173 }
174 if (title) {
175 idRefList.push(labelId);
176 }
177 return idRefList.join(' ');
178 };
179
180 const modalBox = (
181 <ModalBox
182 id={boxId}
183 className={css(className, isVariantIcon(titleIconVariant) && modalStyles.modifiers[titleIconVariant])}
184 variant={variant}
185 position={position}
186 positionOffset={positionOffset}
187 aria-label={ariaLabel}
188 aria-labelledby={ariaLabelledbyFormatted()}
189 aria-describedby={ariaDescribedby || (hasNoBodyWrapper ? null : descriptorId)}
190 {...getOUIAProps(ModalContent.displayName, ouiaId, ouiaSafe)}
191 style={
192 {
193 ...(width && { '--pf-v6-c-modal-box--Width': typeof width !== 'number' ? width : `${width}px` }),
194 ...(maxWidth && {
195 '--pf-v6-c-modal-box--MaxWidth': typeof maxWidth !== 'number' ? maxWidth : `${maxWidth}px`
196 })
197 } as React.CSSProperties
198 }
199 >
200 {showClose && <ModalBoxCloseButton onClose={(event) => onClose(event)} ouiaId={ouiaId} />}
201 {modalBoxHeader}
202 {modalBody}
203 {modalBoxFooter}
204 </ModalBox>
205 );
206 return (
207 <Backdrop id={backdropId}>
208 <FocusTrap
209 active={!disableFocusTrap}
210 focusTrapOptions={{
211 clickOutsideDeactivates: true,
212 tabbableOptions: { displayCheck: 'none' },
213 // FocusTrap's initialFocus can accept false as a value to prevent initial focus.
214 // We want to prevent this in case false is ever passed in.
215 initialFocus: elementToFocus || undefined
216 }}
217 className={css(bullsEyeStyles.bullseye)}
218 >
219 {modalBox}
220 </FocusTrap>
221 </Backdrop>
222 );
223 };
224 ModalContent.displayName = 'ModalContent';