Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gallery elementToScrollTo behavior #645

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [3.127.1] - 2023-10-24

### changed
- Behavior `scrollToElement` added to Gallery component for PLP.

### Changed
- Bump `vtex.store-resources` version.

Expand Down
63 changes: 52 additions & 11 deletions react/Gallery.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment } from 'react'
import React, { Fragment, useEffect } from 'react'
import { ProductList as ProductListStructuredData } from 'vtex.structured-data'

import GalleryLayout from './GalleryLayout'
Expand All @@ -16,17 +16,58 @@
const Gallery: React.FC<
GalleryLegacyProps | GalleryLayoutPropsWithSlots
> = props => {
if ('layouts' in props && props.layouts.length > 0) {
const {
layouts,
lazyItemsRemaining,
products,
showingFacets,
summary,
preferredSKU,
...slots
} = props as GalleryLayoutPropsWithSlots
const {
layouts,
lazyItemsRemaining,
products,
showingFacets,
summary,
preferredSKU,
...slots
} = props as GalleryLayoutPropsWithSlots

useEffect(() => {
const lastClickedProductId = localStorage.getItem('lastClickedProductId')
const isMobile = window.innerWidth <= 768

const delayedExecution = setTimeout(() => {
const scrollToElement = (elementId: string, offset: number) => {
const elementToScrollTo = document.getElementById(elementId)

if (elementToScrollTo) {

Check warning on line 37 in react/Gallery.tsx

View workflow job for this annotation

GitHub Actions / Lint

Prefer an early return to prevent nesting and improve code readability
const rect = elementToScrollTo.getBoundingClientRect()
const isElementVisible =
rect.top >= 0 &&
rect.bottom <=
(window.innerHeight || document.documentElement.clientHeight)

if (!isElementVisible) {
elementToScrollTo.scrollIntoView({
behavior: 'auto',
block: 'center',
inline: 'nearest',
})

setTimeout(() => {
scrollToElement(elementId, offset)
}, 500)
}
}
}

const recursiveScroll = () => {
if (lastClickedProductId) {
scrollToElement(lastClickedProductId, isMobile ? -200 : -200)
}
}

recursiveScroll()
}, 1000)

return () => clearTimeout(delayedExecution)
}, [])

if ('layouts' in props && props.layouts.length > 0) {
return (
<Fragment>
<ProductListStructuredData products={products} />
Expand Down
1 change: 1 addition & 0 deletions react/components/GalleryLayoutItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type { PreferredSKU } from '../GalleryLayout'

interface GalleryLayoutItemProps {
GalleryItemComponent: ComponentType<any>

Check warning on line 11 in react/components/GalleryLayoutItem.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
item: Product
displayMode: string
summary: unknown
Expand Down Expand Up @@ -44,6 +44,7 @@
position,
list: listName,
})
localStorage.setItem('lastClickedProductId', product.productId)
}, [
product,
push,
Expand Down
1 change: 1 addition & 0 deletions react/components/GalleryLayoutRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const GalleryLayoutRow: React.FC<GalleryLayoutRowProps> = ({
]),
'pa4'
)}
id={product.productId}
>
<GalleryItem
GalleryItemComponent={GalleryItemComponent}
Expand Down
Loading