| 1 |
import { Component, createRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Page/page'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import globalBreakpointXl from '@patternfly/react-tokens/dist/esm/t_global_breakpoint_xl'; |
| 5 |
import { debounce, canUseDOM } from '../../helpers/util'; |
| 6 |
import { Drawer, DrawerContent, DrawerPanelContent } from '../Drawer'; |
| 7 |
import { PageBreadcrumb, PageBreadcrumbProps } from './PageBreadcrumb'; |
| 8 |
import { PageGroup, PageGroupProps } from './PageGroup'; |
| 9 |
import { getResizeObserver } from '../../helpers/resizeObserver'; |
| 10 |
import { getBreakpoint, getVerticalBreakpoint } from '../../helpers/util'; |
| 11 |
import { PageContextProvider } from './PageContext'; |
| 12 |
import { PageBody } from './PageBody'; |
| 13 |
|
| 14 |
export enum PageLayouts { |
| 15 |
vertical = 'vertical', |
| 16 |
horizontal = 'horizontal' |
| 17 |
} |
| 18 |
export interface PageProps extends React.HTMLProps<HTMLDivElement> { |
| 19 |
/** Content rendered inside the main section of the page layout (e.g. <PageSection />) */ |
| 20 |
children?: React.ReactNode; |
| 21 |
/** Additional classes added to the page layout */ |
| 22 |
className?: string; |
| 23 |
/** @beta Indicates the layout variant */ |
| 24 |
variant?: 'default' | 'docked'; |
| 25 |
/** @beta Flag indicating the docked nav is expanded on mobile. Only applies when variant is docked. */ |
| 26 |
isDockExpanded?: boolean; |
| 27 |
/** @beta Flag indicating the docked nav should display text on desktop. Only applies when variant is docked, and |
| 28 |
* will handle toggling the visibility of the text in individual isDocked components. |
| 29 |
*/ |
| 30 |
isDockTextExpanded?: boolean; |
| 31 |
/** The horizontal masthead content (e.g. <Masthead />). When using the docked variant, this content will only render at mobile viewports. */ |
| 32 |
masthead?: React.ReactNode; |
| 33 |
/** @beta Content to render in the vertical dock when variant of docked is used. At mobile viewports, this content will be replaced with the content passed to masthead. */ |
| 34 |
dockContent?: React.ReactNode; |
| 35 |
/** Sidebar component for a side nav, recommended to be a PageSidebar. If set to null, the page grid layout |
| 36 |
* will render without a sidebar. |
| 37 |
*/ |
| 38 |
sidebar?: React.ReactNode; |
| 39 |
/** Notification drawer component for an optional notification drawer (e.g. <NotificationDrawer />) */ |
| 40 |
notificationDrawer?: React.ReactNode; |
| 41 |
/** Flag indicating Notification drawer in expanded */ |
| 42 |
isNotificationDrawerExpanded?: boolean; |
| 43 |
/** Sets default drawer size */ |
| 44 |
drawerDefaultSize?: string; |
| 45 |
/** Sets the minimum drawer size*/ |
| 46 |
drawerMinSize?: string; |
| 47 |
/** Sets the maximum drawer size */ |
| 48 |
drawerMaxSize?: string; |
| 49 |
/** Flag indicating if breadcrumb width should be limited */ |
| 50 |
isBreadcrumbWidthLimited?: boolean; |
| 51 |
/** Callback when notification drawer panel is finished expanding. */ |
| 52 |
onNotificationDrawerExpand?: (event: KeyboardEvent | React.MouseEvent | React.TransitionEvent) => void; |
| 53 |
/** Skip to content component for the page */ |
| 54 |
skipToContent?: React.ReactElement<any>; |
| 55 |
/** Sets the value for role on the <main> element */ |
| 56 |
role?: string; |
| 57 |
/** an id to use for the [role="main"] element */ |
| 58 |
mainContainerId?: string; |
| 59 |
/** tabIndex to use for the [role="main"] element, null to unset it */ |
| 60 |
mainTabIndex?: number | null; |
| 61 |
/** |
| 62 |
* If true, manages the sidebar open/close state and there is no need to pass the isSidebarOpen boolean into |
| 63 |
* the sidebar component or add a callback onSidebarToggle function into the Masthead component |
| 64 |
*/ |
| 65 |
isManagedSidebar?: boolean; |
| 66 |
/** Flag indicating if horizontal sub navigation width should be limited */ |
| 67 |
isHorizontalSubnavWidthLimited?: boolean; |
| 68 |
/** |
| 69 |
* If true, the managed sidebar is initially open for desktop view |
| 70 |
*/ |
| 71 |
defaultManagedSidebarIsOpen?: boolean; |
| 72 |
/** |
| 73 |
* Can add callback to be notified when resize occurs, for example to set the sidebar isSidebarOpen prop to false for a width < 768px |
| 74 |
* Returns object { mobileView: boolean, windowSize: number } |
| 75 |
*/ |
| 76 |
onPageResize?: ((event: MouseEvent | TouchEvent | React.KeyboardEvent, object: any) => void) | null; |
| 77 |
/** |
| 78 |
* The page resize observer uses the breakpoints returned from this function when adding the pf-m-breakpoint-[default|sm|md|lg|xl|2xl] class |
| 79 |
* You can override the default getBreakpoint function to return breakpoints at different sizes than the default |
| 80 |
* You can view the default getBreakpoint function here: |
| 81 |
* https://github.com/patternfly/patternfly-react/blob/main/packages/react-core/src/helpers/util.ts |
| 82 |
*/ |
| 83 |
getBreakpoint?: (width: number | null) => 'default' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'; |
| 84 |
/** |
| 85 |
* The page resize observer uses the breakpoints returned from this function when adding the pf-m-breakpoint-[default|sm|md|lg|xl|2xl] class |
| 86 |
* You can override the default getVerticalBreakpoint function to return breakpoints at different sizes than the default |
| 87 |
* You can view the default getVerticalBreakpoint function here: |
| 88 |
* https://github.com/patternfly/patternfly-react/blob/main/packages/react-core/src/helpers/util.ts |
| 89 |
*/ |
| 90 |
getVerticalBreakpoint?: (height: number | null) => 'default' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'; |
| 91 |
/** Banner component for the page. This will be rendered above a breadcrumb if one is also passed. |
| 92 |
*/ |
| 93 |
banner?: React.ReactNode; |
| 94 |
/** Breadcrumb component for the page */ |
| 95 |
breadcrumb?: React.ReactNode; |
| 96 |
/** Horizontal sub navigation component for the page */ |
| 97 |
horizontalSubnav?: React.ReactNode; |
| 98 |
/** Accessible label, can be used to name main section */ |
| 99 |
mainAriaLabel?: string; |
| 100 |
/** Flag indicating if the horizontal sub navigation should be in a group */ |
| 101 |
isHorizontalSubnavGrouped?: boolean; |
| 102 |
/** Flag indicating if the breadcrumb should be in a group */ |
| 103 |
isBreadcrumbGrouped?: boolean; |
| 104 |
/** Additional content of the group */ |
| 105 |
additionalGroupedContent?: React.ReactNode; |
| 106 |
/** HTML component used as main component of the page. Defaults to 'main', only pass in 'div' if another 'main' element already exists. */ |
| 107 |
mainComponent?: 'main' | 'div'; |
| 108 |
/** Additional props of the group */ |
| 109 |
groupProps?: PageGroupProps; |
| 110 |
/** Additional props of the breadcrumb */ |
| 111 |
breadcrumbProps?: PageBreadcrumbProps; |
| 112 |
/** Enables children to fill the available vertical space. Child page sections or groups that should fill should be passed the isFilled property. */ |
| 113 |
isContentFilled?: boolean; |
| 114 |
} |
| 115 |
|
| 116 |
export interface PageState { |
| 117 |
desktopIsSidebarOpen: boolean; |
| 118 |
mobileIsSidebarOpen: boolean; |
| 119 |
mobileView: boolean; |
| 120 |
width: number; |
| 121 |
height: number; |
| 122 |
} |
| 123 |
|
| 124 |
class Page extends Component<PageProps, PageState> { |
| 125 |
static displayName = 'Page'; |
| 126 |
static defaultProps: PageProps = { |
| 127 |
isManagedSidebar: false, |
| 128 |
isBreadcrumbWidthLimited: false, |
| 129 |
defaultManagedSidebarIsOpen: true, |
| 130 |
mainTabIndex: -1, |
| 131 |
isNotificationDrawerExpanded: false, |
| 132 |
onNotificationDrawerExpand: () => null, |
| 133 |
mainComponent: 'main', |
| 134 |
getBreakpoint, |
| 135 |
getVerticalBreakpoint |
| 136 |
}; |
| 137 |
mainRef = createRef<HTMLDivElement>(); |
| 138 |
pageRef = createRef<HTMLDivElement>(); |
| 139 |
observer: any = () => {}; |
| 140 |
|
| 141 |
constructor(props: PageProps) { |
| 142 |
super(props); |
| 143 |
|
| 144 |
const { isManagedSidebar, defaultManagedSidebarIsOpen } = props; |
| 145 |
const managedSidebarOpen = !isManagedSidebar ? true : defaultManagedSidebarIsOpen; |
| 146 |
this.state = { |
| 147 |
desktopIsSidebarOpen: managedSidebarOpen, |
| 148 |
mobileIsSidebarOpen: false, |
| 149 |
mobileView: false, |
| 150 |
width: null, |
| 151 |
height: null |
| 152 |
}; |
| 153 |
} |
| 154 |
|
| 155 |
componentDidMount() { |
| 156 |
const { isManagedSidebar, onPageResize } = this.props; |
| 157 |
if (isManagedSidebar || onPageResize) { |
| 158 |
this.observer = getResizeObserver(this.pageRef.current, this.handleResize); |
| 159 |
const currentRef = this.mainRef.current; |
| 160 |
if (currentRef) { |
| 161 |
currentRef.addEventListener('mousedown', this.handleMainClick); |
| 162 |
currentRef.addEventListener('touchstart', this.handleMainClick); |
| 163 |
} |
| 164 |
// Initial check if should be shown |
| 165 |
this.resize(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
componentWillUnmount() { |
| 170 |
const { isManagedSidebar, onPageResize } = this.props; |
| 171 |
if (isManagedSidebar || onPageResize) { |
| 172 |
this.observer(); |
| 173 |
const currentRef = this.mainRef.current; |
| 174 |
if (currentRef) { |
| 175 |
currentRef.removeEventListener('mousedown', this.handleMainClick); |
| 176 |
currentRef.removeEventListener('touchstart', this.handleMainClick); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
getWindowWidth = () => { |
| 182 |
if (canUseDOM) { |
| 183 |
return this.pageRef.current ? this.pageRef.current.clientWidth : window.innerWidth; |
| 184 |
} else { |
| 185 |
return 1200; |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
isMobile = () => |
| 190 |
// eslint-disable-next-line radix |
| 191 |
this.getWindowWidth() < Number.parseInt(globalBreakpointXl.value, 10) * 16; |
| 192 |
|
| 193 |
resize = (_event?: MouseEvent | TouchEvent | React.KeyboardEvent<Element>) => { |
| 194 |
const { onPageResize } = this.props; |
| 195 |
const mobileView = this.isMobile(); |
| 196 |
if (onPageResize) { |
| 197 |
onPageResize(_event, { mobileView, windowSize: this.getWindowWidth() }); |
| 198 |
} |
| 199 |
if (mobileView !== this.state.mobileView) { |
| 200 |
this.setState({ mobileView }); |
| 201 |
} |
| 202 |
if (this.pageRef?.current) { |
| 203 |
const currentWidth = this.pageRef.current.clientWidth; |
| 204 |
const currentHeight = this.pageRef.current.clientHeight; |
| 205 |
|
| 206 |
if (this.state.width !== currentWidth) { |
| 207 |
this.setState({ width: currentWidth }); |
| 208 |
} |
| 209 |
if (this.state.height !== currentHeight) { |
| 210 |
this.setState({ height: currentHeight }); |
| 211 |
} |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
handleResize = debounce(this.resize, 250); |
| 216 |
|
| 217 |
handleMainClick = () => { |
| 218 |
if (this.isMobile() && this.state.mobileIsSidebarOpen && this.mainRef.current) { |
| 219 |
this.setState({ mobileIsSidebarOpen: false }); |
| 220 |
} |
| 221 |
}; |
| 222 |
|
| 223 |
onSidebarToggleMobile = () => { |
| 224 |
this.setState((prevState) => ({ |
| 225 |
mobileIsSidebarOpen: !prevState.mobileIsSidebarOpen |
| 226 |
})); |
| 227 |
}; |
| 228 |
|
| 229 |
onSidebarToggleDesktop = () => { |
| 230 |
this.setState((prevState) => ({ |
| 231 |
desktopIsSidebarOpen: !prevState.desktopIsSidebarOpen |
| 232 |
})); |
| 233 |
}; |
| 234 |
|
| 235 |
render() { |
| 236 |
const { |
| 237 |
banner, |
| 238 |
breadcrumb, |
| 239 |
isBreadcrumbWidthLimited, |
| 240 |
className, |
| 241 |
children, |
| 242 |
variant, |
| 243 |
isDockExpanded = false, |
| 244 |
isDockTextExpanded = false, |
| 245 |
masthead, |
| 246 |
dockContent, |
| 247 |
sidebar, |
| 248 |
notificationDrawer, |
| 249 |
isNotificationDrawerExpanded, |
| 250 |
onNotificationDrawerExpand, |
| 251 |
drawerDefaultSize, |
| 252 |
drawerMinSize, |
| 253 |
drawerMaxSize, |
| 254 |
isHorizontalSubnavWidthLimited, |
| 255 |
skipToContent, |
| 256 |
role, |
| 257 |
mainContainerId, |
| 258 |
isManagedSidebar, |
| 259 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 260 |
defaultManagedSidebarIsOpen, |
| 261 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 262 |
onPageResize, |
| 263 |
getBreakpoint, |
| 264 |
getVerticalBreakpoint, |
| 265 |
mainAriaLabel, |
| 266 |
mainTabIndex, |
| 267 |
mainComponent, |
| 268 |
horizontalSubnav, |
| 269 |
isHorizontalSubnavGrouped, |
| 270 |
isBreadcrumbGrouped, |
| 271 |
additionalGroupedContent, |
| 272 |
groupProps, |
| 273 |
breadcrumbProps, |
| 274 |
isContentFilled, |
| 275 |
...rest |
| 276 |
} = this.props; |
| 277 |
const { mobileView, mobileIsSidebarOpen, desktopIsSidebarOpen, width, height } = this.state; |
| 278 |
|
| 279 |
const context = { |
| 280 |
isManagedSidebar, |
| 281 |
onSidebarToggle: mobileView ? this.onSidebarToggleMobile : this.onSidebarToggleDesktop, |
| 282 |
isSidebarOpen: mobileView ? mobileIsSidebarOpen : desktopIsSidebarOpen, |
| 283 |
isMobile: mobileView, |
| 284 |
width, |
| 285 |
height, |
| 286 |
getBreakpoint, |
| 287 |
getVerticalBreakpoint |
| 288 |
}; |
| 289 |
|
| 290 |
let nav = null; |
| 291 |
if (horizontalSubnav) { |
| 292 |
nav = ( |
| 293 |
<div className={css(styles.pageMainSubnav, isHorizontalSubnavWidthLimited && styles.modifiers.limitWidth)}> |
| 294 |
<PageBody>{horizontalSubnav}</PageBody> |
| 295 |
</div> |
| 296 |
); |
| 297 |
} |
| 298 |
|
| 299 |
const crumb = breadcrumb ? ( |
| 300 |
<PageBreadcrumb |
| 301 |
stickyOnBreakpoint={breadcrumbProps?.stickyOnBreakpoint} |
| 302 |
isWidthLimited={isBreadcrumbWidthLimited} |
| 303 |
> |
| 304 |
<PageBody>{breadcrumb}</PageBody> |
| 305 |
</PageBreadcrumb> |
| 306 |
) : null; |
| 307 |
|
| 308 |
const isGrouped = isHorizontalSubnavGrouped || isBreadcrumbGrouped || additionalGroupedContent; |
| 309 |
|
| 310 |
const group = isGrouped ? ( |
| 311 |
<PageGroup {...groupProps}> |
| 312 |
{isHorizontalSubnavGrouped && nav} |
| 313 |
{banner} |
| 314 |
{isBreadcrumbGrouped && crumb} |
| 315 |
{additionalGroupedContent} |
| 316 |
</PageGroup> |
| 317 |
) : null; |
| 318 |
|
| 319 |
const Component: keyof React.JSX.IntrinsicElements = mainComponent; |
| 320 |
|
| 321 |
const main = ( |
| 322 |
<div className={css(styles.pageMainContainer, isContentFilled && styles.modifiers.fill)}> |
| 323 |
<Component |
| 324 |
ref={this.mainRef} |
| 325 |
role={role} |
| 326 |
id={mainContainerId} |
| 327 |
className={css(styles.pageMain)} |
| 328 |
tabIndex={mainTabIndex} |
| 329 |
aria-label={mainAriaLabel} |
| 330 |
> |
| 331 |
{group} |
| 332 |
{!isHorizontalSubnavGrouped && nav} |
| 333 |
{banner} |
| 334 |
{!isBreadcrumbGrouped && crumb} |
| 335 |
{children} |
| 336 |
</Component> |
| 337 |
</div> |
| 338 |
); |
| 339 |
|
| 340 |
const panelContent = ( |
| 341 |
<DrawerPanelContent defaultSize={drawerDefaultSize} minSize={drawerMinSize} maxSize={drawerMaxSize}> |
| 342 |
{notificationDrawer} |
| 343 |
</DrawerPanelContent> |
| 344 |
); |
| 345 |
|
| 346 |
return ( |
| 347 |
<PageContextProvider value={context}> |
| 348 |
<div |
| 349 |
ref={this.pageRef} |
| 350 |
{...rest} |
| 351 |
className={css( |
| 352 |
styles.page, |
| 353 |
variant === 'docked' && styles.modifiers.docked, |
| 354 |
width !== null && height !== null && 'pf-m-resize-observer', |
| 355 |
width !== null && `pf-m-breakpoint-${getBreakpoint(width)}`, |
| 356 |
height !== null && `pf-m-height-breakpoint-${getVerticalBreakpoint(height)}`, |
| 357 |
sidebar === null && styles.modifiers.noSidebar, |
| 358 |
className |
| 359 |
)} |
| 360 |
> |
| 361 |
{skipToContent} |
| 362 |
{variant === 'docked' ? ( |
| 363 |
<> |
| 364 |
{masthead} |
| 365 |
<div |
| 366 |
className={css( |
| 367 |
styles.pageDock, |
| 368 |
isDockExpanded && styles.modifiers.expanded, |
| 369 |
isDockTextExpanded && styles.modifiers.textExpanded |
| 370 |
)} |
| 371 |
> |
| 372 |
<div className={css(styles.pageDockMain)}>{dockContent}</div> |
| 373 |
</div> |
| 374 |
</> |
| 375 |
) : ( |
| 376 |
masthead |
| 377 |
)} |
| 378 |
{sidebar} |
| 379 |
{notificationDrawer && ( |
| 380 |
<div className={css(styles.pageDrawer)}> |
| 381 |
<Drawer isExpanded={isNotificationDrawerExpanded} onExpand={(event) => onNotificationDrawerExpand(event)}> |
| 382 |
<DrawerContent panelContent={panelContent}>{main}</DrawerContent> |
| 383 |
</Drawer> |
| 384 |
</div> |
| 385 |
)} |
| 386 |
{!notificationDrawer && main} |
| 387 |
</div> |
| 388 |
</PageContextProvider> |
| 389 |
); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
export { Page }; |