Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-image): Add useArtDirection hook #29231

Merged
merged 1 commit into from
Jan 28, 2021

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Jan 27, 2021

This PR adds a hook to support basic art direction in gatsby-plugin-image. It adds a custom hook, which is passed a "default image" and an array of "art-directed images", which take media queries. It returns an imagedata object that can be passed to the GatsbyImage component. The layout, placeholder, dimensions etc are all taken from the default image, so if you need to change aspect ratio etc, you need to pass a className with media query.

Usage example:

import { GatsbyImage, getImage, useArtDirection } from "gatsby-plugin-image"

import "./style.css"

export function MyImage({ data }) {

  const images = useArtDirection(getImage(data.largeImage), [
    {
      media: "(max-width: 1024px)",
      image: getImage(data.smallImage),
    },
  ])

  return  <GatsbyImage className="art-directed" image={images} />
}

Include something like this, with a matching media query:

@media screen and (max-width: 1024px) {
  .art-directed {
    width: 400px;
    height: 300px;
  }
}
Screen.Recording.2021-01-27.at.16.40.13.mov

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jan 27, 2021
@laurieontech
Copy link
Contributor

Main question is whether you can conditionally assign classes? Looks like it's one class and then you alter it via media query.

We should add some docs to this. But overall it looks reasonable to me.

@ascorbic ascorbic added topic: media Related to gatsby-plugin-image, or general image/media processing topics and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jan 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: media Related to gatsby-plugin-image, or general image/media processing topics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants