@snack-uikit/carousel 0.2.14-preview-95fbee92.0 → 0.2.14-preview-ff8280b4.0

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.
@@ -1,8 +1,8 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { useEffect, useMemo, useRef, useState } from 'react';
2
+ import debounce from 'lodash.debounce';
3
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
3
4
  import { TEST_IDS } from '../../testIds';
4
5
  import { getPageX } from './helpers';
5
- import { useWindowWidthChange } from './hooks';
6
6
  import styles from './styles.module.css';
7
7
  export function ItemProvider({ items, showItems, scrollBy, slideCallback, transition, swipe, page, gap, }) {
8
8
  const containerRef = useRef(null);
@@ -10,8 +10,8 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
10
10
  itemWidth: 200,
11
11
  gap: 0,
12
12
  });
13
- const containerElement = containerRef.current;
14
- useEffect(() => {
13
+ const recalculateItemsSize = useCallback(() => {
14
+ const containerElement = containerRef.current;
15
15
  if (!containerElement) {
16
16
  return;
17
17
  }
@@ -25,7 +25,12 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
25
25
  paddingRight) /
26
26
  showItems;
27
27
  setComputedValues({ itemWidth, gap });
28
- }, [showItems, containerElement]);
28
+ }, [showItems]);
29
+ useEffect(() => {
30
+ recalculateItemsSize();
31
+ window.addEventListener('resize', debounce(recalculateItemsSize, 100));
32
+ return () => window.removeEventListener('resize', recalculateItemsSize);
33
+ }, [recalculateItemsSize]);
29
34
  const itemsTrackerRef = useRef(null);
30
35
  function hideNonVisibleItems() {
31
36
  var _a;
@@ -56,9 +61,6 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
56
61
  hideNonVisibleItems();
57
62
  showVisibleItems(scrollBy, page, showItems);
58
63
  }, [page, scrollBy, showItems]);
59
- useWindowWidthChange((change) => {
60
- setComputedValues(computedValues => (Object.assign(Object.assign({}, computedValues), { itemWidth: computedValues.itemWidth - change })));
61
- });
62
64
  const [drag, setDrag] = useState({
63
65
  initial: transform,
64
66
  start: 0,
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.2.14-preview-95fbee92.0",
7
+ "version": "0.2.14-preview-ff8280b4.0",
8
8
  "sideEffects": [
9
9
  "*.css",
10
10
  "*.woff",
@@ -36,7 +36,8 @@
36
36
  "@snack-uikit/pagination": "0.6.9",
37
37
  "@snack-uikit/utils": "3.3.0",
38
38
  "classnames": "2.3.2",
39
+ "lodash.debounce": "4.0.8",
39
40
  "uncontrollable": "8.0.4"
40
41
  },
41
- "gitHead": "6cc00922d59ee9d6b574beb9c7009bb95c36331a"
42
+ "gitHead": "ce078dfe9d48a2e6d36d34b1eff4fdb5db996d83"
42
43
  }
@@ -1,8 +1,8 @@
1
- import { MouseEvent, ReactElement, TouchEvent, useEffect, useMemo, useRef, useState } from 'react';
1
+ import debounce from 'lodash.debounce';
2
+ import { MouseEvent, ReactElement, TouchEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
3
 
3
4
  import { TEST_IDS } from '../../testIds';
4
5
  import { getPageX } from './helpers';
5
- import { useWindowWidthChange } from './hooks';
6
6
  import styles from './styles.module.scss';
7
7
 
8
8
  export type ItemProviderProps = {
@@ -33,9 +33,9 @@ export function ItemProvider({
33
33
  gap: 0,
34
34
  });
35
35
 
36
- const containerElement = containerRef.current;
36
+ const recalculateItemsSize = useCallback(() => {
37
+ const containerElement = containerRef.current;
37
38
 
38
- useEffect(() => {
39
39
  if (!containerElement) {
40
40
  return;
41
41
  }
@@ -53,7 +53,13 @@ export function ItemProvider({
53
53
  paddingRight) /
54
54
  showItems;
55
55
  setComputedValues({ itemWidth, gap });
56
- }, [showItems, containerElement]);
56
+ }, [showItems]);
57
+
58
+ useEffect(() => {
59
+ recalculateItemsSize();
60
+ window.addEventListener('resize', debounce(recalculateItemsSize, 100));
61
+ return () => window.removeEventListener('resize', recalculateItemsSize);
62
+ }, [recalculateItemsSize]);
57
63
 
58
64
  const itemsTrackerRef = useRef<HTMLDivElement>(null);
59
65
 
@@ -92,10 +98,6 @@ export function ItemProvider({
92
98
  showVisibleItems(scrollBy, page, showItems);
93
99
  }, [page, scrollBy, showItems]);
94
100
 
95
- useWindowWidthChange((change: number) => {
96
- setComputedValues(computedValues => ({ ...computedValues, itemWidth: computedValues.itemWidth - change }));
97
- });
98
-
99
101
  const [drag, setDrag] = useState({
100
102
  initial: transform,
101
103
  start: 0,
@@ -1 +0,0 @@
1
- export declare const useWindowWidthChange: (cb: (changed: number) => void) => void;
@@ -1,20 +0,0 @@
1
- import { useLayoutEffect, useState } from 'react';
2
- const getWindowWidth = () => {
3
- if (typeof window === 'undefined') {
4
- return 0;
5
- }
6
- return window.innerWidth;
7
- };
8
- export const useWindowWidthChange = (cb) => {
9
- const [windowWidth, setWindowWidth] = useState(getWindowWidth());
10
- useLayoutEffect(() => {
11
- const update = () => {
12
- const changed = windowWidth - window.innerWidth;
13
- setWindowWidth(window.innerWidth);
14
- cb(changed);
15
- };
16
- window.addEventListener('resize', update);
17
- return () => window.removeEventListener('resize', update);
18
- }, [cb, windowWidth]);
19
- return;
20
- };
@@ -1,26 +0,0 @@
1
- import { useLayoutEffect, useState } from 'react';
2
-
3
- const getWindowWidth = () => {
4
- if (typeof window === 'undefined') {
5
- return 0;
6
- }
7
-
8
- return window.innerWidth;
9
- };
10
-
11
- export const useWindowWidthChange = (cb: (changed: number) => void) => {
12
- const [windowWidth, setWindowWidth] = useState(getWindowWidth());
13
-
14
- useLayoutEffect(() => {
15
- const update = () => {
16
- const changed = windowWidth - window.innerWidth;
17
- setWindowWidth(window.innerWidth);
18
- cb(changed);
19
- };
20
-
21
- window.addEventListener('resize', update);
22
-
23
- return () => window.removeEventListener('resize', update);
24
- }, [cb, windowWidth]);
25
- return;
26
- };