Skip to content

Commit

Permalink
fix: Properly target internal elements
Browse files Browse the repository at this point in the history
Added support for optional `maxWidth` and `maxHeight` for `fluid` type images.

The internal elements (padding for fluid, bgcolor for fixed) are both lacking class names to target specifically but both are the 1st children. Due to review constraints they are being targeted with a pseudo-selector, if the internal structure changes this will of course break.

Note that the `fixed` type image has width/height attributes as well that are hard-coded to the first image in SSR build. Not tested if that causes any problems, I think it might affect `object-fit: cover`, but only with `critical` images where these attr values are used until image request responds(or re-validates if cached).
  • Loading branch information
polarathene committed Sep 17, 2020
1 parent e179b56 commit 0c0ae42
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions packages/gatsby-image/src/art-direction.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,25 @@ const getShortKey = input => `_` + djb2a(input).toString(36)
// `emotion` does similar with `<style>`, it is not HTML5 spec compliant,
// however web browsers do support using `<style>` outside of `<head>`.
const ResponsiveQueries = ({ imageVariants, selectorClass }) => {
const variantRule = ({ width, height, aspectRatio }) => {
const variantRule = variant => {
const { width, height, maxWidth, maxHeight, aspectRatio } = variant

// '!important' required due to overriding inline styles
// 'aspectRatio' == fluid data
const styleOverride = aspectRatio
? `padding-bottom: ${100 / aspectRatio}% !important;`
: `width: ${width}px !important; height: ${height}px !important;`

return `.${selectorClass} { ${styleOverride} }`
if (aspectRatio) {
return (
`.${selectorClass} {\n` +
(maxWidth ? `${maxWidth}px !important;\n` : ``) +
(maxHeight ? `${maxHeight}px !important;\n` : ``) +
`}.${selectorClass} > :first-child {\n` +
`padding-bottom: ${100 / aspectRatio}% !important;\n}`
)
} else {
return (
`.${selectorClass}, .${selectorClass} > :first-child {\n` +
`width: ${width}px !important; height: ${height}px !important;\n}`
)
}
}

// If there is an image without a `media` condition, the first result
Expand All @@ -44,7 +55,7 @@ const ResponsiveQueries = ({ imageVariants, selectorClass }) => {
{imageVariants
.filter(v => v.media)
.map(v => (
<style key={`${selectorClass}-${v.media}`} media={v.media}>
<style key={`${selectorClass} ${v.media}`} media={v.media}>
{variantRule(v)}
</style>
))}
Expand All @@ -60,7 +71,7 @@ const withArtDirection = props => {

const imageVariants = props.fluid || props.fixed
const uniqueKey = !isBrowser
? getShortKey(imageVariants.map(v => v.srcSet).join(``))
? ` ` + getShortKey(imageVariants.map(v => v.srcSet).join(``))
: ``

return (
Expand All @@ -69,7 +80,7 @@ const withArtDirection = props => {
imageVariants={imageVariants}
selectorClass={uniqueKey}
/>
<GatsbyImage {...rest} className={`${uniqueKey} ${cn}`} />
<GatsbyImage {...rest} className={`${cn}${uniqueKey}`} />
</>
)
}
Expand Down

0 comments on commit 0c0ae42

Please sign in to comment.