-
-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathresponsive-article-menu.tsx
136 lines (116 loc) · 3.07 KB
/
responsive-article-menu.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
import React, { ReactElement, useCallback, useEffect, useRef } from "react";
import { useDispatch } from "react-redux";
import styled from "styled-components";
import { Icon } from "@/components/sprites";
import { useObservable } from "@/state";
import { toggleAside, toggleTOC } from "@/state/common";
import { IsDesktop, IsSmallDesktop, IsTablet, THEME_COLORS } from "@/style";
// Icons
import NewspaperIconSvg from "@/images/icons/newspaper.svg";
import RectangleListIconSvg from "@/images/icons/rectangle-list.svg";
import { IconContainer } from "../misc";
export function ResponsiveArticleMenu(): ReactElement {
const dispatch = useDispatch();
const responsiveMenuRef = useRef<HTMLDivElement>(null);
const hasScrolled$ = useObservable((state) => {
return state.common.yScrollPosition > 20;
});
const handleToggleTOC = useCallback(() => {
dispatch(toggleTOC());
}, []);
const handleToggleAside = useCallback(() => {
dispatch(toggleAside());
}, []);
useEffect(() => {
const subscription = hasScrolled$.subscribe((hasScrolled) => {
responsiveMenuRef.current?.classList.toggle("scrolled", hasScrolled);
});
return () => {
subscription.unsubscribe();
};
}, [hasScrolled$]);
return (
<ResponsiveMenuWrapper>
<ResponsiveMenu ref={responsiveMenuRef}>
<Button onClick={handleToggleTOC} className="toc-toggle">
<IconContainer $size={14}>
<Icon {...RectangleListIconSvg} />
</IconContainer>
Table of contents
</Button>
<Button onClick={handleToggleAside} className="aside-toggle">
<IconContainer $size={14}>
<Icon {...NewspaperIconSvg} />
</IconContainer>
About this article
</Button>
</ResponsiveMenu>
</ResponsiveMenuWrapper>
);
}
const ResponsiveMenuWrapper = styled.div`
position: absolute;
left: 0;
right: 0;
`;
const ResponsiveMenu = styled.div`
position: fixed;
top: 71px;
right: 16px;
left: 16px;
z-index: 3;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
box-sizing: border-box;
width: auto;
height: 60px;
backdrop-filter: blur(2px);
background-image: linear-gradient(
180deg,
${THEME_COLORS.backgroundMenu} 30%,
#0a072100 100%
);
transition: all 100ms linear 0s;
@media only screen and (min-width: 700px) {
right: unset;
left: unset;
width: 660px;
}
${IsDesktop(`
display: none;
`)}
${IsSmallDesktop(`
> .toc-toggle {
display: none;
}
`)}
${IsTablet(`
> .toc-toggle {
display: flex;
}
`)}
`;
const Button = styled.button`
display: flex;
flex-direction: row;
align-items: center;
gap: 8px;
font-size: 0.875rem;
color: ${THEME_COLORS.link};
transition: color 0.2s ease-in-out;
${IconContainer} > svg {
fill: ${THEME_COLORS.link};
transition: fill 0.2s ease-in-out;
}
&.aside-toggle {
margin-left: auto;
}
&:hover {
color: ${THEME_COLORS.linkHover};
${IconContainer} > svg {
fill: ${THEME_COLORS.linkHover};
}
}
`;