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