Skip to content

Commit

Permalink
feat: scroll-restore at all page
Browse files Browse the repository at this point in the history
  • Loading branch information
hyesungoh committed Nov 6, 2023
1 parent 3bad461 commit bcea3ed
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
6 changes: 2 additions & 4 deletions apps/blog/src/hooks/useScrollRestoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ function useScrollRestoration() {
useEffect(() => {
if ('scrollRestoration' in window.history) {
window.history.scrollRestoration = 'manual';
restoreScrollPos(router.asPath);

const onBeforeUnload = event => {
const onBeforeUnload = () => {

Check warning on line 25 in apps/blog/src/hooks/useScrollRestoration.ts

View check run for this annotation

Codecov / codecov/patch

apps/blog/src/hooks/useScrollRestoration.ts#L25

Added line #L25 was not covered by tests
saveScrollPos(router.asPath);
delete event['returnValue'];
};

const onRouteChangeStart = () => {
saveScrollPos(router.asPath);
};

const onRouteChangeComplete = url => {
const onRouteChangeComplete = (url: string) => {
if (shouldScrollRestore) {
shouldScrollRestore = false;
restoreScrollPos(url);
Expand Down
3 changes: 3 additions & 0 deletions apps/blog/src/pages/_app.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NextUIProvider } from '@nextui-org/react';
import { darkTheme, lightTheme } from 'core';
import { KBarProvider, useRegisterActions } from 'kbar';

import useScrollRestoration from '../hooks/useScrollRestoration';
import generateKbarAction from '../libs/kbarActions';

const KbarComponent = dynamic(() => import('core/components/Kbar'), {
Expand All @@ -16,6 +17,8 @@ export default function BlogApp({ Component, pageProps }: AppProps) {
const router = useRouter();
const kbarActions = generateKbarAction(router);

useScrollRestoration();

return (
<NextThemesProvider
defaultTheme="system"
Expand Down
2 changes: 0 additions & 2 deletions apps/blog/src/pages/category/[category].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { MainHeader } from '../../components/Header';
import PostCard from '../../components/PostCard';
import SEO from '../../components/SEO';
import useInfiniteScroll from '../../hooks/useInfiniteScroll';
import useScrollRestoration from '../../hooks/useScrollRestoration';
import { getAllCategories, getAllPostsByCategory } from '../../libs/api';

interface Props {
Expand All @@ -16,7 +15,6 @@ interface Props {

function EachCategory({ category, allPosts }: Props) {
const { theme } = useTheme();
useScrollRestoration();

const { setTarget, elements: posts, isEnded } = useInfiniteScroll<PostType>({ offset: 12, fullElements: allPosts });

Expand Down
2 changes: 0 additions & 2 deletions apps/blog/src/pages/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { MainHeader } from '../components/Header';
import PostCard from '../components/PostCard';
import SEO from '../components/SEO';
import useInfiniteScroll from '../hooks/useInfiniteScroll';
import useScrollRestoration from '../hooks/useScrollRestoration';
import { getAllPosts } from '../libs/api';

interface Props {
Expand All @@ -14,7 +13,6 @@ interface Props {

function Blog({ allPosts }: Props) {
const { theme } = useTheme();
useScrollRestoration();

const {
setTarget,
Expand Down

0 comments on commit bcea3ed

Please sign in to comment.