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' class Layout extends React.Component { static propTypes = { location: PropTypes.object, children: PropTypes.node } 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 } const itemNodes = ReactDOM.findDOMNode(this).querySelectorAll('.underlist > li') const currentNode = itemNodes[itemIndex] this.setState({ underlineLeft: currentNode.offsetLeft, underlineWidth: currentNode.offsetWidth, useSpring }) } componentDidMount() { this.adjustUnderline() } componentDidUpdate(prevProps) { if (prevProps.location.pathname !== this.props.location.pathname) this.adjustUnderline(true) } render() { const { underlineLeft, underlineWidth, useSpring } = this.state const style = { left: useSpring ? spring(underlineLeft, { stiffness: 220 }) : underlineLeft, width: useSpring ? spring(underlineWidth) : underlineWidth } return (

unpkg

{this.props.children}
) } } export default withRouter(Layout)