| 1 |
import { useContext, useEffect, useState } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Wizard/wizard'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { WizardContext } from './WizardContext'; |
| 5 |
import { debounce } from '../../helpers/util'; |
| 6 |
import { getResizeObserver } from '../../helpers/resizeObserver'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Used as a wrapper for WizardStep content, where the wrapping element is customizable. |
| 10 |
*/ |
| 11 |
|
| 12 |
export interface WizardBodyProps extends React.HTMLProps<HTMLElement> { |
| 13 |
/** Anything that can be rendered in the Wizard body */ |
| 14 |
children: React.ReactNode; |
| 15 |
/** Additional classes spread to the wizard body */ |
| 16 |
className?: string; |
| 17 |
/** Flag to remove the default body padding */ |
| 18 |
hasNoPadding?: boolean; |
| 19 |
/** Adds an accessible name to the wrapper element when the content overflows and renders |
| 20 |
* a scrollbar. |
| 21 |
*/ |
| 22 |
'aria-label'?: string; |
| 23 |
/** Adds an accessible name to the wrapper element by passing the the id of one or more elements. |
| 24 |
* The aria-labelledby will only be applied when the content overflows and renders a scrollbar. |
| 25 |
*/ |
| 26 |
'aria-labelledby'?: string; |
| 27 |
/** Component used as the wrapping content container */ |
| 28 |
component?: React.ElementType; |
| 29 |
} |
| 30 |
|
| 31 |
export const WizardBody = ({ |
| 32 |
children, |
| 33 |
className, |
| 34 |
hasNoPadding = false, |
| 35 |
'aria-label': ariaLabel, |
| 36 |
'aria-labelledby': ariaLabelledBy, |
| 37 |
component = 'div', |
| 38 |
...props |
| 39 |
}: WizardBodyProps) => { |
| 40 |
const [hasScrollbar, setHasScrollbar] = useState(false); |
| 41 |
const [previousWidth, setPreviousWidth] = useState<number | undefined>(undefined); |
| 42 |
const WrapperComponent = component; |
| 43 |
const { activeStep, shouldFocusContent, mainWrapperRef } = useContext(WizardContext); |
| 44 |
const defaultAriaLabel = ariaLabel || `${activeStep?.name} content`; |
| 45 |
|
| 46 |
useEffect(() => { |
| 47 |
const resize = () => { |
| 48 |
if (mainWrapperRef?.current) { |
| 49 |
const { offsetWidth, offsetHeight, scrollHeight } = mainWrapperRef.current; |
| 50 |
|
| 51 |
if (previousWidth !== offsetWidth) { |
| 52 |
setPreviousWidth(offsetWidth); |
| 53 |
setHasScrollbar(offsetHeight < scrollHeight); |
| 54 |
} |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
const handleResizeWithDelay = debounce(resize, 250); |
| 59 |
let observer = () => {}; |
| 60 |
|
| 61 |
if (mainWrapperRef?.current) { |
| 62 |
observer = getResizeObserver(mainWrapperRef.current, handleResizeWithDelay); |
| 63 |
const { offsetHeight, scrollHeight } = mainWrapperRef.current; |
| 64 |
|
| 65 |
setHasScrollbar(offsetHeight < scrollHeight); |
| 66 |
setPreviousWidth((mainWrapperRef.current as HTMLElement).offsetWidth); |
| 67 |
} |
| 68 |
|
| 69 |
return () => { |
| 70 |
observer(); |
| 71 |
}; |
| 72 |
}, [previousWidth]); |
| 73 |
|
| 74 |
return ( |
| 75 |
<WrapperComponent |
| 76 |
ref={mainWrapperRef} |
| 77 |
{...(shouldFocusContent && { tabIndex: -1 })} |
| 78 |
{...(component === 'div' && hasScrollbar && { role: 'region' })} |
| 79 |
{...(hasScrollbar && { 'aria-label': defaultAriaLabel, 'aria-labelledby': ariaLabelledBy, tabIndex: 0 })} |
| 80 |
className={css(styles.wizardMain, className)} |
| 81 |
{...props} |
| 82 |
> |
| 83 |
<div className={css(styles.wizardMainBody, hasNoPadding && styles.modifiers.noPadding)}>{children}</div> |
| 84 |
</WrapperComponent> |
| 85 |
); |
| 86 |
}; |
| 87 |
|
| 88 |
WizardBody.displayName = 'WizardBody'; |