Skip to content

Commit ccaab88

Browse files
rafaelrinaldiskipjack
authored andcommitted
feat(sidebar): fixed sidebar on guides page
1 parent 0497e28 commit ccaab88

File tree

3 files changed

+61
-31
lines changed

3 files changed

+61
-31
lines changed

components/sidebar-item/sidebar-item-style.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
cursor: pointer;
2222
color: getColor(dusty-grey);
2323
transition: color 250ms;
24+
margin-left: 1.5rem;
2425

2526
&:hover {
2627
color: getColor(mine-shaft);
@@ -76,4 +77,4 @@
7677
display: none;
7778
}
7879
}
79-
}
80+
}

components/sidebar/sidebar-style.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@import 'mixins';
33

44
.sidebar {
5-
position: relative;
5+
position: absolute;
66
display: none;
77
flex: 1;
88
min-height: 100%;

components/sidebar/sidebar.jsx

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,60 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22
import SidebarItem from '../sidebar-item/sidebar-item';
33

4-
export default props => {
5-
let { sectionName, pages, currentPage } = props;
6-
7-
return (
8-
<nav className="sidebar">
9-
<div className="sidebar__inner">
10-
<h3 className="sidebar-item__version">Version 2.2</h3>
11-
<SidebarItem
12-
url={ `/${sectionName}` }
13-
title="Introduction"
14-
currentPage= { currentPage }
15-
/>
16-
{
17-
pages.map(({ url, title, anchors }, i) =>
18-
<SidebarItem
19-
key={ `sidebar-item-${i}` }
20-
index={i}
21-
url={ `/${url}` }
22-
title={ title }
23-
anchors={ anchors }
24-
currentPage= { currentPage }
25-
/>
26-
)
27-
}
28-
</div>
29-
</nav>
30-
);
31-
};
4+
export default class Sidebar extends Component {
5+
constructor(props) {
6+
super(props);
7+
this.state = {shouldFixSidebar: false};
8+
}
9+
10+
componentDidMount() {
11+
document.addEventListener('scroll', this.onScroll.bind(this));
12+
}
13+
14+
componentWillUnmount() {
15+
document.removeEventListener('scroll', this.onScroll.bind(this));
16+
}
17+
18+
onScroll() {
19+
let { scrollY } = window;
20+
let offsetY = document.querySelector('header').offsetHeight || 55;
21+
let shouldFixSidebar = scrollY >= offsetY;
22+
23+
this.setState({shouldFixSidebar});
24+
}
25+
26+
render() {
27+
let { sectionName, pages, currentPage } = this.props;
28+
let { shouldFixSidebar } = this.state;
29+
30+
return (
31+
<nav className="sidebar" style={ shouldFixSidebar ? {
32+
position: 'fixed',
33+
top: 0
34+
} : null }>
35+
36+
<div className="sidebar__inner">
37+
<h3 className="sidebar-item__version">Version 2.2</h3>
38+
<SidebarItem
39+
url={ `/${sectionName}` }
40+
title="Introduction"
41+
currentPage= { currentPage }
42+
/>
43+
{
44+
pages.map(({ url, title, anchors }, i) =>
45+
<SidebarItem
46+
key={ `sidebar-item-${i}` }
47+
index={i}
48+
url={ `/${url}` }
49+
title={ title }
50+
anchors={ anchors }
51+
currentPage= { currentPage }
52+
/>
53+
)
54+
}
55+
</div>
56+
57+
</nav>
58+
);
59+
}
60+
}

0 commit comments

Comments
 (0)