add spinner on img load

This commit is contained in:
aleensd 2024-11-25 10:20:57 +02:00
parent 783b34e9ff
commit 7ecd69ae7e
4 changed files with 31 additions and 14 deletions

View File

@ -553,6 +553,10 @@
max-width: 90%; max-width: 90%;
} }
.slideshow-image{
position: relative;
}
.slideshow-image img { .slideshow-image img {
display: block; display: block;
width: auto; width: auto;
@ -569,7 +573,7 @@
text-align: start; text-align: start;
font-size: 16px; font-size: 16px;
font-weight: bold; font-weight: bold;
color: #F0F0F0; color: #bdb6b6;
z-index: 1200; z-index: 1200;
} }

View File

@ -1,6 +1,7 @@
import React, { useContext, useEffect, useState } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import { SiteContext } from '../../utils/contexts/'; import { SiteContext } from '../../utils/contexts/';
import { MediaPageStore } from '../../utils/stores/'; import { MediaPageStore } from '../../utils/stores/';
import { SpinnerLoader } from '../_shared';
import Tooltip from '../_shared/ToolTip'; import Tooltip from '../_shared/ToolTip';
export default function ImageViewer() { export default function ImageViewer() {
@ -15,6 +16,7 @@ export default function ImageViewer() {
const [slideshowItems, setSlideshowItems] = useState([]); const [slideshowItems, setSlideshowItems] = useState([]);
const [isModalOpen, setIsModalOpen] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false);
const [currentIndex, setCurrentIndex] = useState(0); const [currentIndex, setCurrentIndex] = useState(0);
const [isImgLoading, setIsImgLoading] = useState(true);
function onImageLoad() { function onImageLoad() {
setImage(getImageUrl()); setImage(getImageUrl());
@ -70,14 +72,19 @@ export default function ImageViewer() {
const onClose = () => setIsModalOpen(false); const onClose = () => setIsModalOpen(false);
const handleNext = () => { const handleNext = () => {
setIsImgLoading(true);
setCurrentIndex((prevIndex) => (prevIndex + 1) % slideshowItems.length); setCurrentIndex((prevIndex) => (prevIndex + 1) % slideshowItems.length);
}; };
const handlePrevious = () => { const handlePrevious = () => {
setIsImgLoading(true);
setCurrentIndex((prevIndex) => (prevIndex - 1 + slideshowItems.length) % slideshowItems.length); setCurrentIndex((prevIndex) => (prevIndex - 1 + slideshowItems.length) % slideshowItems.length);
}; };
const handleDotClick = (index) => setCurrentIndex(index); const handleDotClick = (index) => {
setIsImgLoading(true);
setCurrentIndex(index);
};
const handleImageClick = (index) => { const handleImageClick = (index) => {
const mediaPageUrl = site.url + slideshowItems[index]?.url; const mediaPageUrl = site.url + slideshowItems[index]?.url;
@ -92,22 +99,28 @@ export default function ImageViewer() {
{isModalOpen && slideshowItems && ( {isModalOpen && slideshowItems && (
<div className="modal-overlay" onClick={() => setIsModalOpen(false)}> <div className="modal-overlay" onClick={() => setIsModalOpen(false)}>
<div className="slideshow-container" onClick={(e) => e.stopPropagation()}> <div className="slideshow-container" onClick={(e) => e.stopPropagation()}>
{!isImgLoading && (
<button className="arrow left" onClick={handlePrevious} aria-label="Previous slide"> <button className="arrow left" onClick={handlePrevious} aria-label="Previous slide">
&#8249; &#8249;
</button> </button>
)}
<div className="slideshow-image"> <div className="slideshow-image">
{isImgLoading && <SpinnerLoader size="large"/>}
<img <img
src={site.url + '/' + slideshowItems[currentIndex]?.original_media_url} src={site.url + '/' + slideshowItems[currentIndex]?.original_media_url}
alt={`Slide ${currentIndex + 1}`} alt={`Slide ${currentIndex + 1}`}
onClick={() => handleImageClick(currentIndex)} onClick={() => handleImageClick(currentIndex)}
onLoad={() => setIsImgLoading(false)}
onError={() => setIsImgLoading(false)}
style={{ display: isImgLoading ? 'none' : 'block' }}
/> />
<div className="slideshow-title"> {!isImgLoading && <div className="slideshow-title">{slideshowItems[currentIndex]?.title}</div>}
{slideshowItems[currentIndex]?.title}
</div>
</div> </div>
{!isImgLoading && (
<button className="arrow right" onClick={handleNext} aria-label="Next slide"> <button className="arrow right" onClick={handleNext} aria-label="Next slide">
&#8250; &#8250;
</button> </button>
)}
<div className="dots"> <div className="dots">
{slideshowItems.map((_, index) => ( {slideshowItems.map((_, index) => (
<span <span

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long