Skip to content

Commit

Permalink
[UI/UX] Scrolls to previous location when opening library (#1954)
Browse files Browse the repository at this point in the history
* Scrolls to previous location when opening library

* Fixed typo

* Changed to useLayoutEffect and using ref to access listing

* Cleaner code, no event listener needed anymore

* fix: lint

Co-authored-by: Flávio F Lima <flavioislima@gmail.com>
  • Loading branch information
Snoodelz and flavioislima committed Nov 3, 2022
1 parent a814a2c commit 5041aa8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ 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

0 comments on commit 5041aa8

Please sign in to comment.