@snack-uikit/carousel 0.2.14-preview-95fbee92.0 → 0.2.14-preview-a4b7f13c.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.
- package/dist/components/ItemProvider/ItemProvider.js +15 -8
- package/package.json +3 -2
- package/src/components/ItemProvider/ItemProvider.tsx +19 -9
- package/dist/components/ItemProvider/hooks.d.ts +0 -1
- package/dist/components/ItemProvider/hooks.js +0 -20
- package/src/components/ItemProvider/hooks.ts +0 -26
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import
|
|
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
|
|
14
|
-
|
|
13
|
+
const recalculateItemsSize = useCallback(() => {
|
|
14
|
+
const containerElement = containerRef.current;
|
|
15
15
|
if (!containerElement) {
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
@@ -25,7 +25,17 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
|
|
|
25
25
|
paddingRight) /
|
|
26
26
|
showItems;
|
|
27
27
|
setComputedValues({ itemWidth, gap });
|
|
28
|
-
}, [showItems
|
|
28
|
+
}, [showItems]);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const node = containerRef.current;
|
|
31
|
+
if (!node) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
recalculateItemsSize();
|
|
35
|
+
const observer = new ResizeObserver(debounce(recalculateItemsSize, 100));
|
|
36
|
+
observer.observe(node);
|
|
37
|
+
return () => observer.disconnect();
|
|
38
|
+
}, [recalculateItemsSize]);
|
|
29
39
|
const itemsTrackerRef = useRef(null);
|
|
30
40
|
function hideNonVisibleItems() {
|
|
31
41
|
var _a;
|
|
@@ -56,9 +66,6 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
|
|
|
56
66
|
hideNonVisibleItems();
|
|
57
67
|
showVisibleItems(scrollBy, page, showItems);
|
|
58
68
|
}, [page, scrollBy, showItems]);
|
|
59
|
-
useWindowWidthChange((change) => {
|
|
60
|
-
setComputedValues(computedValues => (Object.assign(Object.assign({}, computedValues), { itemWidth: computedValues.itemWidth - change })));
|
|
61
|
-
});
|
|
62
69
|
const [drag, setDrag] = useState({
|
|
63
70
|
initial: transform,
|
|
64
71
|
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-
|
|
7
|
+
"version": "0.2.14-preview-a4b7f13c.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": "
|
|
42
|
+
"gitHead": "fbb9cb273b770568f1e0887bed83995b081b3351"
|
|
42
43
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
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
|
|
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,21 @@ export function ItemProvider({
|
|
|
53
53
|
paddingRight) /
|
|
54
54
|
showItems;
|
|
55
55
|
setComputedValues({ itemWidth, gap });
|
|
56
|
-
}, [showItems
|
|
56
|
+
}, [showItems]);
|
|
57
|
+
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
const node = containerRef.current;
|
|
60
|
+
|
|
61
|
+
if (!node) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
recalculateItemsSize();
|
|
66
|
+
|
|
67
|
+
const observer = new ResizeObserver(debounce(recalculateItemsSize, 100));
|
|
68
|
+
observer.observe(node);
|
|
69
|
+
return () => observer.disconnect();
|
|
70
|
+
}, [recalculateItemsSize]);
|
|
57
71
|
|
|
58
72
|
const itemsTrackerRef = useRef<HTMLDivElement>(null);
|
|
59
73
|
|
|
@@ -92,10 +106,6 @@ export function ItemProvider({
|
|
|
92
106
|
showVisibleItems(scrollBy, page, showItems);
|
|
93
107
|
}, [page, scrollBy, showItems]);
|
|
94
108
|
|
|
95
|
-
useWindowWidthChange((change: number) => {
|
|
96
|
-
setComputedValues(computedValues => ({ ...computedValues, itemWidth: computedValues.itemWidth - change }));
|
|
97
|
-
});
|
|
98
|
-
|
|
99
109
|
const [drag, setDrag] = useState({
|
|
100
110
|
initial: transform,
|
|
101
111
|
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
|
-
};
|