-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Layout.tsx
135 lines (125 loc) · 4.27 KB
/
Layout.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
import React, { ComponentType, ErrorInfo, Suspense, useState } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import clsx from 'clsx';
import { styled, SxProps } from '@mui/material/styles';
import { AppBar as DefaultAppBar, AppBarProps } from './AppBar';
import { Sidebar as DefaultSidebar, SidebarProps } from './Sidebar';
import { Menu as DefaultMenu, MenuProps } from './Menu';
import { Error, ErrorProps } from './Error';
import { SkipNavigationButton } from '../button';
import { Inspector } from '../preferences';
import { Loading } from './Loading';
export const Layout = (props: LayoutProps) => {
const {
appBar: AppBar = DefaultAppBar,
appBarAlwaysOn,
children,
className,
error: errorComponent,
menu: Menu = DefaultMenu,
sidebar: Sidebar = DefaultSidebar,
...rest
} = props;
const [errorInfo, setErrorInfo] = useState<ErrorInfo | undefined>(
undefined
);
const handleError = (error: Error, info: ErrorInfo) => {
setErrorInfo(info);
};
return (
<Core className={clsx('layout', className)} {...rest}>
<SkipNavigationButton />
<div className={LayoutClasses.appFrame}>
<AppBar alwaysOn={appBarAlwaysOn} />
<main className={LayoutClasses.contentWithSidebar}>
<Sidebar appBarAlwaysOn={appBarAlwaysOn}>
<Menu />
</Sidebar>
<div id="main-content" className={LayoutClasses.content}>
<ErrorBoundary
onError={handleError}
fallbackRender={({ error, resetErrorBoundary }) => (
<Error
error={error}
errorComponent={errorComponent}
errorInfo={errorInfo}
resetErrorBoundary={resetErrorBoundary}
/>
)}
>
<Suspense fallback={<Loading />}>
{children}
</Suspense>
</ErrorBoundary>
</div>
</main>
<Inspector />
</div>
</Core>
);
};
export interface LayoutProps {
appBar?: ComponentType<AppBarProps>;
appBarAlwaysOn?: boolean;
className?: string;
children: React.ReactNode;
error?: ComponentType<ErrorProps>;
menu?: ComponentType<MenuProps>;
sidebar?: ComponentType<SidebarProps>;
sx?: SxProps;
}
export interface LayoutState {
hasError: boolean;
error?: Error;
errorInfo?: ErrorInfo;
}
const PREFIX = 'RaLayout';
export const LayoutClasses = {
appFrame: `${PREFIX}-appFrame`,
contentWithSidebar: `${PREFIX}-contentWithSidebar`,
content: `${PREFIX}-content`,
};
const Core = styled('div', {
name: PREFIX,
overridesResolver: (props, styles) => styles.root,
})(({ theme }) => ({
display: 'flex',
flexDirection: 'column',
zIndex: 1,
minHeight: '100vh',
backgroundColor: theme.palette.background.default,
position: 'relative',
minWidth: 'fit-content',
width: '100%',
color: theme.palette.getContrastText(theme.palette.background.default),
[`& .${LayoutClasses.appFrame}`]: {
display: 'flex',
flexDirection: 'column',
flexGrow: 1,
marginTop: theme.spacing(6),
[theme.breakpoints.down('sm')]: {
marginTop: theme.spacing(7),
},
},
[`& .${LayoutClasses.contentWithSidebar}`]: {
display: 'flex',
flexGrow: 1,
transition: theme.transitions.create('margin', {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.enteringScreen,
}),
},
[`& .${LayoutClasses.content}`]: {
backgroundColor: theme.palette.background.default,
zIndex: 2,
display: 'flex',
flexDirection: 'column',
flexGrow: 1,
flexBasis: 0,
padding: 0,
[theme.breakpoints.up('xs')]: {
paddingRight: theme.spacing(1),
paddingLeft: theme.spacing(1),
},
},
}));