-
Notifications
You must be signed in to change notification settings - Fork 4
/
Header.tsx
130 lines (112 loc) · 3.55 KB
/
Header.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
import React, { useContext } from "react";
import { useRouter } from "next/router";
import styled from "styled-components";
import { opacify } from "polished";
import { AppContext } from "../lib/context";
import { useLanguage } from "../lib/hooks";
import { getPathForLang, getPurePath } from "../lib/utils";
import { Link, Img } from "../lib/components";
import config from "../lib/config";
import constants from "./constants";
import Hamburger from "./Hamburger";
import Language from "./Language";
const Nav = styled.div<{ visible: boolean; color: string }>`
position: relative;
width: 100%;
height: ${constants.headerHeight}px;
display: flex;
justify-content: space-between;
// Set background on mobile
transition: 0.3s;
background: transparent;
@media (max-width: ${constants.mobile}px) {
background: ${(p) => (p.visible ? opacify(1, p.color) : "transparent")};
}
`;
const PCOnly = styled.div`
display: block;
@media (max-width: ${constants.mobile}px) {
display: none;
}
`;
const MobileOnly = styled.div`
display: none;
@media (max-width: ${constants.mobile}px) {
display: block;
}
`;
const Logo = styled.div<{ visible: boolean }>`
height: 100%;
padding: 4px 0;
position: absolute;
left: 51%;
transform: translateX(-50%);
transition: opacity 0.3s;
opacity: 0;
@media (max-width: ${constants.mobile}px) {
opacity: ${(p) => (p.visible ? "1" : "0")};
}
`;
const Left = styled.div``;
const Right = styled.div`
display: flex;
line-height: ${constants.headerHeight}px;
/* Margin to avoid scroll bar */
margin-right: 8px;
@media (max-width: ${constants.mobile}px) {
margin-right: 0;
}
.header-link a {
line-height: ${constants.headerHeight}px;
text-decoration: none;
color: ${constants.fg};
font-weight: bold;
margin: 0 8px;
}
`;
const Header = (): React.ReactElement => {
const { state, dispatch, frontmatter } = useContext(AppContext);
const toggleMenu = (): void =>
dispatch({ type: state.isMenuVisible ? "hideMenu" : "showMenu" });
// Get config for header links
const lang = useLanguage();
const headerItemsConfig = config.header[lang];
// Get current page is root or not
const router = useRouter();
const realPath = getPurePath(router.asPath, lang);
const isRoot = realPath === "/";
const isNavVisible = state.isHeaderVisible;
const isLogoVisible = state.isHeaderVisible || !isRoot;
return (
<Nav visible={isNavVisible} color={frontmatter.color}>
<Logo visible={isLogoVisible}>
<Link href={getPathForLang(lang, "/")}>
<Img
src="/static/images/logo_white.png"
alt="MDMT logo"
height="48"
/>
</Link>
</Logo>
<Left>
<MobileOnly>
<Hamburger
active={state.isMenuVisible}
onClick={toggleMenu}
/>
</MobileOnly>
</Left>
<Right>
{headerItemsConfig.map(([label, url]) => (
<PCOnly key={url}>
<span className="header-link">
<Link href={url}>{label}</Link>
</span>
</PCOnly>
))}
<Language />
</Right>
</Nav>
);
};
export default Header;