@snack-uikit/carousel 0.2.13 → 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,27 +1,36 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
+ import debounce from 'lodash.debounce';
2
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
+ const containerRef = useRef(null);
8
9
  const [computedValues, setComputedValues] = useState({
9
10
  itemWidth: 200,
10
11
  gap: 0,
11
12
  });
12
- const ref = useCallback((node) => {
13
- if (node !== null) {
14
- const styles = getComputedStyle(node);
15
- const gap = Number.parseFloat(styles.getPropertyValue('--gap'));
16
- const paddingLeft = Number.parseFloat(styles.getPropertyValue('padding-left'));
17
- const paddingRight = Number.parseFloat(styles.getPropertyValue('padding-right'));
18
- const itemWidth = (node.getBoundingClientRect().width - (Math.trunc(showItems) - 1) * gap - paddingLeft - paddingRight) /
19
- showItems;
20
- setComputedValues({ itemWidth, gap });
13
+ const recalculateItemsSize = useCallback(() => {
14
+ const containerElement = containerRef.current;
15
+ if (!containerElement) {
16
+ return;
21
17
  }
22
- },
23
- // eslint-disable-next-line react-hooks/exhaustive-deps
24
- [computedValues.itemWidth, showItems]);
18
+ const styles = getComputedStyle(containerElement);
19
+ const gap = Number.parseFloat(styles.getPropertyValue('--gap'));
20
+ const paddingLeft = Number.parseFloat(styles.getPropertyValue('padding-left'));
21
+ const paddingRight = Number.parseFloat(styles.getPropertyValue('padding-right'));
22
+ const itemWidth = (containerElement.getBoundingClientRect().width -
23
+ (Math.trunc(showItems) - 1) * gap -
24
+ paddingLeft -
25
+ paddingRight) /
26
+ showItems;
27
+ setComputedValues({ itemWidth, gap });
28
+ }, [showItems]);
29
+ useEffect(() => {
30
+ recalculateItemsSize();
31
+ window.addEventListener('resize', debounce(recalculateItemsSize, 100));
32
+ return () => window.removeEventListener('resize', recalculateItemsSize);
33
+ }, [recalculateItemsSize]);
25
34
  const itemsTrackerRef = useRef(null);
26
35
  function hideNonVisibleItems() {
27
36
  var _a;
@@ -52,9 +61,6 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
52
61
  hideNonVisibleItems();
53
62
  showVisibleItems(scrollBy, page, showItems);
54
63
  }, [page, scrollBy, showItems]);
55
- useWindowWidthChange((change) => {
56
- setComputedValues(computedValues => (Object.assign(Object.assign({}, computedValues), { itemWidth: computedValues.itemWidth - change })));
57
- });
58
64
  const [drag, setDrag] = useState({
59
65
  initial: transform,
60
66
  start: 0,
@@ -106,7 +112,7 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
106
112
  onMouseMove: handleDragMove,
107
113
  }
108
114
  : {};
109
- return (_jsx("div", { ref: ref, className: styles.itemProvider, "data-pointers": !drag.pointers || undefined, "data-swipe": swipe || undefined, style: Object.assign({}, (Boolean(gap) ? { '--gap': gap } : {})), children: _jsx("div", Object.assign({}, swipeProps, { className: styles.itemTracker, "data-test-id": TEST_IDS.trackLine, style: {
115
+ return (_jsx("div", { ref: containerRef, className: styles.itemProvider, "data-pointers": !drag.pointers || undefined, "data-swipe": swipe || undefined, style: Object.assign({}, (Boolean(gap) ? { '--gap': gap } : {})), children: _jsx("div", Object.assign({}, swipeProps, { className: styles.itemTracker, "data-test-id": TEST_IDS.trackLine, style: {
110
116
  transform: `translateX(${transform - drag.drag}px)`,
111
117
  transition: `transform ${transition}s ease 0s`,
112
118
  }, ref: itemsTrackerRef, children: items.map((item, i) => (_jsx("div", { style: { width: computedValues.itemWidth }, className: styles.itemContainer, role: 'group', "data-test-id": TEST_IDS.trackItem, children: item }, i))) })) }));
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.2.13",
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": "d19da5dedbb93af1e6d86330b6f2590ef117a7c9"
42
+ "gitHead": "ce078dfe9d48a2e6d36d34b1eff4fdb5db996d83"
42
43
  }
@@ -1,8 +1,8 @@
1
+ import debounce from 'lodash.debounce';
1
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 = {
@@ -26,29 +26,40 @@ export function ItemProvider({
26
26
  page,
27
27
  gap,
28
28
  }: ItemProviderProps) {
29
+ const containerRef = useRef<HTMLDivElement>(null);
30
+
29
31
  const [computedValues, setComputedValues] = useState({
30
32
  itemWidth: 200,
31
33
  gap: 0,
32
34
  });
33
35
 
34
- const ref = useCallback(
35
- (node: HTMLDivElement) => {
36
- if (node !== null) {
37
- const styles = getComputedStyle(node);
36
+ const recalculateItemsSize = useCallback(() => {
37
+ const containerElement = containerRef.current;
38
+
39
+ if (!containerElement) {
40
+ return;
41
+ }
38
42
 
39
- const gap = Number.parseFloat(styles.getPropertyValue('--gap'));
40
- const paddingLeft = Number.parseFloat(styles.getPropertyValue('padding-left'));
41
- const paddingRight = Number.parseFloat(styles.getPropertyValue('padding-right'));
43
+ const styles = getComputedStyle(containerElement);
42
44
 
43
- const itemWidth =
44
- (node.getBoundingClientRect().width - (Math.trunc(showItems) - 1) * gap - paddingLeft - paddingRight) /
45
- showItems;
46
- setComputedValues({ itemWidth, gap });
47
- }
48
- },
49
- // eslint-disable-next-line react-hooks/exhaustive-deps
50
- [computedValues.itemWidth, showItems],
51
- );
45
+ const gap = Number.parseFloat(styles.getPropertyValue('--gap'));
46
+ const paddingLeft = Number.parseFloat(styles.getPropertyValue('padding-left'));
47
+ const paddingRight = Number.parseFloat(styles.getPropertyValue('padding-right'));
48
+
49
+ const itemWidth =
50
+ (containerElement.getBoundingClientRect().width -
51
+ (Math.trunc(showItems) - 1) * gap -
52
+ paddingLeft -
53
+ paddingRight) /
54
+ showItems;
55
+ setComputedValues({ itemWidth, gap });
56
+ }, [showItems]);
57
+
58
+ useEffect(() => {
59
+ recalculateItemsSize();
60
+ window.addEventListener('resize', debounce(recalculateItemsSize, 100));
61
+ return () => window.removeEventListener('resize', recalculateItemsSize);
62
+ }, [recalculateItemsSize]);
52
63
 
53
64
  const itemsTrackerRef = useRef<HTMLDivElement>(null);
54
65
 
@@ -87,10 +98,6 @@ export function ItemProvider({
87
98
  showVisibleItems(scrollBy, page, showItems);
88
99
  }, [page, scrollBy, showItems]);
89
100
 
90
- useWindowWidthChange((change: number) => {
91
- setComputedValues(computedValues => ({ ...computedValues, itemWidth: computedValues.itemWidth - change }));
92
- });
93
-
94
101
  const [drag, setDrag] = useState({
95
102
  initial: transform,
96
103
  start: 0,
@@ -163,7 +170,7 @@ export function ItemProvider({
163
170
 
164
171
  return (
165
172
  <div
166
- ref={ref}
173
+ ref={containerRef}
167
174
  className={styles.itemProvider}
168
175
  data-pointers={!drag.pointers || undefined}
169
176
  data-swipe={swipe || undefined}
@@ -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
- };