-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathindex.tsx
More file actions
175 lines (155 loc) · 4.3 KB
/
index.tsx
File metadata and controls
175 lines (155 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React, {
FunctionComponent,
MouseEvent,
useState,
useRef,
useCallback,
ReactElement,
} from 'react';
import { Item, SlideDirection } from '../../types/carousel';
import styles from '../../styles/slider/styles.module.css';
import { getOuterWidth } from '../../helpers';
import { defaultProps } from './defaultProps';
export const ScrollingCarousel: FunctionComponent<SliderProps> = (
userProps: SliderProps,
) => {
const { children, className, leftIcon, rightIcon, triggerClickOn } = {
...defaultProps,
...userProps,
};
const slider = useRef<HTMLDivElement>(null);
const [isDown, setIsDown] = useState(false);
const [position, setPosition] = useState({
startX: 0,
scrollLeft: 0,
});
const showArrows = (): Arrows => {
const sliderElement = slider.current;
return {
left: !!sliderElement && sliderElement.scrollLeft > 0,
right:
!!sliderElement &&
sliderElement.scrollWidth >
sliderElement.scrollLeft + sliderElement.offsetWidth,
};
};
const [showArrow, setShowArrow] = useState<Arrows>(showArrows());
const onScroll = (_: Event) => {
setShowArrow(showArrows());
};
const ref = useCallback(
(node) => {
if (node !== null) {
Object.defineProperty(slider, 'current', { value: node });
setShowArrow(showArrows());
node.addEventListener('scroll', onScroll);
}
},
[slider, children],
);
const mouseDown = (e: MouseEvent) => {
setIsDown(true);
setPosition({
startX: e.pageX - slider.current!.offsetLeft,
scrollLeft: slider.current!.scrollLeft,
});
};
const mouseUp = (_: MouseEvent) => {
setIsDown(false);
setShowArrow(showArrows());
slider.current!.classList.remove(styles.sliding);
};
const mouseMove = (e: MouseEvent) => {
if (!isDown) return;
e.preventDefault();
const eventPosition = e.pageX - slider.current!.offsetLeft;
const slide = eventPosition - position.startX;
if (Math.abs(slide) > triggerClickOn) {
slider.current!.classList.add(styles.sliding);
}
slider.current!.scrollLeft = position.scrollLeft - slide;
};
const calculateSlideAmount = (direction: SlideDirection): number => {
const _slider = slider.current!;
const currentView =
direction === SlideDirection.Left
? _slider.scrollLeft + _slider.offsetWidth
: _slider.scrollLeft;
const childNodes = Array.from(_slider.children) as HTMLElement[];
let nodeWidthSum = 0;
for (const node of childNodes) {
const nodeWidth = getOuterWidth(node);
nodeWidthSum += nodeWidth;
if (nodeWidthSum >= currentView) {
const showingPart =
direction === SlideDirection.Left
? nodeWidthSum - currentView
: nodeWidth;
return (_slider.offsetWidth - showingPart) * direction;
}
}
return _slider.offsetWidth;
};
const slide = (direction: SlideDirection) => {
const slideAmount = calculateSlideAmount(direction);
const start = slider.current!.scrollLeft;
smoothHorizontalScroll(500, slideAmount, start);
};
const smoothHorizontalScroll = (time: number, amount: number, start: number) => {
let curTime = 0;
for (let scrollCounter = 0; curTime <= time; scrollCounter++) {
window.setTimeout(
smoothHorizontalScrollBehavior,
curTime,
(scrollCounter * amount) / 100 + start,
);
curTime += time / 100;
}
};
const smoothHorizontalScrollBehavior = (amount: number) => {
slider.current!.scrollLeft = amount;
};
const getArrow = (
direction: SlideDirection,
data: string,
element?: ReactElement,
) => {
return (
<div data-arrow={data} onClick={() => slide(direction)}>
{element ?? <button aria-label={`${data === 'right' ? 'Sağa' : 'Sola'} kaydır`} />}
</div>
);
};
return (
<div className={`${styles.sliderBase} ${className}`} data-testid="carousel">
{showArrow.left &&
leftIcon &&
getArrow(SlideDirection.Right, 'left', leftIcon)}
{showArrow.right &&
rightIcon &&
getArrow(SlideDirection.Left, 'right', rightIcon)}
<div
data-testid="sliderList"
ref={ref}
onMouseDown={mouseDown}
onMouseLeave={mouseUp}
onMouseUp={mouseUp}
onMouseMove={mouseMove}
className={styles.slider}
>
{children}
</div>
</div>
);
};
export interface SliderProps {
children: Item[];
className?: string;
leftIcon?: ReactElement | null;
rightIcon?: ReactElement | null;
triggerClickOn?: number;
}
export type Arrows = {
left: boolean;
right: boolean;
};