Skip to content

Commit 725c6e8

Browse files
dannywilsKyleAMathews
authored andcommitted
[gatsby-image] Allow specifying HTML tag for gatsby-image wrapping elements (#4022)
* Allow specifying which html tag will be used for gatsby-image wrappers * Update gatsby-image README.md to include new Tag prop
1 parent 8273ddf commit 725c6e8

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

packages/gatsby-image/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Gatsby's GraphQL queries. It combines
77
[Gatsby's native image processing](https://image-processing.gatsbyjs.org/)
88
capabilities with advanced image loading techniques to easily and completely
99
optimize image loading for your sites. `gatsby-image` uses
10-
[gatsby-plugin-sharp](https://www.gatsbyjs.org/packages/gatsby-plugin-sharp/)
10+
[gatsby-plugin-sharp](https://www.gatsbyjs.org/packages/gatsby-plugin-sharp/)
1111
to power its image transformations.
1212

1313
_Warning: gatsby-image is **not** a drop-in replacement for `<img/>`. It's
@@ -250,15 +250,16 @@ prop. e.g. `<Img sizes={sizes} />`
250250
| `fadeIn` | `bool` | Defaults to fading in the image on load |
251251
| `title` | `string` | Passed to the `img` element |
252252
| `alt` | `string` | Passed to the `img` element |
253-
| `className` | `string\|object` | Passed to the wrapper div. Object is needed to support Glamor's css prop |
254-
| `outerWrapperClassName` | `string\|object` | Passed to the outer wrapper div. Object is needed to support Glamor's css prop |
255-
| `style` | `object` | Spread into the default styles in the wrapper div |
253+
| `className` | `string\|object` | Passed to the wrapper element. Object is needed to support Glamor's css prop |
254+
| `outerWrapperClassName` | `string\|object` | Passed to the outer wrapper element. Object is needed to support Glamor's css prop |
255+
| `style` | `object` | Spread into the default styles in the wrapper element |
256256
| `position` | `string` | Defaults to `relative`. Pass in `absolute` to make the component `absolute` positioned |
257257
| `backgroundColor` | `string\|bool` | Set a colored background placeholder. If true, uses "lightgray" for the color. You can also pass in any valid color string. |
258258
| `onLoad` | `func` | A callback that is called when the full-size image has loaded. |
259+
| `Tag` | `string` | Which HTML tag to use for wrapping elements. Defaults to `div`. |
259260

260261
## Image processing arguments
261-
[gatsby-plugin-sharp](/packages/gatsby-plugin-sharp) supports many additional arguments for transforming your images like
262+
[gatsby-plugin-sharp](/packages/gatsby-plugin-sharp) supports many additional arguments for transforming your images like
262263
`quality`,`sizeByPixelDensity`,`pngCompressionLevel`,`cropFocus`,`greyscale` and many more. See its documentation for more.
263264

264265
## Some other stuff to be aware of

packages/gatsby-image/src/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ class Image extends React.Component {
187187
sizes,
188188
resolutions,
189189
backgroundColor,
190+
Tag,
190191
} = convertProps(this.props)
191192

192193
let bgColor
@@ -207,7 +208,7 @@ class Image extends React.Component {
207208

208209
// The outer div is necessary to reset the z-index to 0.
209210
return (
210-
<div
211+
<Tag
211212
className={`${
212213
outerWrapperClassName ? outerWrapperClassName : ``
213214
} gatsby-image-outer-wrapper`}
@@ -217,7 +218,7 @@ class Image extends React.Component {
217218
position: style.position === `absolute` ? `initial` : `relative`,
218219
}}
219220
>
220-
<div
221+
<Tag
221222
className={`${className ? className : ``} gatsby-image-wrapper`}
222223
style={{
223224
position: `relative`,
@@ -228,7 +229,7 @@ class Image extends React.Component {
228229
ref={this.handleRef}
229230
>
230231
{/* Preserve the aspect ratio. */}
231-
<div
232+
<Tag
232233
style={{
233234
width: `100%`,
234235
paddingBottom: `${100 / image.aspectRatio}%`,
@@ -259,7 +260,7 @@ class Image extends React.Component {
259260

260261
{/* Show a solid background color. */}
261262
{bgColor && (
262-
<div
263+
<Tag
263264
title={title}
264265
style={{
265266
backgroundColor: bgColor,
@@ -298,8 +299,8 @@ class Image extends React.Component {
298299
__html: noscriptImg({ alt, title, ...image }),
299300
}}
300301
/>
301-
</div>
302-
</div>
302+
</Tag>
303+
</Tag>
303304
)
304305
}
305306

@@ -327,7 +328,7 @@ class Image extends React.Component {
327328

328329
// The outer div is necessary to reset the z-index to 0.
329330
return (
330-
<div
331+
<Tag
331332
className={`${
332333
outerWrapperClassName ? outerWrapperClassName : ``
333334
} gatsby-image-outer-wrapper`}
@@ -337,7 +338,7 @@ class Image extends React.Component {
337338
position: style.position === `absolute` ? `initial` : `relative`,
338339
}}
339340
>
340-
<div
341+
<Tag
341342
className={`${className ? className : ``} gatsby-image-wrapper`}
342343
style={divStyle}
343344
ref={this.handleRef}
@@ -366,7 +367,7 @@ class Image extends React.Component {
366367

367368
{/* Show a solid background color. */}
368369
{bgColor && (
369-
<div
370+
<Tag
370371
title={title}
371372
style={{
372373
backgroundColor: bgColor,
@@ -409,8 +410,8 @@ class Image extends React.Component {
409410
}),
410411
}}
411412
/>
412-
</div>
413-
</div>
413+
</Tag>
414+
</Tag>
414415
)
415416
}
416417

@@ -421,6 +422,7 @@ class Image extends React.Component {
421422
Image.defaultProps = {
422423
fadeIn: true,
423424
alt: ``,
425+
Tag: `div`,
424426
}
425427

426428
Image.propTypes = {
@@ -440,6 +442,7 @@ Image.propTypes = {
440442
position: PropTypes.string,
441443
backgroundColor: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
442444
onLoad: PropTypes.func,
445+
Tag: PropTypes.string,
443446
}
444447

445448
export default Image

0 commit comments

Comments
 (0)