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

apply IE polyfill styles to placeholder images too #22863

Merged
merged 2 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Specifically, we collect the following information for _all_ telemetry events:

- Timestamp of the occurrence.
- Command invoked (e.g. `build` or `develop`).
- Gatsby machine ID: This is generated with UUID and stored in global gatsby config at ` ~/.config/gatsby/config.json`.
- Gatsby machine ID: This is generated with UUID and stored in global gatsby config at `~/.config/gatsby/config.json`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For those looking, this unrelated fix is because it was breaking linting tests

- Unique session ID: This is generated on each run with UUID.
- One-way hash of the current working directory or a hash of the git remote.
- General OS level information (operating system, version, CPU architecture, and whether the command is run inside a CI).
Expand Down
21 changes: 12 additions & 9 deletions packages/gatsby-image/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,12 @@ const noscriptImg = props => {
// Earlier versions of gatsby-image during the 2.x cycle did not wrap
// the `Img` component in a `picture` element. This maintains compatibility
// until a breaking change can be introduced in the next major release
const Placeholder = ({
src,
imageVariants,
generateSources,
spreadProps,
ariaHidden,
}) => {
const baseImage = <Img src={src} {...spreadProps} ariaHidden={ariaHidden} />
const Placeholder = React.forwardRef((props, ref) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding forwardRef would usually be a breaking change, but as this is an internal component I think it's ok

const { src, imageVariants, generateSources, spreadProps, ariaHidden } = props

const baseImage = (
<Img ref={ref} src={src} {...spreadProps} ariaHidden={ariaHidden} />
)

return imageVariants.length > 1 ? (
<picture>
Expand All @@ -279,7 +277,7 @@ const Placeholder = ({
) : (
baseImage
)
}
})

const Img = React.forwardRef((props, ref) => {
const {
Expand Down Expand Up @@ -357,6 +355,7 @@ class Image extends React.Component {
}

this.imageRef = React.createRef()
this.placeholderRef = props.placeholderRef || React.createRef()
this.handleImageLoaded = this.handleImageLoaded.bind(this)
this.handleRef = this.handleRef.bind(this)
}
Expand Down Expand Up @@ -517,6 +516,7 @@ class Image extends React.Component {
{image.base64 && (
<Placeholder
ariaHidden
ref={this.placeholderRef}
src={image.base64}
spreadProps={placeholderImageProps}
imageVariants={imageVariants}
Expand All @@ -528,6 +528,7 @@ class Image extends React.Component {
{image.tracedSVG && (
<Placeholder
ariaHidden
ref={this.placeholderRef}
src={image.tracedSVG}
spreadProps={placeholderImageProps}
imageVariants={imageVariants}
Expand Down Expand Up @@ -618,6 +619,7 @@ class Image extends React.Component {
{image.base64 && (
<Placeholder
ariaHidden
ref={this.placeholderRef}
src={image.base64}
spreadProps={placeholderImageProps}
imageVariants={imageVariants}
Expand All @@ -629,6 +631,7 @@ class Image extends React.Component {
{image.tracedSVG && (
<Placeholder
ariaHidden
ref={this.placeholderRef}
src={image.tracedSVG}
spreadProps={placeholderImageProps}
imageVariants={imageVariants}
Expand Down
21 changes: 16 additions & 5 deletions packages/gatsby-image/src/withIEPolyfill/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Image from "../index"

class ImageWithIEPolyfill extends Component {
imageRef = this.props.innerRef || createRef()
placeholderRef = createRef()

// Load object-fit/position polyfill if required (e.g. in IE)
componentDidMount() {
Expand All @@ -12,24 +13,34 @@ class ImageWithIEPolyfill extends Component {
typeof testImg.style.objectFit === `undefined` ||
typeof testImg.style.objectPosition === `undefined`
) {
import(`object-fit-images`).then(({ default: ObjectFitImages }) =>
import(`object-fit-images`).then(({ default: ObjectFitImages }) => {
ObjectFitImages(this.imageRef.current.imageRef.current)
)
ObjectFitImages(this.placeholderRef.current)
})
}
}

render() {
const { objectFit, objectPosition, ...props } = this.props

const polyfillStyle = {
objectFit: objectFit,
objectPosition: objectPosition,
fontFamily: `"object-fit: ${objectFit}; object-position: ${objectPosition}"`,
}

return (
<Image
ref={this.imageRef}
placeholderRef={this.placeholderRef}
{...props}
imgStyle={{
...props.imgStyle,
objectFit: objectFit,
objectPosition: objectPosition,
fontFamily: `"object-fit: ${objectFit}; object-position: ${objectPosition}"`,
...polyfillStyle,
}}
placeholderStyle={{
...props.placeholderStyle,
...polyfillStyle,
}}
/>
)
Expand Down