@snack-uikit/carousel 0.2.20 → 0.2.21-preview-e6983aad.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/Control/Control.js +1 -3
- package/dist/components/ItemProvider/ItemProvider.js +23 -12
- package/package.json +2 -2
- package/src/components/Control/Control.tsx +1 -3
- package/src/components/ItemProvider/ItemProvider.tsx +41 -13
- package/src/components/ItemProvider/styles.module.scss +1 -1
|
@@ -16,7 +16,5 @@ import { extractSupportProps } from '@snack-uikit/utils';
|
|
|
16
16
|
import styles from './styles.module.css';
|
|
17
17
|
export const Control = forwardRef((_a, ref) => {
|
|
18
18
|
var { onClick, variant } = _a, rest = __rest(_a, ["onClick", "variant"]);
|
|
19
|
-
return (_jsx("button", Object.assign({ ref: ref, className: styles.control, onClick: ()
|
|
20
|
-
onClick === null || onClick === void 0 ? void 0 : onClick();
|
|
21
|
-
}, type: 'button', "data-variant": variant }, extractSupportProps(rest), { children: _jsx(ChevronLeftSVG, { size: 24, className: styles.icon }) })));
|
|
19
|
+
return (_jsx("button", Object.assign({ ref: ref, className: styles.control, onClick: onClick, type: 'button', "data-variant": variant }, extractSupportProps(rest), { children: _jsx(ChevronLeftSVG, { size: 24, className: styles.icon }) })));
|
|
22
20
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import debounce from 'lodash.debounce';
|
|
3
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef, useState, } from 'react';
|
|
4
4
|
import { TEST_IDS } from '../../testIds';
|
|
5
5
|
import { getPageX } from './helpers';
|
|
6
6
|
import styles from './styles.module.css';
|
|
@@ -37,30 +37,28 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
|
|
|
37
37
|
return () => observer.disconnect();
|
|
38
38
|
}, [recalculateItemsSize]);
|
|
39
39
|
const itemsTrackerRef = useRef(null);
|
|
40
|
-
|
|
40
|
+
const hideNonVisibleItems = () => {
|
|
41
41
|
var _a;
|
|
42
|
-
(_a = itemsTrackerRef.current) === null || _a === void 0 ? void 0 : _a.querySelectorAll(`[data-test-id=${TEST_IDS.trackItem}]`).forEach(
|
|
42
|
+
(_a = itemsTrackerRef.current) === null || _a === void 0 ? void 0 : _a.querySelectorAll(`[data-test-id=${TEST_IDS.trackItem}]`).forEach(slide => {
|
|
43
43
|
slide.setAttribute('aria-hidden', '0');
|
|
44
|
-
slide.querySelectorAll('a, button, select, input, textarea, [tabindex="0"]').forEach(
|
|
44
|
+
slide.querySelectorAll('a, button, select, input, textarea, [tabindex="0"]').forEach(focusableElement => {
|
|
45
45
|
focusableElement.setAttribute('tabindex', '-5');
|
|
46
46
|
focusableElement.classList.add(styles.hiddenItem);
|
|
47
47
|
});
|
|
48
48
|
});
|
|
49
|
-
}
|
|
50
|
-
|
|
49
|
+
};
|
|
50
|
+
const showVisibleItems = (slide, page, show) => {
|
|
51
51
|
var _a;
|
|
52
52
|
(_a = itemsTrackerRef.current) === null || _a === void 0 ? void 0 : _a.querySelectorAll(`[data-test-id=${TEST_IDS.trackItem}]`).forEach((item, i) => {
|
|
53
53
|
if (i >= page * slide && i < page * slide + Math.trunc(show)) {
|
|
54
54
|
item.removeAttribute('aria-hidden');
|
|
55
|
-
item
|
|
56
|
-
.querySelectorAll('a, button, select, input, textarea, [tabindex="-5"]')
|
|
57
|
-
.forEach(function (focusableElement) {
|
|
55
|
+
item.querySelectorAll('a, button, select, input, textarea, [tabindex="-5"]').forEach(focusableElement => {
|
|
58
56
|
focusableElement.setAttribute('tabindex', '0');
|
|
59
57
|
focusableElement.classList.remove(styles.hiddenItem);
|
|
60
58
|
});
|
|
61
59
|
}
|
|
62
60
|
});
|
|
63
|
-
}
|
|
61
|
+
};
|
|
64
62
|
const transform = useMemo(() => Number(-(page * scrollBy * computedValues.itemWidth + computedValues.gap * page * scrollBy)), [computedValues.gap, computedValues.itemWidth, page, scrollBy]);
|
|
65
63
|
useEffect(() => {
|
|
66
64
|
hideNonVisibleItems();
|
|
@@ -117,8 +115,21 @@ export function ItemProvider({ items, showItems, scrollBy, slideCallback, transi
|
|
|
117
115
|
onMouseMove: handleDragMove,
|
|
118
116
|
}
|
|
119
117
|
: {};
|
|
120
|
-
|
|
118
|
+
const handleSlideClick = useCallback(e => {
|
|
119
|
+
var _a, _b;
|
|
120
|
+
const slideRect = e.currentTarget.getBoundingClientRect();
|
|
121
|
+
const containerRect = (_a = containerRef.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
|
|
122
|
+
const containerViewport = (_b = containerRect === null || containerRect === void 0 ? void 0 : containerRect.right) !== null && _b !== void 0 ? _b : 0;
|
|
123
|
+
const slidePositionDelta = containerViewport - slideRect.right;
|
|
124
|
+
const minPos = Math.min(slidePositionDelta, slideRect.x);
|
|
125
|
+
if (minPos < 0) {
|
|
126
|
+
slideCallback(slidePositionDelta);
|
|
127
|
+
}
|
|
128
|
+
}, [slideCallback]);
|
|
129
|
+
return (_jsx("div", { ref: containerRef, className: styles.itemProvider, "data-pointers": !drag.pointers || undefined, "data-swipe": swipe || undefined, "data-gap": gap, style: Object.assign({}, (Boolean(gap) ? { '--gap': gap } : {})), children: _jsx("div", Object.assign({}, swipeProps, { className: styles.itemTracker, "data-test-id": TEST_IDS.trackLine, style: {
|
|
121
130
|
transform: `translateX(${transform - drag.drag}px)`,
|
|
122
131
|
transition: `transform ${transition}s ease 0s`,
|
|
123
|
-
}, ref: itemsTrackerRef, children: items.map((item, i) => (
|
|
132
|
+
}, ref: itemsTrackerRef, children: items.map((item, i) => (
|
|
133
|
+
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
|
|
134
|
+
_jsx("div", { style: { width: computedValues.itemWidth }, className: styles.itemContainer, role: 'group', "data-test-id": TEST_IDS.trackItem, onClick: handleSlideClick, children: item }, i))) })) }));
|
|
124
135
|
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.2.
|
|
7
|
+
"version": "0.2.21-preview-e6983aad.0",
|
|
8
8
|
"sideEffects": [
|
|
9
9
|
"*.css",
|
|
10
10
|
"*.woff",
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"lodash.debounce": "4.0.8",
|
|
40
40
|
"uncontrollable": "8.0.4"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "b0f78be1b5a0b02fcd7c8fed0c0f39f912bf0f30"
|
|
43
43
|
}
|
|
@@ -15,9 +15,7 @@ export const Control = forwardRef<HTMLButtonElement, ControlProps>(
|
|
|
15
15
|
<button
|
|
16
16
|
ref={ref}
|
|
17
17
|
className={styles.control}
|
|
18
|
-
onClick={
|
|
19
|
-
onClick?.();
|
|
20
|
-
}}
|
|
18
|
+
onClick={onClick}
|
|
21
19
|
type='button'
|
|
22
20
|
data-variant={variant}
|
|
23
21
|
{...extractSupportProps(rest)}
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import debounce from 'lodash.debounce';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
MouseEvent,
|
|
4
|
+
MouseEventHandler,
|
|
5
|
+
ReactElement,
|
|
6
|
+
TouchEvent,
|
|
7
|
+
useCallback,
|
|
8
|
+
useEffect,
|
|
9
|
+
useMemo,
|
|
10
|
+
useRef,
|
|
11
|
+
useState,
|
|
12
|
+
} from 'react';
|
|
3
13
|
|
|
4
14
|
import { TEST_IDS } from '../../testIds';
|
|
5
15
|
import { getPageX } from './helpers';
|
|
@@ -73,30 +83,28 @@ export function ItemProvider({
|
|
|
73
83
|
|
|
74
84
|
const itemsTrackerRef = useRef<HTMLDivElement>(null);
|
|
75
85
|
|
|
76
|
-
|
|
77
|
-
itemsTrackerRef.current?.querySelectorAll(`[data-test-id=${TEST_IDS.trackItem}]`).forEach(
|
|
86
|
+
const hideNonVisibleItems = () => {
|
|
87
|
+
itemsTrackerRef.current?.querySelectorAll(`[data-test-id=${TEST_IDS.trackItem}]`).forEach(slide => {
|
|
78
88
|
slide.setAttribute('aria-hidden', '0');
|
|
79
89
|
|
|
80
|
-
slide.querySelectorAll('a, button, select, input, textarea, [tabindex="0"]').forEach(
|
|
90
|
+
slide.querySelectorAll('a, button, select, input, textarea, [tabindex="0"]').forEach(focusableElement => {
|
|
81
91
|
focusableElement.setAttribute('tabindex', '-5');
|
|
82
92
|
focusableElement.classList.add(styles.hiddenItem);
|
|
83
93
|
});
|
|
84
94
|
});
|
|
85
|
-
}
|
|
95
|
+
};
|
|
86
96
|
|
|
87
|
-
|
|
97
|
+
const showVisibleItems = (slide: number, page: number, show: number) => {
|
|
88
98
|
itemsTrackerRef.current?.querySelectorAll(`[data-test-id=${TEST_IDS.trackItem}]`).forEach((item, i) => {
|
|
89
99
|
if (i >= page * slide && i < page * slide + Math.trunc(show)) {
|
|
90
100
|
item.removeAttribute('aria-hidden');
|
|
91
|
-
item
|
|
92
|
-
.
|
|
93
|
-
.
|
|
94
|
-
|
|
95
|
-
focusableElement.classList.remove(styles.hiddenItem);
|
|
96
|
-
});
|
|
101
|
+
item.querySelectorAll('a, button, select, input, textarea, [tabindex="-5"]').forEach(focusableElement => {
|
|
102
|
+
focusableElement.setAttribute('tabindex', '0');
|
|
103
|
+
focusableElement.classList.remove(styles.hiddenItem);
|
|
104
|
+
});
|
|
97
105
|
}
|
|
98
106
|
});
|
|
99
|
-
}
|
|
107
|
+
};
|
|
100
108
|
|
|
101
109
|
const transform = useMemo(
|
|
102
110
|
() => Number(-(page * scrollBy * computedValues.itemWidth + computedValues.gap * page * scrollBy)),
|
|
@@ -130,9 +138,11 @@ export function ItemProvider({
|
|
|
130
138
|
|
|
131
139
|
const handleDragFinish = (e: MouseEvent | TouchEvent) => {
|
|
132
140
|
e.persist();
|
|
141
|
+
|
|
133
142
|
if (drag.finished) {
|
|
134
143
|
return;
|
|
135
144
|
}
|
|
145
|
+
|
|
136
146
|
if (Math.abs(drag.drag) < swipeActivateLength) {
|
|
137
147
|
return setDrag({
|
|
138
148
|
initial: transform,
|
|
@@ -178,12 +188,28 @@ export function ItemProvider({
|
|
|
178
188
|
}
|
|
179
189
|
: {};
|
|
180
190
|
|
|
191
|
+
const handleSlideClick: MouseEventHandler = useCallback(
|
|
192
|
+
e => {
|
|
193
|
+
const slideRect = e.currentTarget.getBoundingClientRect();
|
|
194
|
+
const containerRect = containerRef.current?.getBoundingClientRect();
|
|
195
|
+
const containerViewport = containerRect?.right ?? 0;
|
|
196
|
+
const slidePositionDelta = containerViewport - slideRect.right;
|
|
197
|
+
const minPos = Math.min(slidePositionDelta, slideRect.x);
|
|
198
|
+
|
|
199
|
+
if (minPos < 0) {
|
|
200
|
+
slideCallback(slidePositionDelta);
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
[slideCallback],
|
|
204
|
+
);
|
|
205
|
+
|
|
181
206
|
return (
|
|
182
207
|
<div
|
|
183
208
|
ref={containerRef}
|
|
184
209
|
className={styles.itemProvider}
|
|
185
210
|
data-pointers={!drag.pointers || undefined}
|
|
186
211
|
data-swipe={swipe || undefined}
|
|
212
|
+
data-gap={gap}
|
|
187
213
|
style={{
|
|
188
214
|
...(Boolean(gap) ? { '--gap': gap } : {}),
|
|
189
215
|
}}
|
|
@@ -199,12 +225,14 @@ export function ItemProvider({
|
|
|
199
225
|
ref={itemsTrackerRef}
|
|
200
226
|
>
|
|
201
227
|
{items.map((item, i) => (
|
|
228
|
+
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
|
|
202
229
|
<div
|
|
203
230
|
key={i}
|
|
204
231
|
style={{ width: computedValues.itemWidth }}
|
|
205
232
|
className={styles.itemContainer}
|
|
206
233
|
role='group'
|
|
207
234
|
data-test-id={TEST_IDS.trackItem}
|
|
235
|
+
onClick={handleSlideClick}
|
|
208
236
|
>
|
|
209
237
|
{item}
|
|
210
238
|
</div>
|