-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNavigation.tsx
More file actions
209 lines (189 loc) · 5.71 KB
/
Navigation.tsx
File metadata and controls
209 lines (189 loc) · 5.71 KB
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
import * as React from 'react';
import { useEffect } from 'react';
import { FaArrowLeft, FaArrowRight, FaBars } from 'react-icons/fa';
import { useNavigate, useLocation } from 'react-router-dom';
import styled from 'styled-components';
import { ButtonBar } from './Button';
import { useCurrentSubject } from '../helpers/useCurrentSubject';
import { useSettings } from '../helpers/AppSettings';
import { SideBar } from './SideBar';
import ResourceContextMenu from './ResourceContextMenu';
import { isRunningInTauri } from '../helpers/tauri';
import { shortcuts } from './HotKeyWrapper';
import { MenuBarDropdownTrigger } from './ResourceContextMenu/MenuBarDropdownTrigger';
import { NavBarSpacer } from './NavBarSpacer';
import { Searchbar } from './Searchbar';
import { useMediaQuery } from '../hooks/useMediaQuery';
interface NavWrapperProps {
children: React.ReactNode;
}
/** Wraps the entire app and adds a navbar at the bottom or the top */
export function NavWrapper({ children }: NavWrapperProps): JSX.Element {
const { navbarTop, navbarFloating } = useSettings();
const contentRef = React.useRef<HTMLDivElement>(null);
const location = useLocation();
useEffect(() => {
contentRef?.current?.scrollTo(0, 0);
}, [location]);
return (
<>
{navbarTop && <NavBar />}
<SideBarWrapper>
<SideBar />
<Content
ref={contentRef}
navbarTop={navbarTop}
navbarFloating={navbarFloating}
>
<NavBarSpacer position='top' />
{children}
</Content>
</SideBarWrapper>
{!navbarTop && <NavBar />}
</>
);
}
interface ContentProps {
navbarTop: boolean;
navbarFloating: boolean;
}
const Content = styled.div<ContentProps>`
display: block;
flex: 1;
overflow-y: auto;
`;
/** Persistently shown navigation bar */
function NavBar(): JSX.Element {
const [subject] = useCurrentSubject();
const navigate = useNavigate();
const { navbarTop, navbarFloating, sideBarLocked, setSideBarLocked } =
useSettings();
const [showButtons, setShowButtons] = React.useState<boolean>(true);
const machesStandalone = useMediaQuery(
'(display-mode: standalone) or (display-mode: fullscreen)',
);
const isInStandaloneMode = React.useMemo<boolean>(
() =>
machesStandalone ||
//@ts-ignore
window.navigator.standalone ||
document.referrer.includes('android-app://') ||
isRunningInTauri(),
[machesStandalone],
);
/** Hide buttons if the input element is quite small */
function maybeHideButtons(event: React.FocusEvent<HTMLInputElement>) {
if (event.target.getBoundingClientRect().width < 280) {
setShowButtons(false);
}
}
const ConditionalNavbar = navbarFloating ? NavBarFloating : NavBarFixed;
return (
<ConditionalNavbar
top={navbarTop}
aria-label='search'
floating={navbarFloating}
>
{showButtons && (
<React.Fragment>
<ButtonBar
leftPadding
type='button'
onClick={() => setSideBarLocked(!sideBarLocked)}
title={`Show / hide sidebar (${shortcuts.sidebarToggle})`}
data-test='sidebar-toggle'
>
<FaBars />
</ButtonBar>
{isInStandaloneMode && (
<>
<ButtonBar
type='button'
title='Go back'
onClick={() => navigate(-1)}
>
<FaArrowLeft />
</ButtonBar>{' '}
<ButtonBar
type='button'
title='Go forward'
onClick={() => navigate(1)}
>
<FaArrowRight />
</ButtonBar>
</>
)}
</React.Fragment>
)}
<Searchbar
subject={subject}
onFocus={maybeHideButtons}
onBlur={() => setShowButtons(true)}
/>
{showButtons && subject && (
<ResourceContextMenu
isMainMenu
subject={subject}
trigger={MenuBarDropdownTrigger}
/>
)}
</ConditionalNavbar>
);
}
interface NavBarStyledProps {
floating: boolean;
top: boolean;
}
/** Don't use this directly - use NavBarFloating or NavBarFixed */
const NavBarBase = styled.div<NavBarStyledProps>`
/* transition: all 0.2s; */
position: fixed;
z-index: ${p => p.theme.zIndex.sidebar};
height: 2.5rem;
display: flex;
border: solid 1px ${props => props.theme.colors.bg2};
background-color: ${props => props.theme.colors.bg};
`;
/** Width of the floating navbar in rem */
const NavBarFloating = styled(NavBarBase)`
box-shadow: ${props => props.theme.boxShadow};
box-sizing: border-box;
border-radius: 999px;
overflow: hidden;
max-width: calc(100% - 2rem);
width: ${props => props.theme.containerWidth + 1}rem;
margin: auto;
/* Center fixed item */
left: 50%;
margin-left: -${props => (props.theme.containerWidth + 1) / 2}rem;
margin-right: -${props => (props.theme.containerWidth + 1) / 2}rem;
top: ${props => (props.top ? '2rem' : 'auto')};
bottom: ${props => (props.top ? 'auto' : '1rem')};
@media (max-width: ${props => props.theme.containerWidth}rem) {
max-width: calc(100% - 1rem);
left: auto;
right: auto;
margin-left: 0.5rem;
bottom: 0.5rem;
}
`;
const NavBarFixed = styled(NavBarBase)`
top: ${props => (props.top ? '0' : 'auto')};
bottom: ${props => (props.top ? 'auto' : '0')};
left: 0;
right: 0;
border-width: 0;
border-bottom: ${props =>
props.top ? 'solid 1px ' + props.theme.colors.bg2 : 'none'};
border-top: ${props =>
!props.top ? 'solid 1px ' + props.theme.colors.bg2 : 'none'};
`;
const SideBarWrapper = styled('div')`
display: flex;
height: 100vh;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
`;