|
| 1 | +import React, { useEffect, useRef, useState } from 'react' |
| 2 | +import type { ReactCarouselProps } from './carousel' |
| 3 | + |
| 4 | +import ConditionalWrapper from '../ConditionalWrapper/ConditionalWrapper.tsx' |
| 5 | +import Pagination from '../Pagination/Pagination.tsx' |
| 6 | +import Progress from '../Progress/Progress.tsx' |
| 7 | + |
| 8 | +import { classNames } from '../../utils/classNames' |
| 9 | +import { debounce as debounceScroll } from '../../utils/debounce' |
| 10 | + |
| 11 | +import styles from './carousel.module.scss' |
| 12 | + |
| 13 | +import type { PaginationEventType } from '../Pagination/pagination' |
| 14 | + |
| 15 | +const Carousel = ({ |
| 16 | + items, |
| 17 | + visibleItems = 1, |
| 18 | + subText, |
| 19 | + scrollSnap = true, |
| 20 | + progress, |
| 21 | + pagination, |
| 22 | + effect, |
| 23 | + debounce = 20, |
| 24 | + className, |
| 25 | + wrapperClassName, |
| 26 | + paginationClassName, |
| 27 | + onScroll, |
| 28 | + children |
| 29 | +}: ReactCarouselProps) => { |
| 30 | + const carouselContainer = useRef<HTMLDivElement>(null) |
| 31 | + const carousel = useRef<HTMLUListElement>(null) |
| 32 | + const carouselItems = useRef<any>(null) |
| 33 | + const paginated = useRef(false) |
| 34 | + const currentPage = useRef(1) |
| 35 | + |
| 36 | + const [progressValue, setProgressValue] = useState(0) |
| 37 | + const [updatedSubText, setUpdatedSubText] = useState(subText) |
| 38 | + |
| 39 | + const classes = classNames([ |
| 40 | + styles.carousel, |
| 41 | + className |
| 42 | + ]) |
| 43 | + |
| 44 | + const containerClasses = classNames([ |
| 45 | + styles.container, |
| 46 | + scrollSnap && styles.snap |
| 47 | + ]) |
| 48 | + |
| 49 | + const wrapperClasses = classNames([ |
| 50 | + styles.wrapper, |
| 51 | + effect && styles[effect], |
| 52 | + wrapperClassName |
| 53 | + ]) |
| 54 | + |
| 55 | + const paginationWrapperClasses = classNames([ |
| 56 | + styles['pagination-wrapper'], |
| 57 | + paginationClassName |
| 58 | + ]) |
| 59 | + |
| 60 | + const paginationClasses = classNames([ |
| 61 | + styles.pagination, |
| 62 | + !subText && paginationClassName |
| 63 | + ]) |
| 64 | + |
| 65 | + const totalPages = Math.ceil(items / visibleItems!) |
| 66 | + const subTextValue = subText?.match(/\{0\}|\{1\}/g) ? subText : undefined |
| 67 | + const style = visibleItems > 1 |
| 68 | + ? { '--w-slide-width': `${100 / visibleItems}%;` } as React.CSSProperties |
| 69 | + : undefined |
| 70 | + |
| 71 | + const updateValues = (page: number) => { |
| 72 | + const activeElement = carouselItems.current[page - 1] |
| 73 | + |
| 74 | + Array.from(carouselItems.current).forEach(li => (li as HTMLLIElement).removeAttribute('data-active')) |
| 75 | + activeElement.dataset.active = 'true' |
| 76 | + |
| 77 | + if (subTextValue) { |
| 78 | + setUpdatedSubText( |
| 79 | + subTextValue |
| 80 | + .replace('{0}', String(page)) |
| 81 | + .replace('{1}', String(totalPages)) |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + if (progress) { |
| 86 | + const percentage = (100 / (totalPages - 1)) |
| 87 | + |
| 88 | + setProgressValue(percentage * (page - 1)) |
| 89 | + } |
| 90 | + |
| 91 | + onScroll?.(page) |
| 92 | + } |
| 93 | + |
| 94 | + const scroll = debounceScroll((event: Event) => { |
| 95 | + if (paginated.current) { |
| 96 | + paginated.current = false |
| 97 | + } else { |
| 98 | + const target = event.target as HTMLDivElement |
| 99 | + const scrollLeft = target.scrollLeft |
| 100 | + const itemWidth = target.children[0].clientWidth |
| 101 | + const page = Math.round(scrollLeft / itemWidth) + 1 |
| 102 | + |
| 103 | + currentPage.current = page |
| 104 | + |
| 105 | + updateValues(page) |
| 106 | + } |
| 107 | + }, debounce) |
| 108 | + |
| 109 | + const paginate = (event: PaginationEventType) => { |
| 110 | + const liElement = carouselItems.current[event.page - 1] |
| 111 | + |
| 112 | + liElement.scrollIntoView({ behavior: 'smooth', block: 'nearest' }) |
| 113 | + |
| 114 | + currentPage.current = event.page |
| 115 | + paginated.current = true |
| 116 | + |
| 117 | + updateValues(event.page) |
| 118 | + } |
| 119 | + |
| 120 | + useEffect(() => { |
| 121 | + const usedInAstro = carousel.current?.children[0].nodeName === 'ASTRO-SLOT' |
| 122 | + |
| 123 | + carouselItems.current = usedInAstro |
| 124 | + ? carousel.current.querySelectorAll('li') |
| 125 | + : carousel.current?.children |
| 126 | + |
| 127 | + carouselContainer.current?.addEventListener('scroll', scroll) |
| 128 | + |
| 129 | + return () => { |
| 130 | + carouselContainer.current?.removeEventListener('scroll', scroll) |
| 131 | + } |
| 132 | + }, []) |
| 133 | + |
| 134 | + return ( |
| 135 | + <section className={classes}> |
| 136 | + <div className={containerClasses} ref={carouselContainer}> |
| 137 | + <ul className={wrapperClasses} style={style} ref={carousel}> |
| 138 | + {children} |
| 139 | + </ul> |
| 140 | + </div> |
| 141 | + <ConditionalWrapper |
| 142 | + condition={!!(subText || progress)} |
| 143 | + wrapper={children => ( |
| 144 | + <div className={paginationWrapperClasses}>{children}</div> |
| 145 | + )} |
| 146 | + > |
| 147 | + {progress && ( |
| 148 | + <Progress value={progressValue} /> |
| 149 | + )} |
| 150 | + <Pagination |
| 151 | + type="arrows" |
| 152 | + {...pagination} |
| 153 | + currentPage={currentPage.current} |
| 154 | + totalPages={totalPages} |
| 155 | + className={paginationClasses} |
| 156 | + onChange={paginate} |
| 157 | + /> |
| 158 | + {updatedSubText && ( |
| 159 | + <span className={styles.subtext}> |
| 160 | + {updatedSubText |
| 161 | + .replace('{0}', '1') |
| 162 | + .replace('{1}', String(totalPages)) |
| 163 | + } |
| 164 | + </span> |
| 165 | + )} |
| 166 | + </ConditionalWrapper> |
| 167 | + </section> |
| 168 | + ) |
| 169 | +} |
| 170 | + |
| 171 | +export default Carousel |
0 commit comments