forked from riccardo-forina/use-patternfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppLayout.tsx
150 lines (139 loc) · 4.27 KB
/
AppLayout.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
import * as React from 'react';
import {
Nav,
NavList,
NavVariants,
Page,
PageHeader,
PageSidebar,
SkipToContent,
PageHeaderProps,
} from '@patternfly/react-core';
import { AppNavExpandable, IAppNavExpandableProps } from './AppNavExpandable';
import { AppNavGroup, IAppNavGroupProps } from './AppNavGroup';
import { AppNavItem, IAppNavItemProps } from './AppNavItem';
export interface IAppLayoutContext {
setBreadcrumb: (breadcrumb: React.ReactNode) => void;
}
export const AppLayoutContext = React.createContext<IAppLayoutContext>({
setBreadcrumb: () => void 0,
});
export interface IAppLayoutProps
extends Pick<PageHeaderProps, 'logo'>,
Pick<PageHeaderProps, 'logoProps'>,
Pick<PageHeaderProps, 'avatar'>,
Pick<PageHeaderProps, 'toolbar'> {
navVariant?: 'vertical' | 'horizontal';
navItems?: Array<
IAppNavItemProps | IAppNavExpandableProps | IAppNavGroupProps | undefined
>;
navGroupsStyle?: 'grouped' | 'expandable';
startWithOpenNav?: boolean;
theme?: 'dark' | 'light';
}
export const AppLayout: React.FunctionComponent<IAppLayoutProps> = ({
logo,
logoProps,
navVariant = 'horizontal',
navItems = [],
navGroupsStyle = 'grouped',
toolbar,
avatar,
startWithOpenNav = true,
theme = 'dark',
children,
}) => {
const [isNavOpen, setIsNavOpen] = React.useState(startWithOpenNav);
const [isMobileView, setIsMobileView] = React.useState(true);
const [isNavOpenMobile, setIsNavOpenMobile] = React.useState(false);
const [breadcrumb, setBreadcrumb] = React.useState<
React.ReactNode | undefined
>();
const previousBreadcrumb = React.useRef<React.ReactNode | null>();
const handleSetBreadcrumb = React.useCallback(
(newBreadcrumb: React.ReactNode) => {
if (previousBreadcrumb.current !== newBreadcrumb) {
previousBreadcrumb.current = newBreadcrumb;
setBreadcrumb(previousBreadcrumb.current);
}
},
[setBreadcrumb, previousBreadcrumb]
);
const onNavToggleMobile = () => {
setIsNavOpenMobile(!isNavOpenMobile);
};
const onNavToggle = () => {
setIsNavOpen(!isNavOpen);
};
const onPageResize = (props: { mobileView: boolean; windowSize: number }) => {
setIsMobileView(props.mobileView);
};
React.useEffect(() => {
setIsNavOpen(startWithOpenNav);
}, [startWithOpenNav, setIsNavOpen]);
const isVertical = navVariant === 'vertical';
const variant = isVertical ? NavVariants.default : NavVariants.horizontal;
const Navigation =
navItems.length > 0 ? (
<Nav id="nav-primary-simple" theme={theme}>
<NavList id="nav-list-simple" variant={variant}>
{navItems.map((navItem, idx) => {
if (navItem && navItem.hasOwnProperty('items') && isVertical) {
return navGroupsStyle === 'expandable' ? (
<AppNavExpandable
{...(navItem as IAppNavExpandableProps)}
key={idx}
/>
) : (
<AppNavGroup {...(navItem as IAppNavGroupProps)} key={idx} />
);
} else {
return (
<AppNavItem {...(navItem as IAppNavItemProps)} key={idx} />
);
}
})}
</NavList>
</Nav>
) : null;
const Header = (
<PageHeader
logo={logo}
logoProps={logoProps}
avatar={avatar}
toolbar={toolbar}
showNavToggle={isVertical}
isNavOpen={isVertical ? isNavOpen : undefined}
onNavToggle={isMobileView ? onNavToggleMobile : onNavToggle}
topNav={isVertical ? undefined : Navigation}
/>
);
const Sidebar =
navVariant === 'vertical' ? (
<PageSidebar
nav={Navigation}
isNavOpen={isMobileView ? isNavOpenMobile : isNavOpen}
theme={theme}
data-testid="app-sidebar"
/>
) : (
undefined
);
const PageSkipToContent = (
<SkipToContent href="#primary-app-container">Skip to Content</SkipToContent>
);
return (
<AppLayoutContext.Provider value={{ setBreadcrumb: handleSetBreadcrumb }}>
<Page
mainContainerId="primary-app-container"
header={Header}
sidebar={Sidebar}
breadcrumb={breadcrumb}
onPageResize={onPageResize}
skipToContent={PageSkipToContent}
>
{children}
</Page>
</AppLayoutContext.Provider>
);
};