generated from eea/volto-addon-template
/
Carousel.jsx
183 lines (167 loc) · 4.56 KB
/
Carousel.jsx
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
176
177
178
179
180
181
182
183
import React from 'react';
import { Button, Icon } from 'semantic-ui-react';
import loadable from '@loadable/component';
import ResponsiveContainer from '@eeacms/volto-listing-block/components/ResponsiveContainer';
import 'slick-carousel/slick/slick.css';
import 'slick-carousel/slick/slick-theme.css';
import UniversalCard from '@eeacms/volto-listing-block/components/UniversalCard/UniversalCard';
const Slider = loadable(() => import('react-slick'));
const tabletBreakpoint = 768;
const mobileLargeBreakpoint = 767;
const mobileBreakpoint = 480;
const getSlidesToShow = (items, _slidesToShow) => {
if (_slidesToShow <= 0) return 1;
if (items.length >= _slidesToShow) return parseInt(_slidesToShow);
return items.length;
};
const getSlidesToScroll = (items, _slidesToShow, _slidesToScroll) => {
if (_slidesToScroll <= 0) return 1;
const slidesToShow = getSlidesToShow(items, _slidesToShow);
if (slidesToShow >= _slidesToScroll) return parseInt(_slidesToScroll);
return slidesToShow;
};
const PrevArrow = (props) => {
const { onClick } = props;
return (
<Button
aria-label="Previous slide"
className="slider-arrow prev-arrow tablet or lower hidden"
icon
onClick={onClick}
>
<Icon className="ri-arrow-left-s-line" />
</Button>
);
};
const NextArrow = (props) => {
const { className, onClick } = props;
return (
<Button
aria-label="Next slide"
className={
(className.indexOf('slick-disabled') !== -1 ? 'slick-disabled' : '') +
' slider-arrow next-arrow tablet or lower hidden'
}
icon
onClick={onClick}
>
<Icon className="ri-arrow-right-s-line" />
</Button>
);
};
const CardsCarousel = ({ block, items, ...rest }) => {
const slider = React.useRef(null);
const dots_parent = React.useRef(null);
const slidesToShow = getSlidesToShow(items, rest.slidesToShow || 4);
const itemsLength = items.length;
const settings = {
dots: itemsLength > 1,
infinite: true,
arrows: itemsLength > slidesToShow,
initialSlide: 0,
lazyLoad: null,
slidesToShow: slidesToShow,
slidesToScroll: getSlidesToScroll(
items,
rest.slidesToShow || 4,
rest.slidesToScroll || 1,
),
nextArrow: <NextArrow />,
prevArrow: <PrevArrow />,
customPaging: (i) => (
<button className={'slider-dots-button'} aria-current={i === 0}>
<span className="slick-dot-icon" aria-hidden="true" />
<span className="slick-sr-only">Go to slide {i + 1}</span>
</button>
),
appendDots: (dots) => (
<ul ref={dots_parent} className={'slick-dots'}>
{dots}
</ul>
),
afterChange: () => {
const dots = dots_parent.current;
if (dots) {
dots.querySelectorAll('.slider-dots-button').forEach(function (el) {
el.setAttribute(
'aria-current',
el.parentElement.className === 'slick-active',
);
});
}
},
responsive: [
{
breakpoint: tabletBreakpoint,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
},
},
{
breakpoint: mobileLargeBreakpoint,
settings: {
slidesToShow: 2,
slidesToScroll: 2,
},
},
{
breakpoint: mobileBreakpoint,
settings: {
slidesToShow: 1,
slidesToScroll: 1,
},
},
],
};
return itemsLength > 0 ? (
<ResponsiveContainer>
{({ parentWidth }) => (
<div
className="cards-carousel"
role={'region'}
aria-label={'carousel'}
style={{ '--carousel-max-width': `${parentWidth}px` }}
>
<Slider {...settings} ref={slider}>
{items.map((item, index) => (
<UniversalCard
key={`card-${block}-${index}`}
{...rest}
block={block}
item={item}
/>
))}
</Slider>
</div>
)}
</ResponsiveContainer>
) : null;
};
CardsCarousel.schemaEnhancer = (args) => {
const { schema } = args;
schema.fieldsets.splice(1, 0, {
id: 'carousel',
title: 'Carousel',
fields: ['slidesToShow', 'slidesToScroll'],
});
return {
...schema,
properties: {
...schema.properties,
slidesToShow: {
title: 'Slides to show',
type: 'number',
default: 4,
minimum: 1,
},
slidesToScroll: {
title: 'Slides to scroll',
type: 'number',
default: 1,
minimum: 1,
},
},
};
};
export default CardsCarousel;