-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
Copy pathNavigationBar.tsx
70 lines (64 loc) · 1.96 KB
/
NavigationBar.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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import * as React from 'react';
import {
FileTabs,
useSandpack,
useSandpackNavigation,
} from '@codesandbox/sandpack-react';
import {OpenInCodeSandboxButton} from './OpenInCodeSandboxButton';
import {ResetButton} from './ResetButton';
import {DownloadButton} from './DownloadButton';
import {FilesDropdown} from './FilesDropdown';
export function NavigationBar({
showDownload,
onReset,
}: {
showDownload: boolean;
onReset: () => void;
}) {
const {sandpack} = useSandpack();
const [dropdownActive, setDropdownActive] = React.useState(false);
const {openPaths, clients} = sandpack;
const clientId = Object.keys(clients)[0];
const {refresh} = useSandpackNavigation(clientId);
const resizeHandler = React.useCallback(() => {
const width = window.innerWidth || document.documentElement.clientWidth;
if (!dropdownActive && width < 640) {
setDropdownActive(true);
}
if (dropdownActive && width >= 640) {
setDropdownActive(false);
}
}, [dropdownActive]);
React.useEffect(() => {
if (openPaths.length > 1) {
resizeHandler();
window.addEventListener('resize', resizeHandler);
return () => {
window.removeEventListener('resize', resizeHandler);
};
}
return;
}, [openPaths.length, resizeHandler]);
const handleReset = () => {
sandpack.resetAllFiles();
refresh();
onReset();
};
return (
<div className="bg-wash dark:bg-card-dark flex justify-between items-center relative z-10 border-b border-border dark:border-border-dark rounded-t-lg rounded-b-none">
<div className="px-4 lg:px-6">
{dropdownActive ? <FilesDropdown /> : <FileTabs />}
</div>
<div
className="px-3 flex items-center justify-end grow text-right"
translate="yes">
{showDownload && <DownloadButton />}
<ResetButton onReset={handleReset} />
<OpenInCodeSandboxButton />
</div>
</div>
);
}