Skip to content

Commit

Permalink
Add css blur when placeholder=blur
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle committed Jun 9, 2021
1 parent a008d01 commit 75d15d6
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,16 @@ function removePlaceholder(
// This is the desired behavior for now, and will be revisited when error
// handling is worked on for the image component itself.
setTimeout(() => {
element.style.filter = 'none'
element.style.backgroundSize = 'none'
element.style.backgroundImage = 'none'
}, 1500)
} else {
element.onload = () => {
if (!element.src.startsWith('data:')) {
setTimeout(() => {
element.style.filter = 'none'
element.style.backgroundSize = 'none'
element.style.backgroundImage = 'none'
}, 1500)
}
Expand Down Expand Up @@ -389,11 +393,9 @@ export default function Image({
const heightInt = getInt(height)
const qualityInt = getInt(quality)

const MIN_IMG_SIZE_FOR_PLACEHOLDER = 5000
const tooSmallForBlurryPlaceholder =
widthInt && heightInt && widthInt * heightInt < MIN_IMG_SIZE_FOR_PLACEHOLDER
const shouldShowBlurryPlaceholder =
placeholder === 'blur' && !tooSmallForBlurryPlaceholder
// Show blur if larger than 5000px such as 100 x 50
const showBlurPlaceholder =
placeholder === 'blur' && (widthInt || 0) * (heightInt || 0) > 5000

let wrapperStyle: JSX.IntrinsicElements['div']['style'] | undefined
let sizerStyle: JSX.IntrinsicElements['div']['style'] | undefined
Expand Down Expand Up @@ -421,8 +423,9 @@ export default function Image({
objectFit,
objectPosition,

...(shouldShowBlurryPlaceholder
...(showBlurPlaceholder
? {
filter: 'blur(20px)',
backgroundSize: 'cover',
backgroundImage: `url("${blurDataURL}")`,
}
Expand Down

0 comments on commit 75d15d6

Please sign in to comment.