forked from crowetic/commerce
* Moved everything * Figuring out how to make imports work * Updated exports * Added missing exports * Added @vercel/commerce-local to `site` * Updated commerce config * Updated exports and commerce config * Updated commerce hoc * Fixed exports in local * Added publish config * Updated imports in site * It's actually working * Don't use debugger in dev for better speeds * Improved DX when editing packages * Set up eslint with husky * Updated prettier config * Added prettier setup to every package * Moved bigcommerce * Moved Bigcommerce to src and package updates * Updated setup of bigcommerce * Moved definitions script * Moved commercejs * Move to src * Fixed types in commercejs * Moved kibocommerce * Moved kibocommerce to src * Added package/tsconfig to kibocommerce * Fixed imports and other things * Moved ordercloud * Moved ordercloud to src * Fixed imports * Added missing prettier files * Moved Saleor * Moved Saleor to src * Fixed imports * Replaced all imports to @commerce * Added prettierignore/rc to all providers * Moved shopify to src * Build shopify in packages * Moved Spree * Moved spree to src * Updated spree * Moved swell * Moved swell to src * Fixed type imports in swell * Moved Vendure to packages * Moved vendure to src * Fixed imports in vendure * Added codegen to saleor * Updated codegen setup for shopify * Added codegen to vendure * Added codegen to kibocommerce * Added all packages to site's deps * Updated codegen setup in bigcommerce * Minor fixes * Updated providers' names in site * Updated packages based on Bel's changes * Updated turbo to latest * Fixed ts complains * Set npm engine in root * New lockfile install * remove engines * Regen lockfile * Switched from npm to yarn * Updated typesVersions in all packages * Moved dep * Updated SWR to the just released 1.2.0 * Removed "isolatedModules" from packages * Updated list of providers and default * Updated swell declaration * Removed next import from kibocommerce * Added COMMERCE_PROVIDER log * Added another log * Updated turbo config * Updated docs * Removed test logs Co-authored-by: Jared Palmer <jared@jaredpalmer.com>
130 lines
3.7 KiB
TypeScript
130 lines
3.7 KiB
TypeScript
import { useKeenSlider } from 'keen-slider/react'
|
|
import React, {
|
|
Children,
|
|
isValidElement,
|
|
useState,
|
|
useRef,
|
|
useEffect,
|
|
} from 'react'
|
|
import cn from 'classnames'
|
|
import { a } from '@react-spring/web'
|
|
import s from './ProductSlider.module.css'
|
|
import ProductSliderControl from '../ProductSliderControl'
|
|
|
|
interface ProductSliderProps {
|
|
children: React.ReactNode[]
|
|
className?: string
|
|
}
|
|
|
|
const ProductSlider: React.FC<ProductSliderProps> = ({
|
|
children,
|
|
className = '',
|
|
}) => {
|
|
const [currentSlide, setCurrentSlide] = useState(0)
|
|
const [isMounted, setIsMounted] = useState(false)
|
|
const sliderContainerRef = useRef<HTMLDivElement>(null)
|
|
const thumbsContainerRef = useRef<HTMLDivElement>(null)
|
|
|
|
const [ref, slider] = useKeenSlider<HTMLDivElement>({
|
|
loop: true,
|
|
slides: { perView: 1 },
|
|
created: () => setIsMounted(true),
|
|
slideChanged(s) {
|
|
const slideNumber = s.track.details.rel
|
|
setCurrentSlide(slideNumber)
|
|
|
|
if (thumbsContainerRef.current) {
|
|
const $el = document.getElementById(`thumb-${slideNumber}`)
|
|
if (slideNumber >= 3) {
|
|
thumbsContainerRef.current.scrollLeft = $el!.offsetLeft
|
|
} else {
|
|
thumbsContainerRef.current.scrollLeft = 0
|
|
}
|
|
}
|
|
},
|
|
})
|
|
|
|
// Stop the history navigation gesture on touch devices
|
|
useEffect(() => {
|
|
const preventNavigation = (event: TouchEvent) => {
|
|
// Center point of the touch area
|
|
const touchXPosition = event.touches[0].pageX
|
|
// Size of the touch area
|
|
const touchXRadius = event.touches[0].radiusX || 0
|
|
|
|
// We set a threshold (10px) on both sizes of the screen,
|
|
// if the touch area overlaps with the screen edges
|
|
// it's likely to trigger the navigation. We prevent the
|
|
// touchstart event in that case.
|
|
if (
|
|
touchXPosition - touchXRadius < 10 ||
|
|
touchXPosition + touchXRadius > window.innerWidth - 10
|
|
)
|
|
event.preventDefault()
|
|
}
|
|
|
|
const slider = sliderContainerRef.current!
|
|
|
|
slider.addEventListener('touchstart', preventNavigation)
|
|
|
|
return () => {
|
|
if (slider) {
|
|
slider.removeEventListener('touchstart', preventNavigation)
|
|
}
|
|
}
|
|
}, [])
|
|
|
|
const onPrev = React.useCallback(() => slider.current?.prev(), [slider])
|
|
const onNext = React.useCallback(() => slider.current?.next(), [slider])
|
|
|
|
return (
|
|
<div className={cn(s.root, className)} ref={sliderContainerRef}>
|
|
<div
|
|
ref={ref}
|
|
className={cn(s.slider, { [s.show]: isMounted }, 'keen-slider')}
|
|
>
|
|
{slider && <ProductSliderControl onPrev={onPrev} onNext={onNext} />}
|
|
{Children.map(children, (child) => {
|
|
// Add the keen-slider__slide className to children
|
|
if (isValidElement(child)) {
|
|
return {
|
|
...child,
|
|
props: {
|
|
...child.props,
|
|
className: `${
|
|
child.props.className ? `${child.props.className} ` : ''
|
|
}keen-slider__slide`,
|
|
},
|
|
}
|
|
}
|
|
return child
|
|
})}
|
|
</div>
|
|
|
|
<a.div className={s.album} ref={thumbsContainerRef}>
|
|
{slider &&
|
|
Children.map(children, (child, idx) => {
|
|
if (isValidElement(child)) {
|
|
return {
|
|
...child,
|
|
props: {
|
|
...child.props,
|
|
className: cn(child.props.className, s.thumb, {
|
|
[s.selected]: currentSlide === idx,
|
|
}),
|
|
id: `thumb-${idx}`,
|
|
onClick: () => {
|
|
slider.current?.moveToIdx(idx)
|
|
},
|
|
},
|
|
}
|
|
}
|
|
return child
|
|
})}
|
|
</a.div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default ProductSlider
|