unpkg/modules/client/main/Layout.js

136 lines
3.5 KiB
JavaScript
Raw Normal View History

require("./Layout.css");
2018-04-04 05:16:09 +00:00
const React = require("react");
const PropTypes = require("prop-types");
const { Switch, Route, Link, withRouter } = require("react-router-dom");
const { Motion, spring } = require("react-motion");
2018-04-04 05:16:09 +00:00
const WindowSize = require("./WindowSize");
const About = require("./About");
const Stats = require("./Stats");
const Home = require("./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
2018-02-18 02:00:56 +00:00
};
2016-07-20 19:26:15 +00:00
2016-05-20 18:58:58 +00:00
state = {
underlineLeft: 0,
underlineWidth: 0,
useSpring: false,
stats: null
2018-02-18 02:00:56 +00:00
};
2016-05-20 18:58:58 +00:00
adjustUnderline = (useSpring = false) => {
2018-02-18 02:00:56 +00:00
let itemIndex;
2016-05-20 18:58:58 +00:00
switch (this.props.location.pathname) {
2017-11-25 21:25:01 +00:00
case "/stats":
2018-02-18 02:00:56 +00:00
itemIndex = 1;
break;
2017-11-25 21:25:01 +00:00
case "/about":
2018-02-18 02:00:56 +00:00
itemIndex = 2;
break;
2017-11-25 21:25:01 +00:00
case "/":
2016-05-20 18:58:58 +00:00
default:
2018-02-18 02:00:56 +00:00
itemIndex = 0;
2016-05-20 18:58:58 +00:00
}
2018-02-18 02:00:56 +00:00
const itemNodes = this.listNode.querySelectorAll("li");
const currentNode = itemNodes[itemIndex];
2016-05-20 18:58:58 +00:00
this.setState({
underlineLeft: currentNode.offsetLeft,
underlineWidth: currentNode.offsetWidth,
useSpring
2018-02-18 02:00:56 +00:00
});
};
2016-05-20 18:58:58 +00:00
componentDidMount() {
2018-02-18 02:00:56 +00:00
this.adjustUnderline();
2017-11-25 21:25:01 +00:00
fetch("/_stats?period=last-month")
.then(res => res.json())
2018-02-18 02:00:56 +00:00
.then(stats => this.setState({ stats }));
if (window.localStorage) {
2018-02-18 02:00:56 +00:00
const savedStats = window.localStorage.savedStats;
2018-02-18 02:00:56 +00:00
if (savedStats) this.setState({ stats: JSON.parse(savedStats) });
window.onbeforeunload = () => {
2018-02-18 02:00:56 +00:00
localStorage.savedStats = JSON.stringify(this.state.stats);
};
}
}
2016-05-20 18:58:58 +00:00
componentDidUpdate(prevProps) {
2018-02-18 02:00:56 +00:00
if (prevProps.location.pathname !== this.props.location.pathname)
this.adjustUnderline(true);
2016-05-20 18:58:58 +00:00
}
render() {
2018-02-18 02:00:56 +00:00
const { underlineLeft, underlineWidth, useSpring } = this.state;
2016-05-20 18:58:58 +00:00
const style = {
2018-02-18 02:00:56 +00:00
left: useSpring
? spring(underlineLeft, { stiffness: 220 })
: underlineLeft,
2016-05-20 18:58:58 +00:00
width: useSpring ? spring(underlineWidth) : underlineWidth
2018-02-18 02:00:56 +00:00
};
2016-05-20 18:58:58 +00:00
return (
2018-04-04 05:16:09 +00:00
<div className="layout">
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">
2018-02-18 02:00:56 +00:00
<ol
2018-04-04 05:16:09 +00:00
className="layout-navList"
2018-02-18 02:00:56 +00:00
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
2018-04-04 05:16:09 +00:00
className="layout-navUnderline"
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>
2018-02-18 02:00:56 +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>
2018-02-18 02:00:56 +00:00
);
2016-05-20 18:58:58 +00:00
}
}
module.exports = withRouter(Layout);