| 1 |
import { useEffect, useRef, useState } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Wizard/wizard'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { WizardDrawerWrapper } from './WizardDrawerWrapper'; |
| 5 |
import { Drawer, DrawerContent } from '../../../components/Drawer'; |
| 6 |
import { WizardStep } from './Wizard'; |
| 7 |
import { debounce } from '../../../helpers/util'; |
| 8 |
import { getResizeObserver } from '../../../helpers/resizeObserver'; |
| 9 |
|
| 10 |
export interface WizardBodyProps { |
| 11 |
/** Anything that can be rendered in the Wizard body */ |
| 12 |
children: any; |
| 13 |
/** Set to true to remove the default body padding */ |
| 14 |
hasNoBodyPadding: boolean; |
| 15 |
/** Adds an accessible name to the wizard body when the body content overflows and renders |
| 16 |
* a scrollbar. |
| 17 |
*/ |
| 18 |
'aria-label'?: string; |
| 19 |
/** Adds an accessible name to the wizard body by passing the the id of one or more elements. |
| 20 |
* The aria-labelledby will only be applied when the body content overflows and renders a scrollbar. |
| 21 |
*/ |
| 22 |
'aria-labelledby': string; |
| 23 |
/** Component used as the primary content container */ |
| 24 |
mainComponent?: React.ElementType; |
| 25 |
/** The currently active WizardStep */ |
| 26 |
activeStep: WizardStep; |
| 27 |
hasDrawer?: boolean; |
| 28 |
/** Flag indicating the wizard drawer is expanded */ |
| 29 |
isDrawerExpanded?: boolean; |
| 30 |
/** Callback function for when the drawer is toggled */ |
| 31 |
onExpandDrawer?: () => void; |
| 32 |
} |
| 33 |
|
| 34 |
export const WizardBody: React.FunctionComponent<WizardBodyProps> = ({ |
| 35 |
children, |
| 36 |
hasNoBodyPadding = false, |
| 37 |
'aria-label': ariaLabel, |
| 38 |
'aria-labelledby': ariaLabelledBy, |
| 39 |
mainComponent = 'div', |
| 40 |
hasDrawer, |
| 41 |
isDrawerExpanded, |
| 42 |
onExpandDrawer, |
| 43 |
activeStep |
| 44 |
}: WizardBodyProps) => { |
| 45 |
const MainComponent = mainComponent; |
| 46 |
const [hasScrollbar, setHasScrollbar] = useState(false); |
| 47 |
const [previousWidth, setPreviousWidth] = useState<number | undefined>(undefined); |
| 48 |
const wizardBodyRef = useRef(null); |
| 49 |
|
| 50 |
useEffect(() => { |
| 51 |
const resize = () => { |
| 52 |
if (wizardBodyRef?.current) { |
| 53 |
const { offsetWidth, offsetHeight, scrollHeight } = wizardBodyRef.current; |
| 54 |
|
| 55 |
if (previousWidth !== offsetWidth) { |
| 56 |
setPreviousWidth(offsetWidth); |
| 57 |
setHasScrollbar(offsetHeight < scrollHeight); |
| 58 |
} |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
const handleResizeWithDelay = debounce(resize, 250); |
| 63 |
let observer = () => {}; |
| 64 |
|
| 65 |
if (wizardBodyRef?.current) { |
| 66 |
observer = getResizeObserver(wizardBodyRef.current, handleResizeWithDelay); |
| 67 |
const { offsetHeight, scrollHeight } = wizardBodyRef.current; |
| 68 |
|
| 69 |
setHasScrollbar(offsetHeight < scrollHeight); |
| 70 |
setPreviousWidth((wizardBodyRef.current as HTMLElement).offsetWidth); |
| 71 |
} |
| 72 |
|
| 73 |
return () => { |
| 74 |
observer(); |
| 75 |
}; |
| 76 |
}, []); |
| 77 |
|
| 78 |
return ( |
| 79 |
<MainComponent |
| 80 |
{...(mainComponent === 'div' && hasScrollbar && { role: 'region' })} |
| 81 |
{...(hasScrollbar && { 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, tabIndex: 0 })} |
| 82 |
ref={wizardBodyRef} |
| 83 |
className={css(styles.wizardMain)} |
| 84 |
> |
| 85 |
<WizardDrawerWrapper |
| 86 |
hasDrawer={hasDrawer && activeStep.drawerPanelContent} |
| 87 |
wrapper={(children: React.ReactNode) => ( |
| 88 |
<Drawer isInline isExpanded={isDrawerExpanded} onExpand={onExpandDrawer}> |
| 89 |
<DrawerContent panelContent={activeStep.drawerPanelContent}>{children}</DrawerContent> |
| 90 |
</Drawer> |
| 91 |
)} |
| 92 |
> |
| 93 |
<div className={css(styles.wizardMainBody, hasNoBodyPadding && styles.modifiers.noPadding)}>{children}</div> |
| 94 |
</WizardDrawerWrapper> |
| 95 |
</MainComponent> |
| 96 |
); |
| 97 |
}; |
| 98 |
WizardBody.displayName = 'WizardBody'; |