/
index.tsx
145 lines (139 loc) · 4.53 KB
/
index.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
import React, { FC } from 'react';
import { Product } from 'shared/types/product/Product';
import { SwiperOptions } from 'swiper';
import Slider from 'components/commercetools-ui/atoms/slider';
import Subtitle, { SubtitleProps } from 'components/commercetools-ui/atoms/subtitle';
import Typography from 'components/commercetools-ui/atoms/typography';
import Wrapper from 'components/HOC/wrapper';
import useClassNames from 'helpers/hooks/useClassNames';
import useTouchDevice from 'helpers/hooks/useTouchDevice';
import { mediumDesktop, tablet } from 'helpers/utils/screensizes';
import { Reference } from 'types/reference';
import useTrack from './useTrack';
import Link from '../../content/link';
import Tile from '../product-tile';
export interface ProductSliderProps extends Partial<SwiperOptions> {
products: Product[];
title: string;
titleVariant?: 'xs' | 'sm' | 'lg';
subline?: string;
subtitleVariant?: SubtitleProps['variant'];
ctaLabel?: string;
ctaLink?: Reference;
clearDefaultWrapperStyles?: boolean;
slidesPerView?: number;
disableProductQuickView?: boolean;
disableProductWishlistButton?: boolean;
disableProductVariants?: boolean;
innerArrows?: boolean;
solidArrows?: boolean;
onProductClick?: (product: Product) => void;
classNames?: {
title?: string;
};
variant?: 'normal' | 'cart';
}
const ProductSlider: FC<ProductSliderProps> = ({
products,
title,
subline,
ctaLabel,
ctaLink,
onProductClick,
titleVariant = 'lg',
subtitleVariant = 'lg',
clearDefaultWrapperStyles = false,
disableProductQuickView = false,
disableProductWishlistButton = false,
disableProductVariants = false,
innerArrows = true,
solidArrows = true,
breakpoints = {},
classNames = {},
...props
}) => {
const { isTouchDevice } = useTouchDevice();
const { trackClick } = useTrack();
const titleClassName = useClassNames([
'mb-12 text-20',
classNames.title,
{
xs: 'md:text-16',
sm: 'md:text-18 lg:text-22',
lg: 'md:text-22 lg:text-28',
}[titleVariant ?? 'lg'],
]);
return (
<Wrapper background="neutral-200" clearDefaultStyles={clearDefaultWrapperStyles}>
<div>
<Typography className={titleClassName} as="h3">
{title}
</Typography>
{(subline || ctaLink) && (
<div className="mt-8 flex items-center justify-between md:mt-16 lg:mt-14">
{subline && <Subtitle subtitle={subline} variant={subtitleVariant} />}
{ctaLink && (
<div className="hidden lg:block">
<Link target={ctaLink} withArrow>
<span className="font-medium leading-[24px] text-secondary-black">{ctaLabel}</span>
</Link>
</div>
)}
</div>
)}
{ctaLink && (
<div className="mt-20 block md:mt-16 lg:hidden">
<Link target={ctaLink} withArrow>
<span className="font-medium leading-[24px] text-secondary-black">{ctaLabel}</span>
</Link>
</div>
)}
</div>
<div className="mt-20 md:mt-24 lg:mt-20">
<div className="relative mt-6 w-full">
<Slider
slidesPerView={isTouchDevice ? 2.3 : 1.3}
slidesPerGroup={1}
dots={false}
arrows
innerArrows={innerArrows}
solidArrows={solidArrows}
nextButtonStyles={{ transform: 'translateY(-150%)' }}
prevButtonStyles={{ transform: 'translateY(-150%)' }}
allowTouchMove
spaceBetween={8}
breakpoints={{
[tablet]: {
slidesPerView: 2.3,
spaceBetween: 25,
...(breakpoints[tablet] ?? {}),
},
[mediumDesktop]: {
slidesPerView: 4,
slidesPerGroup: 4,
spaceBetween: 25,
...(breakpoints[mediumDesktop] ?? {}),
},
}}
{...props}
>
{products.map((product, index) => (
<Tile
key={product.productId}
disableQuickView={disableProductQuickView}
disableWishlistButton={disableProductWishlistButton}
disableVariants={disableProductVariants}
product={product}
onClick={() => {
trackClick(product, index + 1);
onProductClick?.(product);
}}
/>
))}
</Slider>
</div>
</div>
</Wrapper>
);
};
export default ProductSlider;