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 1 commit
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ 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.
- `lazyItemsRemaining` removed in Gallery component and GalleryLayout. `lazyRender` removed in GalleryLayoutRow component.

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

Expand Down
57 changes: 45 additions & 12 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,23 +16,56 @@
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, 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 30 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} />
<GalleryLayout
layouts={layouts}
lazyItemsRemaining={lazyItemsRemaining}
products={products}
showingFacets={showingFacets}
summary={summary}
Expand Down
23 changes: 0 additions & 23 deletions react/GalleryLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import React, { useContext, useEffect, useMemo } from 'react'
import type { ComponentType } from 'react'
import classNames from 'classnames'
import { ProductListContext } from 'vtex.product-list-context'
import { Spinner } from 'vtex.styleguide'
import { useCssHandles, applyModifiers } from 'vtex.css-handles'
import { useResponsiveValue } from 'vtex.responsive-values'
import type { MaybeResponsiveInput } from 'vtex.responsive-values'
import { useRuntime } from 'vtex.render-runtime'
import { SearchPageContext } from 'vtex.search-page-context'

import GalleryLayoutRow from './components/GalleryLayoutRow'
Expand All @@ -20,8 +18,6 @@ import {
import { useBreadcrumb } from './hooks/useBreadcrumb'
import { useSearchTitle } from './hooks/useSearchTitle'

const LAZY_RENDER_THRESHOLD = 2

const CSS_HANDLES = ['gallery'] as const

const { ProductListProvider } = ProductListContext
Expand All @@ -37,7 +33,6 @@ export type Slots = Record<string, ComponentType>

export interface GalleryLayoutProps {
layouts: LayoutOption[]
lazyItemsRemaining: number
products: Product[]
showingFacets: boolean
summary: unknown
Expand All @@ -54,7 +49,6 @@ export type PreferredSKU =

const GalleryLayout: React.FC<GalleryLayoutProps> = ({
layouts,
lazyItemsRemaining,
products,
showingFacets,
summary,
Expand All @@ -63,7 +57,6 @@ const GalleryLayout: React.FC<GalleryLayoutProps> = ({
}) => {
const { trackingId } = useContext(SettingsContext) || {}
const handles = useCssHandles(CSS_HANDLES)
const { getSettings } = useRuntime()
const { selectedGalleryLayout } = useSearchPageState()
const searchPageStateDispatch = useSearchPageStateDispatch()

Expand Down Expand Up @@ -148,17 +141,13 @@ const GalleryLayout: React.FC<GalleryLayoutProps> = ({
}
)

const isLazyRenderEnabled =
getSettings('vtex.store')?.enableSearchRenderingOptimization

return (
<ProductListProvider listName={listName as string}>
<div id="gallery-layout-container" className={galleryClasses}>
{galleryRows.map((rowProducts, index) => (
<GalleryLayoutRow
key={`${currentLayoutOption.name}-${index}`}
products={rowProducts}
lazyRender={!!isLazyRenderEnabled && index >= LAZY_RENDER_THRESHOLD}
summary={summary}
displayMode="normal"
itemsPerRow={itemsPerRow}
Expand All @@ -169,18 +158,6 @@ const GalleryLayout: React.FC<GalleryLayoutProps> = ({
GalleryItemComponent={slots[currentLayoutOption.component]}
/>
))}
{typeof lazyItemsRemaining === 'number' && lazyItemsRemaining > 0 && (
<div
style={{
width: '100%',
// Approximate number, just to add scroll leeway
height: 300 * Math.ceil(lazyItemsRemaining / itemsPerRow),
}}
className="flex justify-center pt10"
>
<Spinner />
</div>
)}
</div>
<ProductListEventCaller />
</ProductListProvider>
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
4 changes: 1 addition & 3 deletions react/components/GalleryLayoutRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ interface GalleryLayoutRowProps {
displayMode: string
GalleryItemComponent: ComponentType
itemsPerRow: number
lazyRender: boolean
products: Product[]
summary: unknown
rowIndex: number
Expand All @@ -28,7 +27,6 @@ const GalleryLayoutRow: React.FC<GalleryLayoutRowProps> = ({
GalleryItemComponent,
displayMode,
itemsPerRow,
lazyRender,
products,
summary,
currentLayoutName,
Expand All @@ -44,7 +42,6 @@ const GalleryLayoutRow: React.FC<GalleryLayoutRowProps> = ({
}

const { hasBeenViewed, dummyElement } = useRenderOnView({
lazyRender,
offset: 900,
})

Expand All @@ -68,6 +65,7 @@ const GalleryLayoutRow: React.FC<GalleryLayoutRowProps> = ({
]),
'pa4'
)}
id={product.productId}
>
<GalleryItem
GalleryItemComponent={GalleryItemComponent}
Expand Down
Loading