-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
Layout.tsx
150 lines (138 loc) · 4.56 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import type { ReactElement, ReactNode } from "react";
import { useEffect, useRef, useState } from "react";
import type { ConfiguredIcons } from "@react-md/icon";
import type { LayoutConfiguration } from "@react-md/layout";
import {
Configuration,
Layout as RMDLayout,
useLayoutNavigation,
} from "@react-md/layout";
import {
ArrowDropDownSVGIcon,
ArrowUpwardSVGIcon,
CheckBoxSVGIcon,
CheckSVGIcon,
ErrorOutlineSVGIcon,
FileUploadSVGIcon,
KeyboardArrowDownSVGIcon,
KeyboardArrowLeftSVGIcon,
KeyboardArrowRightSVGIcon,
MenuSVGIcon,
NotificationsSVGIcon,
RadioButtonCheckedSVGIcon,
RemoveRedEyeSVGIcon,
} from "@react-md/material-icons";
import type { MenuConfiguration } from "@react-md/menu";
import { useCrossFadeTransition } from "@react-md/transition";
import type { AppSizeListenerProps } from "@react-md/utils";
import { useIsomorphicLayoutEffect } from "@react-md/utils";
import type { CodePreference } from "components/CodePreference";
import { CodePreferenceProvider } from "components/CodePreference";
import { IdProvider } from "components/IdProvider";
import LinkUnstyled from "components/LinkUnstyled";
import TableOfContents from "components/TableOfContents";
import { TOCVisibilityProvider } from "components/TableOfContents/VisibilityContext";
import navItems from "constants/navItems";
import Actions from "./Actions";
import { Provider } from "./fixedAppBarContext";
import NavHeaderTitle from "./NavHeaderTitle";
export interface LayoutProps
extends Required<Pick<AppSizeListenerProps, "defaultSize">> {
title: string;
pathname: string;
children: ReactNode;
defaultPreference: CodePreference;
}
const icons: ConfiguredIcons = {
back: <KeyboardArrowLeftSVGIcon />,
checkbox: <CheckBoxSVGIcon />,
dropdown: <ArrowDropDownSVGIcon />,
expander: <KeyboardArrowDownSVGIcon />,
error: <ErrorOutlineSVGIcon />,
forward: <KeyboardArrowRightSVGIcon />,
menu: <MenuSVGIcon />,
notification: <NotificationsSVGIcon />,
radio: <RadioButtonCheckedSVGIcon />,
password: <RemoveRedEyeSVGIcon />,
selected: <CheckSVGIcon />,
sort: <ArrowUpwardSVGIcon />,
upload: <FileUploadSVGIcon />,
};
const menuConfiguration: Readonly<MenuConfiguration> = {
renderAsSheet: "phone",
};
let devLayoutConf: LayoutConfiguration | undefined;
if (process.env.NODE_ENV !== "production") {
devLayoutConf = {
landscapeTabletLayout: "temporary",
desktopLayout: "temporary",
largeDesktopLayout: "temporary",
};
}
export default function Layout({
children,
title,
pathname,
defaultSize,
defaultPreference,
}: LayoutProps): ReactElement {
const [elevated, setElevated] = useState(pathname !== "/");
const rendered = useRef(false);
useEffect(() => {
if (!rendered.current) {
rendered.current = true;
return;
}
setElevated(pathname !== "/");
}, [pathname]);
const prevPathname = useRef(pathname);
const { elementProps, transitionTo } = useCrossFadeTransition();
useIsomorphicLayoutEffect(() => {
if (prevPathname.current === pathname) {
return;
}
// since the sandbox route is a full page modal, don't want to transition
// to make it appear smoother between the two
const isTransitionable =
!prevPathname.current.startsWith("/sandbox") &&
!pathname.startsWith("/sandbox");
prevPathname.current = pathname;
if (isTransitionable) {
transitionTo("enter");
}
}, [pathname, transitionTo]);
return (
<Configuration
icons={icons}
defaultSize={defaultSize}
menuConfiguration={menuConfiguration}
>
<TOCVisibilityProvider pathname={pathname}>
<IdProvider>
<CodePreferenceProvider defaultPreference={defaultPreference}>
<RMDLayout
appBarProps={{
fixedElevation: elevated,
children: <Actions />,
}}
title={title.replace("react-md@v2 - ", "")}
mainProps={elementProps}
treeProps={useLayoutNavigation(
navItems,
// I don't add each blog to the navigation tree, but still want to
// show that a blog is being viewed
pathname.replace(/^\/blog.*$/, "/blog"),
LinkUnstyled
)}
navHeaderProps={{ children: <NavHeaderTitle /> }}
{...devLayoutConf}
>
<TableOfContents pathname={pathname} />
<Provider value={setElevated}>{children}</Provider>
</RMDLayout>
</CodePreferenceProvider>
</IdProvider>
</TOCVisibilityProvider>
</Configuration>
);
}