Skip to content

Commit eece98a

Browse files
committed
Make the top "sidebar" disappear when scrolling down on mobile
1 parent 9fe7380 commit eece98a

File tree

6 files changed

+71
-11
lines changed

6 files changed

+71
-11
lines changed

src/assets/scss/_progress.scss

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
@import "../../assets/scss/variables";
2+
@import "../../assets/scss/mixins";
3+
14
#gatsby-plugin-page-progress {
25
height: 2px;
36
background-color: gray;
47
transition: none;
8+
z-index: 2
9+
}
10+
11+
@include breakpoint-sm {
12+
#gatsby-plugin-page-progress {
13+
z-index: unset;
14+
}
515
}

src/assets/scss/_variables.scss

+2
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ $page-sm-padding-left: 25%;
4242
$page-md-padding-top: 30px;
4343
$page-md-padding-left: 35%;
4444

45+
$topbar-height: 44px;
46+
4547
$footer-padding: 2.5rem;

src/components/Layout/index.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class Layout extends React.Component {
2121
<Helmet>
2222
<title>{subtitle}Eyal Roth</title>
2323
</Helmet>
24+
<Sidebar globalLinkId={this.props.globalLinkId} />
2425
<div className="content-wrap">
25-
<Sidebar globalLinkId={this.props.globalLinkId} />
2626
{children}
2727
</div>
2828
<Footer />

src/components/Layout/style.scss

+7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ html {
1212

1313
.content-wrap {
1414
lost-center: $layout-width;
15+
padding-top: $topbar-height;
1516
padding-bottom: $footer-padding;
1617

1718
max-width: $layout-breakpoint-lg;
1819
margin: 0 auto;
1920
}
2021

22+
@include breakpoint-sm {
23+
.content-wrap {
24+
padding-top: unset;
25+
}
26+
}
27+
2128
@include breakpoint-xlg {
2229
.content-wrap {
2330
max-width: $layout-breakpoint-xlg;

src/components/Sidebar/index.jsx

+31-9
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ class Sidebar extends React.Component {
1818

1919
constructor(props) {
2020
super(props)
21+
2122
if (!globalState.sidebar.mode) {
2223
globalState.sidebar.mode = SidebarMode.Main
2324
}
24-
this.state = { mode: globalState.sidebar.mode }
25+
this.state = { isEnabled: true, mode: globalState.sidebar.mode }
26+
this.lastScrollY = null
27+
28+
this.handleScroll = this.handleScroll.bind(this)
2529
}
2630

2731
changeMode(newMode) {
@@ -68,17 +72,35 @@ class Sidebar extends React.Component {
6872
compose(profileImg, authorTitle, menu, contact, menuButton, contactButton) {
6973

7074
return (
71-
<div id="sidebar" className="sidebar">
72-
{menuButton}
73-
{profileImg}
74-
{authorTitle}
75-
{contact}
76-
{menu}
77-
{contactButton}
78-
</div>
75+
<Toggle isEnabled={this.state.isEnabled}>
76+
<div id="sidebar" className="sidebar">
77+
{menuButton}
78+
{profileImg}
79+
{authorTitle}
80+
{contact}
81+
{menu}
82+
{contactButton}
83+
</div>
84+
</Toggle>
7985
)
8086
}
8187

88+
componentDidMount() {
89+
window.addEventListener('scroll', this.handleScroll);
90+
}
91+
92+
componentWillUnmount() {
93+
window.removeEventListener('scroll', this.handleScroll);
94+
}
95+
96+
handleScroll() {
97+
const isScrollUp = this.lastScrollY && window.scrollY < this.lastScrollY
98+
this.lastScrollY = window.scrollY
99+
const isEnabled = isScrollUp || scrollY <= window.innerHeight
100+
if (isEnabled != this.state.isEnabled) {
101+
this.setState({isEnabled})
102+
}
103+
}
82104

83105
renderProfileImg(author) {
84106
return (

src/components/Sidebar/style.scss

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ $transition-timing: 0.2s;
99

1010
.sidebar {
1111
width: 100%;
12-
position: relative;
1312
border-bottom: 1px solid #E7E7E7;
1413
display: inline-block;
14+
15+
position: fixed;
16+
background-color: $color-white;
17+
transition: transform 0.5s ease-out;
18+
z-index: 1;
19+
height: $topbar-height;
20+
21+
@include disabled {
22+
transform: translateY(-$topbar-height - 10);
23+
}
1524

1625
&__author {
1726
&-img {
@@ -180,6 +189,16 @@ $transition-timing: 0.2s;
180189
display: unset;
181190
text-align: center;
182191

192+
position: relative;
193+
background-color: unset;
194+
transition: unset;
195+
z-index: unset;
196+
height: unset;
197+
198+
@include disabled {
199+
transform: unset;
200+
}
201+
183202
&__author {
184203
&-img {
185204
display: block;

0 commit comments

Comments
 (0)