-
Notifications
You must be signed in to change notification settings - Fork 64
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
fix: re-render Background only if a larger image is needed #782
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5275e25
fix(BackgroundImpl): re-render only if a larger image is needed
ericdeansanchez 4dd1499
refactor: component should update if props or imgixParams change
ericdeansanchez 987dd74
chore(release): 9.1.0
314dc82
Merge branch 'main' into e/background
ericdeansanchez 77d4e7e
Merge branch 'main' into e/background
ericdeansanchez 8efba7d
Merge branch 'main' into e/background
ericdeansanchez 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 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,120 +1,155 @@ | ||
import React from "react"; | ||
import { withContentRect } from "react-measure"; | ||
import { PACKAGE_VERSION } from './constants'; | ||
import { PACKAGE_VERSION } from "./constants"; | ||
import constructUrl from "./constructUrl"; | ||
import extractQueryParams from "./extractQueryParams"; | ||
import findClosest from "./findClosest"; | ||
import targetWidths from "./targetWidths"; | ||
|
||
|
||
const noop = () => {}; | ||
|
||
const findNearestWidth = (actualWidth) => | ||
findClosest(actualWidth, targetWidths); | ||
|
||
const toFixed = (dp, value) => +value.toFixed(dp); | ||
|
||
const BackgroundImpl = (props) => { | ||
const { | ||
measureRef, | ||
measure, | ||
contentRect, | ||
imgixParams = {}, | ||
onLoad, | ||
disableLibraryParam, | ||
src, | ||
children, | ||
className = "", | ||
} = props; | ||
const { w: forcedWidth, h: forcedHeight } = imgixParams; | ||
const hasDOMDimensions = | ||
contentRect.bounds.width != null && contentRect.bounds.height != null; | ||
const htmlAttributes = props.htmlAttributes || {}; | ||
const dpr = toFixed(2, imgixParams.dpr || global.devicePixelRatio || 1); | ||
const ref = htmlAttributes.ref; | ||
const onRef = (el) => { | ||
measureRef(el); | ||
if (typeof ref === "function") { | ||
ref(el); | ||
} | ||
}; | ||
class BackgroundImpl extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
} | ||
|
||
shouldComponentUpdate(nextProps) { | ||
const contentRect = this.props.contentRect; | ||
const bounds = contentRect.bounds; | ||
const { width: prevWidth, height: prevHeight } = bounds; | ||
|
||
const { width, height } = (() => { | ||
const bothWidthAndHeightPassed = | ||
forcedWidth != null && forcedHeight != null; | ||
const nextContentRect = nextProps.contentRect; | ||
const nextBounds = nextContentRect.bounds; | ||
const { width: nextWidth, height: nextHeight } = nextBounds; | ||
|
||
if (bothWidthAndHeightPassed) { | ||
return { width: forcedWidth, height: forcedHeight }; | ||
// If neither of the previous nor next dimensions are present, | ||
// re-render. | ||
if (!nextWidth || !nextHeight || !prevWidth || !prevHeight) { | ||
return true; | ||
} | ||
|
||
if (!hasDOMDimensions) { | ||
return { width: undefined, height: undefined }; | ||
// The component has been rendered at least twice by this point | ||
// and both the previous and next dimensions should be defined. | ||
// Only update if the nextWidth is greater than the prevWidth. | ||
if (prevWidth && nextWidth && nextWidth > prevWidth) { | ||
return true; | ||
} | ||
const ar = contentRect.bounds.width / contentRect.bounds.height; | ||
|
||
const neitherWidthNorHeightPassed = | ||
forcedWidth == null && forcedHeight == null; | ||
if (neitherWidthNorHeightPassed) { | ||
const width = findNearestWidth(contentRect.bounds.width); | ||
const height = Math.ceil(width / ar); | ||
return { width, height }; | ||
|
||
// Similarly, only update if the next height is greater than | ||
// the previous height. | ||
if (prevHeight && nextHeight && nextHeight > prevHeight) { | ||
return true; | ||
} | ||
if (forcedWidth != null) { | ||
const height = Math.ceil(forcedWidth / ar); | ||
return { width: forcedWidth, height }; | ||
} else if (forcedHeight != null) { | ||
const width = Math.ceil(forcedHeight * ar); | ||
return { width, height: forcedHeight }; | ||
|
||
return false; | ||
} | ||
|
||
render() { | ||
const { | ||
measureRef, | ||
contentRect, | ||
imgixParams = {}, | ||
onLoad, | ||
disableLibraryParam, | ||
src, | ||
children, | ||
className = "", | ||
} = this.props; | ||
const { w: forcedWidth, h: forcedHeight } = imgixParams; | ||
const hasDOMDimensions = | ||
contentRect.bounds.width != null && contentRect.bounds.height != null; | ||
const htmlAttributes = this.props.htmlAttributes || {}; | ||
const dpr = toFixed(2, imgixParams.dpr || global.devicePixelRatio || 1); | ||
const ref = htmlAttributes.ref; | ||
const onRef = (el) => { | ||
measureRef(el); | ||
if (typeof ref === "function") { | ||
ref(el); | ||
} | ||
}; | ||
|
||
const { width, height } = (() => { | ||
const bothWidthAndHeightPassed = | ||
forcedWidth != null && forcedHeight != null; | ||
|
||
if (bothWidthAndHeightPassed) { | ||
return { width: forcedWidth, height: forcedHeight }; | ||
} | ||
|
||
if (!hasDOMDimensions) { | ||
return { width: undefined, height: undefined }; | ||
} | ||
const ar = contentRect.bounds.width / contentRect.bounds.height; | ||
|
||
const neitherWidthNorHeightPassed = | ||
forcedWidth == null && forcedHeight == null; | ||
if (neitherWidthNorHeightPassed) { | ||
const width = findNearestWidth(contentRect.bounds.width); | ||
const height = Math.ceil(width / ar); | ||
return { width, height }; | ||
} | ||
if (forcedWidth != null) { | ||
const height = Math.ceil(forcedWidth / ar); | ||
return { width: forcedWidth, height }; | ||
} else if (forcedHeight != null) { | ||
const width = Math.ceil(forcedHeight * ar); | ||
return { width, height: forcedHeight }; | ||
} | ||
})(); | ||
const isReady = width != null && height != null; | ||
|
||
const commonProps = { | ||
...htmlAttributes, | ||
}; | ||
|
||
if (!isReady) { | ||
return ( | ||
<div | ||
{...commonProps} | ||
className={`react-imgix-bg-loading ${className}`} | ||
ref={onRef} | ||
> | ||
{children} | ||
</div> | ||
); | ||
} | ||
})(); | ||
const isReady = width != null && height != null; | ||
|
||
const commonProps = { | ||
...htmlAttributes, | ||
}; | ||
const renderedSrc = (() => { | ||
const [rawSrc, params] = extractQueryParams(src); | ||
const srcOptions = { | ||
...params, | ||
fit: "crop", | ||
...imgixParams, | ||
...(disableLibraryParam ? {} : { ixlib: `react-${PACKAGE_VERSION}` }), | ||
width, | ||
height, | ||
dpr, | ||
}; | ||
|
||
return constructUrl(rawSrc, srcOptions); | ||
})(); | ||
|
||
const style = { | ||
...htmlAttributes.style, | ||
backgroundImage: `url(${renderedSrc})`, | ||
backgroundSize: | ||
(htmlAttributes.style || {}).backgroundSize !== undefined | ||
? htmlAttributes.style.backgroundSize | ||
: "cover", | ||
}; | ||
|
||
if (!isReady) { | ||
return ( | ||
<div | ||
{...commonProps} | ||
className={`react-imgix-bg-loading ${className}`} | ||
ref={onRef} | ||
> | ||
<div {...commonProps} className={className} ref={onRef} style={style}> | ||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
const renderedSrc = (() => { | ||
const [rawSrc, params] = extractQueryParams(src); | ||
const srcOptions = { | ||
...params, | ||
fit: "crop", | ||
...imgixParams, | ||
...(disableLibraryParam ? {} : { ixlib: `react-${PACKAGE_VERSION}` }), | ||
width, | ||
height, | ||
dpr, | ||
}; | ||
|
||
return constructUrl(rawSrc, srcOptions); | ||
})(); | ||
|
||
const style = { | ||
...htmlAttributes.style, | ||
backgroundImage: `url(${renderedSrc})`, | ||
backgroundSize: | ||
(htmlAttributes.style || {}).backgroundSize !== undefined | ||
? htmlAttributes.style.backgroundSize | ||
: "cover", | ||
}; | ||
|
||
return ( | ||
<div {...commonProps} className={className} ref={onRef} style={style}> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
} | ||
const Background = withContentRect("bounds")(BackgroundImpl); | ||
|
||
export { Background, BackgroundImpl as __BackgroundImpl }; |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also needs to handle cases when other props change, e.g.
imgixParams
,disableLibraryParam
, etc.