-
Notifications
You must be signed in to change notification settings - Fork 26
/
Carousel.tsx
175 lines (162 loc) · 4.46 KB
/
Carousel.tsx
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 from 'react';
import styled, { CSSObject } from 'styled-components';
import { SpaceProps } from '@heathmont/moon-themes';
import IconChevronLeft from '../private/icons/IconChevronLeft';
import Reel from '../reel/Reel';
import { withHorizontalScroll } from '../withHorizontalScroll/withHorizontalScroll';
import { CarouselControl, CarouselControlCaption } from './control';
/**
* Settings
*
* If a browser supports the new scroll snap specification, use the styles
* and disable legacy spec (keyed with 👴)
*/
const currentScrollingSpec = `@supports (scroll-snap-align: start)`;
/**
* Carousel
*
* The outer Carousel container, allowing absolute positioning inside.
*
* `<any>` justification: https://coingaming.atlassian.net/browse/SPO-4963.
*/
const CarouselWrapper = styled.div<any>({
position: 'relative',
width: '100%',
});
/**
* Carousel Scroll
*
* The list of Carousel Items, controlling overflow and scroll behavior.
*/
const ItemsScrollWrapper = styled(Reel.withComponent('ul'))({
scrollSnapType: 'mandatory',
scrollSnapDestination: '0 50%',
scrollSnapPointsX: 'repeat(100%)',
[currentScrollingSpec]: {
scrollSnapType: 'x mandatory',
scrollSnapDestination: 'unset' /* 👴 */,
scrollSnapPointsX: 'unset' /* 👴 */,
},
});
ItemsScrollWrapper.defaultProps = {
space: 'default',
};
/**
* Carousel Scroll Item
*
* The individual `list-item` of Carousel Scroll
*/
const CarouselScrollItem = styled.li<any>({
scrollSnapCoordinate: '0 0',
[currentScrollingSpec]: {
scrollSnapAlign: 'center',
scrollSnapCoordinate: 'unset' /* 👴 */,
},
});
const DefaultScrollToLeftButton = ({
scrollLeftCaption = 'Scroll Left',
disabled = false,
...rest
}) =>
disabled ? null : (
<CarouselControl {...rest}>
<CarouselControlCaption>{scrollLeftCaption}</CarouselControlCaption>
<IconChevronLeft />
</CarouselControl>
);
const DefaultScrollToRightButton = ({
scrollRightCaption = 'Scroll Right',
disabled = false,
...rest
}) =>
disabled ? null : (
<CarouselControl {...rest}>
<CarouselControlCaption>{scrollRightCaption}</CarouselControlCaption>
<IconChevronLeft />
</CarouselControl>
);
type CarouselProps = {
items: any[];
scrollToLeftButton?: any;
scrollToRightButton?: any;
scrollLeftCaption?: any;
scrollRightCaption?: any;
step?: number;
scrollTo?: number;
space?: SpaceProps | CSSObject['margin'];
};
const Carousel: React.FC<CarouselProps> = ({
items,
step,
scrollTo,
scrollToLeftButton,
scrollToRightButton,
scrollLeftCaption,
scrollRightCaption,
space,
}) => {
const {
itemRef,
scrollLeftToStep,
scrollRightToStep,
containerRef,
scrollToIndex,
canScrollLeft,
canScrollRight,
} = withHorizontalScroll({ scrollStep: step || 5 });
React.useEffect(() => {
if (!scrollTo) {
return;
}
// We scroll for another extra item because we defined our THRESHOLD = 0.75;
// It means that item will be visible for 75%.
// We scroll one more to guarantee 100% visibility.
// "items.length - 1" because indices start from 0.
if (scrollTo && scrollTo < items.length - 1) {
scrollToIndex(scrollTo + 1);
}
// No point for scroll another extra item because that's the last one
if (scrollTo && scrollTo === items.length - 1) {
scrollToIndex(scrollTo);
}
}, []);
return (
<CarouselWrapper>
{scrollToLeftButton ? (
scrollToLeftButton({
scrollToStep: scrollLeftToStep,
disabled: !canScrollLeft,
})
) : (
<DefaultScrollToLeftButton
onClick={scrollLeftToStep}
disabled={!canScrollLeft}
scrollLeftCaption={scrollLeftCaption}
/>
)}
<ItemsScrollWrapper space={space} ref={containerRef}>
{items.map(
(item: React.ReactNode, index: string | number | undefined) => (
// eslint-disable-next-line react/no-array-index-key
<CarouselScrollItem key={index} ref={itemRef}>
{item}
</CarouselScrollItem>
)
)}
</ItemsScrollWrapper>
{scrollToRightButton ? (
scrollToRightButton({
scrollToStep: scrollRightToStep,
disabled: !canScrollRight,
})
) : (
<DefaultScrollToRightButton
onClick={scrollRightToStep}
disabled={!canScrollRight}
scrollRightCaption={scrollRightCaption}
/>
)}
</CarouselWrapper>
);
};
export default Carousel;