unpkg/client/Layout.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-04-19 23:55:03 +00:00
import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
import { Motion, spring } from 'react-motion'
import { withRouter, Link } from 'react-router-dom'
import WindowSize from './WindowSize'
2016-05-20 18:58:58 +00:00
class Layout extends React.Component {
2016-07-20 19:26:15 +00:00
static propTypes = {
location: PropTypes.object,
children: PropTypes.node
}
2016-05-20 18:58:58 +00:00
state = {
underlineLeft: 0,
underlineWidth: 0,
useSpring: false
}
adjustUnderline = (useSpring = false) => {
let itemIndex
switch (this.props.location.pathname) {
case '/about':
itemIndex = 2
break
case '/stats':
itemIndex = 1
break
case '/':
default:
itemIndex = 0
}
2017-04-19 23:55:03 +00:00
const itemNodes = ReactDOM.findDOMNode(this).querySelectorAll('.underlist > li')
2016-05-20 18:58:58 +00:00
const currentNode = itemNodes[itemIndex]
this.setState({
underlineLeft: currentNode.offsetLeft,
underlineWidth: currentNode.offsetWidth,
useSpring
})
}
componentDidMount() {
2016-05-20 18:58:58 +00:00
this.adjustUnderline()
}
2016-05-20 18:58:58 +00:00
componentDidUpdate(prevProps) {
2016-05-20 18:58:58 +00:00
if (prevProps.location.pathname !== this.props.location.pathname)
this.adjustUnderline(true)
}
render() {
2016-05-20 18:58:58 +00:00
const { underlineLeft, underlineWidth, useSpring } = this.state
const style = {
left: useSpring ? spring(underlineLeft, { stiffness: 220 }) : underlineLeft,
width: useSpring ? spring(underlineWidth) : underlineWidth
}
return (
<div>
<WindowSize onChange={this.adjustUnderline}/>
2017-06-09 20:49:27 +00:00
<div className="wrapper">
<header>
<h1>unpkg</h1>
<nav>
<ol className="underlist">
<li><Link to="/">Home</Link></li>
<li><Link to="/stats">Stats</Link></li>
<li><Link to="/about">About</Link></li>
</ol>
<Motion defaultStyle={{ left: underlineLeft, width: underlineWidth }} style={style}>
{s => (
<div
className="underlist-underline"
style={{
WebkitTransform: `translate3d(${s.left}px,0,0)`,
transform: `translate3d(${s.left}px,0,0)`,
width: s.width
}}
/>
)}
</Motion>
</nav>
</header>
</div>
2016-05-20 18:58:58 +00:00
{this.props.children}
2017-06-09 20:49:27 +00:00
<div className="wrapper">
<footer>
<p>&copy; 2016-{(new Date).getFullYear()} Michael Jackson</p>
</footer>
</div>
2016-05-20 18:58:58 +00:00
</div>
)
}
}
export default withRouter(Layout)