mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 15:06:59 +00:00
improvement
This commit is contained in:
parent
aad0a909ac
commit
7ea1ea8fd7
@ -72,7 +72,7 @@ const ProductSidebar: FC<ProductSidebarProps> = ({ className }) => {
|
|||||||
{process.env.COMMERCE_CUSTOMFIELDS_ENABLED &&
|
{process.env.COMMERCE_CUSTOMFIELDS_ENABLED &&
|
||||||
product.customFields &&
|
product.customFields &&
|
||||||
product.customFields.length > 0 && (
|
product.customFields.length > 0 && (
|
||||||
<Collapse title="Technical Details">
|
<Collapse title="Specifications">
|
||||||
<ProductCustomFields fields={product.customFields} />
|
<ProductCustomFields fields={product.customFields} />
|
||||||
</Collapse>
|
</Collapse>
|
||||||
)}
|
)}
|
||||||
|
@ -11,6 +11,7 @@ import { a } from '@react-spring/web'
|
|||||||
import s from './ProductSlider.module.css'
|
import s from './ProductSlider.module.css'
|
||||||
import ProductSliderControl from '../ProductSliderControl'
|
import ProductSliderControl from '../ProductSliderControl'
|
||||||
import { useProduct } from '../context'
|
import { useProduct } from '../context'
|
||||||
|
import { ProductImage } from '@commerce/types/product'
|
||||||
|
|
||||||
interface ProductSliderProps {
|
interface ProductSliderProps {
|
||||||
children: React.ReactNode[]
|
children: React.ReactNode[]
|
||||||
@ -21,8 +22,9 @@ const ProductSlider: React.FC<ProductSliderProps> = ({
|
|||||||
children,
|
children,
|
||||||
className = '',
|
className = '',
|
||||||
}) => {
|
}) => {
|
||||||
const { imageIndex, setImageIndex } = useProduct()
|
const { product, variant } = useProduct()
|
||||||
const [currentSlide, setCurrentSlide] = useState(imageIndex ?? 0)
|
|
||||||
|
const [currentSlide, setCurrentSlide] = useState(0)
|
||||||
const [isMounted, setIsMounted] = useState(false)
|
const [isMounted, setIsMounted] = useState(false)
|
||||||
const sliderContainerRef = useRef<HTMLDivElement>(null)
|
const sliderContainerRef = useRef<HTMLDivElement>(null)
|
||||||
const thumbsContainerRef = useRef<HTMLDivElement>(null)
|
const thumbsContainerRef = useRef<HTMLDivElement>(null)
|
||||||
@ -30,9 +32,7 @@ const ProductSlider: React.FC<ProductSliderProps> = ({
|
|||||||
const [ref, slider] = useKeenSlider<HTMLDivElement>({
|
const [ref, slider] = useKeenSlider<HTMLDivElement>({
|
||||||
loop: true,
|
loop: true,
|
||||||
slides: { perView: 1 },
|
slides: { perView: 1 },
|
||||||
dragStarted: () => {
|
dragStarted: () => {},
|
||||||
setImageIndex(null)
|
|
||||||
},
|
|
||||||
created: () => setIsMounted(true),
|
created: () => setIsMounted(true),
|
||||||
slideChanged(s) {
|
slideChanged(s) {
|
||||||
const slideNumber = s.track.details.rel
|
const slideNumber = s.track.details.rel
|
||||||
@ -48,6 +48,17 @@ const ProductSlider: React.FC<ProductSliderProps> = ({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const index = product.images.findIndex((image: ProductImage) => {
|
||||||
|
return image.url === variant?.image?.url
|
||||||
|
})
|
||||||
|
if (index !== -1) {
|
||||||
|
slider.current?.moveToIdx(index, false, {
|
||||||
|
duration: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [variant, product, slider])
|
||||||
|
|
||||||
// Stop the history navigation gesture on touch devices
|
// Stop the history navigation gesture on touch devices
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const preventNavigation = (event: TouchEvent) => {
|
const preventNavigation = (event: TouchEvent) => {
|
||||||
@ -78,23 +89,13 @@ const ProductSlider: React.FC<ProductSliderProps> = ({
|
|||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (imageIndex !== null) {
|
|
||||||
slider.current?.moveToIdx(imageIndex, false, {
|
|
||||||
duration: 0,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}, [imageIndex, slider])
|
|
||||||
|
|
||||||
const onPrev = React.useCallback(() => {
|
const onPrev = React.useCallback(() => {
|
||||||
setImageIndex(null)
|
|
||||||
slider.current?.prev()
|
slider.current?.prev()
|
||||||
}, [setImageIndex, slider])
|
}, [slider])
|
||||||
|
|
||||||
const onNext = React.useCallback(() => {
|
const onNext = React.useCallback(() => {
|
||||||
setImageIndex(null)
|
|
||||||
slider.current?.next()
|
slider.current?.next()
|
||||||
}, [setImageIndex, slider])
|
}, [slider])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn(s.root, className)} ref={sliderContainerRef}>
|
<div className={cn(s.root, className)} ref={sliderContainerRef}>
|
||||||
@ -133,7 +134,6 @@ const ProductSlider: React.FC<ProductSliderProps> = ({
|
|||||||
}),
|
}),
|
||||||
id: `thumb-${idx}`,
|
id: `thumb-${idx}`,
|
||||||
onClick: () => {
|
onClick: () => {
|
||||||
setImageIndex(null)
|
|
||||||
slider.current?.moveToIdx(idx)
|
slider.current?.moveToIdx(idx)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -13,8 +13,6 @@ import { getProductVariant, selectDefaultOptionFromProduct } from './helpers'
|
|||||||
|
|
||||||
export interface ProductContextValue {
|
export interface ProductContextValue {
|
||||||
product: Product
|
product: Product
|
||||||
imageIndex: number | null
|
|
||||||
setImageIndex: Dispatch<SetStateAction<number | null>>
|
|
||||||
price: string
|
price: string
|
||||||
variant: ProductVariant
|
variant: ProductVariant
|
||||||
selectedOptions: SelectedOptions
|
selectedOptions: SelectedOptions
|
||||||
@ -34,7 +32,6 @@ export const ProductProvider: FC<ProductProviderProps> = ({
|
|||||||
product,
|
product,
|
||||||
children,
|
children,
|
||||||
}) => {
|
}) => {
|
||||||
const [imageIndex, setImageIndex] = useState<number | null>(null)
|
|
||||||
const [selectedOptions, setSelectedOptions] = useState<SelectedOptions>({})
|
const [selectedOptions, setSelectedOptions] = useState<SelectedOptions>({})
|
||||||
|
|
||||||
useEffect(
|
useEffect(
|
||||||
@ -53,26 +50,15 @@ export const ProductProvider: FC<ProductProviderProps> = ({
|
|||||||
currencyCode: variant?.price?.currencyCode || product.price.currencyCode!,
|
currencyCode: variant?.price?.currencyCode || product.price.currencyCode!,
|
||||||
})
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const index = product.images.findIndex((image: ProductImage) => {
|
|
||||||
return image.url === variant?.image?.url
|
|
||||||
})
|
|
||||||
if (index !== -1) {
|
|
||||||
setImageIndex(index)
|
|
||||||
}
|
|
||||||
}, [variant, product])
|
|
||||||
|
|
||||||
const value = useMemo(
|
const value = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
price,
|
price,
|
||||||
product,
|
product,
|
||||||
variant,
|
variant,
|
||||||
imageIndex,
|
|
||||||
setImageIndex,
|
|
||||||
selectedOptions,
|
selectedOptions,
|
||||||
setSelectedOptions,
|
setSelectedOptions,
|
||||||
}),
|
}),
|
||||||
[price, product, variant, imageIndex, selectedOptions]
|
[price, product, selectedOptions, variant]
|
||||||
)
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user