summaryrefslogtreecommitdiff
path: root/src/lib/product/components/ProductSlider.jsx
blob: 931a8db71150d4e1652e762a8606e61fc7205dd2 (plain)
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
import { Swiper, SwiperSlide } from 'swiper/react'
import { FreeMode } from 'swiper'
import ProductCard from './ProductCard'
import 'swiper/css'
import Image from '@/core/components/elements/Image/Image'
import Link from '@/core/components/elements/Link/Link'
import { useRef } from 'react'
import useDevice from '@/core/hooks/useDevice'

const bannerClassName =
  'absolute rounded-r top-0 left-0 h-full w-auto md:w-[20%] idt-transition border border-gray_r-6'

const ProductSlider = ({ products, simpleTitle = false, bannerMode = false }) => {
  const bannerRef = useRef('')
  const { isMobile, isDesktop } = useDevice()

  const changeBannerOpacity = (swiper) => {
    if (!bannerMode) return
    const calculateOpacity = (132 + swiper.translate) / 100
    bannerRef.current.style = `opacity: ${calculateOpacity > 0 ? calculateOpacity : 0}`
  }

  return (
    <>
      {bannerMode && (
        <div ref={bannerRef}>
          <Image
            src={products.banner.image}
            alt={products.banner.name}
            style={{ opacity: 1 }}
            className={bannerClassName}
          />
        </div>
      )}
      {(isMobile || isDesktop) && (
        <Swiper
          freeMode={{ enabled: true, sticky: false }}
          slidesPerView={isMobile ? 2.2 : 5.6}
          spaceBetween={isMobile ? 8 : 16}
          onSliderMove={changeBannerOpacity}
          onSlideChangeTransitionEnd={changeBannerOpacity}
          onSlideChangeTransitionStart={changeBannerOpacity}
          prefix='product'
          modules={[FreeMode]}
        >
          {bannerMode && (
            <SwiperSlide>
              <Link href={products.banner.url} className='w-full h-full block'></Link>
            </SwiperSlide>
          )}
          {products?.products?.map((product, index) => (
            <SwiperSlide key={index}>
              <ProductCard product={product} simpleTitle={simpleTitle} />
            </SwiperSlide>
          ))}
        </Swiper>
      )}
    </>
  )
}

export default ProductSlider