From a37bc7696555467fbdc5c06a106c7ff948ac5192 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Thu, 17 Oct 2019 12:55:27 +0700 Subject: [PATCH] finished slider --- .../node/NodeImageSlideBlock/index.tsx | 41 ++++++++++--------- .../node/NodeImageSlideBlock/styles.scss | 11 +---- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/components/node/NodeImageSlideBlock/index.tsx b/src/components/node/NodeImageSlideBlock/index.tsx index 30066358..40c4f71c 100644 --- a/src/components/node/NodeImageSlideBlock/index.tsx +++ b/src/components/node/NodeImageSlideBlock/index.tsx @@ -27,7 +27,7 @@ interface IProps { const getX = event => (event.touches ? event.touches[0].clientX : event.clientX); const NodeImageSlideBlock: FC = ({ node, is_loading, updateLayout }) => { - const [is_animated, setIsAnimated] = useState(false); + // const [is_animated, setIsAnimated] = useState(false); const [current, setCurrent] = useState(0); const [height, setHeight] = useState(320); const [loaded, setLoaded] = useState>({}); @@ -72,19 +72,9 @@ const NodeImageSlideBlock: FC = ({ node, is_loading, updateLayout }) => ]); // update outside hooks - useEffect(() => updateLayout(), [loaded]); + useEffect(() => updateLayout(), [loaded, height]); - useEffect(() => { - updateSizes(); - // - // if (!refs || !refs.current[current] || !loaded[current]) return setHeight(320); - // - // const el = refs.current[current]; - // - // const element_height = el.getBoundingClientRect && el.getBoundingClientRect().height; - // - // setHeight(element_height); - }, [refs, current, loaded]); + useEffect(() => updateSizes(), [refs, current, loaded]); useEffect(() => { if (!wrap || !wrap.current) return; @@ -95,15 +85,11 @@ const NodeImageSlideBlock: FC = ({ node, is_loading, updateLayout }) => const next = Math.max(heights[Math.ceil(selected)] || 320, 320); const now = prev - (prev - next) * (selected % 1); + if (current !== Math.round(selected)) setCurrent(Math.round(selected)); + setHeight(now); }, [offset, heights]); - // useEffect(() => { - // const timer = setTimeout(() => setIsAnimated(true), 250); - // - // return () => clearTimeout(timer); - // }, []); - const onDrag = useCallback( event => { if ( @@ -162,8 +148,23 @@ const NodeImageSlideBlock: FC = ({ node, is_loading, updateLayout }) => }; }, [onDrag, stopDragging]); + const changeCurrent = useCallback( + (item: number) => { + const { width } = wrap.current.getBoundingClientRect(); + setOffset(-1 * item * width); + }, + [wrap] + ); + return ( -
+
+ +