-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do scroll restoration in userland (#1550)
* do scroll restoration in userland. lol. fucking lmao * e2e test it baby * persist scroll positions in sessionStorage * see if it passes CI with the reload taken out
- Loading branch information
1 parent
ef0a179
commit 4636fb7
Showing
4 changed files
with
99 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useEffect } from 'react' | ||
import { useLocation, useNavigation } from 'react-router-dom' | ||
|
||
function getScrollPosition(key: string) { | ||
const pos = window.sessionStorage.getItem(key) | ||
return pos && /^[0-9]+$/.test(pos) ? parseInt(pos, 10) : 0 | ||
} | ||
|
||
function setScrollPosition(key: string, pos: number) { | ||
window.sessionStorage.setItem(key, pos.toString()) | ||
} | ||
|
||
/** | ||
* Given a ref to a scrolling container element, keep track of its scroll | ||
* position before navigation and restore it on return (e.g., back/forward nav). | ||
* Note that `location.key` is used in the cache key, not `location.pathname`, | ||
* so the same path navigated to at different points in the history stack will | ||
* not share the same scroll position. | ||
*/ | ||
export function useScrollRestoration(container: React.RefObject<HTMLElement>) { | ||
const key = `scroll-position-${useLocation().key}` | ||
const { state } = useNavigation() | ||
useEffect(() => { | ||
if (state === 'loading') { | ||
setScrollPosition(key, container.current?.scrollTop ?? 0) | ||
} else if (state === 'idle') { | ||
container.current?.scrollTo(0, getScrollPosition(key)) | ||
} | ||
}, [key, state, container]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { type Page, expect, test } from './utils' | ||
|
||
async function expectScrollTop(page: Page, expected: number) { | ||
const container = await page.getByTestId('scroll-container') | ||
const getScrollTop = () => container.evaluate((el: HTMLElement) => el.scrollTop) | ||
await expect.poll(getScrollTop).toBe(expected) | ||
} | ||
|
||
async function scrollTo(page: Page, to: number) { | ||
const container = await page.getByTestId('scroll-container') | ||
await container.evaluate((el: HTMLElement, to) => el.scrollTo(0, to), to) | ||
} | ||
|
||
test('scroll restore', async ({ page }) => { | ||
// open small window to make scrolling easier | ||
await page.setViewportSize({ width: 800, height: 500 }) | ||
|
||
// nav to disks and scroll it | ||
await page.goto('/projects/mock-project/disks') | ||
await expectScrollTop(page, 0) | ||
await scrollTo(page, 143) | ||
|
||
// nav to snapshots | ||
await page.getByRole('link', { name: 'Snapshots' }).click() | ||
await expectScrollTop(page, 0) | ||
|
||
// go back to disks, scroll is restored, scroll it some more | ||
await page.goBack() | ||
await expect(page).toHaveURL('/projects/mock-project/disks') | ||
await expectScrollTop(page, 143) | ||
await scrollTo(page, 190) | ||
|
||
// go forward to snapshots, now scroll it | ||
await page.goForward() | ||
await expect(page).toHaveURL('/projects/mock-project/snapshots') | ||
await expectScrollTop(page, 0) | ||
await scrollTo(page, 30) | ||
|
||
// new nav to disks | ||
await page.getByRole('link', { name: 'Disks' }).click() | ||
await expectScrollTop(page, 0) | ||
|
||
// this is too flaky so forget it for now | ||
|
||
// random reload in there because we use sessionStorage. note we are | ||
// deliberately on the disks page here because there's a quirk in playwright | ||
// that seems to reset to the disks page on reload | ||
// await page.reload() | ||
|
||
// back to snapshots, scroll is restored | ||
await page.goBack() | ||
await expect(page).toHaveURL('/projects/mock-project/snapshots') | ||
await expectScrollTop(page, 30) | ||
|
||
// back again to disks, newer scroll value is restored | ||
await page.goBack() | ||
await expect(page).toHaveURL('/projects/mock-project/disks') | ||
await expectScrollTop(page, 190) | ||
|
||
// forward again to newest disks history entry, scroll remains 0 | ||
await page.goForward() | ||
await page.goForward() | ||
await expect(page).toHaveURL('/projects/mock-project/disks') | ||
await expectScrollTop(page, 0) | ||
}) |
This file was deleted.
Oops, something went wrong.