From 8cfed8d19f51458fc5351f091688e29e4c2c6cf9 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Fri, 29 Mar 2019 22:49:27 +0100 Subject: [PATCH] center current project in project nav * reverts ba1aac7640454195d9df1e736dd722042758dca4 --- src/components/molecules/ProjectNav.jsx | 133 ++++++++++++------------ src/templates/Project.jsx | 2 +- 2 files changed, 69 insertions(+), 66 deletions(-) diff --git a/src/components/molecules/ProjectNav.jsx b/src/components/molecules/ProjectNav.jsx index e41aa17..c9932ad 100644 --- a/src/components/molecules/ProjectNav.jsx +++ b/src/components/molecules/ProjectNav.jsx @@ -24,70 +24,75 @@ const query = graphql` } ` -class ProjectLink extends PureComponent { +class Project extends PureComponent { + static propTypes = { + node: PropTypes.object.isRequired, + currentSlug: PropTypes.string.isRequired, + refCurrentItem: PropTypes.any + } + render() { - const { node } = this.props + const { node, currentSlug, refCurrentItem } = this.props + const isCurrent = node.slug === currentSlug return ( - - {node.title} -

{node.title}

- - ) - } -} - -ProjectLink.propTypes = { - node: PropTypes.object -} - -export default class ProjectNav extends PureComponent { - state = { - scrolledToCurrent: false - } - - componentDidMount() { - // this.scrollToCurrent() - this.setState({ scrolledToCurrent: true }) - } - - componentDidUpdate() { - // this.scrollToCurrent() - } - - componentWillUnmount() { - this.setState({ scrolledToCurrent: false }) - } - - scrollToCurrent = () => { - const current = this.currentItem - const currentLeft = current.getBoundingClientRect().left - const currentWidth = current.clientWidth - const finalPosition = currentLeft - window.innerWidth / 2 + currentWidth / 2 - - this.scrollContainer.scrollLeft += finalPosition - } - - Project({ node }) { - // const current = node.slug === slug - - return ( -
current && (this.currentItem = node)} - > - +
+ + {node.title} +

{node.title}

+
) } +} + +export default class ProjectNav extends PureComponent { + static propTypes = { + currentSlug: PropTypes.string.isRequired + } + + state = { + scrollLeftPosition: 0 + } + + scrollContainer = React.createRef() + currentItem = React.createRef() + + componentDidMount() { + this.scrollToCurrent() + } + + componentDidUpdate() { + this.scrollToCurrent() + } + + scrollToCurrent = () => { + const scrollContainer = this.scrollContainer.current + const activeItem = this.currentItem.current + const scrollRect = scrollContainer.getBoundingClientRect() + const activeRect = activeItem.getBoundingClientRect() + const scrollLeftPosition = + activeRect.left - + scrollRect.left - + scrollRect.width / 2 + + activeRect.width / 2 + + scrollContainer.scrollLeft += this.state.scrollLeftPosition + + this.setState(state => { + return { + ...state, + scrollLeftPosition + } + }) + } render() { - const { slug } = this.props + const { currentSlug } = this.props return ( (this.scrollContainer = node)} - > + ) @@ -110,7 +117,3 @@ export default class ProjectNav extends PureComponent { ) } } - -ProjectNav.propTypes = { - slug: PropTypes.string -} diff --git a/src/templates/Project.jsx b/src/templates/Project.jsx index db0de74..cdbc92a 100644 --- a/src/templates/Project.jsx +++ b/src/templates/Project.jsx @@ -75,7 +75,7 @@ export default class Project extends PureComponent { - + ) }