|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, { useCallback, useLayoutEffect, useMemo, useRef } from 'react' |
| 4 | +import { CarouselConfig, DefaultCarouselConfig } from './defaults'; |
| 5 | +import { throttledDebounce } from '../../utils'; |
| 6 | +import { CarouselButtonProps, CarouselContainer, CarouselContainerProps, CarouselControlProps, CarouselControls, CarouselItem, CarouselItemProps, CarouselNextButton, CarouselPreviousButton } from './CarouselComponents'; |
| 7 | + |
| 8 | +export interface CarouselContextType { |
| 9 | + containerRef: React.RefObject<HTMLDivElement>; |
| 10 | + totalCarouselItems: number; |
| 11 | + goToNextSlide: () => void; |
| 12 | + goToPreviousSlide: () => void; |
| 13 | + possibleDirection: { |
| 14 | + canGoToNextSlide: boolean; |
| 15 | + canGoToPreviousSlide: boolean; |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +const CarouselContext = React.createContext<CarouselContextType | null>(null) |
| 20 | + |
| 21 | +export const useCarousel = () => { |
| 22 | + const context = React.useContext(CarouselContext) |
| 23 | + if (!context) { |
| 24 | + throw new Error('useCarousel must be used within a CarouselProvider') |
| 25 | + } |
| 26 | + return context |
| 27 | +} |
| 28 | + |
| 29 | +export interface CarouselProviderProps { |
| 30 | + children: React.ReactNode; |
| 31 | + containerRef: React.RefObject<HTMLDivElement>; |
| 32 | + config?: CarouselConfig; |
| 33 | +} |
| 34 | + |
| 35 | +const CarouselProvider: React.FC<CarouselProviderProps> = ({ children, containerRef, config = DefaultCarouselConfig }) => { |
| 36 | + const {stepWidthInPercent} = config; |
| 37 | + |
| 38 | + const [carouselWidth, setCarouselWidth] = React.useState(0); |
| 39 | + const [scrollableWidth, setScrollableWidth] = React.useState(0); |
| 40 | + const [scrollLeft, setScrollLeft] = React.useState(0); |
| 41 | + |
| 42 | + const possibleDirection = useMemo(() => { |
| 43 | + console.log("I ran update direction") |
| 44 | + if (!containerRef.current) return { canGoToNextSlide: false, canGoToPreviousSlide: false }; |
| 45 | + const canGoToNextSlide = scrollLeft < scrollableWidth - carouselWidth; |
| 46 | + const canGoToPreviousSlide = scrollLeft > 0; |
| 47 | + return { canGoToNextSlide, canGoToPreviousSlide }; |
| 48 | + }, [containerRef, scrollableWidth, carouselWidth, scrollLeft]); |
| 49 | + |
| 50 | + const handleScroll = throttledDebounce(() => { |
| 51 | + if (!containerRef.current) return; |
| 52 | + setScrollLeft(containerRef.current?.scrollLeft ?? 0); |
| 53 | + }, 200); |
| 54 | + |
| 55 | + // init update containerRef details on mount and resize |
| 56 | + useLayoutEffect(() => { |
| 57 | + if (!containerRef.current) return; |
| 58 | + |
| 59 | + const updateSize = throttledDebounce(() => { |
| 60 | + setCarouselWidth(containerRef.current?.clientWidth ?? 0); |
| 61 | + setScrollableWidth(containerRef.current?.scrollWidth ?? 0); |
| 62 | + setScrollLeft(containerRef.current?.scrollLeft ?? 0); |
| 63 | + console.log("i updated size", "width", containerRef.current?.clientWidth, "scrollable", containerRef.current?.scrollWidth) |
| 64 | + }, 200); |
| 65 | + |
| 66 | + const resizeObserver = new ResizeObserver(updateSize); |
| 67 | + resizeObserver.observe(containerRef.current); |
| 68 | + |
| 69 | + // Initial size update |
| 70 | + updateSize(); |
| 71 | + |
| 72 | + return () => { |
| 73 | + if (containerRef.current) { |
| 74 | + resizeObserver.unobserve(containerRef.current); |
| 75 | + } |
| 76 | + }; |
| 77 | + }, []); |
| 78 | + |
| 79 | + // update scroll position on scroll |
| 80 | + useLayoutEffect(() => { |
| 81 | + if (!containerRef.current) return; |
| 82 | + |
| 83 | + containerRef.current?.addEventListener('scroll', handleScroll); |
| 84 | + |
| 85 | + return () => { |
| 86 | + if (containerRef.current) { |
| 87 | + containerRef.current.removeEventListener('scroll', handleScroll); |
| 88 | + } |
| 89 | + }; |
| 90 | + }, []); |
| 91 | + |
| 92 | + const totalCarouselItems = useMemo(() => { |
| 93 | + console.log(containerRef.current) |
| 94 | + return containerRef.current?.children.length ?? 0 |
| 95 | + }, [containerRef]) |
| 96 | + |
| 97 | + const goToNextSlide = useCallback(() => { |
| 98 | + if (!containerRef.current) return; |
| 99 | + const stepWidth = containerRef.current.clientWidth * stepWidthInPercent / 100 |
| 100 | + const responsiveStepWidth = stepWidth < containerRef.current.children[0].clientWidth ? containerRef.current.clientWidth : stepWidth; |
| 101 | + const scrollLeft = containerRef.current.scrollLeft + responsiveStepWidth; |
| 102 | + containerRef.current.scrollTo({ |
| 103 | + left: scrollLeft, |
| 104 | + behavior: 'smooth', |
| 105 | + }); |
| 106 | + }, [containerRef, stepWidthInPercent]); |
| 107 | + |
| 108 | + const goToPreviousSlide = useCallback(() => { |
| 109 | + if (!containerRef.current) return; |
| 110 | + const stepWidth = containerRef.current.clientWidth * stepWidthInPercent / 100 |
| 111 | + // const responsiveStepWidth = Math.max(containerRef.current.clientWidth, containerRef.current.clientWidth * stepWidthInPercent / 100) ; |
| 112 | + const responsiveStepWidth = stepWidth < containerRef.current.children[0].clientWidth ? containerRef.current.clientWidth : stepWidth; |
| 113 | + const scrollLeft = Math.max(0, containerRef.current.scrollLeft - responsiveStepWidth); |
| 114 | + containerRef.current.scrollTo({ |
| 115 | + left: scrollLeft, |
| 116 | + behavior: 'smooth', |
| 117 | + }); |
| 118 | + }, [containerRef, stepWidthInPercent]); |
| 119 | + |
| 120 | + return ( |
| 121 | + <CarouselContext.Provider value={{containerRef, totalCarouselItems, goToNextSlide, goToPreviousSlide, possibleDirection }}> |
| 122 | + {children} |
| 123 | + </CarouselContext.Provider> |
| 124 | + ) |
| 125 | +} |
| 126 | + |
| 127 | +export const Carousel: React.FC<Omit<CarouselProviderProps, 'containerRef'>> & { |
| 128 | + Container: React.FC<CarouselContainerProps>; |
| 129 | + Item: React.FC<CarouselItemProps>; |
| 130 | + Controls: React.FC<CarouselControlProps>; |
| 131 | + PreviousButton: React.FC<CarouselButtonProps>; |
| 132 | + NextButton: React.FC<CarouselButtonProps>; |
| 133 | +} = ({ children, config }) => { |
| 134 | + const containerRef = useRef<HTMLDivElement>(null) |
| 135 | + return ( |
| 136 | + <CarouselProvider containerRef={containerRef} config={config}> |
| 137 | + {children} |
| 138 | + </CarouselProvider> |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +Carousel.Container = CarouselContainer; |
| 143 | +Carousel.Item = CarouselItem; |
| 144 | +Carousel.Controls = CarouselControls; |
| 145 | +Carousel.PreviousButton = CarouselPreviousButton; |
| 146 | +Carousel.NextButton = CarouselNextButton; |
0 commit comments