-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
(feat): Add lazy loading for images #784
Closed
senthil-athiban
wants to merge
5
commits into
code100x:main
from
senthil-athiban:feature/lazy-load-image
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a837ba9
(feat): Add lazy loading for images
senthil-athiban 941fbd6
(feat): add custom lazy load img
senthil-athiban 66d185d
(fix): replace img tag to lazyloadimg
senthil-athiban 655298d
merge main
senthil-athiban 5ea9c04
Merge branch 'main' of https://github.com/code100x/cms into feature/l…
senthil-athiban 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use client'; | ||
import Image from 'next/image'; | ||
import React, { useState, useRef, useEffect } from 'react'; | ||
|
||
const registerObserver = ( | ||
ref: React.RefObject<Element>, | ||
setShowImage: React.Dispatch<React.SetStateAction<boolean>>, | ||
) => { | ||
if (!ref.current) { | ||
console.log(' Ref is not attached to a DOM element'); | ||
return; | ||
} | ||
const observer = new IntersectionObserver((entries, observer) => { | ||
entries.forEach((entry) => { | ||
if (!entry.isIntersecting) return; | ||
setShowImage(true); | ||
observer.disconnect(); | ||
}); | ||
}); | ||
observer.observe(ref.current); | ||
}; | ||
|
||
interface LazyLoadImageProps { | ||
src: string; | ||
alt: string; | ||
className: string; | ||
width: number; | ||
height: number; | ||
} | ||
export const LazyLoadImage = ({ | ||
src, | ||
alt, | ||
className, | ||
width, | ||
height, | ||
}: LazyLoadImageProps) => { | ||
const [showImage, setshowImage] = useState(false); | ||
const imgRef = useRef(null); | ||
useEffect(() => { | ||
registerObserver(imgRef, setshowImage); | ||
}, []); | ||
return showImage ? ( | ||
<Image | ||
alt={alt} | ||
src={src} | ||
className={className} | ||
width={width} | ||
height={height} | ||
/> | ||
) : ( | ||
<Image | ||
alt={alt} | ||
src={src} | ||
className={className} | ||
width={width} | ||
height={height} | ||
ref={imgRef} | ||
style={{ filter: 'blur(4px)' }} | ||
/> | ||
); | ||
}; |
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
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.
what is this complex logic? NextJs Image its already loas images like that...
what i was saying is until the image isnt loaded show the skeleton
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 is to observe the viewport by writing custom logic. Utilizing this LazyLoadImage component, we can customize our own skeleton, and styling effect.