unpkg/client/Layout.js

124 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-11-25 21:25:01 +00:00
import React from "react"
import PropTypes from "prop-types"
import { Motion, spring } from "react-motion"
import { Switch, Route, Link, withRouter } from "react-router-dom"
import WindowSize from "./WindowSize"
import About from "./About"
import Stats from "./Stats"
import Home from "./Home"
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,
stats: null
2016-05-20 18:58:58 +00:00
}
adjustUnderline = (useSpring = false) => {
let itemIndex
switch (this.props.location.pathname) {
2017-11-25 21:25:01 +00:00
case "/stats":
2016-05-20 18:58:58 +00:00
itemIndex = 1
break
2017-11-25 21:25:01 +00:00
case "/about":
itemIndex = 2
break
2017-11-25 21:25:01 +00:00
case "/":
2016-05-20 18:58:58 +00:00
default:
itemIndex = 0
}
2017-11-25 21:25:01 +00:00
const itemNodes = this.listNode.querySelectorAll("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()
2017-11-25 21:25:01 +00:00
fetch("/_stats?period=last-month")
.then(res => res.json())
.then(stats => this.setState({ stats }))
if (window.localStorage) {
const savedStats = window.localStorage.savedStats
2017-11-08 16:57:15 +00:00
if (savedStats) this.setState({ stats: JSON.parse(savedStats) })
window.onbeforeunload = () => {
localStorage.savedStats = JSON.stringify(this.state.stats)
}
}
}
2016-05-20 18:58:58 +00:00
componentDidUpdate(prevProps) {
2017-11-25 21:25:01 +00:00
if (prevProps.location.pathname !== this.props.location.pathname) this.adjustUnderline(true)
2016-05-20 18:58:58 +00:00
}
render() {
2016-05-20 18:58:58 +00:00
const { underlineLeft, underlineWidth, useSpring } = this.state
const style = {
2017-11-25 21:25:01 +00:00
left: useSpring ? spring(underlineLeft, { stiffness: 220 }) : underlineLeft,
2016-05-20 18:58:58 +00:00
width: useSpring ? spring(underlineWidth) : underlineWidth
}
return (
<div>
2017-11-08 16:57:15 +00:00
<WindowSize onChange={this.adjustUnderline} />
2017-06-09 20:49:27 +00:00
<div className="wrapper">
<header>
<h1 className="layout-title">unpkg</h1>
<nav className="layout-nav">
2017-11-25 21:25:01 +00:00
<ol className="layout-nav-list" ref={node => (this.listNode = node)}>
2017-11-08 16:57:15 +00:00
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/stats">Stats</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
2017-06-09 20:49:27 +00:00
</ol>
<Motion
defaultStyle={{ left: underlineLeft, width: underlineWidth }}
style={style}
children={style => (
2017-06-09 20:49:27 +00:00
<div
className="layout-nav-underline"
2017-06-09 20:49:27 +00:00
style={{
WebkitTransform: `translate3d(${style.left}px,0,0)`,
transform: `translate3d(${style.left}px,0,0)`,
width: style.width
2017-06-09 20:49:27 +00:00
}}
/>
)}
/>
2017-06-09 20:49:27 +00:00
</nav>
</header>
</div>
<Switch>
2017-11-25 21:25:01 +00:00
<Route path="/stats" render={() => <Stats data={this.state.stats} />} />
2017-11-08 16:57:15 +00:00
<Route path="/about" component={About} />
<Route path="/" component={Home} />
</Switch>
2016-05-20 18:58:58 +00:00
</div>
)
}
}
export default withRouter(Layout)