Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import NavItem from '../NavItem'; import ProgressIndicator from '../ProgressIndicator'; import { withScrolling } from '../../lib/ScrollContext'; import styles from './Navbar.scss'; class Navbar extends PureComponent { static propTypes = { scrollTop: PropTypes.number.isRequired, }; constructor(props) { super(props); this.navbar = React.createRef(); this.navmenu = React.createRef(); this.state = { sticky: false, top: 0, indicatorLeft: 0, indicatorWidth: 0, indicatorVisible: false, links: [ { slug: 'about', icon: 'person' }, { slug: 'resume', icon: 'reorder' }, ], }; } componentDidMount() { const { links } = this.state; const navRef = this.navbar.current; const navMenuRef = this.navmenu.current; window.addEventListener('resize', this.handleResize); this.handleScroll(); this.setState({ top: parseInt(this.getComputedStyle(navRef, 'top'), 10), indicatorWidth: (navMenuRef && navMenuRef.offsetWidth) / links.length, }); } componentWillUnmount() { window.removeEventListener('resize', this.handleResize); // clearInterval(this.timer); } getComputedStyle = (el, prop) => { const { getComputedStyle } = window; // In one fell swoop return ( // If we have getComputedStyle getComputedStyle // Query it // TODO: From CSS-Query notes, we might need (node, null) for FF ? getComputedStyle(el) // Otherwise, we are in IE and use currentStyle : el.currentStyle )[ // Switch to camelCase for CSSOM // DEV: Grabbed from jQuery // https://github.com/jquery/jquery/blob/1.9-stable/src/css.js#L191-L194 // https://github.com/jquery/jquery/blob/1.9-stable/src/core.js#L593-L597 prop.replace(/-(\w)/gi, (word, letter) => { letter.toUpperCase(); }) ]; } handleScroll = () => { const { scrollTop } = this.props; const { sticky, top } = this.state; const clientRect = this.navbar.current.getBoundingClientRect(); const isOffScreen = scrollTop > top && clientRect.top < 0; if (scrollTop <= top && sticky) { this.setState({ sticky: false, }); } if (isOffScreen && !sticky) { this.setState({ sticky: true, }); } requestAnimationFrame(this.handleScroll); } handleResize = () => { const { links } = this.state; const navMenuRef = this.navmenu.current; this.setState({ indicatorWidth: (navMenuRef && navMenuRef.offsetWidth) / links.length, }); } handleMouseOver = (event) => { const { links } = this.state; const navMenuRef = this.navmenu.current; this.setState({ indicatorLeft: event.currentTarget.offsetLeft, indicatorWidth: (navMenuRef && navMenuRef.offsetWidth) / links.length, indicatorVisible: true, }); } handleMouseOut = () => { this.setState({ indicatorVisible: false, }); } render() { const { sticky, indicatorLeft, indicatorWidth, indicatorVisible, links, } = this.state; return ( <nav className={`${styles.navbar}${sticky ? ` ${styles['navbar--sticky']}` : ''}`} onMouseLeave={this.handleMouseOut} onBlur={this.handleMouseOut} ref={this.navbar} > <ProgressIndicator /> <ul className={`${styles.nav}${sticky ? ` ${styles['nav--sticky']}` : ''}`} ref={this.navmenu}> {links.map(link => ( <li className={styles.nav__item} onMouseOver={this.handleMouseOver} onFocus={this.handleMouseOver} > <NavItem activeClassName={styles['nav__link--active']} className={`${styles.nav__link}${sticky ? ` ${styles['nav__link--sticky']}` : ''}`} to={`/${link.slug}`} hoverClassName={styles['nav__link--hover']} > <i className={`material-icons ${styles.nav__icon} ${styles[`nav__icon--${link.icon}`]}`} /> <span className={styles.nav__label}>{link.slug.toUpperCase()}</span> </NavItem> </li> ))} <span className={styles.nav__indicator} style={{ width: indicatorWidth, left: indicatorLeft, height: indicatorVisible ? '3px' : 0, }} /> </ul> </nav> ); } } export default withScrolling(Navbar); |