Skip to content

Commit

Permalink
Loading animation and some extra logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Nemeseri committed Oct 26, 2024
1 parent efb4f21 commit bdce4cd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
19 changes: 12 additions & 7 deletions src/assets/street-photography.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@
max-height: none;
}

.street-photography dialog .img-skeleton {
.street-photography .loading .loader {
display: none;
aspect-ratio: 3/4;
width: auto;
height: 100%;
background: #eee;
width: 20px;
margin: 0 auto;
opacity: 0.4;
aspect-ratio: 1;
border-radius: 50%;
animation: l5 1s infinite linear alternate;
}
@keyframes l5 {
0% {box-shadow: 25px 0 #fff, -25px 0 #fff2;background: #fff }
33% {box-shadow: 25px 0 #fff, -25px 0 #fff2;background: #fff2}
66% {box-shadow: 25px 0 #fff2,-25px 0 #fff; background: #fff2}
100%{box-shadow: 25px 0 #fff2,-25px 0 #fff; background: #fff }
}

.street-photography dialog.loading .img-skeleton {
.street-photography .loading .loader {
display: block;
}

Expand Down
26 changes: 23 additions & 3 deletions src/components/GalleryDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ type GalleryDialogProps = {
}

export default function GalleryDialog({ photo, handleClose, handleKeys }: GalleryDialogProps) {
const [isLoading, setIsLoading] = useState(false);
const [isLoading, setIsLoading] = useState<boolean>(false);
const dialogRef = useRef<HTMLDialogElement>(null);
const timeoutRef = useRef<number | undefined>();
const isOpen = !!photo;

useEffect(() =>{
Expand All @@ -19,14 +20,33 @@ export default function GalleryDialog({ photo, handleClose, handleKeys }: Galler
}
}, [isOpen]);

useEffect(() => {
// delay the loader animation to prevent flicker on fast img loads
if (isLoading) {
timeoutRef.current = window.setTimeout(() => {
if (dialogRef.current) {
dialogRef.current.classList.add('loading');
}
}, 200);
} else {
if (dialogRef.current) {
dialogRef.current.classList.remove('loading');
}
}

return () => {
clearTimeout(timeoutRef.current);
};
}, [isLoading]);

useEffect(() => {
setIsLoading(true);
}, [photo]);

return (
<dialog onClick={handleClose} onKeyDown={handleKeys} ref={dialogRef} className={isLoading ? 'loading' : ''}>
<dialog onClick={handleClose} onKeyDown={handleKeys} ref={dialogRef}>
<button>Close</button>
<div className='img-skeleton'></div>
<div className='loader'></div>
{photo &&
<img src={photo.largeUrl}
key={photo.id}
Expand Down

0 comments on commit bdce4cd

Please sign in to comment.