1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2024-06-28 00:27:40 +02:00
portfolio/src/components/molecules/ProjectNav.jsx

110 lines
2.4 KiB
React
Raw Normal View History

import React, { Component } from 'react'
2018-04-16 22:32:30 +02:00
import PropTypes from 'prop-types'
2018-06-22 18:54:29 +02:00
import { Link, graphql, StaticQuery } from 'gatsby'
2018-05-04 01:58:43 +02:00
import Img from 'gatsby-image'
2018-06-22 20:54:04 +02:00
import FullWidth from '../atoms/FullWidth'
2018-06-11 19:48:38 +02:00
import styles from './ProjectNav.module.scss'
2018-04-16 22:32:30 +02:00
const query = graphql`
query {
allProjectsYaml {
edges {
node {
title
slug
img {
childImageSharp {
fluid(maxWidth: 500, quality: 85) {
...GatsbyImageSharpFluid_noBase64
}
}
}
}
}
}
}
`
2018-06-12 11:16:17 +02:00
const ProjectLink = ({ node }) => (
<Link className={styles.link} to={node.slug}>
<Img
className={styles.image}
2018-06-19 22:48:33 +02:00
fluid={node.img.childImageSharp.fluid}
2018-06-12 11:16:17 +02:00
alt={node.title}
/>
<h1 className={styles.title}>{node.title}</h1>
</Link>
)
2018-09-06 14:28:01 +02:00
export default class ProjectNav extends Component {
state = {
scrolledToCurrent: false
}
componentDidMount() {
this.scrollToCurrent()
2018-06-23 16:31:08 +02:00
this.setState({ scrolledToCurrent: true })
}
componentDidUpdate() {
this.scrollToCurrent()
}
2018-06-23 16:31:08 +02:00
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
2018-07-10 17:06:38 +02:00
this.scrollContainer.scrollLeft += finalPosition
}
render() {
2018-06-22 18:54:29 +02:00
const { slug } = this.props
return (
<StaticQuery
query={query}
2018-06-22 18:54:29 +02:00
render={data => {
const projects = data.allProjectsYaml.edges
2018-06-12 11:16:17 +02:00
return (
2018-06-22 20:54:04 +02:00
<FullWidth>
<nav
className={styles.projectNav}
2018-07-10 17:06:38 +02:00
ref={node => (this.scrollContainer = node)}
2018-06-22 20:54:04 +02:00
>
{projects.map(({ node }) => {
const current = node.slug === slug
2018-06-22 18:54:29 +02:00
2018-06-22 20:54:04 +02:00
return (
<div
className={styles.item}
key={node.slug}
2018-07-10 17:06:38 +02:00
ref={node => current && (this.currentItem = node)}
2018-06-22 20:54:04 +02:00
>
<ProjectLink node={node} />
</div>
)
})}
</nav>
</FullWidth>
2018-06-12 11:16:17 +02:00
)
2018-06-22 18:54:29 +02:00
}}
/>
)
}
}
2018-06-12 11:16:17 +02:00
ProjectLink.propTypes = {
node: PropTypes.object
}
2018-04-16 22:32:30 +02:00
ProjectNav.propTypes = {
2018-06-22 18:54:29 +02:00
slug: PropTypes.string
}