| 1 |
import { useContext, useEffect } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Page/page'; |
| 4 |
import { formatBreakpointMods } from '../../helpers/util'; |
| 5 |
import { PageContext } from './PageContext'; |
| 6 |
export interface PageGroupProps extends React.HTMLProps<HTMLDivElement> { |
| 7 |
/** Additional classes to apply to the PageGroup */ |
| 8 |
className?: string; |
| 9 |
/** Content rendered inside of the PageGroup */ |
| 10 |
children?: React.ReactNode; |
| 11 |
/** Modifier indicating if the PageBreadcrumb is sticky to the top or bottom at various breakpoints */ |
| 12 |
stickyOnBreakpoint?: { |
| 13 |
default?: 'top' | 'bottom'; |
| 14 |
sm?: 'top' | 'bottom'; |
| 15 |
md?: 'top' | 'bottom'; |
| 16 |
lg?: 'top' | 'bottom'; |
| 17 |
xl?: 'top' | 'bottom'; |
| 18 |
'2xl'?: 'top' | 'bottom'; |
| 19 |
}; |
| 20 |
/** @beta Applies the base sticky positioning to the top or bottom of the scroll parent container. */ |
| 21 |
stickyBase?: 'top' | 'bottom'; |
| 22 |
/** @beta Flag indicating if the group has stuck styling, applied when the group is not at the edge of the scroll parent container. */ |
| 23 |
isStickyStuck?: boolean; |
| 24 |
/** Enables the page group to fill the available vertical space if true, or disable filling if false. */ |
| 25 |
isFilled?: boolean; |
| 26 |
/** Modifier indicating if PageGroup should have a shadow at the top */ |
| 27 |
hasShadowTop?: boolean; |
| 28 |
/** Modifier indicating if PageGroup should have a shadow at the bottom */ |
| 29 |
hasShadowBottom?: boolean; |
| 30 |
/** Flag indicating if the PageGroup has a scrolling overflow */ |
| 31 |
hasOverflowScroll?: boolean; |
| 32 |
/** Adds an accessible name to the page group when the hasOverflowScroll prop is set to true. */ |
| 33 |
'aria-label'?: string; |
| 34 |
/** Adds plain styling to the page group. */ |
| 35 |
isPlain?: boolean; |
| 36 |
/** @beta Prevents the page group from automatically applying plain styling when glass theme is enabled. */ |
| 37 |
isNoPlainOnGlass?: boolean; |
| 38 |
} |
| 39 |
|
| 40 |
export const PageGroup = ({ |
| 41 |
className = '', |
| 42 |
children, |
| 43 |
stickyOnBreakpoint, |
| 44 |
stickyBase, |
| 45 |
isStickyStuck = false, |
| 46 |
isFilled, |
| 47 |
hasShadowTop = false, |
| 48 |
hasShadowBottom = false, |
| 49 |
hasOverflowScroll = false, |
| 50 |
'aria-label': ariaLabel, |
| 51 |
isPlain = false, |
| 52 |
isNoPlainOnGlass = false, |
| 53 |
...props |
| 54 |
}: PageGroupProps) => { |
| 55 |
const { height, getVerticalBreakpoint } = useContext(PageContext); |
| 56 |
|
| 57 |
useEffect(() => { |
| 58 |
if (hasOverflowScroll && !ariaLabel) { |
| 59 |
/* eslint-disable no-console */ |
| 60 |
console.warn('PageGroup: An accessible aria-label is required when hasOverflowScroll is set to true.'); |
| 61 |
} |
| 62 |
}, [hasOverflowScroll, ariaLabel]); |
| 63 |
|
| 64 |
return ( |
| 65 |
<div |
| 66 |
{...props} |
| 67 |
className={css( |
| 68 |
styles.pageMainGroup, |
| 69 |
formatBreakpointMods(stickyOnBreakpoint, styles, 'sticky-', getVerticalBreakpoint(height), true), |
| 70 |
stickyBase === 'top' && styles.modifiers.stickyTopBase, |
| 71 |
stickyBase === 'bottom' && styles.modifiers.stickyBottomBase, |
| 72 |
isStickyStuck && stickyBase === 'top' && styles.modifiers.stickyTopStuck, |
| 73 |
isStickyStuck && stickyBase === 'bottom' && styles.modifiers.stickyBottomStuck, |
| 74 |
isFilled === false && styles.modifiers.noFill, |
| 75 |
isFilled === true && styles.modifiers.fill, |
| 76 |
hasShadowTop && styles.modifiers.shadowTop, |
| 77 |
hasShadowBottom && styles.modifiers.shadowBottom, |
| 78 |
hasOverflowScroll && styles.modifiers.overflowScroll, |
| 79 |
isPlain && styles.modifiers.plain, |
| 80 |
isNoPlainOnGlass && styles.modifiers.noPlainOnGlass, |
| 81 |
className |
| 82 |
)} |
| 83 |
{...(hasOverflowScroll && { tabIndex: 0, role: 'region', 'aria-label': ariaLabel })} |
| 84 |
> |
| 85 |
{children} |
| 86 |
</div> |
| 87 |
); |
| 88 |
}; |
| 89 |
PageGroup.displayName = 'PageGroup'; |