|
1 | | -import React from 'react'; |
| 1 | +import React, { Component } from 'react'; |
2 | 2 | import SidebarItem from '../sidebar-item/sidebar-item'; |
3 | 3 |
|
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) => |
| 4 | +export default class Sidebar extends Component { |
| 5 | + constructor(props) { |
| 6 | + super(props); |
| 7 | + |
| 8 | + this.state = { |
| 9 | + fixed: false, |
| 10 | + top: 0 |
| 11 | + }; |
| 12 | + } |
| 13 | + |
| 14 | + componentDidMount() { |
| 15 | + document.addEventListener('scroll', this._recalculate.bind(this)); |
| 16 | + } |
| 17 | + |
| 18 | + componentWillUnmount() { |
| 19 | + document.removeEventListener('scroll', this._recalculate.bind(this)); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Re-calculate fixed state and position |
| 24 | + * |
| 25 | + */ |
| 26 | + _recalculate() { |
| 27 | + let { scrollY, innerHeight } = window; |
| 28 | + let { scrollHeight } = document.body; |
| 29 | + let { offsetHeight } = this._container; |
| 30 | + let distToBottom = scrollHeight - scrollY - innerHeight; |
| 31 | + let headerHeight = document.querySelector('header').offsetHeight; |
| 32 | + let footerHeight = document.querySelector('footer').offsetHeight; |
| 33 | + let allowedHeight = distToBottom < footerHeight ? innerHeight - (footerHeight - distToBottom) : offsetHeight; |
| 34 | + let extraHeight = offsetHeight > allowedHeight ? offsetHeight - allowedHeight : 0; |
| 35 | + let fixed = scrollY >= headerHeight; |
| 36 | + |
| 37 | + this.setState({ |
| 38 | + fixed, |
| 39 | + top: scrollY - headerHeight - extraHeight |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + render() { |
| 44 | + let { sectionName, pages, currentPage } = this.props; |
| 45 | + let { fixed, top, allowedHeight } = this.state; |
| 46 | + let isGuides = sectionName === 'guides'; |
| 47 | + |
| 48 | + return ( |
| 49 | + <nav |
| 50 | + className="sidebar" |
| 51 | + ref={ ref => this._container = ref } |
| 52 | + style={ fixed ? { |
| 53 | + top: top |
| 54 | + } : null }> |
| 55 | + |
| 56 | + <div className="sidebar__inner"> |
| 57 | + <h3 className="sidebar-item__version">Version 2.2</h3> |
| 58 | + |
| 59 | + <SidebarItem |
| 60 | + url={ `/${sectionName}` } |
| 61 | + title="Introduction" |
| 62 | + currentPage= { currentPage } |
| 63 | + /> |
| 64 | + |
| 65 | + { isGuides ? ( |
18 | 66 | <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 | | -}; |
| 67 | + url={ `/get-started` } |
| 68 | + title="Get Started" /> |
| 69 | + ) : null } |
| 70 | + { |
| 71 | + pages.map(({ url, title, anchors }, i) => |
| 72 | + <SidebarItem |
| 73 | + key={ `sidebar-item-${i}` } |
| 74 | + index={i} |
| 75 | + url={ `/${url}` } |
| 76 | + title={ title } |
| 77 | + anchors={ anchors } |
| 78 | + currentPage= { currentPage } |
| 79 | + onToggle={ this._recalculate.bind(this) } /> |
| 80 | + ) |
| 81 | + } |
| 82 | + </div> |
| 83 | + |
| 84 | + </nav> |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
0 commit comments