@treely/strapi-slices 5.3.2 → 5.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@treely/strapi-slices",
3
- "version": "5.3.2",
3
+ "version": "5.3.3",
4
4
  "license": "MIT",
5
5
  "author": "Tree.ly GmbH",
6
6
  "description": "@treely/strapi-slices is a open source library maintained by Tree.ly.",
@@ -1,4 +1,4 @@
1
- import React, { useEffect, useRef, useState } from 'react';
1
+ import React, { useCallback, useEffect, useMemo, useRef } from 'react';
2
2
  import { BoemlyModal, Flex, IconButton } from 'boemly';
3
3
  import { useKey, useLockBodyScroll } from 'react-use';
4
4
  import StrapiImage from '../../models/strapi/StrapiImage';
@@ -9,52 +9,62 @@ export interface FullScreenImageProps {
9
9
  images: StrapiImage[];
10
10
  isOpen: boolean;
11
11
  onClose: () => void;
12
- openIndex?: number;
12
+ currentIndex?: number;
13
+ setCurrentIndex?: (callback: (c: number) => number) => void;
13
14
  }
14
15
 
15
16
  export const FullScreenImage = ({
16
17
  images,
17
18
  isOpen,
18
19
  onClose,
19
- openIndex,
20
+ currentIndex = 0,
21
+ setCurrentIndex,
20
22
  }: FullScreenImageProps) => {
21
- const [imageIndex, setImageIndex] = useState(0);
22
23
  const containerRef = useRef<HTMLDivElement>(null);
23
24
 
24
25
  useLockBodyScroll(isOpen);
25
26
 
26
- const canMoveRight = imageIndex < images.length - 1;
27
- const canMoveLeft = imageIndex !== 0;
27
+ const canMoveRight = useMemo(
28
+ () => currentIndex < images.length - 1,
29
+ [currentIndex, images.length]
30
+ );
31
+ const canMoveLeft = useMemo(() => currentIndex !== 0, [currentIndex]);
28
32
 
29
- const onRight = () => setImageIndex((p) => (canMoveRight ? p + 1 : p));
30
- const onLeft = () => setImageIndex((p) => (canMoveLeft ? p - 1 : p));
33
+ const onRight = useCallback(
34
+ () => canMoveRight && setCurrentIndex && setCurrentIndex((c) => c + 1),
35
+ [canMoveRight]
36
+ );
37
+ const onLeft = useCallback(
38
+ () => canMoveLeft && setCurrentIndex && setCurrentIndex((c) => c - 1),
39
+ [canMoveLeft]
40
+ );
31
41
 
32
42
  useKey('ArrowRight', onRight, {}, [onRight]);
33
43
  useKey('ArrowLeft', onLeft, {}, [onLeft]);
34
44
 
35
45
  useEffect(() => {
36
- if (openIndex !== undefined) {
37
- setImageIndex(openIndex);
46
+ if (!!isOpen) {
47
+ setTimeout(() => {
48
+ containerRef?.current?.scrollTo({
49
+ left: currentIndex * containerRef.current.clientWidth,
50
+ behavior: 'instant',
51
+ });
52
+ }, 10);
38
53
  }
39
- }, [openIndex]);
54
+ }, [isOpen]);
40
55
 
41
56
  useEffect(() => {
42
57
  if (containerRef.current) {
43
58
  containerRef.current.scrollTo({
44
- left: imageIndex * containerRef.current.clientWidth,
59
+ left: currentIndex * containerRef.current.clientWidth,
45
60
  behavior: 'smooth',
46
61
  });
47
62
  }
48
- }, [imageIndex, containerRef]);
49
-
50
- const onCloseLocal = () => {
51
- setImageIndex(0);
52
- onClose();
53
- };
63
+ }, [currentIndex, containerRef]);
54
64
 
55
65
  return (
56
66
  <BoemlyModal
57
- onClose={onCloseLocal}
67
+ onClose={onClose}
58
68
  isOpen={isOpen}
59
69
  title=""
60
70
  trigger=""
@@ -178,7 +178,8 @@ export const FullWidthImageSlider: React.FC<FullWidthImageSliderProps> = ({
178
178
 
179
179
  <FullScreenImage
180
180
  images={slice.images.map((image) => image.img)}
181
- openIndex={sliderIndex}
181
+ currentIndex={sliderIndex}
182
+ setCurrentIndex={setSliderIndex}
182
183
  isOpen={isOpen}
183
184
  onClose={() => setIsOpen(false)}
184
185
  />