| 1 |
import { css } from '@patternfly/react-styles'; |
| 2 |
import backgroundImage from '@patternfly/react-tokens/dist/esm/c_about_modal_box_BackgroundImage'; |
| 3 |
import { AboutModalBoxContent } from './AboutModalBoxContent'; |
| 4 |
import { AboutModalBoxHeader } from './AboutModalBoxHeader'; |
| 5 |
import { AboutModalBoxBrand } from './AboutModalBoxBrand'; |
| 6 |
import { AboutModalBoxCloseButton } from './AboutModalBoxCloseButton'; |
| 7 |
import { AboutModalBox } from './AboutModalBox'; |
| 8 |
import { Modal, ModalVariant } from '../Modal'; |
| 9 |
import { useSSRSafeId } from '../../helpers'; |
| 10 |
|
| 11 |
export interface AboutModalProps extends React.HTMLProps<HTMLDivElement> { |
| 12 |
/** Content rendered inside the about modal */ |
| 13 |
children: React.ReactNode; |
| 14 |
/** Additional classes added to the about modal */ |
| 15 |
className?: string; |
| 16 |
/** Flag to show the about modal */ |
| 17 |
isOpen?: boolean; |
| 18 |
/** A callback for when the close button is clicked */ |
| 19 |
onClose?: (event: React.MouseEvent | MouseEvent | KeyboardEvent) => void; |
| 20 |
/** Product name */ |
| 21 |
productName?: string; |
| 22 |
/** Trademark information */ |
| 23 |
trademark?: string; |
| 24 |
/** The URL of the image for the brand */ |
| 25 |
brandImageSrc: string; |
| 26 |
/** The alternate text of the brand image */ |
| 27 |
brandImageAlt: string; |
| 28 |
/** The URL or file path of the image for the background */ |
| 29 |
backgroundImageSrc?: string; |
| 30 |
/** Prevents the about modal from rendering content inside a container; allows for more flexible layouts */ |
| 31 |
hasNoContentContainer?: boolean; |
| 32 |
/** The parent container to append the modal to. Defaults to document.body */ |
| 33 |
appendTo?: HTMLElement | (() => HTMLElement); |
| 34 |
/** Aria label for the about modal. This should be used when no productName prop is provided */ |
| 35 |
'aria-label'?: string; |
| 36 |
/** Set aria label to the close button */ |
| 37 |
closeButtonAriaLabel?: string; |
| 38 |
/** Flag to disable focus trap */ |
| 39 |
disableFocusTrap?: boolean; |
| 40 |
} |
| 41 |
|
| 42 |
export const AboutModal: React.FunctionComponent<AboutModalProps> = ({ |
| 43 |
children, |
| 44 |
className, |
| 45 |
isOpen = false, |
| 46 |
onClose = (_e): any => undefined, |
| 47 |
productName, |
| 48 |
trademark, |
| 49 |
backgroundImageSrc, |
| 50 |
brandImageSrc, |
| 51 |
brandImageAlt, |
| 52 |
hasNoContentContainer = false, |
| 53 |
appendTo, |
| 54 |
closeButtonAriaLabel, |
| 55 |
'aria-label': ariaLabel, |
| 56 |
disableFocusTrap, |
| 57 |
...props |
| 58 |
}: AboutModalProps) => { |
| 59 |
const ariaLabelledBy = useSSRSafeId('pf-about-modal-title-'); |
| 60 |
|
| 61 |
if (brandImageSrc && !brandImageAlt) { |
| 62 |
// eslint-disable-next-line no-console |
| 63 |
console.error( |
| 64 |
'AboutModal:', |
| 65 |
'brandImageAlt is required when a brandImageSrc is specified, and should not be an empty string.' |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
if (!productName && !ariaLabel) { |
| 70 |
// eslint-disable-next-line no-console |
| 71 |
console.error('AboutModal:', 'Either productName or ariaLabel is required for component to be accessible'); |
| 72 |
} |
| 73 |
|
| 74 |
if (!isOpen) { |
| 75 |
return null; |
| 76 |
} |
| 77 |
return ( |
| 78 |
<Modal |
| 79 |
isOpen={isOpen} |
| 80 |
variant={ModalVariant.large} |
| 81 |
{...(productName && { 'aria-labelledby': ariaLabelledBy })} |
| 82 |
aria-label={ariaLabel} |
| 83 |
onEscapePress={onClose} |
| 84 |
appendTo={appendTo} |
| 85 |
disableFocusTrap={disableFocusTrap} |
| 86 |
> |
| 87 |
<AboutModalBox |
| 88 |
style={ |
| 89 |
backgroundImageSrc ? ({ [backgroundImage.name]: `url(${backgroundImageSrc})` } as React.CSSProperties) : {} |
| 90 |
} |
| 91 |
className={css(className)} |
| 92 |
> |
| 93 |
<AboutModalBoxBrand src={brandImageSrc} alt={brandImageAlt} /> |
| 94 |
<AboutModalBoxCloseButton aria-label={closeButtonAriaLabel} onClose={onClose} /> |
| 95 |
{productName && <AboutModalBoxHeader id={ariaLabelledBy} productName={productName} />} |
| 96 |
<AboutModalBoxContent trademark={trademark} hasNoContentContainer={hasNoContentContainer} {...props}> |
| 97 |
{children} |
| 98 |
</AboutModalBoxContent> |
| 99 |
</AboutModalBox> |
| 100 |
</Modal> |
| 101 |
); |
| 102 |
}; |
| 103 |
AboutModal.displayName = 'AboutModal'; |