@@ -4,34 +4,49 @@ import SidebarItem from '../sidebar-item/sidebar-item';
4
4
export default class Sidebar extends Component {
5
5
constructor ( props ) {
6
6
super ( props ) ;
7
- this . state = { shouldFixSidebar : false } ;
7
+
8
+ this . state = {
9
+ fixed : false ,
10
+ top : 0
11
+ } ;
8
12
}
9
13
10
14
componentDidMount ( ) {
11
- document . addEventListener ( 'scroll' , this . onScroll . bind ( this ) ) ;
15
+ document . addEventListener ( 'scroll' , this . _recalculate . bind ( this ) ) ;
12
16
}
13
17
14
18
componentWillUnmount ( ) {
15
- document . removeEventListener ( 'scroll' , this . onScroll . bind ( this ) ) ;
19
+ document . removeEventListener ( 'scroll' , this . _recalculate . bind ( this ) ) ;
16
20
}
17
21
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 ;
22
32
23
- this . setState ( { shouldFixSidebar} ) ;
33
+ this . setState ( {
34
+ fixed,
35
+ top : scrollY - headerHeight - extraHeight
36
+ } ) ;
24
37
}
25
38
26
39
render ( ) {
27
40
let { sectionName, pages, currentPage } = this . props ;
28
- let { shouldFixSidebar } = this . state ;
41
+ let { fixed , top , allowedHeight } = this . state ;
29
42
30
43
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 } >
35
50
36
51
< div className = "sidebar__inner" >
37
52
< h3 className = "sidebar-item__version" > Version 2.2</ h3 >
0 commit comments