Toggle lightbox captions on image click

This commit is contained in:
TerryM
2026-05-27 13:37:14 +08:00
parent a6fda3cd03
commit d3d054ad32

View File

@@ -84,6 +84,7 @@ function LightboxView({
onClose: () => void; onClose: () => void;
}) { }) {
const [index, setIndex] = useState(startIndex); const [index, setIndex] = useState(startIndex);
const [isCaptionVisible, setIsCaptionVisible] = useState(true);
const touchStartX = useRef<number | null>(null); const touchStartX = useRef<number | null>(null);
const goPrev = useCallback( const goPrev = useCallback(
@@ -110,8 +111,13 @@ function LightboxView({
}; };
}, [goPrev, goNext, onClose]); }, [goPrev, goNext, onClose]);
useEffect(() => {
setIsCaptionVisible(true);
}, [captionText, index]);
const current = images[index]; const current = images[index];
const caption = captionText?.trim(); const caption = captionText?.trim();
const showCaption = !!caption && isCaptionVisible;
if (!current) return null; if (!current) return null;
return createPortal( return createPortal(
@@ -165,12 +171,15 @@ function LightboxView({
<div <div
className={`flex min-h-0 w-full flex-1 items-center justify-center px-4 pt-16 ${ className={`flex min-h-0 w-full flex-1 items-center justify-center px-4 pt-16 ${
caption ? "pb-3" : "pb-16" showCaption ? "pb-3" : "pb-16"
}`} }`}
> >
<div <div
className="max-h-full max-w-[92vw]" className="flex h-full w-full items-center justify-center"
onClick={(e) => e.stopPropagation()} onClick={(e) => {
e.stopPropagation();
if (caption) setIsCaptionVisible((visible) => !visible);
}}
onTouchStart={(e) => { onTouchStart={(e) => {
touchStartX.current = e.touches[0].clientX; touchStartX.current = e.touches[0].clientX;
}} }}
@@ -187,13 +196,13 @@ function LightboxView({
<img <img
src={current.url} src={current.url}
alt={current.filename} alt={current.filename}
className="max-h-full max-w-[92vw] object-contain select-none" className="max-h-full max-w-full object-contain select-none"
draggable={false} draggable={false}
/> />
</div> </div>
</div> </div>
{caption ? ( {showCaption ? (
<div <div
className="shrink-0 border-t border-white/10 bg-gradient-to-t from-black via-black/90 to-black/70 px-4 py-4 text-sm leading-snug text-white sm:px-6" className="shrink-0 border-t border-white/10 bg-gradient-to-t from-black via-black/90 to-black/70 px-4 py-4 text-sm leading-snug text-white sm:px-6"
onClick={(e) => e.stopPropagation()} onClick={(e) => e.stopPropagation()}