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

111 lines
2.6 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
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>
)
class ProjectNav extends Component {
constructor(props) {
super(props)
this.scrollToCurrent = this.scrollToCurrent.bind(this)
}
componentDidMount() {
this.scrollToCurrent()
}
componentDidUpdate() {
this.scrollToCurrent()
}
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
}
render() {
2018-06-22 18:54:29 +02:00
const { slug } = this.props
return (
<StaticQuery
query={graphql`
query ProjectsNavQuery {
allProjectsYaml {
edges {
node {
title
slug
img {
childImageSharp {
fluid(maxWidth: 500, quality: 85) {
...GatsbyImageSharpFluid_noBase64
}
}
}
}
}
}
}
`}
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}
ref={node => {
this.scrollContainer = node
}}
>
{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}
ref={node => {
if (current) this.currentItem = node
}}
>
<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
}
2018-04-16 22:32:30 +02:00
export default ProjectNav