Skip to content

Commit ed6e8d9

Browse files
authored
fix(status-page): remember scroll position when navigating home (#259)
1 parent 87be124 commit ed6e8d9

6 files changed

Lines changed: 34 additions & 1 deletion

File tree

app/ca/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ThumbnailGrid from '@/components/ThumbnailGrid';
33
import Thumbnail from '@/components/Thumbnail';
44
import Header from '@/components/Header';
55
import Footer from '@/components/Footer';
6+
import ScrollRestore from '@/components/ScrollRestore';
67
import { getTranslations } from '@/lib/translation';
78

89
import statuses from '@/lib/statuses';
@@ -12,6 +13,7 @@ export default async function Home() {
1213

1314
return (
1415
<>
16+
<ScrollRestore />
1517
<Header t={t} />
1618
<main>
1719
<Usage t={t} />

app/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ThumbnailGrid from '@/components/ThumbnailGrid';
33
import Thumbnail from '@/components/Thumbnail';
44
import Header from '@/components/Header';
55
import Footer from '@/components/Footer';
6+
import ScrollRestore from '@/components/ScrollRestore';
67
import { getTranslations } from '@/lib/translation';
78

89
import statuses from '@/lib/statuses';
@@ -12,6 +13,7 @@ export default async function Home() {
1213

1314
return (
1415
<>
16+
<ScrollRestore />
1517
<Header t={t} />
1618
<main>
1719
<Usage t={t} />

app/status/[status]/page.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export default async function Info({ params }: { params: { status: string } }) {
2121
<Header t={t} />
2222
<main>
2323
<nav>
24-
<Link href="/" className="text-white">{`< ${t.BACK_TO_HOME}`}</Link>
24+
<Link
25+
href="/"
26+
className="text-white"
27+
scroll={false}
28+
>{`< ${t.BACK_TO_HOME}`}</Link>
2529
</nav>
2630

2731
<h1 className="text-center my-12">
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use client';
2+
3+
import { useEffect } from 'react';
4+
5+
const ScrollRestore = () => {
6+
useEffect(() => {
7+
const savedPosition = sessionStorage.getItem('homeScrollPosition');
8+
if (savedPosition) {
9+
window.scrollTo(0, parseInt(savedPosition, 10));
10+
sessionStorage.removeItem('homeScrollPosition');
11+
}
12+
}, []);
13+
14+
return null;
15+
};
16+
17+
export default ScrollRestore;

components/ScrollRestore/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './ScrollRestore';

components/Thumbnail/Thumbnail.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use client';
2+
13
import Link from 'next/link';
24
import Image from 'next/image';
35

@@ -10,10 +12,15 @@ type ThumbnailProps = {
1012
const Thumbnail = ({ code, description, t }: ThumbnailProps) => {
1113
const hrefBase = t.LOCALE === 'ca' ? '/ca' : '';
1214

15+
const saveScrollPosition = () => {
16+
sessionStorage.setItem('homeScrollPosition', window.scrollY.toString());
17+
};
18+
1319
return (
1420
<div id={`${code}`} className="flex flex-col flex-grow h-full text-white overflow-hidden rounded shadow bg-[--interactive]">
1521
<Link
1622
href={`${hrefBase}/status/${code}`}
23+
onClick={saveScrollPosition}
1724
className="text-white no-underline"
1825
>
1926
<div className="pt-[56.25%] relative overflow-hidden">

0 commit comments

Comments
 (0)