Skip to content

Commit

Permalink
Merge pull request #50 from mateuszmanczak04/39-keep-scroll-level-aft…
Browse files Browse the repository at this point in the history
…er-navigating

39 keep scroll level after navigating
  • Loading branch information
mateuszmanczak04 authored Sep 17, 2024
2 parents 96d3090 + f2ba1a3 commit b8ab2e6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"editor.codeActionsOnSave": {
"source.organizeImports": "always"
}
}
7 changes: 7 additions & 0 deletions src/app/calendar/_context/calendar-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface CalendarContextProps {
rowHeight: number;
zoomIn: () => void;
zoomOut: () => void;
scrollTop: number;
setScrollTop: (newValue: number) => void;
}

const CalendarContext = createContext({} as CalendarContextProps);
Expand All @@ -39,6 +41,9 @@ export const CalendarContextProvider = ({
const containerRef = useRef<HTMLDivElement | null>(null);
const { settings, update: updateSettings } = useSettings();

// Keeping the same calendar scroll y level after switching routes
const [scrollTop, setScrollTop] = useState(0);

if (!settings) return null; // TOOD: handle this

// Returns a date object which is X days after "currentFirstDay"
Expand Down Expand Up @@ -151,6 +156,8 @@ export const CalendarContextProvider = ({
getRelativePosition,
getDateFromPosition,
displayedDays: settings.displayedDays,
scrollTop,
setScrollTop,
}}>
{children}
</CalendarContext.Provider>
Expand Down
20 changes: 19 additions & 1 deletion src/app/calendar/page.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
'use client';

import ErrorMessage from '@/components/common/error-message';
import { useEffect, useRef } from 'react';
import useCourses from '../courses/_hooks/use-courses';
import useNotes from '../notes/_hooks/use-notes';
import Grid from './_components/grid';
import Header from './_components/header';
import Notes from './_components/notes';
import TopBar from './_components/top-bar';
import { useCalendarContext } from './_context/calendar-context';

const CalendarPage = () => {
const { error: notesError } = useNotes();
const { error: coursesError } = useCourses();

// Used to keep the same calendar scroll y level even after
// switching routes
const { scrollTop, setScrollTop } = useCalendarContext();
const scrollContainerRef = useRef<HTMLDivElement | null>(null);

const handleScroll = () => {
setScrollTop(scrollContainerRef.current!.scrollTop);
};

useEffect(() => {
scrollContainerRef.current!.scrollTop = scrollTop;
}, [scrollTop]);

return (
<div className='flex h-full flex-col'>
{/* Year and month: */}
Expand All @@ -31,7 +46,10 @@ const CalendarPage = () => {

<TopBar />

<div className='relative overflow-y-scroll overscroll-none outline-none scrollbar-hide'>
<div
className='relative overflow-y-scroll overscroll-none scroll-auto outline-none scrollbar-hide'
onScroll={handleScroll}
ref={scrollContainerRef}>
{/* Just grid: */}
<Grid />

Expand Down

0 comments on commit b8ab2e6

Please sign in to comment.