From 1cc630da4247e75a18629d960768d06239b0175b Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 24 Aug 2021 18:40:19 +0200 Subject: details and gallery --- src/views/Gallery.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/views/Gallery.js (limited to 'src/views/Gallery.js') diff --git a/src/views/Gallery.js b/src/views/Gallery.js new file mode 100644 index 0000000..7bdf18b --- /dev/null +++ b/src/views/Gallery.js @@ -0,0 +1,75 @@ +/** + * Detail view, displaying text plus media + */ + +import React, { useState, useEffect } from "react"; + +export default function Gallery({ images, visible }) { + const hasItems = !!images?.length; + const oneItem = images?.length === 1; + + const [index, setIndex] = useState(0); + const [opacity, setOpacity] = useState(0); + useEffect(() => { + setIndex(0); + setOpacity(0); + setTimeout(() => setOpacity(1), 500); + }, [images]); + + function previous() { + setOpacity(0); + setTimeout(() => setIndex(Math.max(0, index - 1)), 100); + // setTimeout(() => setOpacity(1), 500); + } + function next() { + setOpacity(0); + setTimeout(() => setIndex(Math.min(images.length - 1, index + 1)), 100); + // setTimeout(() => setOpacity(1), 500); + } + function nextOrWrap() { + if (oneItem) return; + setOpacity(0); + setTimeout(() => setIndex(mod(index + 1, images.length)), 100); + // setTimeout(() => setOpacity(1), 500); + } + function appear() { + setOpacity(1); + } + + if (!hasItems) { + return
; + } + + return ( +
+
+ {visible && !!images[index] && ( + + )} +
+
+ {!oneItem && ( + 0 ? 1 : 0 }} + /> + )} + {!oneItem && ( + + )} +
+
+ ); +} + +const mod = (n, m) => n - m * Math.floor(n / m); -- cgit v1.2.3-70-g09d2