-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathNavbar.tsx
183 lines (170 loc) · 4.95 KB
/
Navbar.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
/** @jsxImportSource @emotion/react */
import { DataCampLogo } from '@datacamp/waffles/brand';
import { Button } from '@datacamp/waffles/button';
import { Heading } from '@datacamp/waffles/heading';
import { mediaQuery } from '@datacamp/waffles/helpers';
import { DataCampBrand, MoonSolid, SunSolid } from '@datacamp/waffles/icon';
import { Input } from '@datacamp/waffles/input';
import { Paragraph } from '@datacamp/waffles/paragraph';
import {
darkThemeStyle,
lightThemeStyle,
theme as themeTokens,
} from '@datacamp/waffles/theme';
import { tokens } from '@datacamp/waffles/tokens';
import styled from '@emotion/styled';
import { useRouter } from 'next/router';
import { useContext, useState } from 'react';
import { FaGithub } from 'react-icons/fa';
import { ThemeContext } from '../pages/_app';
const Header = styled.header({
'&[data-theme="dark"]': {
...darkThemeStyle,
},
'&[data-theme="light"]': {
...lightThemeStyle,
},
display: 'flex',
flexDirection: 'column',
gap: tokens.spacing.medium,
margin: '0 auto',
maxWidth: 1600, // make sure the header doesn't get too wide, but still get's wider than the content
padding: `${tokens.spacing.large} ${tokens.spacing.medium}`,
[mediaQuery.aboveMedium]: {
alignItems: 'center',
flexDirection: 'unset',
gap: 0,
justifyContent: 'space-between',
padding: `${tokens.spacing.medium} 70px`,
},
});
const LogoWrapper = styled.div(`
display: flex;
align-items: center;
&:hover {
cursor: pointer;
}
`);
const RightContainer = styled.div({
alignItems: 'center',
display: 'flex',
flexDirection: 'column-reverse',
gap: tokens.spacing.medium,
[mediaQuery.aboveMedium]: {
flexDirection: 'unset',
justifyContent: 'space-between',
},
});
const ButtonContainer = styled.div({
display: 'flex',
flex: '1 1 100%',
flexGrow: 1,
gap: tokens.spacing.small,
width: '100%',
});
const VerticalDivider = styled.hr(`
border: 1px inset;
color: ${themeTokens.border.strong};
display: block;
height: 30px;
margin: 0 ${tokens.spacing.small};
width: 1px;
`);
const inputStyle = { flexGrow: 1, minWidth: '343px' };
const buttonStyle = {
height: tokens.sizing.medium,
};
export default function Navbar() {
const [searchInput, setSearchInput] = useState('');
const router = useRouter();
const { theme, toggleTheme } = useContext(ThemeContext);
function onSubmitSearch(e) {
e.preventDefault();
if (!searchInput) return;
router.push(`/search?q=${encodeURIComponent(searchInput)}`);
setSearchInput('');
}
const showSearch = router.pathname !== '/';
return (
<Header data-theme={theme}>
<nav>
<LogoWrapper>
<a href="/">
<Heading css={{ color: themeTokens.text.main, margin: 0 }}>
Rdocumentation
</Heading>
</a>
<div>
<VerticalDivider />
</div>
<a
css={{ display: 'flex', flexDirection: 'column' }}
href="https://www.datacamp.com"
>
<Paragraph
size="small"
style={{
color: themeTokens.text.secondary,
marginBottom: 0,
}}
variant="secondary"
>
powered by
</Paragraph>
<DataCampLogo css={{ flexShrink: 0, height: 18, width: 86 }} />
</a>
</LogoWrapper>
</nav>
<RightContainer>
{showSearch && (
<form onSubmit={onSubmitSearch} style={{ width: '100%' }}>
<Input
id="searchBarNav"
name="searchBarNav"
onChange={(event) => setSearchInput(event.target.value)}
placeholder="Search all packages and functions"
size="medium"
style={inputStyle}
type="search"
value={searchInput}
/>
</form>
)}
<ButtonContainer>
<Button
aria-label="toggle dark mode"
className="p-1"
css={{ ...buttonStyle, padding: '12px' }}
onClick={toggleTheme}
type="button"
>
{theme === 'light' ? <MoonSolid /> : <SunSolid />}
</Button>
<Button
aria-label="github repository"
as="a"
css={{ ...buttonStyle, padding: '12px' }}
href="https://github.com/datacamp/rdocumentation-2.0"
rel="noopener noreferrer"
target="_blank"
>
<FaGithub />
</Button>
<Button
as="a"
css={{
...buttonStyle,
flexGrow: 1,
padding: `${tokens.spacing.small} ${tokens.spacing.medium}`,
}}
href="https://www.datacamp.com/category/r"
iconLeft={<DataCampBrand />}
variant="primary"
>
Learn R Programming
</Button>
</ButtonContainer>
</RightContainer>
</Header>
);
}