@cloud-ru/ds-carousel 1.0.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 +8 -0
- package/LICENSE +201 -0
- package/README.md +124 -0
- package/dist/cjs/Carousel.d.ts +75 -0
- package/dist/cjs/Carousel.js +55 -0
- package/dist/cjs/constants.d.ts +12 -0
- package/dist/cjs/constants.js +15 -0
- package/dist/cjs/helperComponents/Control/Control.d.ts +7 -0
- package/dist/cjs/helperComponents/Control/Control.js +14 -0
- package/dist/cjs/helperComponents/Control/index.d.ts +1 -0
- package/dist/cjs/helperComponents/Control/index.js +17 -0
- package/dist/cjs/helperComponents/Control/styles.module.css +24 -0
- package/dist/cjs/helperComponents/ItemProvider/ItemProvider.d.ts +12 -0
- package/dist/cjs/helperComponents/ItemProvider/ItemProvider.js +130 -0
- package/dist/cjs/helperComponents/ItemProvider/index.d.ts +1 -0
- package/dist/cjs/helperComponents/ItemProvider/index.js +17 -0
- package/dist/cjs/helperComponents/ItemProvider/styles.module.css +44 -0
- package/dist/cjs/helperComponents/index.d.ts +2 -0
- package/dist/cjs/helperComponents/index.js +18 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +19 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/style.css +100 -0
- package/dist/cjs/styles.module.css +32 -0
- package/dist/cjs/types.d.ts +2 -0
- package/dist/cjs/types.js +2 -0
- package/dist/esm/Carousel.d.ts +75 -0
- package/dist/esm/Carousel.js +49 -0
- package/dist/esm/constants.d.ts +12 -0
- package/dist/esm/constants.js +12 -0
- package/dist/esm/helperComponents/Control/Control.d.ts +7 -0
- package/dist/esm/helperComponents/Control/Control.js +8 -0
- package/dist/esm/helperComponents/Control/index.d.ts +1 -0
- package/dist/esm/helperComponents/Control/index.js +1 -0
- package/dist/esm/helperComponents/Control/styles.module.css +24 -0
- package/dist/esm/helperComponents/ItemProvider/ItemProvider.d.ts +12 -0
- package/dist/esm/helperComponents/ItemProvider/ItemProvider.js +124 -0
- package/dist/esm/helperComponents/ItemProvider/index.d.ts +1 -0
- package/dist/esm/helperComponents/ItemProvider/index.js +1 -0
- package/dist/esm/helperComponents/ItemProvider/styles.module.css +44 -0
- package/dist/esm/helperComponents/index.d.ts +2 -0
- package/dist/esm/helperComponents/index.js +2 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/style.css +100 -0
- package/dist/esm/styles.module.css +32 -0
- package/dist/esm/types.d.ts +2 -0
- package/dist/esm/types.js +1 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -0
- package/package.json +46 -0
- package/src/Carousel.tsx +207 -0
- package/src/constants.ts +13 -0
- package/src/helperComponents/Control/Control.tsx +36 -0
- package/src/helperComponents/Control/index.ts +1 -0
- package/src/helperComponents/Control/styles.module.scss +27 -0
- package/src/helperComponents/ItemProvider/ItemProvider.tsx +220 -0
- package/src/helperComponents/ItemProvider/index.ts +1 -0
- package/src/helperComponents/ItemProvider/styles.module.scss +55 -0
- package/src/helperComponents/index.ts +2 -0
- package/src/index.ts +3 -0
- package/src/styles.module.scss +42 -0
- package/src/types.ts +3 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { debounce, SwipeCallback, useLayoutEffect, useSwipeable } from '@cloud-ru/ds-utils';
|
|
2
|
+
import mergeRefs from 'merge-refs';
|
|
3
|
+
import {
|
|
4
|
+
Children,
|
|
5
|
+
MouseEventHandler,
|
|
6
|
+
PropsWithChildren,
|
|
7
|
+
useCallback,
|
|
8
|
+
useEffect,
|
|
9
|
+
useMemo,
|
|
10
|
+
useRef,
|
|
11
|
+
useState,
|
|
12
|
+
} from 'react';
|
|
13
|
+
|
|
14
|
+
import { TEST_IDS } from '../../constants';
|
|
15
|
+
import styles from './styles.module.scss';
|
|
16
|
+
|
|
17
|
+
export type ItemProviderProps = PropsWithChildren<{
|
|
18
|
+
showItems: number;
|
|
19
|
+
scrollBy: number;
|
|
20
|
+
slideCallback(direction: number): void;
|
|
21
|
+
transition: number;
|
|
22
|
+
swipe: boolean;
|
|
23
|
+
swipeActivateLength: number;
|
|
24
|
+
page: number;
|
|
25
|
+
gap?: string;
|
|
26
|
+
}>;
|
|
27
|
+
|
|
28
|
+
function parseCssLength(value: string): number {
|
|
29
|
+
const parsed = Number.parseFloat(value);
|
|
30
|
+
|
|
31
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const hideNonVisibleItems = (trackerElement: HTMLDivElement) => {
|
|
35
|
+
trackerElement?.querySelectorAll(`[data-test-id=${TEST_IDS.trackItem}]`).forEach(slide => {
|
|
36
|
+
slide.setAttribute('aria-hidden', '0');
|
|
37
|
+
|
|
38
|
+
slide.querySelectorAll('a, button, select, input, textarea, [tabindex="0"]').forEach(focusableElement => {
|
|
39
|
+
focusableElement.setAttribute('tabindex', '-5');
|
|
40
|
+
focusableElement.classList.add(styles.hiddenItem);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const showVisibleItems = (trackerElement: HTMLDivElement, slide: number, page: number, show: number) => {
|
|
46
|
+
trackerElement.querySelectorAll(`[data-test-id=${TEST_IDS.trackItem}]`).forEach((item, i) => {
|
|
47
|
+
if (i >= page * slide && i < page * slide + Math.trunc(show)) {
|
|
48
|
+
item.removeAttribute('aria-hidden');
|
|
49
|
+
item.querySelectorAll('a, button, select, input, textarea, [tabindex="-5"]').forEach(focusableElement => {
|
|
50
|
+
focusableElement.setAttribute('tabindex', '0');
|
|
51
|
+
focusableElement.classList.remove(styles.hiddenItem);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export function ItemProvider({
|
|
58
|
+
children,
|
|
59
|
+
showItems,
|
|
60
|
+
scrollBy,
|
|
61
|
+
slideCallback,
|
|
62
|
+
transition,
|
|
63
|
+
swipe,
|
|
64
|
+
swipeActivateLength,
|
|
65
|
+
page,
|
|
66
|
+
gap,
|
|
67
|
+
}: ItemProviderProps) {
|
|
68
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
69
|
+
const itemsTrackerRef = useRef<HTMLDivElement>(null);
|
|
70
|
+
|
|
71
|
+
const [computedValues, setComputedValues] = useState({
|
|
72
|
+
itemWidth: 0,
|
|
73
|
+
gap: 0,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const recalculateItemsSize = useCallback(() => {
|
|
77
|
+
const containerElement = containerRef.current;
|
|
78
|
+
const trackerElement = itemsTrackerRef.current;
|
|
79
|
+
|
|
80
|
+
if (!containerElement || !trackerElement) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const containerStyles = getComputedStyle(containerElement);
|
|
85
|
+
const trackerStyles = getComputedStyle(trackerElement);
|
|
86
|
+
|
|
87
|
+
const gapValue = gap ? parseCssLength(gap) : parseCssLength(trackerStyles.columnGap || trackerStyles.gap);
|
|
88
|
+
const paddingLeft = parseCssLength(containerStyles.paddingLeft);
|
|
89
|
+
const paddingRight = parseCssLength(containerStyles.paddingRight);
|
|
90
|
+
|
|
91
|
+
const gapsCount = Math.max(Math.trunc(showItems) - 1, 0);
|
|
92
|
+
const contentWidth = containerElement.getBoundingClientRect().width - paddingLeft - paddingRight;
|
|
93
|
+
const itemWidth = (contentWidth - gapsCount * gapValue) / showItems;
|
|
94
|
+
|
|
95
|
+
setComputedValues({ itemWidth, gap: gapValue });
|
|
96
|
+
}, [showItems, gap]);
|
|
97
|
+
|
|
98
|
+
useLayoutEffect(() => {
|
|
99
|
+
const node = containerRef.current;
|
|
100
|
+
|
|
101
|
+
if (!node) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
recalculateItemsSize();
|
|
106
|
+
|
|
107
|
+
const observer = new ResizeObserver(debounce(recalculateItemsSize, 100));
|
|
108
|
+
observer.observe(node);
|
|
109
|
+
return () => observer.disconnect();
|
|
110
|
+
}, [recalculateItemsSize]);
|
|
111
|
+
|
|
112
|
+
const transform = useMemo(
|
|
113
|
+
() => Number(-(page * scrollBy * computedValues.itemWidth + computedValues.gap * page * scrollBy)),
|
|
114
|
+
[computedValues.gap, computedValues.itemWidth, page, scrollBy],
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
const trackerElement = itemsTrackerRef.current;
|
|
119
|
+
if (!trackerElement) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
hideNonVisibleItems(trackerElement);
|
|
124
|
+
showVisibleItems(trackerElement, scrollBy, page, showItems);
|
|
125
|
+
}, [page, scrollBy, showItems]);
|
|
126
|
+
|
|
127
|
+
const handleSlideClick: MouseEventHandler = useCallback(
|
|
128
|
+
e => {
|
|
129
|
+
if (showItems === 1) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const slideRect = e.currentTarget.getBoundingClientRect();
|
|
134
|
+
const containerRect = containerRef.current?.getBoundingClientRect();
|
|
135
|
+
const containerViewport = containerRect?.right ?? 0;
|
|
136
|
+
const slidePositionDelta = containerViewport - slideRect.right;
|
|
137
|
+
const minPos = Math.min(slidePositionDelta, slideRect.x);
|
|
138
|
+
|
|
139
|
+
if (minPos < 0) {
|
|
140
|
+
slideCallback(slidePositionDelta);
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
[showItems, slideCallback],
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const itemWidth = useMemo(() => {
|
|
147
|
+
if (computedValues.itemWidth > 0) {
|
|
148
|
+
return computedValues.itemWidth;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const fallbackWidth = Math.floor(100 / showItems);
|
|
152
|
+
return `${fallbackWidth}%`;
|
|
153
|
+
}, [computedValues.itemWidth, showItems]);
|
|
154
|
+
|
|
155
|
+
const [drag, setDrag] = useState({ drag: 0, pointers: true });
|
|
156
|
+
|
|
157
|
+
const handleSwiping: SwipeCallback = eventData => {
|
|
158
|
+
setDrag({ drag: eventData.deltaX, pointers: Math.abs(eventData.deltaX) < Number.MIN_SAFE_INTEGER });
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const handleSwiped: SwipeCallback = eventData => {
|
|
162
|
+
if (Math.abs(drag.drag) < swipeActivateLength) {
|
|
163
|
+
return setDrag({ drag: 0, pointers: true });
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
slideCallback(eventData.dir === 'Left' ? -1 : 1);
|
|
167
|
+
setDrag({ drag: 0, pointers: true });
|
|
168
|
+
return;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const { ref, ...swipeProps } = useSwipeable({
|
|
172
|
+
onSwiping: handleSwiping,
|
|
173
|
+
onSwiped: handleSwiped,
|
|
174
|
+
trackMouse: true,
|
|
175
|
+
enabled: swipe,
|
|
176
|
+
availableDirections: ['Left', 'Right'],
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<div
|
|
181
|
+
ref={containerRef}
|
|
182
|
+
className={styles.itemProvider}
|
|
183
|
+
data-pointers={!drag.pointers || undefined}
|
|
184
|
+
data-swipe={swipe || undefined}
|
|
185
|
+
data-gap={gap}
|
|
186
|
+
style={{
|
|
187
|
+
...(gap ? { '--gap': gap } : {}),
|
|
188
|
+
}}
|
|
189
|
+
>
|
|
190
|
+
<div
|
|
191
|
+
{...swipeProps}
|
|
192
|
+
className={styles.itemTracker}
|
|
193
|
+
data-test-id={TEST_IDS.trackLine}
|
|
194
|
+
// Гасим нативный HTML5 drag контента слайдов (`<img>` и т.п.): при mouse-swipe
|
|
195
|
+
// браузер иначе стартует перетаскивание картинки-призрака вместо переключения
|
|
196
|
+
// слайда. `-webkit-user-drag` на родителе не наследуется дочерними img.
|
|
197
|
+
onDragStart={event => event.preventDefault()}
|
|
198
|
+
style={{
|
|
199
|
+
transform: `translateX(${transform + drag.drag}px)`,
|
|
200
|
+
transition: `transform ${transition}s ease 0s`,
|
|
201
|
+
}}
|
|
202
|
+
ref={mergeRefs(ref as (element: HTMLDivElement) => void, itemsTrackerRef)}
|
|
203
|
+
>
|
|
204
|
+
{Children.toArray(children).map((item, i) => (
|
|
205
|
+
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
|
|
206
|
+
<div
|
|
207
|
+
key={i}
|
|
208
|
+
style={{ width: itemWidth }}
|
|
209
|
+
className={styles.itemContainer}
|
|
210
|
+
role='group'
|
|
211
|
+
data-test-id={TEST_IDS.trackItem}
|
|
212
|
+
onClick={handleSlideClick}
|
|
213
|
+
>
|
|
214
|
+
{item}
|
|
215
|
+
</div>
|
|
216
|
+
))}
|
|
217
|
+
</div>
|
|
218
|
+
</div>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ItemProvider';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
@use '@cloud-ru/ds-figma-variables/build/scss/styles/styles.module' as base;
|
|
2
|
+
@use '@cloud-ru/ds-figma-variables/build/scss/components/carousel.module' as carousel;
|
|
3
|
+
|
|
4
|
+
.itemProvider {
|
|
5
|
+
--gap: #{base.$sn-primitive-dimension-16};
|
|
6
|
+
|
|
7
|
+
// Vertical inset only (Figma content). Inline padding created a gutter where the
|
|
8
|
+
// next slide painted through when gap < padding — overflow clips at the padding edge.
|
|
9
|
+
// Slide outline/border is contained by .itemContainer padding instead.
|
|
10
|
+
@include base.composite-var(carousel.$carousel, 'anatomy', 'contentWrapper', 'content');
|
|
11
|
+
|
|
12
|
+
overflow-x: hidden;
|
|
13
|
+
display: flex;
|
|
14
|
+
flex: 1 0 0;
|
|
15
|
+
align-items: flex-start;
|
|
16
|
+
|
|
17
|
+
box-sizing: border-box;
|
|
18
|
+
width: 100%;
|
|
19
|
+
// Must come after composite-var, otherwise token paddings override this.
|
|
20
|
+
padding-inline: 0;
|
|
21
|
+
|
|
22
|
+
&[data-swipe] {
|
|
23
|
+
cursor: grab;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
&[data-swipe][data-pointers] {
|
|
27
|
+
cursor: grabbing;
|
|
28
|
+
user-select: none;
|
|
29
|
+
|
|
30
|
+
-webkit-user-drag: none;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.itemContainer {
|
|
35
|
+
flex-shrink: 0;
|
|
36
|
+
box-sizing: border-box;
|
|
37
|
+
// Reserve space inside the slide for outline/border so it is not clipped by
|
|
38
|
+
// overflow and does not paint into a shared gutter with the next slide.
|
|
39
|
+
padding: base.$sn-primitive-strokeWeight-strokeRegular;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.itemTracker {
|
|
43
|
+
display: flex;
|
|
44
|
+
gap: var(--gap, base.$sn-primitive-dimension-16);
|
|
45
|
+
box-sizing: border-box;
|
|
46
|
+
height: 100%;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.hiddenItem {
|
|
50
|
+
pointer-events: none;
|
|
51
|
+
|
|
52
|
+
* {
|
|
53
|
+
pointer-events: none;
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
@use '@cloud-ru/ds-figma-variables/build/scss/styles/styles.module' as base;
|
|
2
|
+
@use '@cloud-ru/ds-figma-variables/build/scss/components/carousel.module' as carousel;
|
|
3
|
+
|
|
4
|
+
.control {
|
|
5
|
+
pointer-events: none;
|
|
6
|
+
opacity: 0;
|
|
7
|
+
transition: opacity 0.2s;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.carousel {
|
|
11
|
+
position: relative;
|
|
12
|
+
|
|
13
|
+
display: flex;
|
|
14
|
+
flex-direction: column;
|
|
15
|
+
align-items: center;
|
|
16
|
+
|
|
17
|
+
box-sizing: border-box;
|
|
18
|
+
width: 100%;
|
|
19
|
+
|
|
20
|
+
&[data-controls-visibility="always"],
|
|
21
|
+
&[data-controls-visibility="hover"]:hover {
|
|
22
|
+
& .control {
|
|
23
|
+
pointer-events: auto;
|
|
24
|
+
opacity: 1;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.pagination {
|
|
30
|
+
@include base.composite-var(carousel.$carousel, 'anatomy', 'sliderWrapper');
|
|
31
|
+
|
|
32
|
+
display: flex;
|
|
33
|
+
justify-content: center;
|
|
34
|
+
box-sizing: border-box;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.carouselBase {
|
|
38
|
+
position: relative;
|
|
39
|
+
box-sizing: border-box;
|
|
40
|
+
width: 100%;
|
|
41
|
+
outline: none;
|
|
42
|
+
}
|
package/src/types.ts
ADDED