| 1 |
import { useRef, useState } from 'react'; |
| 2 |
import modalStyles from '@patternfly/react-styles/css/components/ModalBox/modal-box'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { capitalize } from '../../helpers'; |
| 5 |
import { Tooltip } from '../Tooltip'; |
| 6 |
import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon'; |
| 7 |
import RhUiErrorFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-error-fill-icon'; |
| 8 |
import RhUiWarningFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-warning-fill-icon'; |
| 9 |
import RhUiInformationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-information-fill-icon'; |
| 10 |
import RhUiNotificationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-notification-fill-icon'; |
| 11 |
import { useIsomorphicLayoutEffect } from '../../helpers'; |
| 12 |
|
| 13 |
export const isVariantIcon = (icon: any): icon is string => |
| 14 |
['success', 'danger', 'warning', 'info', 'custom'].includes(icon as string); |
| 15 |
|
| 16 |
export interface ModalBoxTitleProps { |
| 17 |
/** Additional classes added to the modal box title. */ |
| 18 |
className?: string; |
| 19 |
/** Id of the modal box title. */ |
| 20 |
id?: string; |
| 21 |
/** Content rendered inside the modal box title. */ |
| 22 |
title: React.ReactNode; |
| 23 |
/** Optional alert icon (or other) to show before the title. When the predefined alert types |
| 24 |
* are used the default styling will be automatically applied. */ |
| 25 |
titleIconVariant?: 'success' | 'danger' | 'warning' | 'info' | 'custom' | React.ComponentType<any>; |
| 26 |
/** Optional title label text for screen readers. */ |
| 27 |
titleScreenReaderText?: string; |
| 28 |
} |
| 29 |
|
| 30 |
export const ModalBoxTitle: React.FunctionComponent<ModalBoxTitleProps> = ({ |
| 31 |
className, |
| 32 |
id, |
| 33 |
title, |
| 34 |
titleIconVariant, |
| 35 |
titleScreenReaderText, |
| 36 |
...props |
| 37 |
}: ModalBoxTitleProps) => { |
| 38 |
const [hasTooltip, setHasTooltip] = useState(false); |
| 39 |
const h1 = useRef<HTMLHeadingElement>(null); |
| 40 |
const label = |
| 41 |
titleScreenReaderText || |
| 42 |
(isVariantIcon(titleIconVariant) ? `${capitalize(titleIconVariant)} alert:` : titleScreenReaderText); |
| 43 |
const variantIcons = { |
| 44 |
success: <RhUiCheckCircleFillIcon />, |
| 45 |
danger: <RhUiErrorFillIcon />, |
| 46 |
warning: <RhUiWarningFillIcon />, |
| 47 |
info: <RhUiInformationFillIcon />, |
| 48 |
custom: <RhUiNotificationFillIcon /> |
| 49 |
}; |
| 50 |
const CustomIcon = !isVariantIcon(titleIconVariant) && titleIconVariant; |
| 51 |
|
| 52 |
useIsomorphicLayoutEffect(() => { |
| 53 |
setHasTooltip(h1.current && h1.current.offsetWidth < h1.current.scrollWidth); |
| 54 |
}, []); |
| 55 |
|
| 56 |
const content = ( |
| 57 |
<h1 |
| 58 |
id={id} |
| 59 |
ref={h1} |
| 60 |
className={css( |
| 61 |
modalStyles.modalBoxTitle, |
| 62 |
titleIconVariant && modalStyles.modifiers.icon, |
| 63 |
isVariantIcon(titleIconVariant) && modalStyles.modifiers[titleIconVariant], |
| 64 |
className |
| 65 |
)} |
| 66 |
{...props} |
| 67 |
> |
| 68 |
{titleIconVariant && ( |
| 69 |
<span className={css(modalStyles.modalBoxTitleIcon)}> |
| 70 |
{isVariantIcon(titleIconVariant) ? variantIcons[titleIconVariant] : <CustomIcon />} |
| 71 |
</span> |
| 72 |
)} |
| 73 |
{label && <span className="pf-v6-screen-reader">{label}</span>} |
| 74 |
<span className={css(modalStyles.modalBoxTitleText)}>{title}</span> |
| 75 |
</h1> |
| 76 |
); |
| 77 |
|
| 78 |
return hasTooltip ? <Tooltip content={title}>{content}</Tooltip> : content; |
| 79 |
}; |
| 80 |
ModalBoxTitle.displayName = 'ModalBoxTitle'; |