Skip to content

Commit 51510a9

Browse files
committed
Updating sticky Sidebar behavior
Taking more conditions into account such as space available and overflow Still a few kinks to work out but I think this is close to merge-able
1 parent ccfc86d commit 51510a9

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

components/sidebar/sidebar-style.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
.sidebar {
55
position: absolute;
66
display: none;
7-
flex: 1;
8-
min-height: 100%;
7+
width: 100%;
8+
max-height: 100%;
9+
overflow: auto;
910

1011
@include break {
1112
display:block;
@@ -14,6 +15,5 @@
1415

1516
.sidebar__inner {
1617
padding: 1.5em;
17-
overflow-y: auto;
18-
overflow-x: hidden;
18+
overflow: hidden;
1919
}

components/sidebar/sidebar.jsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,49 @@ import SidebarItem from '../sidebar-item/sidebar-item';
44
export default class Sidebar extends Component {
55
constructor(props) {
66
super(props);
7-
this.state = {shouldFixSidebar: false};
7+
8+
this.state = {
9+
fixed: false,
10+
top: 0
11+
};
812
}
913

1014
componentDidMount() {
11-
document.addEventListener('scroll', this.onScroll.bind(this));
15+
document.addEventListener('scroll', this._recalculate.bind(this));
1216
}
1317

1418
componentWillUnmount() {
15-
document.removeEventListener('scroll', this.onScroll.bind(this));
19+
document.removeEventListener('scroll', this._recalculate.bind(this));
1620
}
1721

18-
onScroll() {
19-
let { scrollY } = window;
20-
let offsetY = document.querySelector('header').offsetHeight || 55;
21-
let shouldFixSidebar = scrollY >= offsetY;
22+
_recalculate() {
23+
let { scrollY, innerHeight } = window;
24+
let { scrollHeight } = document.body;
25+
let { offsetHeight } = this._container;
26+
let distToBottom = scrollHeight - scrollY - innerHeight;
27+
let headerHeight = document.querySelector('header').offsetHeight;
28+
let footerHeight = document.querySelector('footer').offsetHeight;
29+
let allowedHeight = distToBottom < footerHeight ? innerHeight - (footerHeight - distToBottom) : offsetHeight;
30+
let extraHeight = offsetHeight > allowedHeight ? offsetHeight - allowedHeight : 0;
31+
let fixed = scrollY >= headerHeight;
2232

23-
this.setState({shouldFixSidebar});
33+
this.setState({
34+
fixed,
35+
top: scrollY - headerHeight - extraHeight
36+
});
2437
}
2538

2639
render() {
2740
let { sectionName, pages, currentPage } = this.props;
28-
let { shouldFixSidebar } = this.state;
41+
let { fixed, top, allowedHeight } = this.state;
2942

3043
return (
31-
<nav className="sidebar" style={ shouldFixSidebar ? {
32-
position: 'fixed',
33-
top: 0
34-
} : null }>
44+
<nav
45+
className="sidebar"
46+
ref={ ref => this._container = ref }
47+
style={ fixed ? {
48+
top: top
49+
} : null }>
3550

3651
<div className="sidebar__inner">
3752
<h3 className="sidebar-item__version">Version 2.2</h3>

0 commit comments

Comments
 (0)