| 1 |
import { FocusTrap } from '../../helpers'; |
| 2 |
import bullsEyeStyles from '@patternfly/react-styles/css/layouts/Bullseye/bullseye'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { getOUIAProps, OUIAProps } from '../../helpers'; |
| 5 |
import { Backdrop } from '../Backdrop'; |
| 6 |
import { ModalBoxCloseButton } from './ModalBoxCloseButton'; |
| 7 |
import { ModalBox } from './ModalBox'; |
| 8 |
|
| 9 |
export interface ModalContentProps extends OUIAProps { |
| 10 |
/** Id to use for the modal box description. This should match the ModalHeader labelId or descriptorId. */ |
| 11 |
'aria-describedby'?: string; |
| 12 |
/** Accessible descriptor of the modal. */ |
| 13 |
'aria-label'?: string; |
| 14 |
/** Id to use for the modal box label. This should include the ModalHeader labelId. */ |
| 15 |
'aria-labelledby'?: string; |
| 16 |
/** Id of the modal box container. */ |
| 17 |
boxId: string; |
| 18 |
/** Id of the backdrop. */ |
| 19 |
backdropId?: string; |
| 20 |
/** Content rendered inside the modal. */ |
| 21 |
children: React.ReactNode; |
| 22 |
/** Additional classes added to the modal box. */ |
| 23 |
className?: string; |
| 24 |
/** Additional classes added to the modal backdrop. */ |
| 25 |
backdropClassName?: string; |
| 26 |
/** Flag to disable focus trap. */ |
| 27 |
disableFocusTrap?: boolean; |
| 28 |
/** The element to focus when the modal opens. By default the first |
| 29 |
* focusable element will receive focus. |
| 30 |
*/ |
| 31 |
elementToFocus?: HTMLElement | SVGElement | string; |
| 32 |
/** Id of the focus trap */ |
| 33 |
focusTrapId?: string; |
| 34 |
/** Flag to show the modal. */ |
| 35 |
isOpen?: boolean; |
| 36 |
/** A callback for when the close button is clicked. */ |
| 37 |
onClose?: (event: KeyboardEvent | React.MouseEvent) => void; |
| 38 |
/** Position of the modal. By default a modal will be positioned vertically and horizontally centered. */ |
| 39 |
position?: 'default' | 'top'; |
| 40 |
/** Offset from alternate position. Can be any valid CSS length/percentage. */ |
| 41 |
positionOffset?: string; |
| 42 |
/** Variant of the modal. */ |
| 43 |
variant?: 'small' | 'medium' | 'large' | 'default'; |
| 44 |
/** Default width of the modal. */ |
| 45 |
width?: number | string; |
| 46 |
/** Maximum width of the modal. */ |
| 47 |
maxWidth?: number | string; |
| 48 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 49 |
ouiaId?: number | string; |
| 50 |
/** 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. */ |
| 51 |
ouiaSafe?: boolean; |
| 52 |
} |
| 53 |
|
| 54 |
export const ModalContent: React.FunctionComponent<ModalContentProps> = ({ |
| 55 |
backdropClassName, |
| 56 |
children, |
| 57 |
className, |
| 58 |
isOpen = false, |
| 59 |
'aria-label': ariaLabel, |
| 60 |
'aria-describedby': ariaDescribedby, |
| 61 |
'aria-labelledby': ariaLabelledby, |
| 62 |
onClose, |
| 63 |
variant = 'default', |
| 64 |
position, |
| 65 |
positionOffset, |
| 66 |
width, |
| 67 |
maxWidth, |
| 68 |
boxId, |
| 69 |
backdropId, |
| 70 |
disableFocusTrap = false, |
| 71 |
ouiaId, |
| 72 |
ouiaSafe = true, |
| 73 |
elementToFocus, |
| 74 |
focusTrapId, |
| 75 |
...props |
| 76 |
}: ModalContentProps) => { |
| 77 |
if (!isOpen) { |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
const getAriaLabelledBy = (): string | undefined => { |
| 82 |
if (ariaLabelledby) { |
| 83 |
return ariaLabelledby; |
| 84 |
} |
| 85 |
if (ariaLabel) { |
| 86 |
return undefined; |
| 87 |
} |
| 88 |
return boxId; |
| 89 |
}; |
| 90 |
|
| 91 |
const modalBox = ( |
| 92 |
<ModalBox |
| 93 |
className={css(className)} |
| 94 |
variant={variant} |
| 95 |
position={position} |
| 96 |
positionOffset={positionOffset} |
| 97 |
aria-label={ariaLabel} |
| 98 |
aria-labelledby={getAriaLabelledBy()} |
| 99 |
aria-describedby={ariaDescribedby} |
| 100 |
{...getOUIAProps(ModalContent.displayName, ouiaId, ouiaSafe)} |
| 101 |
style={ |
| 102 |
{ |
| 103 |
...(width && { '--pf-v6-c-modal-box--Width': typeof width !== 'number' ? width : `${width}px` }), |
| 104 |
...(maxWidth && { |
| 105 |
'--pf-v6-c-modal-box--MaxWidth': typeof maxWidth !== 'number' ? maxWidth : `${maxWidth}px` |
| 106 |
}) |
| 107 |
} as React.CSSProperties |
| 108 |
} |
| 109 |
{...props} |
| 110 |
id={boxId} |
| 111 |
> |
| 112 |
{onClose && <ModalBoxCloseButton onClose={(event) => onClose(event)} ouiaId={ouiaId} />} |
| 113 |
{children} |
| 114 |
</ModalBox> |
| 115 |
); |
| 116 |
return ( |
| 117 |
<Backdrop className={css(backdropClassName)} id={backdropId}> |
| 118 |
<FocusTrap |
| 119 |
active={!disableFocusTrap} |
| 120 |
focusTrapOptions={{ |
| 121 |
clickOutsideDeactivates: true, |
| 122 |
tabbableOptions: { displayCheck: 'none' }, |
| 123 |
// FocusTrap's initialFocus can accept false as a value to prevent initial focus. |
| 124 |
// We want to prevent this in case false is ever passed in. |
| 125 |
initialFocus: elementToFocus || undefined |
| 126 |
}} |
| 127 |
className={css(bullsEyeStyles.bullseye)} |
| 128 |
id={focusTrapId} |
| 129 |
> |
| 130 |
{modalBox} |
| 131 |
</FocusTrap> |
| 132 |
</Backdrop> |
| 133 |
); |
| 134 |
}; |
| 135 |
ModalContent.displayName = 'ModalContent'; |