-
Notifications
You must be signed in to change notification settings - Fork 10.2k
feat(gatsby-image): Add gatsby-image/withIEPolyfill export with object-fit/position support #12681
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
Merged
wardpeet
merged 8 commits into
gatsbyjs:master
from
ooloth:topics/gatsby-image-with-ie-polyfill
Mar 26, 2019
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
90e5d56
Merge pull request #4 from gatsbyjs/master
ooloth dc909ff
Add a withIEPolyfill wrapper for gatsby-image
ooloth 951c9f9
Ignore compiled source file
ooloth 2fae59e
Apply polyfill to current image only
ooloth 1f9b866
rm withIEPolyfill
wardpeet ef08cee
update ref handling to accept an external ref
wardpeet 0ccf2b8
fix image ref handling in polyfill
wardpeet b58b21e
Merge branch 'master' into pr/12681
wardpeet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| /*.js | ||
| yarn.lock | ||
| /withIEPolyfill/*.js | ||
| yarn.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import React, { Component, createRef } from "react" | ||
| import PropTypes from "prop-types" | ||
| import Image from "../index" | ||
|
|
||
| class ImageWithIEPolyfill extends Component { | ||
| constructor(props) { | ||
| super(props) | ||
| this.imageRef = createRef() | ||
| } | ||
|
|
||
| // Load object-fit/position polyfill if required (e.g. in IE) | ||
| componentDidMount() { | ||
| const testImg = document.createElement(`img`) | ||
| if ( | ||
| typeof testImg.style.objectFit === `undefined` || | ||
| typeof testImg.style.objectPosition === `undefined` | ||
| ) { | ||
| import(`object-fit-images`).then(({ default: ObjectFitImages }) => | ||
| ObjectFitImages(this.imageRef.current.querySelector(`img`)) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| render() { | ||
| const { objectFit, objectPosition, ...props } = this.props | ||
|
|
||
| return ( | ||
| <Image | ||
| ref={this.imageRef} | ||
| {...props} | ||
| imgStyle={{ | ||
| ...props.imgStyle, | ||
| objectFit: objectFit, | ||
| objectPosition: objectPosition, | ||
| fontFamily: `"object-fit: ${objectFit}; object-position: ${objectPosition}"`, | ||
| }} | ||
| /> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| ImageWithIEPolyfill.propTypes = { | ||
| objectFit: PropTypes.string, | ||
| objectPosition: PropTypes.string, | ||
| } | ||
|
|
||
| ImageWithIEPolyfill.defaultProps = { | ||
| objectFit: `cover`, | ||
| objectPosition: `50% 50%`, | ||
| } | ||
|
|
||
| export default ImageWithIEPolyfill | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| "use strict"; | ||
|
ooloth marked this conversation as resolved.
Outdated
|
||
|
|
||
| var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
|
|
||
| exports.__esModule = true; | ||
| exports.default = void 0; | ||
|
|
||
| var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); | ||
|
|
||
| var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); | ||
|
|
||
| var _react = _interopRequireDefault(require("react")); | ||
|
|
||
| var _propTypes = _interopRequireDefault(require("prop-types")); | ||
|
|
||
| var _index = _interopRequireDefault(require("../index")); | ||
|
|
||
| var _jsxFileName = "/Users/michael/Sites/open-source/forks/gatsby/packages/gatsby-image/src/withIEPolyfill/index.js"; | ||
| // Dynamically load polyfill for object-fit/object-position if needed (e.g. in IE) | ||
| var testImg = document.createElement("img"); | ||
|
|
||
| if (typeof testImg.style.objectFit === "undefined" || typeof testImg.style.objectPosition === "undefined") { | ||
| import("object-fit-images").then(function (_ref) { | ||
| var ObjectFitImages = _ref.default; | ||
| return ObjectFitImages(); | ||
| }); | ||
| } | ||
|
|
||
| function ImageWithIEPolyfill(_ref2) { | ||
| var objectFit = _ref2.objectFit, | ||
| objectPosition = _ref2.objectPosition, | ||
| props = (0, _objectWithoutPropertiesLoose2.default)(_ref2, ["objectFit", "objectPosition"]); | ||
| return _react.default.createElement(_index.default, (0, _extends2.default)({}, props, { | ||
| imgStyle: (0, _extends2.default)({}, props.imgStyle, { | ||
| objectFit: objectFit, | ||
| objectPosition: objectPosition, | ||
| fontFamily: "\"object-fit: " + objectFit + "; object-position: " + objectPosition + "\"" | ||
| }), | ||
| __source: { | ||
| fileName: _jsxFileName, | ||
| lineNumber: 18 | ||
| }, | ||
| __self: this | ||
| })); | ||
| } | ||
|
|
||
| ImageWithIEPolyfill.propTypes = { | ||
| objectFit: _propTypes.default.string, | ||
| objectPosition: _propTypes.default.string | ||
| }; | ||
| ImageWithIEPolyfill.defaultProps = { | ||
| objectFit: "cover", | ||
| objectPosition: "50% 50%" | ||
| }; | ||
| var _default = ImageWithIEPolyfill; | ||
| exports.default = _default; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.