@@ -4,34 +4,49 @@ import SidebarItem from '../sidebar-item/sidebar-item';
44export 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