/
TopBar.tsx
239 lines (230 loc) · 6.97 KB
/
TopBar.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
import { useEffect, useRef } from 'react';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { usePlausible } from 'next-plausible';
import {
AppBar as MuiAppBar,
Box,
IconButton as MuiIconButton,
styled,
Toolbar as MuiToolbar,
Tooltip,
Typography,
useMediaQuery,
useTheme,
Select,
MenuItem,
ThemeProvider,
createTheme,
} from '@mui/material';
import DarkModeIcon from '@mui/icons-material/DarkMode';
import GitHubIcon from '@mui/icons-material/GitHub';
import Image from 'next/image';
import LightModeIcon from '@mui/icons-material/LightMode';
import MenuIcon from '@mui/icons-material/Menu';
import MenuOpenIcon from '@mui/icons-material/MenuOpen';
import { useThemeContext } from '../../styles/ThemeContext';
import docsearch from '@docsearch/js';
import '@docsearch/css';
const AppBar = styled(MuiAppBar)({
zIndex: 5,
});
const Toolbar = styled(MuiToolbar)({
display: 'flex',
justifyContent: 'space-between',
});
const IconButton = styled(MuiIconButton)({
color: '#fff',
height: '3rem',
width: '3rem',
});
interface Props {
navOpen: boolean;
setNavOpen: (navOpen: boolean) => void;
}
export const TopBar = ({ navOpen, setNavOpen }: Props) => {
const isMounted = useRef(false);
const { pathname } = useRouter();
const plausible = usePlausible();
const theme = useTheme();
const isMobile = useMediaQuery('(max-width: 600px)');
const isTablet = useMediaQuery('(max-width: 900px)');
const isDesktop = useMediaQuery('(min-width: 1500px)');
const isXLDesktop = useMediaQuery('(min-width: 1800px)');
const { isLightTheme, setIsLightTheme } = useThemeContext();
useEffect(() => {
if (typeof window !== 'undefined') {
docsearch({
appId: '1W9SWN5ZAH',
apiKey: '680b219eaef484622046bf76cef8544a',
indexName: 'material-react-table',
container: '#docsearch',
});
}
}, []);
useEffect(() => {
if (typeof window === 'undefined') return;
if (isMounted.current && isXLDesktop) {
try {
(window as any).ethicalads?.load?.();
} catch (e) {
console.log(e);
}
}
isMounted.current = true;
}, [
isXLDesktop,
isTablet,
isDesktop,
isMobile,
pathname,
theme.palette.mode,
]);
return (
<>
<style global jsx>
{`
:root {
--docsearch-primary-color: #1565c0;
--docsearch-highlight-color: #1565c0;
--docsearch-logo-color: #1565c0;
${!isLightTheme
? `--docsearch-container-background: rgba(11, 11, 11, 0.8);
--docsearch-footer-background: #222;
--docsearch-hit-background: #333;
--docsearch-hit-color: #fff;
--docsearch-hit-shadow: none;
--docsearch-modal-background: #222;
--docsearch-modal-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
--docsearch-searchbox-background: #000;
--docsearch-searchbox-focus-background: #000;
--docsearch-text-color: #fff;
`
: ''}
}
`}
</style>
<AppBar sx={{ opacity: 0.95 }} position="fixed">
<Toolbar sx={{ p: '2px 4px' }} disableGutters variant="dense">
<Box sx={{ display: 'flex' }}>
{!isDesktop && (
<IconButton
aria-label="Open nav menu"
onClick={() => setNavOpen(!navOpen)}
>
{navOpen ? (
<MenuOpenIcon color="inherit" />
) : (
<MenuIcon color="inherit" />
)}
</IconButton>
)}
<Link href="/" passHref legacyBehavior>
<Typography
sx={{
alignItems: 'center',
cursor: 'pointer',
display: 'flex',
fontSize: isTablet ? '1.6rem' : '1.5rem',
gap: '1rem',
}}
variant="h1"
>
<Image
alt="MRT logo"
src="/mrt_logo.svg"
height={isTablet ? 35 : 40}
width={isTablet ? 35 : 40}
/>
{!isMobile && 'Material React Table'}
</Typography>
</Link>
<ThemeProvider
theme={createTheme({ ...theme, palette: { mode: 'dark' } })}
>
<Select
onOpen={() => plausible('version-select')}
MenuProps={{ disableScrollLock: true }}
value="v2"
size="small"
sx={{ m: '8px', height: '30px' }}
>
<Link
legacyBehavior
href={`https://v1.material-react-table.com/${pathname}`}
>
<MenuItem sx={{ m: 0 }} value="v1">
V1
</MenuItem>
</Link>
<MenuItem sx={{ m: 0 }} value="v2">
V2
</MenuItem>
</Select>
</ThemeProvider>
</Box>
<Box
onClick={() => plausible('open-search')}
id="docsearch"
sx={{
display: 'grid',
width: isDesktop ? '400px' : !isTablet ? '250px' : undefined,
}}
/>
<Box
sx={{
display: 'flex',
alignItems: 'center',
gap: '0.25rem',
}}
>
<Tooltip arrow title="Github">
<a
href="https://github.com/KevinVandy/material-react-table"
rel="noopener"
target="_blank"
>
<IconButton aria-label="Github" size="small">
<GitHubIcon />
</IconButton>
</a>
</Tooltip>
<Tooltip arrow title="Discord">
<a
href="https://discord.gg/5wqyRx6fnm"
rel="noopener"
target="_blank"
>
<IconButton aria-label="Discord" size="small">
<img
alt="Discord"
height={20}
style={{
padding: '-3px',
borderRadius: '50%',
}}
src="/Discord-Logo-White.svg"
/>
</IconButton>
</a>
</Tooltip>
<Tooltip arrow title="Toggle Light/Dark Mode">
<IconButton
aria-label="Toggle Light/Dark Mode"
onClick={() => {
setIsLightTheme(!isLightTheme);
plausible(
`toggle-theme-${isLightTheme ? 'dark' : 'light'}-mode`,
);
}}
size="small"
>
{isLightTheme ? <LightModeIcon /> : <DarkModeIcon />}
</IconButton>
</Tooltip>
</Box>
</Toolbar>
</AppBar>
</>
);
};