Skip to content

Commit

Permalink
bug: fix pagination usage
Browse files Browse the repository at this point in the history
Closes #2302
  • Loading branch information
rupali-codes authored Mar 24, 2024
2 parents d9cd699 + f3ca82b commit a73f4e1
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react'
import React, { useEffect, useState, useRef } from 'react'
import { useTheme } from 'next-themes'
import clsx from 'clsx'
import { MdArrowForwardIos } from 'react-icons/md'
Expand All @@ -20,14 +20,31 @@ export default function Pagination({
}: PaginationProps) {
const { resolvedTheme } = useTheme()
const isDarkMode = resolvedTheme === 'dark'
const [scrollingElement, setScrollingElement] = useState<HTMLElement | Window | undefined>()
const paginationRef = useRef<HTMLDivElement>(null);

const scrollToTop = () => {
window.scrollTo({ top: 0, behavior: 'smooth' })
if (scrollingElement) scrollingElement.scrollTo({ top: 0, behavior: 'smooth' })
}

const changePage = (page: number) => {
handlePageChange(page)
}

useEffect(() => {
if (paginationRef.current) {
const findClosestScrollElement = (element: HTMLElement | null): HTMLElement | Window => {
if (!element) return window
const { overflowY } = getComputedStyle(element)
const scrollableVariants = ['auto', 'scroll']
return scrollableVariants.some(v => overflowY === v)
? element
: findClosestScrollElement(element.parentElement)
}
setScrollingElement(findClosestScrollElement(paginationRef.current))
}
}, [])

useEffect(() => {
scrollToTop()
}, [currentPage])
Expand All @@ -37,7 +54,7 @@ export default function Pagination({
return (
<>
{totalPages && totalPages.length > 1 && (
<div>
<div ref={paginationRef}>
<div
className={
currentPage == totalPages.length && remainderOfCards <= 3
Expand Down

0 comments on commit a73f4e1

Please sign in to comment.