Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix inconsistent month picker width and add year drill-down (#70)

Tássio Noronha46ba686379eb
diff --git a/frontend/src/components/ui/calendar.tsx b/frontend/src/components/ui/calendar.tsx
index f7b1a427..15fc6c61 100644
--- a/frontend/src/components/ui/calendar.tsx
+++ b/frontend/src/components/ui/calendar.tsx
@@ -7,10 +7,14 @@ import {
addDays,
addMonths,
subMonths,
+ addYears,
+ subYears,
isSameMonth,
isSameDay,
isToday,
format,
+ setMonth as dfSetMonth,
+ setYear as dfSetYear,
type Locale,
} from 'date-fns'
import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react'
@@ -25,6 +29,9 @@ interface CalendarProps {
className?: string
}
+type View = 'days' | 'months' | 'years'
+const YEAR_PAGE_SIZE = 12
+
function Calendar({
selected,
defaultMonth,
@@ -33,6 +40,33 @@ function Calendar({
className,
}: CalendarProps) {
const [viewMonth, setViewMonth] = useState(defaultMonth ?? selected ?? new Date())
+ const [view, setView] = useState<View>('days')
+
+ const currentYear = viewMonth.getFullYear()
+ const yearPageStart = Math.floor(currentYear / YEAR_PAGE_SIZE) * YEAR_PAGE_SIZE
+
+ const goPrev = () => {
+ if (view === 'days') setViewMonth(subMonths(viewMonth, 1))
+ else if (view === 'months') setViewMonth(subYears(viewMonth, 1))
+ else setViewMonth(subYears(viewMonth, YEAR_PAGE_SIZE))
+ }
+ const goNext = () => {
+ if (view === 'days') setViewMonth(addMonths(viewMonth, 1))
+ else if (view === 'months') setViewMonth(addYears(viewMonth, 1))
+ else setViewMonth(addYears(viewMonth, YEAR_PAGE_SIZE))
+ }
+
+ const headerLabel =
+ view === 'days'
+ ? format(viewMonth, 'LLLL yyyy', { locale })
+ : view === 'months'
+ ? format(viewMonth, 'yyyy', { locale })
+ : `${yearPageStart} – ${yearPageStart + YEAR_PAGE_SIZE - 1}`
+
+ const onHeaderClick = () => {
+ if (view === 'days') setView('months')
+ else if (view === 'months') setView('years')
+ }
const monthStart = startOfMonth(viewMonth)
const monthEnd = endOfMonth(viewMonth)
@@ -56,64 +90,131 @@ function Calendar({
}
return (
- <div className={cn('p-3', className)}>
- {/* Header: nav + month label */}
+ <div className={cn('p-3 w-[252px]', className)}>
+ {/* Header: nav + caption */}
<div className="flex items-center justify-between mb-3">
<button
type="button"
- onClick={() => setViewMonth(subMonths(viewMonth, 1))}
+ onClick={goPrev}
className="size-7 inline-flex items-center justify-center rounded-lg border border-border bg-transparent text-muted-foreground hover:text-foreground hover:bg-muted/50 transition-colors"
>
<ChevronLeftIcon className="size-4" />
</button>
- <span className="text-sm font-medium text-foreground capitalize">
- {format(viewMonth, 'LLLL yyyy', { locale })}
- </span>
<button
type="button"
- onClick={() => setViewMonth(addMonths(viewMonth, 1))}
+ onClick={onHeaderClick}
+ disabled={view === 'years'}
+ className={cn(
+ 'text-sm font-medium text-foreground capitalize px-2 py-1 rounded-md transition-colors',
+ view !== 'years' && 'hover:bg-muted/60 cursor-pointer',
+ view === 'years' && 'cursor-default',
+ )}
+ >
+ {headerLabel}
+ </button>
+ <button
+ type="button"
+ onClick={goNext}
className="size-7 inline-flex items-center justify-center rounded-lg border border-border bg-transparent text-muted-foreground hover:text-foreground hover:bg-muted/50 transition-colors"
>
<ChevronRightIcon className="size-4" />
</button>
</div>
- {/* Weekday headers */}
- <div className="grid grid-cols-7 mb-1">
- {weekdayLabels.map((label, i) => (
- <div key={i} className="text-center text-[11px] font-medium text-muted-foreground py-1">
- {label}
+ {view === 'days' && (
+ <>
+ <div className="grid grid-cols-7 mb-1">
+ {weekdayLabels.map((label, i) => (
+ <div key={i} className="text-center text-[11px] font-medium text-muted-foreground py-1">
+ {label}
+ </div>
+ ))}
</div>
- ))}
- </div>
+ <div className="grid grid-cols-7">
+ {weeks.map((week, wi) =>
+ week.map((d, di) => {
+ const inMonth = isSameMonth(d, viewMonth)
+ const isSelected = selected && isSameDay(d, selected)
+ const today = isToday(d)
- {/* Day grid */}
- <div className="grid grid-cols-7">
- {weeks.map((week, wi) =>
- week.map((d, di) => {
- const inMonth = isSameMonth(d, viewMonth)
- const isSelected = selected && isSameDay(d, selected)
- const today = isToday(d)
+ return (
+ <button
+ key={`${wi}-${di}`}
+ type="button"
+ onClick={() => onSelect?.(d)}
+ className={cn(
+ 'size-8 inline-flex items-center justify-center rounded-lg text-sm transition-colors',
+ !inMonth && 'text-muted-foreground/40',
+ inMonth && !isSelected && 'text-foreground hover:bg-muted/60',
+ today && !isSelected && 'bg-accent text-accent-foreground font-medium',
+ isSelected && 'bg-primary text-primary-foreground font-semibold',
+ )}
+ >
+ {d.getDate()}
+ </button>
+ )
+ }),
+ )}
+ </div>
+ </>
+ )}
+ {view === 'months' && (
+ <div className="grid grid-cols-3 gap-1">
+ {Array.from({ length: 12 }).map((_, i) => {
+ const monthDate = dfSetMonth(viewMonth, i)
+ const isCurrent =
+ selected &&
+ selected.getFullYear() === monthDate.getFullYear() &&
+ selected.getMonth() === i
return (
<button
- key={`${wi}-${di}`}
+ key={i}
type="button"
- onClick={() => onSelect?.(d)}
+ onClick={() => {
+ setViewMonth(monthDate)
+ setView('days')
+ }}
className={cn(
- 'size-8 inline-flex items-center justify-center rounded-lg text-sm transition-colors',
- !inMonth && 'text-muted-foreground/40',
- inMonth && !isSelected && 'text-foreground hover:bg-muted/60',
- today && !isSelected && 'bg-accent text-accent-foreground font-medium',
- isSelected && 'bg-primary text-primary-foreground font-semibold',
+ 'h-10 rounded-lg text-sm capitalize transition-colors',
+ isCurrent
+ ? 'bg-primary text-primary-foreground font-semibold'
+ : 'text-foreground hover:bg-muted/60',
)}
>
- {d.getDate()}
+ {format(monthDate, 'MMM', { locale })}
</button>
)
- }),
- )}
- </div>
+ })}
+ </div>
+ )}
+
+ {view === 'years' && (
+ <div className="grid grid-cols-3 gap-1">
+ {Array.from({ length: YEAR_PAGE_SIZE }).map((_, i) => {
+ const y = yearPageStart + i
+ const isCurrent = selected && selected.getFullYear() === y
+ return (
+ <button
+ key={y}
+ type="button"
+ onClick={() => {
+ setViewMonth(dfSetYear(viewMonth, y))
+ setView('months')
+ }}
+ className={cn(
+ 'h-10 rounded-lg text-sm transition-colors',
+ isCurrent
+ ? 'bg-primary text-primary-foreground font-semibold'
+ : 'text-foreground hover:bg-muted/60',
+ )}
+ >
+ {y}
+ </button>
+ )
+ })}
+ </div>
+ )}
</div>
)
}
diff --git a/frontend/src/pages/budgets.tsx b/frontend/src/pages/budgets.tsx
index 96ef9574..c118cd9a 100644
--- a/frontend/src/pages/budgets.tsx
+++ b/frontend/src/pages/budgets.tsx
@@ -143,7 +143,7 @@ export default function BudgetsPage() {
<PopoverTrigger asChild>
<button
type="button"
- className="inline-flex items-center gap-2 border border-border rounded-lg px-3 py-1.5 text-sm bg-card text-foreground hover:bg-muted/50 transition-all cursor-pointer"
+ className="inline-flex items-center justify-center gap-2 border border-border rounded-lg px-3 py-1.5 text-sm bg-card text-foreground hover:bg-muted/50 transition-all cursor-pointer min-w-[180px]"
>
<CalendarIcon className="size-3.5 text-muted-foreground" />
{monthTitle}
diff --git a/frontend/src/pages/dashboard.tsx b/frontend/src/pages/dashboard.tsx
index 60d9eb52..d3e80684 100644
--- a/frontend/src/pages/dashboard.tsx
+++ b/frontend/src/pages/dashboard.tsx
@@ -349,7 +349,7 @@ export default function DashboardPage() {
<PopoverTrigger asChild>
<button
type="button"
- className="inline-flex items-center gap-2 border border-border rounded-lg px-3 py-1.5 text-sm bg-card text-foreground hover:bg-muted/50 transition-all cursor-pointer"
+ className="inline-flex items-center justify-center gap-2 border border-border rounded-lg px-3 py-1.5 text-sm bg-card text-foreground hover:bg-muted/50 transition-all cursor-pointer min-w-[180px]"
>
<CalendarIcon className="size-3.5 text-muted-foreground" />
{new Date(selectedMonth + '-02').toLocaleDateString(locale, { month: 'long', year: 'numeric' }).replace(/^\w/, c => c.toUpperCase())}