@snack-uikit/carousel 0.4.18 → 0.5.1-preview-9917f1e7.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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # 0.5.0 (2025-03-29)
7
+
8
+
9
+ ### Features
10
+
11
+ * **PDS-1119:** carousel autoswipe ([ff064ac](https://github.com/cloud-ru-tech/snack-uikit/commit/ff064acc23974c47acf2e1c3a52638fd482fecdb))
12
+
13
+
14
+
15
+
16
+
6
17
  ## 0.4.18 (2025-03-19)
7
18
 
8
19
  ### Only dependencies have been changed
package/README.md CHANGED
@@ -18,6 +18,7 @@
18
18
  | scrollBy | `number` | Math.trunc(show) | Сдвиг айтемов при смене 1 страницы |
19
19
  | transition | `number` | 0.4 | Время переключения 1 страницы (в s) |
20
20
  | swipe | `boolean` | true | Переключение страниц свайпом |
21
+ | autoSwipe | `number` | - | Автоматическое переключение слайдов в секундах |
21
22
  | swipeActivateLength | `number` | 48 | Минимальная длина в px для активации свайпа |
22
23
  | arrows | `boolean` | true | Использовать стрелки для переключения страниц |
23
24
  | pagination | `boolean` | true | Использовать пагинацию для переключения страниц |
@@ -13,6 +13,8 @@ export type CarouselProps = WithSupportProps<{
13
13
  transition?: number;
14
14
  /** Переключение страниц свайпом @default true */
15
15
  swipe?: boolean;
16
+ /** Автоматическое переключение слайдов в секундах */
17
+ autoSwipe?: number;
16
18
  /** Минимальная длина в px для активации свайпа @default 48 */
17
19
  swipeActivateLength?: number;
18
20
  /** Использовать стрелки для переключения страниц @default true */
@@ -29,4 +31,4 @@ export type CarouselProps = WithSupportProps<{
29
31
  /** Цикличная прокрутка @default false*/
30
32
  infiniteScroll?: boolean;
31
33
  }>;
32
- export declare function Carousel({ children: items, showItems, scrollBy: scrollByProp, transition, swipe, arrows, pagination, className, gap, state, infiniteScroll, swipeActivateLength, ...rest }: CarouselProps): import("react/jsx-runtime").JSX.Element;
34
+ export declare function Carousel({ children: items, showItems, scrollBy: scrollByProp, transition, swipe, arrows, pagination, className, gap, state, infiniteScroll, swipeActivateLength, autoSwipe, ...rest }: CarouselProps): import("react/jsx-runtime").JSX.Element;
@@ -41,9 +41,11 @@ function Carousel(_a) {
41
41
  gap,
42
42
  state,
43
43
  infiniteScroll = false,
44
- swipeActivateLength = 48
44
+ swipeActivateLength = 48,
45
+ autoSwipe
45
46
  } = _a,
46
- rest = __rest(_a, ["children", "showItems", "scrollBy", "transition", "swipe", "arrows", "pagination", "className", "gap", "state", "infiniteScroll", "swipeActivateLength"]);
47
+ rest = __rest(_a, ["children", "showItems", "scrollBy", "transition", "swipe", "arrows", "pagination", "className", "gap", "state", "infiniteScroll", "swipeActivateLength", "autoSwipe"]);
48
+ const timerRef = (0, react_1.useRef)();
47
49
  const scrollBy = (0, react_1.useMemo)(() => scrollByProp !== null && scrollByProp !== void 0 ? scrollByProp : Math.trunc(showItems), [showItems, scrollByProp]);
48
50
  const [page, setPage] = (0, uncontrollable_1.useUncontrolledProp)(state === null || state === void 0 ? void 0 : state.page, (_b = state === null || state === void 0 ? void 0 : state.page) !== null && _b !== void 0 ? _b : 0, newPage => {
49
51
  const result = typeof newPage === 'function' ? newPage(page) : newPage;
@@ -68,6 +70,13 @@ function Carousel(_a) {
68
70
  }
69
71
  onLeftArrowClick();
70
72
  }, [onLeftArrowClick, onRightArrowClick]);
73
+ (0, react_1.useEffect)(() => {
74
+ if (!autoSwipe || !infiniteScroll) {
75
+ return;
76
+ }
77
+ timerRef.current = setTimeout(() => onRightArrowClick(), autoSwipe * 1000);
78
+ return () => clearTimeout(timerRef.current);
79
+ }, [autoSwipe, infiniteScroll, onRightArrowClick]);
71
80
  return (0, jsx_runtime_1.jsxs)("div", Object.assign({
72
81
  className: (0, classnames_1.default)(styles_module_scss_1.default.carousel, className)
73
82
  }, (0, utils_1.extractSupportProps)(rest), {
@@ -96,6 +96,9 @@ function ItemProvider(_ref) {
96
96
  }, [page, scrollBy, showItems]);
97
97
  const handleSlideClick = (0, react_1.useCallback)(e => {
98
98
  var _a, _b;
99
+ if (showItems === 1) {
100
+ return;
101
+ }
99
102
  const slideRect = e.currentTarget.getBoundingClientRect();
100
103
  const containerRect = (_a = containerRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
101
104
  const containerViewport = (_b = containerRect === null || containerRect === void 0 ? void 0 : containerRect.right) !== null && _b !== void 0 ? _b : 0;
@@ -104,7 +107,7 @@ function ItemProvider(_ref) {
104
107
  if (minPos < 0) {
105
108
  slideCallback(slidePositionDelta);
106
109
  }
107
- }, [slideCallback]);
110
+ }, [showItems, slideCallback]);
108
111
  const itemWidth = (0, react_1.useMemo)(() => {
109
112
  if (computedValues.itemWidth > 0) {
110
113
  return computedValues.itemWidth;
@@ -13,6 +13,8 @@ export type CarouselProps = WithSupportProps<{
13
13
  transition?: number;
14
14
  /** Переключение страниц свайпом @default true */
15
15
  swipe?: boolean;
16
+ /** Автоматическое переключение слайдов в секундах */
17
+ autoSwipe?: number;
16
18
  /** Минимальная длина в px для активации свайпа @default 48 */
17
19
  swipeActivateLength?: number;
18
20
  /** Использовать стрелки для переключения страниц @default true */
@@ -29,4 +31,4 @@ export type CarouselProps = WithSupportProps<{
29
31
  /** Цикличная прокрутка @default false*/
30
32
  infiniteScroll?: boolean;
31
33
  }>;
32
- export declare function Carousel({ children: items, showItems, scrollBy: scrollByProp, transition, swipe, arrows, pagination, className, gap, state, infiniteScroll, swipeActivateLength, ...rest }: CarouselProps): import("react/jsx-runtime").JSX.Element;
34
+ export declare function Carousel({ children: items, showItems, scrollBy: scrollByProp, transition, swipe, arrows, pagination, className, gap, state, infiniteScroll, swipeActivateLength, autoSwipe, ...rest }: CarouselProps): import("react/jsx-runtime").JSX.Element;
@@ -11,7 +11,7 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  };
12
12
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
13
13
  import cn from 'classnames';
14
- import { useCallback, useMemo } from 'react';
14
+ import { useCallback, useEffect, useMemo, useRef } from 'react';
15
15
  import { useUncontrolledProp } from 'uncontrollable';
16
16
  import { PaginationSlider } from '@snack-uikit/pagination';
17
17
  import { extractSupportProps } from '@snack-uikit/utils';
@@ -21,7 +21,8 @@ import { ItemProvider } from '../ItemProvider';
21
21
  import styles from './styles.module.css';
22
22
  export function Carousel(_a) {
23
23
  var _b;
24
- var { children: items, showItems = 1, scrollBy: scrollByProp, transition = 0.4, swipe = true, arrows = true, pagination = true, className, gap, state, infiniteScroll = false, swipeActivateLength = 48 } = _a, rest = __rest(_a, ["children", "showItems", "scrollBy", "transition", "swipe", "arrows", "pagination", "className", "gap", "state", "infiniteScroll", "swipeActivateLength"]);
24
+ var { children: items, showItems = 1, scrollBy: scrollByProp, transition = 0.4, swipe = true, arrows = true, pagination = true, className, gap, state, infiniteScroll = false, swipeActivateLength = 48, autoSwipe } = _a, rest = __rest(_a, ["children", "showItems", "scrollBy", "transition", "swipe", "arrows", "pagination", "className", "gap", "state", "infiniteScroll", "swipeActivateLength", "autoSwipe"]);
25
+ const timerRef = useRef();
25
26
  const scrollBy = useMemo(() => scrollByProp !== null && scrollByProp !== void 0 ? scrollByProp : Math.trunc(showItems), [showItems, scrollByProp]);
26
27
  const [page, setPage] = useUncontrolledProp(state === null || state === void 0 ? void 0 : state.page, (_b = state === null || state === void 0 ? void 0 : state.page) !== null && _b !== void 0 ? _b : 0, newPage => {
27
28
  const result = typeof newPage === 'function' ? newPage(page) : newPage;
@@ -46,6 +47,13 @@ export function Carousel(_a) {
46
47
  }
47
48
  onLeftArrowClick();
48
49
  }, [onLeftArrowClick, onRightArrowClick]);
50
+ useEffect(() => {
51
+ if (!autoSwipe || !infiniteScroll) {
52
+ return;
53
+ }
54
+ timerRef.current = setTimeout(() => onRightArrowClick(), autoSwipe * 1000);
55
+ return () => clearTimeout(timerRef.current);
56
+ }, [autoSwipe, infiniteScroll, onRightArrowClick]);
49
57
  return (_jsxs("div", Object.assign({ className: cn(styles.carousel, className) }, extractSupportProps(rest), { children: [_jsxs("div", { className: styles.carouselBase, children: [_jsx(ItemProvider, { showItems: showItems, scrollBy: scrollBy, swipe: swipe, transition: transition, items: items, slideCallback: slideCallback, page: page, gap: gap, swipeActivateLength: swipeActivateLength }), arrows && (_jsxs(_Fragment, { children: [(infiniteScroll || page > 0) && (_jsx(Control, { onClick: onLeftArrowClick, variant: 'prev', "data-test-id": TEST_IDS.arrowPrev })), (infiniteScroll || page + 1 < total) && (_jsx(Control, { onClick: onRightArrowClick, variant: 'next', "data-test-id": TEST_IDS.arrowNext }))] }))] }), pagination && (_jsx("div", { className: styles.pagination, children: _jsx(PaginationSlider, { "data-test-id": TEST_IDS.pagination, page: page + 1, onChange: (page) => {
50
58
  setPage(page - 1);
51
59
  }, total: total }) }))] })));
@@ -78,6 +78,9 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
78
78
  }, [page, scrollBy, showItems]);
79
79
  const handleSlideClick = useCallback(e => {
80
80
  var _a, _b;
81
+ if (showItems === 1) {
82
+ return;
83
+ }
81
84
  const slideRect = e.currentTarget.getBoundingClientRect();
82
85
  const containerRect = (_a = containerRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
83
86
  const containerViewport = (_b = containerRect === null || containerRect === void 0 ? void 0 : containerRect.right) !== null && _b !== void 0 ? _b : 0;
@@ -86,7 +89,7 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
86
89
  if (minPos < 0) {
87
90
  slideCallback(slidePositionDelta);
88
91
  }
89
- }, [slideCallback]);
92
+ }, [showItems, slideCallback]);
90
93
  const itemWidth = useMemo(() => {
91
94
  if (computedValues.itemWidth > 0) {
92
95
  return computedValues.itemWidth;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.4.18",
7
+ "version": "0.5.1-preview-9917f1e7.0",
8
8
  "sideEffects": [
9
9
  "*.css",
10
10
  "*.woff",
@@ -44,5 +44,5 @@
44
44
  "merge-refs": "1.3.0",
45
45
  "uncontrollable": "8.0.4"
46
46
  },
47
- "gitHead": "e14c6ec6fbc59b086b7ed348403eb1bac740b9c3"
47
+ "gitHead": "70c9a8daac46a4a93689ec8471a674f7a84df430"
48
48
  }
@@ -1,5 +1,5 @@
1
1
  import cn from 'classnames';
2
- import { ReactElement, useCallback, useMemo } from 'react';
2
+ import { ReactElement, useCallback, useEffect, useMemo, useRef } from 'react';
3
3
  import { useUncontrolledProp } from 'uncontrollable';
4
4
 
5
5
  import { PaginationSlider } from '@snack-uikit/pagination';
@@ -23,6 +23,8 @@ export type CarouselProps = WithSupportProps<{
23
23
  transition?: number;
24
24
  /** Переключение страниц свайпом @default true */
25
25
  swipe?: boolean;
26
+ /** Автоматическое переключение слайдов в секундах */
27
+ autoSwipe?: number;
26
28
  /** Минимальная длина в px для активации свайпа @default 48 */
27
29
  swipeActivateLength?: number;
28
30
  /** Использовать стрелки для переключения страниц @default true */
@@ -53,8 +55,11 @@ export function Carousel({
53
55
  state,
54
56
  infiniteScroll = false,
55
57
  swipeActivateLength = 48,
58
+ autoSwipe,
56
59
  ...rest
57
60
  }: CarouselProps) {
61
+ const timerRef = useRef<NodeJS.Timeout>();
62
+
58
63
  const scrollBy = useMemo(() => scrollByProp ?? Math.trunc(showItems), [showItems, scrollByProp]);
59
64
 
60
65
  const [page, setPage] = useUncontrolledProp<number>(state?.page, state?.page ?? 0, newPage => {
@@ -89,6 +94,16 @@ export function Carousel({
89
94
  [onLeftArrowClick, onRightArrowClick],
90
95
  );
91
96
 
97
+ useEffect(() => {
98
+ if (!autoSwipe || !infiniteScroll) {
99
+ return;
100
+ }
101
+
102
+ timerRef.current = setTimeout(() => onRightArrowClick(), autoSwipe * 1000);
103
+
104
+ return () => clearTimeout(timerRef.current);
105
+ }, [autoSwipe, infiniteScroll, onRightArrowClick]);
106
+
92
107
  return (
93
108
  <div className={cn(styles.carousel, className)} {...extractSupportProps(rest)}>
94
109
  <div className={styles.carouselBase}>
@@ -110,6 +110,10 @@ export function ItemProvider({
110
110
 
111
111
  const handleSlideClick: MouseEventHandler = useCallback(
112
112
  e => {
113
+ if (showItems === 1) {
114
+ return;
115
+ }
116
+
113
117
  const slideRect = e.currentTarget.getBoundingClientRect();
114
118
  const containerRect = containerRef.current?.getBoundingClientRect();
115
119
  const containerViewport = containerRect?.right ?? 0;
@@ -120,7 +124,7 @@ export function ItemProvider({
120
124
  slideCallback(slidePositionDelta);
121
125
  }
122
126
  },
123
- [slideCallback],
127
+ [showItems, slideCallback],
124
128
  );
125
129
 
126
130
  const itemWidth = useMemo(() => {