redesign: YearSelector as a compact dropdown next to MonthSelector

The pill-row year switcher from the previous commit added a whole
extra row of buttons above the month pills, which read as cluttered.
Replaced it with a single dropdown button (current year + chevron)
placed to the right of MonthSelector on all three pages, matching how
a lot of finance-app date pickers separate month (frequent) from year
(occasional) navigation.
This commit is contained in:
ZProger
2026-08-16 22:15:17 +03:00
parent f3fbe4d546
commit acff447673
5 changed files with 86 additions and 32 deletions

View File

@@ -1,3 +1,5 @@
import { useEffect, useRef, useState } from "react";
import { ChevronDown } from "lucide-react";
import { cn } from "@/lib/utils";
interface YearSelectorProps {
@@ -6,27 +8,73 @@ interface YearSelectorProps {
onChange: (year: number) => void;
}
/** Compact dropdown, most recent year first — sits to the right of
* MonthSelector instead of competing with it as a second row of pills. */
export function YearSelector({ years, year, onChange }: YearSelectorProps) {
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
const sortedYears = [...years].sort((a, b) => b - a);
useEffect(() => {
if (!open) return;
function handlePointerDown(event: MouseEvent) {
if (rootRef.current && !rootRef.current.contains(event.target as Node)) {
setOpen(false);
}
}
function handleKeyDown(event: KeyboardEvent) {
if (event.key === "Escape") setOpen(false);
}
document.addEventListener("mousedown", handlePointerDown);
document.addEventListener("keydown", handleKeyDown);
return () => {
document.removeEventListener("mousedown", handlePointerDown);
document.removeEventListener("keydown", handleKeyDown);
};
}, [open]);
return (
<div className="flex gap-2 overflow-x-auto pb-1 [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
{years.map((value) => {
const active = value === year;
return (
<button
key={value}
type="button"
onClick={() => onChange(value)}
className={cn(
"shrink-0 rounded-full border px-3.5 py-1.5 text-sm font-medium transition-colors",
active
? "border-text-primary bg-text-primary text-surface-1"
: "border-border bg-surface-1 text-text-secondary hover:bg-surface-2"
)}
>
{value}
</button>
);
})}
<div ref={rootRef} className="relative shrink-0">
<button
type="button"
onClick={() => setOpen((prev) => !prev)}
aria-haspopup="listbox"
aria-expanded={open}
className="flex h-9 items-center gap-1.5 rounded-lg border border-border bg-surface-1 px-3 text-sm font-medium text-text-primary transition-colors hover:bg-surface-2"
>
{year}
<ChevronDown size={14} className={cn("text-text-muted transition-transform", open && "rotate-180")} />
</button>
{open && (
<ul
role="listbox"
className="absolute right-0 top-full z-20 mt-1.5 max-h-64 w-24 overflow-y-auto rounded-lg border border-border bg-surface-1 py-1 shadow-lg"
>
{sortedYears.map((value) => {
const active = value === year;
return (
<li key={value}>
<button
type="button"
role="option"
aria-selected={active}
onClick={() => {
onChange(value);
setOpen(false);
}}
className={cn(
"block w-full px-3 py-1.5 text-left text-sm transition-colors",
active ? "bg-surface-2 font-semibold text-text-primary" : "text-text-secondary hover:bg-surface-2"
)}
>
{value}
</button>
</li>
);
})}
</ul>
)}
</div>
);
}

View File

@@ -54,9 +54,11 @@ export function BudgetPage() {
<div className="space-y-5">
<AlertBanner />
<div className="space-y-2">
<div className="flex items-center gap-3">
<div className="min-w-0 flex-1">
<MonthSelector month={month} onChange={setMonth} />
</div>
<YearSelector years={years ?? [now.getFullYear()]} year={year} onChange={setYear} />
<MonthSelector month={month} onChange={setMonth} />
</div>
<Card>

View File

@@ -35,9 +35,11 @@ export function DashboardPage() {
<div className="space-y-5">
<AlertBanner excludeKeys={["risky_allocation_exceeded"]} />
<div className="space-y-2">
<div className="flex items-center gap-3">
<div className="min-w-0 flex-1">
<MonthSelector month={month} onChange={setMonth} />
</div>
<YearSelector years={years ?? [now.getFullYear()]} year={year} onChange={setYear} />
<MonthSelector month={month} onChange={setMonth} />
</div>
{isError && (

View File

@@ -63,7 +63,16 @@ export function TransactionsPage() {
return (
<div className="space-y-5">
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
<div className="space-y-2">
<div className="flex min-w-0 flex-1 items-center gap-3">
<div className="min-w-0 flex-1">
<MonthSelector
month={month}
onChange={(value) => {
setMonth(value);
setPage(1);
}}
/>
</div>
<YearSelector
years={years ?? [now.getFullYear()]}
year={year}
@@ -72,13 +81,6 @@ export function TransactionsPage() {
setPage(1);
}}
/>
<MonthSelector
month={month}
onChange={(value) => {
setMonth(value);
setPage(1);
}}
/>
</div>
<Button onClick={openCreateModal} className="w-full sm:w-auto">
<Plus size={16} />