-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Allow custom props to Gatsby-Image component #24876
Comments
I think this would be useful for ARIA attributes, even though Gatsby Image adds an empty |
Would also be useful for data attributes ( |
The question I have here why would you want to augment the image itself and not add a wrapper around it? We'll be making the gatsby-image more modular in the future to make this use case possible. As of know I rather do not want to add this feature as it adds extra complexity without a benefit. @marcysutton if I add |
@wardpeet An extra wrapper is one way to go, that's true. One side effect of requiring an additional wrapper around the already wrapper-heavy So, one benefit of passing valid HTML attributes through to the root DOM element would be avoiding that extra, unrelated work. |
Responding here as per wardpeets request on the associated PR.
@RetroCraft this is how passing of props is being supported internally for gatsby/packages/gatsby-image/src/index.js Lines 265 to 270 in b8a355d
If you were to use a dummy component, you can specify props in a more natural way if you prefer. You can then acquire the props object to pass those through, or if we accepted the component as a prop itself, we could do the same internally, albeit I'm not fond of that. const data = (<div testProp="hello world" />)
console.log(data.props) That said, the spread destructuring has been used internally since the inception of the component, and it probably should avoid that too.. gatsby/packages/gatsby-image/src/index.js Line 294 in b8a355d
I'm inclined to agree with @wardpeet that it might be best to hold off until the refactor, which should ideally allow for users to compose their own variants based on their needs, without many features depending on being upstreamed to be practical. I'm not against a prop for passing attributes through, as that's more controlled. A wrapper component can then be used like this(I understand it's not really anymore convenient than passing/extracting props from a dummy component or props object), using import Image from "gatsby-image"
export const Img = (props) => {
const customProps = _.omit(props, Image.propTypes)
return (<Image {...props} passthroughProps={customProps} />)
}
// ... import `Img` and use:
<Img fluid={fluid} data-test="Hello World" /> or import Image from "gatsby-image"
// Not particularly advised approach, could pass as a prop instead of a child
export const Img = (props) => {
const attributes = props.children[0].props
const customProps = _.omit(attributes, Image.propTypes)
return (<Image {...props} passthroughProps={customProps} />)
}
// ... import `Img` and use:
<Img fluid={fluid}>
<ImgAttrs data-test="Hello World" />
</Img> Something like that should avoid any issues that @ooloth raises, due to using a higher order component. The next version of |
Thank you for opening this issue. Gatsby-image was created so people can have an easy way to setup images from Gatsby. If we enable custom props, this could lead to bad a11y, weird bugs and more. For these reasons, I'll close this PR. We hope to have a better gatsby-image component in the near future that can be used more granular and fixes your issue. |
For anyone interested in the workaround and caveats, you can find a snippet example here. You should be able to import that instead and substitute the FWIW, I tried to make a good case for it, and how it should be minimal maintenance burden(potential for "weird bugs"), better DX(I assume this was meant over a11y, otherwise I'm confused what was implied as bad a11y) vs unfortunate workaround. I know that the bundling of attributes into an object prop isn't the best DX, but it does alleviate the maintenance concerns (as someone who has regularly contributed to and reviewed @ooloth is the workaround ok for you? You could assign a fixed |
@polarathene Thanks! Passing props/attributes to a wrapper element is a workable solution for now (I didn't realize how complicated the pass-through request would turn out to be). It would be awkward as a permanent approach, but @wardpeet mentioned there's a broader solution in the works, so I'm looking forward to that. |
Summary
Custom props should be allowed to be passed to the root tag of a Gatsby Image component.
Basic example
In this example, customProp and customProps should be passed to the root element of the Image component.
Motivation
I'm sure there are several use cases where custom props would be useful. A good example is for flip animations. React-flip-toolkit, requires custom props to be passed to the root element of a React component. Without the ability to pass custom props, flip animations are not possible.
The text was updated successfully, but these errors were encountered: