Last active
September 18, 2020 07:47
-
-
Save mgrubinger/4b5770c5aec1b1670f839ab9acb14649 to your computer and use it in GitHub Desktop.
Gatsby generic Image component. Use with <Image filename="myimg.jpg"/>. Original idea: https://noahgilmore.com/blog/easy-gatsby-image-components/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Img from "gatsby-image"; | |
import { StaticQuery, graphql } from "gatsby"; | |
import React from "react"; | |
export default props => ( | |
<StaticQuery | |
query={graphql` | |
query { | |
images: allFile { | |
edges { | |
node { | |
relativePath | |
name | |
childImageSharp { | |
sizes(maxWidth: 600) { | |
...GatsbyImageSharpSizes | |
} | |
} | |
} | |
} | |
} | |
} | |
`} | |
render={data => { | |
const image = data.images.edges.find(n => { | |
return n.node.relativePath.includes(props.filename); | |
}); | |
if (!image) { | |
return null; | |
} | |
const imageSizes = image.node.childImageSharp.sizes; | |
return ( | |
<Img | |
alt={props.alt} | |
sizes={imageSizes} | |
style={props.style} | |
imgStyle={props.imageStyle} | |
className={props.className} | |
/> | |
); | |
}} | |
/> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment