Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/Page/Page.js

raw
1 import { __rest } from "tslib";
2 import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3 import { Component, createRef } from 'react';
4 import styles from '@patternfly/react-styles/css/components/Page/page.mjs';
5 import { css } from '@patternfly/react-styles';
6 import globalBreakpointXl from '@patternfly/react-tokens/dist/esm/t_global_breakpoint_xl';
7 import { debounce, canUseDOM } from '../../helpers/util';
8 import { Drawer, DrawerContent, DrawerPanelContent } from '../Drawer';
9 import { PageBreadcrumb } from './PageBreadcrumb';
10 import { PageGroup } from './PageGroup';
11 import { getResizeObserver } from '../../helpers/resizeObserver';
12 import { getBreakpoint, getVerticalBreakpoint } from '../../helpers/util';
13 import { PageContextProvider } from './PageContext';
14 import { PageBody } from './PageBody';
15 export var PageLayouts;
16 (function (PageLayouts) {
17 PageLayouts["vertical"] = "vertical";
18 PageLayouts["horizontal"] = "horizontal";
19 })(PageLayouts || (PageLayouts = {}));
20 class Page extends Component {
21 constructor(props) {
22 super(props);
23 this.mainRef = createRef();
24 this.pageRef = createRef();
25 this.observer = () => { };
26 this.getWindowWidth = () => {
27 if (canUseDOM) {
28 return this.pageRef.current ? this.pageRef.current.clientWidth : window.innerWidth;
29 }
30 else {
31 return 1200;
32 }
33 };
34 this.isMobile = () =>
35 // eslint-disable-next-line radix
36 this.getWindowWidth() < Number.parseInt(globalBreakpointXl.value, 10) * 16;
37 this.resize = (_event) => {
38 var _a;
39 const { onPageResize } = this.props;
40 const mobileView = this.isMobile();
41 if (onPageResize) {
42 onPageResize(_event, { mobileView, windowSize: this.getWindowWidth() });
43 }
44 if (mobileView !== this.state.mobileView) {
45 this.setState({ mobileView });
46 }
47 if ((_a = this.pageRef) === null || _a === void 0 ? void 0 : _a.current) {
48 const currentWidth = this.pageRef.current.clientWidth;
49 const currentHeight = this.pageRef.current.clientHeight;
50 if (this.state.width !== currentWidth) {
51 this.setState({ width: currentWidth });
52 }
53 if (this.state.height !== currentHeight) {
54 this.setState({ height: currentHeight });
55 }
56 }
57 };
58 this.handleResize = debounce(this.resize, 250);
59 this.handleMainClick = () => {
60 if (this.isMobile() && this.state.mobileIsSidebarOpen && this.mainRef.current) {
61 this.setState({ mobileIsSidebarOpen: false });
62 }
63 };
64 this.onSidebarToggleMobile = () => {
65 this.setState((prevState) => ({
66 mobileIsSidebarOpen: !prevState.mobileIsSidebarOpen
67 }));
68 };
69 this.onSidebarToggleDesktop = () => {
70 this.setState((prevState) => ({
71 desktopIsSidebarOpen: !prevState.desktopIsSidebarOpen
72 }));
73 };
74 const { isManagedSidebar, defaultManagedSidebarIsOpen } = props;
75 const managedSidebarOpen = !isManagedSidebar ? true : defaultManagedSidebarIsOpen;
76 this.state = {
77 desktopIsSidebarOpen: managedSidebarOpen,
78 mobileIsSidebarOpen: false,
79 mobileView: false,
80 width: null,
81 height: null
82 };
83 }
84 componentDidMount() {
85 const { isManagedSidebar, onPageResize } = this.props;
86 if (isManagedSidebar || onPageResize) {
87 this.observer = getResizeObserver(this.pageRef.current, this.handleResize);
88 const currentRef = this.mainRef.current;
89 if (currentRef) {
90 currentRef.addEventListener('mousedown', this.handleMainClick);
91 currentRef.addEventListener('touchstart', this.handleMainClick);
92 }
93 // Initial check if should be shown
94 this.resize();
95 }
96 }
97 componentWillUnmount() {
98 const { isManagedSidebar, onPageResize } = this.props;
99 if (isManagedSidebar || onPageResize) {
100 this.observer();
101 const currentRef = this.mainRef.current;
102 if (currentRef) {
103 currentRef.removeEventListener('mousedown', this.handleMainClick);
104 currentRef.removeEventListener('touchstart', this.handleMainClick);
105 }
106 }
107 }
108 render() {
109 const _a = this.props, { banner, breadcrumb, isBreadcrumbWidthLimited, className, children, variant, isDockExpanded = false, isDockTextExpanded = false, masthead, dockContent, sidebar, notificationDrawer, isNotificationDrawerExpanded, onNotificationDrawerExpand, drawerDefaultSize, drawerMinSize, drawerMaxSize, isHorizontalSubnavWidthLimited, skipToContent, role, mainContainerId, isManagedSidebar,
110 // eslint-disable-next-line @typescript-eslint/no-unused-vars
111 defaultManagedSidebarIsOpen,
112 // eslint-disable-next-line @typescript-eslint/no-unused-vars
113 onPageResize, getBreakpoint, getVerticalBreakpoint, mainAriaLabel, mainTabIndex, mainComponent, horizontalSubnav, isHorizontalSubnavGrouped, isBreadcrumbGrouped, additionalGroupedContent, groupProps, breadcrumbProps, isContentFilled } = _a, rest = __rest(_a, ["banner", "breadcrumb", "isBreadcrumbWidthLimited", "className", "children", "variant", "isDockExpanded", "isDockTextExpanded", "masthead", "dockContent", "sidebar", "notificationDrawer", "isNotificationDrawerExpanded", "onNotificationDrawerExpand", "drawerDefaultSize", "drawerMinSize", "drawerMaxSize", "isHorizontalSubnavWidthLimited", "skipToContent", "role", "mainContainerId", "isManagedSidebar", "defaultManagedSidebarIsOpen", "onPageResize", "getBreakpoint", "getVerticalBreakpoint", "mainAriaLabel", "mainTabIndex", "mainComponent", "horizontalSubnav", "isHorizontalSubnavGrouped", "isBreadcrumbGrouped", "additionalGroupedContent", "groupProps", "breadcrumbProps", "isContentFilled"]);
114 const { mobileView, mobileIsSidebarOpen, desktopIsSidebarOpen, width, height } = this.state;
115 const context = {
116 isManagedSidebar,
117 onSidebarToggle: mobileView ? this.onSidebarToggleMobile : this.onSidebarToggleDesktop,
118 isSidebarOpen: mobileView ? mobileIsSidebarOpen : desktopIsSidebarOpen,
119 isMobile: mobileView,
120 width,
121 height,
122 getBreakpoint,
123 getVerticalBreakpoint
124 };
125 let nav = null;
126 if (horizontalSubnav) {
127 nav = (_jsx("div", { className: css(styles.pageMainSubnav, isHorizontalSubnavWidthLimited && styles.modifiers.limitWidth), children: _jsx(PageBody, { children: horizontalSubnav }) }));
128 }
129 const crumb = breadcrumb ? (_jsx(PageBreadcrumb, { stickyOnBreakpoint: breadcrumbProps === null || breadcrumbProps === void 0 ? void 0 : breadcrumbProps.stickyOnBreakpoint, isWidthLimited: isBreadcrumbWidthLimited, children: _jsx(PageBody, { children: breadcrumb }) })) : null;
130 const isGrouped = isHorizontalSubnavGrouped || isBreadcrumbGrouped || additionalGroupedContent;
131 const group = isGrouped ? (_jsxs(PageGroup, Object.assign({}, groupProps, { children: [isHorizontalSubnavGrouped && nav, banner, isBreadcrumbGrouped && crumb, additionalGroupedContent] }))) : null;
132 const Component = mainComponent;
133 const main = (_jsx("div", { className: css(styles.pageMainContainer, isContentFilled && styles.modifiers.fill), children: _jsxs(Component, { ref: this.mainRef, role: role, id: mainContainerId, className: css(styles.pageMain), tabIndex: mainTabIndex, "aria-label": mainAriaLabel, children: [group, !isHorizontalSubnavGrouped && nav, banner, !isBreadcrumbGrouped && crumb, children] }) }));
134 const panelContent = (_jsx(DrawerPanelContent, { defaultSize: drawerDefaultSize, minSize: drawerMinSize, maxSize: drawerMaxSize, children: notificationDrawer }));
135 return (_jsx(PageContextProvider, { value: context, children: _jsxs("div", Object.assign({ ref: this.pageRef }, rest, { className: css(styles.page, variant === 'docked' && styles.modifiers.docked, 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), children: [skipToContent, variant === 'docked' ? (_jsxs(_Fragment, { children: [masthead, _jsx("div", { className: css(styles.pageDock, isDockExpanded && styles.modifiers.expanded, isDockTextExpanded && styles.modifiers.textExpanded), children: _jsx("div", { className: css(styles.pageDockMain), children: dockContent }) })] })) : (masthead), sidebar, notificationDrawer && (_jsx("div", { className: css(styles.pageDrawer), children: _jsx(Drawer, { isExpanded: isNotificationDrawerExpanded, onExpand: (event) => onNotificationDrawerExpand(event), children: _jsx(DrawerContent, { panelContent: panelContent, children: main }) }) })), !notificationDrawer && main] })) }));
136 }
137 }
138 Page.displayName = 'Page';
139 Page.defaultProps = {
140 isManagedSidebar: false,
141 isBreadcrumbWidthLimited: false,
142 defaultManagedSidebarIsOpen: true,
143 mainTabIndex: -1,
144 isNotificationDrawerExpanded: false,
145 onNotificationDrawerExpand: () => null,
146 mainComponent: 'main',
147 getBreakpoint,
148 getVerticalBreakpoint
149 };
150 export { Page };
151 //# sourceMappingURL=Page.js.map