Skip to content

Instantly share code, notes, and snippets.

@mgrubinger
Last active September 18, 2020 07:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgrubinger/4b5770c5aec1b1670f839ab9acb14649 to your computer and use it in GitHub Desktop.
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/
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