diff --git a/app/ui/MonthCard.tsx b/app/ui/MonthCard.tsx index 9ee73e4..b66db90 100644 --- a/app/ui/MonthCard.tsx +++ b/app/ui/MonthCard.tsx @@ -1,10 +1,10 @@ "use client"; -import { FC } from "react"; +import { FC, useEffect, useRef } from "react"; import { formatYearMonth } from "../lib/format"; import { YearMonth } from "../lib/db-types"; import { formatCurrency } from "../lib/formatStrings"; -import { redirect, useParams, useRouter } from "next/navigation"; +import { useRouter } from "next/navigation"; export interface MonthCardProps { yearMonth: YearMonth, @@ -16,12 +16,20 @@ export interface MonthCardProps { export const MonthCard:FC = ({ yearMonth, children, monthlyExpense, expanded }) => { const router = useRouter(); + const elRef = useRef(null); // setting the `month` will activate the accordion belonging to that month - const handleChange = (event:any) => router.push(`/?year=${yearMonth.year}&month=${yearMonth.month}`) + const handleChange = (event:any) => router.push(`/?year=${yearMonth.year}&month=${yearMonth.month}`); + + useEffect(() => { + if(expanded && elRef.current) { + // if the element i selected > scroll it into view + elRef.current.scrollIntoView({ behavior: 'smooth', block: 'center' }); + } + }, []); return( -
+
{`${formatYearMonth(yearMonth)}`}