Skip to content

Instantly share code, notes, and snippets.

@jamiejohnsonkc
Last active July 25, 2020 00:23
Show Gist options
  • Select an option

  • Save jamiejohnsonkc/fea04c1b0d9c05fde9ff93b3fd53b594 to your computer and use it in GitHub Desktop.

Select an option

Save jamiejohnsonkc/fea04c1b0d9c05fde9ff93b3fd53b594 to your computer and use it in GitHub Desktop.
custom gatsby fluid image hook comp & implementation
/** @jsx jsx */
import { jsx } from "theme-ui"
import React from "react"
import { graphql, useStaticQuery } from "gatsby"
import styled from "@emotion/styled"
import BackgroundImage from "gatsby-background-image"
import useBackgroundImage from "./useBackgroundImage"
const BackgroundImageContainer = ({ className }) => {
const data = useBackgroundImage()
return (
<div>
<BackgroundImage
Tag="section"
className={className}
fluid={data}
backgroundColor={`#040e18`}
>
<h1>From the comp</h1>
</BackgroundImage>
</div>
)
}
export default BackgroundImageContainer
/** @jsx jsx */
import { jsx } from "theme-ui"
import React from "react"
import { graphql, useStaticQuery } from "gatsby"
import styled from "@emotion/styled"
const useBackgroundImage = () => {
const data = useStaticQuery(graphql`
query FluidBackgroundImage {
file(relativePath: { eq: "gatsby-astronaut.png" }) {
childImageSharp {
fluid {
base64
aspectRatio
src
srcSet
sizes
}
}
}
}
`)
return data.file.childImageSharp.fluid
}
console.log(useBackgroundImage)
export default useBackgroundImage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment