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

Scrolls to previous location when opening library #1954

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ export default function SidebarLinks() {
classNames('Sidebar__item', { active: isActive })
}
to={'/'}
onClick={async () =>
onClick={() => {
localStorage.setItem('scrollPosition', '0')
refreshLibrary({ runInBackground: false, fullRefresh: true })
}
}}

>
<>
<div className="Sidebar__itemIcon">
Expand Down
20 changes: 18 additions & 2 deletions src/frontend/screens/Library/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import React, {
useEffect,
useMemo,
useRef,
useState
useState,
useLayoutEffect
} from 'react'

import ArrowDropUp from '@mui/icons-material/ArrowDropUp'
Expand Down Expand Up @@ -81,6 +82,21 @@ export default function Library(): JSX.Element {
)
const { t } = useTranslation()
const backToTopElement = useRef(null)
const listing = useRef<HTMLDivElement>(null)

//Remember scroll position
useLayoutEffect(() => {
const scrollPosition = parseInt(storage?.getItem('scrollPosition') || '0')

if (listing.current !== null && scrollPosition > 0) {
listing.current.scrollTo(0, scrollPosition)
}
return () => {
if (listing.current !== null) {
storage?.setItem('scrollPosition', listing.current.scrollTop.toString())
}
}
}, [listing])

// bind back to top button
useEffect(() => {
Expand Down Expand Up @@ -291,7 +307,7 @@ export default function Library(): JSX.Element {
return (
<>
<Header />
<div className="listing">
<div className="listing" ref={listing}>
<span id="top" />
{showRecentGames && (
<RecentlyPlayed
Expand Down