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

Closes #2302 #2305

Merged
merged 1 commit into from
Mar 24, 2024
Merged
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
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
Loading