Skip to content

Commit

Permalink
fix: ProductGrid scroll restoration
Browse files Browse the repository at this point in the history
  • Loading branch information
schettn committed Aug 4, 2023
1 parent fbd1a28 commit 923a6be
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
52 changes: 27 additions & 25 deletions src/components/molecules/ProductGrid/ProductGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {ProductCard} from '../ProductCard'
export interface ProductGridProps extends SimpleGridProps {
heading?: string
products: ShopifyProduct[]
scrollRestoration?: boolean
prefixPath?: string
searchLocation?: string
wholesale?: boolean
Expand All @@ -23,6 +24,7 @@ export interface ProductGridProps extends SimpleGridProps {
export const ProductGrid = ({
heading,
products,
scrollRestoration,
prefixPath,
searchLocation,
wholesale,
Expand All @@ -32,26 +34,15 @@ export const ProductGrid = ({
? useBreakpointValue(gridProps.columns as any)
: 0 || 0

const containerRef = useRef(null)

const location = useLocation()

// Save the scroll position on unmounting the component
useEffect(() => {
return () => {
if (containerRef.current) {
sessionStorage.setItem('scrollPosition', containerRef.current.scrollTop)
}
}
}, [location.pathname])
const scrollPosition = window.sessionStorage.getItem('scrollPosition')

// Restore the scroll position on mounting the component
useEffect(() => {
const scrollPosition = sessionStorage.getItem('scrollPosition')
if (scrollPosition && containerRef.current) {
containerRef.current.scrollTop = parseInt(scrollPosition, 10)
if (scrollPosition && scrollRestoration) {
window.scrollTo(0, parseInt(scrollPosition))

window.sessionStorage.removeItem('scrollPosition')
}
}, [location.pathname])
}, [scrollRestoration])

return (
<>
Expand All @@ -61,17 +52,28 @@ export const ProductGrid = ({
</Box>
)}

<SimpleGrid ref={containerRef} {...gridProps}>
<SimpleGrid {...gridProps}>
{products.map((item, index) => {
return (
<ProductCard
prefixPath={prefixPath}
searchLocation={searchLocation}
product={item}
<Box
id={`product-${item.id}`}
key={item.id}
left={(index + 1) % v === 0}
wholesale={wholesale}
/>
onClick={() => {
if (scrollRestoration) {
window.sessionStorage.setItem(
'scrollPosition',
window.scrollY.toString()
)
}
}}>
<ProductCard
prefixPath={prefixPath}
searchLocation={searchLocation}
product={item}
left={(index + 1) % v === 0}
wholesale={wholesale}
/>
</Box>
)
})}
</SimpleGrid>
Expand Down
5 changes: 1 addition & 4 deletions src/components/templates/ProductTemplate/ProductTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ export const ProductTemplate = ({
<Stack spacing={12}>
<Breadcrumb separator={<ChevronRightIcon boxSize="6" />}>
<BreadcrumbItem>
<BreadcrumbLink
onClick={() => {
window.history.back()
}}>
<BreadcrumbLink as={Link} to="/products">
Alle Artikel
</BreadcrumbLink>
</BreadcrumbItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const ProductsTemplate = (props: ProductsTemplateProps) => {
return (
<Box w="full" pt="4" pl={{base: '2', md: '12'}} pr={{base: '2', md: '12'}}>
<ProductGrid
scrollRestoration
wholesale={props.wholesale}
products={props.products}
columns={{base: 1, sm: 2, md: 3, lg: 3, xl: 4, '2xl': 5}}
Expand Down

0 comments on commit 923a6be

Please sign in to comment.