forked from dabit3/gatsby-auth-starter-aws-amplify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a component with a html property to change the image
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from "react" | ||
import { StaticQuery, graphql } from "gatsby" | ||
import Img from "gatsby-image" | ||
|
||
/* | ||
* This component is built using `gatsby-image` to automatically serve optimized | ||
* images with lazy loading and reduced file sizes. The image is loaded using a | ||
* `useStaticQuery`, which allows us to load the image from directly within this | ||
* component, rather than having to pass the image data down from pages. | ||
* | ||
* For more information, see the docs: | ||
* - `gatsby-image`: https://gatsby.dev/gatsby-image | ||
* - `useStaticQuery`: https://www.gatsbyjs.org/docs/use-static-query/ | ||
* - https://noahgilmore.com/blog/easy-gatsby-image-components/ | ||
*/ | ||
|
||
const Image = 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} /> | ||
}} | ||
/> | ||
) | ||
|
||
export default Image |