|
@@ -1,4 +1,13 @@
|
|
-import React, { ComponentPropsWithoutRef, ReactNode, RefObject, forwardRef, useImperativeHandle, useRef } from 'react'
|
|
|
|
|
|
+import Glider from 'glider-js'
|
|
|
|
+import React, {
|
|
|
|
+ ComponentPropsWithoutRef,
|
|
|
|
+ ReactNode,
|
|
|
|
+ RefObject,
|
|
|
|
+ forwardRef,
|
|
|
|
+ useEffect,
|
|
|
|
+ useImperativeHandle,
|
|
|
|
+ useRef,
|
|
|
|
+} from 'react'
|
|
|
|
|
|
import { Container, Dots, GliderContainer, Track } from './Carousel.style'
|
|
import { Container, Dots, GliderContainer, Track } from './Carousel.style'
|
|
|
|
|
|
@@ -31,6 +40,27 @@ export const Carousel = forwardRef<
|
|
ref
|
|
ref
|
|
) => {
|
|
) => {
|
|
const dotsRef = useRef<HTMLDivElement>(null)
|
|
const dotsRef = useRef<HTMLDivElement>(null)
|
|
|
|
+ const gliderInstanceRef = useRef<Glider.Static<HTMLElement>>()
|
|
|
|
+ const slidesToScrollRef = useRef<number>(0)
|
|
|
|
+
|
|
|
|
+ const onAnimated = () => {
|
|
|
|
+ if (gliderInstanceRef.current && gliderOptions.responsive) {
|
|
|
|
+ const breakpointIndex = gliderOptions.responsive.findIndex(
|
|
|
|
+ (item) => item.breakpoint === gliderInstanceRef.current?.breakpoint
|
|
|
|
+ )
|
|
|
|
+ const slidesToScroll = gliderOptions.responsive[breakpointIndex].settings.slidesToScroll as number
|
|
|
|
+ const itemsRemainder = gliderInstanceRef.current.slides.length % slidesToScrollRef.current || slidesToScroll
|
|
|
|
+ if (nextArrowRef.current && nextArrowRef.current?.classList.contains('disabled') && itemsRemainder) {
|
|
|
|
+ gliderInstanceRef.current.setOption({ slidesToScroll: itemsRemainder }, false)
|
|
|
|
+ } else {
|
|
|
|
+ gliderInstanceRef.current.setOption({ slidesToScroll: slidesToScrollRef.current || slidesToScroll }, false)
|
|
|
|
+ if (!slidesToScrollRef.current) {
|
|
|
|
+ slidesToScrollRef.current = slidesToScroll
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
const {
|
|
const {
|
|
ref: gliderRef,
|
|
ref: gliderRef,
|
|
getContainerProps,
|
|
getContainerProps,
|
|
@@ -39,13 +69,32 @@ export const Carousel = forwardRef<
|
|
getPrevArrowProps,
|
|
getPrevArrowProps,
|
|
getNextArrowProps,
|
|
getNextArrowProps,
|
|
getDotsProps,
|
|
getDotsProps,
|
|
|
|
+ glider,
|
|
} = useGlider<HTMLDivElement>({
|
|
} = useGlider<HTMLDivElement>({
|
|
slidesToShow,
|
|
slidesToShow,
|
|
|
|
+ onAnimated,
|
|
arrows: { prev: prevArrowRef.current, next: nextArrowRef.current },
|
|
arrows: { prev: prevArrowRef.current, next: nextArrowRef.current },
|
|
dots: dotsRef.current,
|
|
dots: dotsRef.current,
|
|
...gliderOptions,
|
|
...gliderOptions,
|
|
})
|
|
})
|
|
|
|
|
|
|
|
+ const resetSlidesToScroll = () => {
|
|
|
|
+ slidesToScrollRef.current = 0
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ window.addEventListener('resize', resetSlidesToScroll)
|
|
|
|
+
|
|
|
|
+ return () => {
|
|
|
|
+ window.removeEventListener('resize', resetSlidesToScroll)
|
|
|
|
+ }
|
|
|
|
+ }, [])
|
|
|
|
+
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ if (!glider) return
|
|
|
|
+ gliderInstanceRef.current = glider
|
|
|
|
+ }, [glider])
|
|
|
|
+
|
|
useImperativeHandle(ref, () => ({
|
|
useImperativeHandle(ref, () => ({
|
|
getPrevArrowProps,
|
|
getPrevArrowProps,
|
|
getNextArrowProps,
|
|
getNextArrowProps,
|