feat: add month and year picker to calendar (#3104)

This commit is contained in:
Bingxi Zhao (Frank)
2026-12-02 05:28:10 -05:00
4 changed files with 300 additions and 9 deletions

View File

@@ -2,7 +2,7 @@
import { browserStorage } from '@/shared/storage'
import { useEffect, useMemo, useRef, useState } from 'react'
import { CalendarDays, ChevronLeft, ChevronRight, Plus, Search, PanelLeft, X } from 'lucide-react'
import { CalendarDays, ChevronDown, ChevronLeft, ChevronRight, Plus, Search, PanelLeft, X } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { PageHeader } from '@/components/layout/FeaturePage'
import HeaderActionButton from '@/features/chat/components/HeaderActionButton'
@@ -50,6 +50,7 @@ import {
} from '../model/items'
import { AnchoredPopover, anchorOf, type Anchor } from './AnchoredPopover'
import { CalendarRail } from './CalendarRail'
import { MonthYearPicker } from './MiniMonth'
import { TimeGrid, type GridDraft } from './TimeGrid'
import { MonthView } from './MonthView'
import { AgendaView, SearchResults } from './AgendaView'
@@ -71,6 +72,7 @@ type Panel = { anchor: Anchor } & (
| { kind: 'delete'; item: EventItem }
| { kind: 'calendar-delete'; calendar: api.CalendarEntry }
| { kind: 'move'; item: EventItem; start: Date; end: Date }
| { kind: 'jump' }
)
const center: Anchor = { top: 110, bottom: 140, left: 280, right: 280 }
@@ -367,7 +369,24 @@ export default function CalendarPage() {
<HeaderActionButton icon={ChevronLeft} label={t('Previous')} onClick={() => navigate(-1)} />
<HeaderActionButton icon={ChevronRight} label={t('Next')} onClick={() => navigate(1)} />
</div>
<h2>{heading}</h2>
<h2>
<button
type="button"
className="dt-cal-heading-button"
data-calendar-keep-popover
aria-expanded={panel?.kind === 'jump'}
onClick={event =>
setPanel(prev =>
prev?.kind === 'jump'
? null
: { kind: 'jump', anchor: anchorOf(event.currentTarget) ?? center }
)
}
>
<span>{heading}</span>
<ChevronDown size={15} strokeWidth={1.8} aria-hidden />
</button>
</h2>
</>
)
const tools = (
@@ -557,10 +576,31 @@ export default function CalendarPage() {
<AnchoredPopover
anchor={panel.anchor}
onClose={close}
width={panel.kind === 'edit' ? 410 : 350}
label={t(panel.kind === 'edit' ? 'Edit event' : 'Calendar details')}
width={panel.kind === 'edit' ? 410 : panel.kind === 'jump' ? 248 : 350}
label={t(
panel.kind === 'edit'
? 'Edit event'
: panel.kind === 'jump'
? 'Calendar'
: 'Calendar details'
)}
>
{panel.kind === 'edit' ? (
{panel.kind === 'jump' ? (
<div className="dt-cal-ym-popover">
<MonthYearPicker
activeYear={cursor.getFullYear()}
activeMonth={cursor.getMonth()}
today={now}
locale={i18n.language}
onSelect={(year, month) => {
const lastDay = new Date(year, month + 1, 0).getDate()
setCursor(new Date(year, month, Math.min(cursor.getDate(), lastDay)))
setQuery('')
setPanel(null)
}}
/>
</div>
) : panel.kind === 'edit' ? (
<EventEditor
initial={panel.draft}
calendars={entries}

View File

@@ -1,7 +1,7 @@
'use client'
import { useState } from 'react'
import { ChevronLeft, ChevronRight } from 'lucide-react'
import { ChevronDown, ChevronLeft, ChevronRight } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import Tooltip from '@/shared/ui/Tooltip'
import {
@@ -15,6 +15,108 @@ import {
startOfMonth,
} from '../model/dates'
const MONTHS = Array.from({ length: 12 }, (_, i) => i)
/** Compact month & 12-year drill-down picker used by both the toolbar and mini calendar. */
export function MonthYearPicker({
activeYear,
activeMonth,
today,
locale,
onSelect,
}: {
activeYear: number
activeMonth: number
today: Date
locale: string
onSelect: (year: number, month: number) => void
}) {
const { t } = useTranslation()
const [year, setYear] = useState(activeYear)
const [followedYear, setFollowedYear] = useState(activeYear)
const [pickingYear, setPickingYear] = useState(false)
const [yearStart, setYearStart] = useState(activeYear - 4)
if (activeYear !== followedYear) {
setFollowedYear(activeYear)
setYear(activeYear)
setYearStart(activeYear - 4)
}
const todayYear = today.getFullYear()
const todayMonth = today.getMonth()
const yearRange = `${yearStart} – ${yearStart + 11}`
return (
<div className="dt-cal-ym-picker">
<div className="dt-cal-ym-head">
<button
type="button"
aria-label={t(pickingYear ? 'Previous' : 'Previous year')}
className="dt-cal-icon-button"
onClick={() => (pickingYear ? setYearStart(s => s - 12) : setYear(y => y - 1))}
>
<ChevronLeft size={15} strokeWidth={1.8} />
</button>
<button
type="button"
className="dt-cal-ym-year"
aria-expanded={pickingYear}
onClick={() => {
if (!pickingYear) setYearStart(year - 4)
setPickingYear(open => !open)
}}
>
<span>{pickingYear ? yearRange : year}</span>
<ChevronDown size={13} strokeWidth={1.8} aria-hidden />
</button>
<button
type="button"
aria-label={t(pickingYear ? 'Next' : 'Next year')}
className="dt-cal-icon-button"
onClick={() => (pickingYear ? setYearStart(s => s + 12) : setYear(y => y + 1))}
>
<ChevronRight size={15} strokeWidth={1.8} />
</button>
</div>
{pickingYear ? (
<div className="dt-cal-ym-grid" role="group" aria-label={yearRange}>
{MONTHS.map(offset => {
const y = yearStart + offset
return (
<button
key={y}
type="button"
className="dt-cal-ym-month"
aria-pressed={y === year}
aria-current={y === todayYear ? 'date' : undefined}
onClick={() => {
setYear(y)
setPickingYear(false)
}}
>
{y}
</button>
)
})}
</div>
) : (
<div className="dt-cal-ym-grid" role="group" aria-label={String(year)}>
{MONTHS.map(m => (
<button
key={m}
type="button"
className="dt-cal-ym-month"
aria-pressed={year === activeYear && m === activeMonth}
aria-current={year === todayYear && m === todayMonth ? 'date' : undefined}
onClick={() => onSelect(year, m)}
>
{formatDate(new Date(year, m, 1), locale, { month: 'short' })}
</button>
))}
</div>
)}
</div>
)
}
/** A small month to jump around in; the shown range is shaded. */
export function MiniMonth({
cursor,
@@ -34,6 +136,7 @@ export function MiniMonth({
const locale = i18n.language
const [shown, setShown] = useState(() => startOfMonth(cursor))
const [followed, setFollowed] = useState(dateKey(startOfMonth(cursor)))
const [pickingMonth, setPickingMonth] = useState(false)
// Follow the main view when it leaves the month on display.
const cursorMonth = dateKey(startOfMonth(cursor))
if (cursorMonth !== followed) {
@@ -45,7 +148,15 @@ export function MiniMonth({
return (
<div className="dt-cal-mini">
<div className="dt-cal-mini-head">
<span className="dt-cal-mini-title">{formatMonthTitle(shown, locale)}</span>
<button
type="button"
className="dt-cal-mini-title"
aria-expanded={pickingMonth}
onClick={() => setPickingMonth(open => !open)}
>
<span>{formatMonthTitle(shown, locale)}</span>
<ChevronDown size={13} strokeWidth={1.8} aria-hidden />
</button>
<Tooltip label={t('Previous month')}>
<button
type="button"
@@ -67,7 +178,19 @@ export function MiniMonth({
</button>
</Tooltip>
</div>
<div className="dt-cal-mini-grid" role="grid" aria-label={formatMonthTitle(shown, locale)}>
{pickingMonth ? (
<MonthYearPicker
activeYear={shown.getFullYear()}
activeMonth={shown.getMonth()}
today={today}
locale={locale}
onSelect={(year, month) => {
setShown(new Date(year, month, 1))
setPickingMonth(false)
}}
/>
) : (
<div className="dt-cal-mini-grid" role="grid" aria-label={formatMonthTitle(shown, locale)}>
<div role="row" className="dt-cal-mini-row">
{weeks[0].map(day => (
<span key={day.getDay()} role="columnheader" className="dt-cal-mini-weekday">
@@ -103,7 +226,8 @@ export function MiniMonth({
})}
</div>
))}
</div>
</div>
)}
</div>
)
}

View File

@@ -51,6 +51,20 @@
margin: 0 10px 0 6px;
font-variant-numeric: tabular-nums;
}
.dt-cal-heading-button {
display: inline-flex;
align-items: center;
gap: 5px;
padding: 3px 8px;
margin: -3px -8px;
border-radius: 7px;
font: inherit;
letter-spacing: inherit;
}
.dt-cal-heading-button:hover,
.dt-cal-heading-button[aria-expanded='true'] {
background: var(--secondary);
}
.dt-cal-today {
height: 30px;
margin-left: 4px;
@@ -251,6 +265,70 @@ button.dt-cal-rail-link {
}
.dt-cal-mini-title {
flex: 1;
display: inline-flex;
align-items: center;
gap: 4px;
padding: 2px 6px;
margin-left: -6px;
border-radius: 6px;
font-weight: 600;
text-align: left;
}
.dt-cal-mini-title:hover,
.dt-cal-mini-title[aria-expanded='true'] {
background: var(--secondary);
}
.dt-cal-ym-popover {
padding: 12px;
}
.dt-cal-ym-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 6px;
margin-bottom: 10px;
}
.dt-cal-ym-year {
flex: 1;
height: 28px;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 4px;
padding: 0 8px;
border-radius: 6px;
color: var(--foreground);
font-size: 13px;
font-weight: 600;
font-variant-numeric: tabular-nums;
transition: background-color 120ms;
}
.dt-cal-ym-year:hover,
.dt-cal-ym-year[aria-expanded='true'] {
background: var(--secondary);
}
.dt-cal-ym-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 4px;
}
.dt-cal-ym-month {
height: 30px;
border-radius: 6px;
font-size: 12px;
font-variant-numeric: tabular-nums;
color: var(--foreground);
transition: background-color 120ms, color 120ms;
}
.dt-cal-ym-month:hover {
background: var(--accent, var(--secondary));
}
.dt-cal-ym-month[aria-current='date'] {
box-shadow: inset 0 0 0 1px var(--primary);
}
.dt-cal-ym-month[aria-pressed='true'] {
background: var(--primary);
color: var(--primary-foreground);
font-weight: 600;
}
.dt-cal-icon-button {

View File

@@ -185,6 +185,55 @@ it('does not trigger calendar shortcuts while typing in the editor', async () =>
expect(screen.getByRole('tab', { name: 'Week' })).toHaveAttribute('aria-selected', 'true')
})
it('jumps to a chosen month and year from the toolbar heading and toggles closed', async () => {
mount()
await waitFor(() => expect(screen.getByRole('button', { name: 'New event' })).toBeEnabled())
const heading = screen.getByRole('heading', { level: 2 })
const toggle = within(heading).getByRole('button')
fireEvent.click(toggle)
expect(screen.getByRole('dialog', { name: 'Calendar' })).toBeInTheDocument()
// Let AnchoredPopover attach its document pointerdown listener before toggling closed.
await act(async () => {
await new Promise(resolve => setTimeout(resolve, 0))
})
fireEvent.pointerDown(toggle)
fireEvent.click(toggle)
expect(screen.queryByRole('dialog', { name: 'Calendar' })).not.toBeInTheDocument()
fireEvent.click(toggle)
const dialog = within(screen.getByRole('dialog', { name: 'Calendar' }))
const currentYear = new Date().getFullYear()
const targetYear = currentYear + 14
fireEvent.click(dialog.getByRole('button', { name: String(currentYear) }))
fireEvent.click(dialog.getByRole('button', { name: 'Next' }))
fireEvent.click(dialog.getByRole('button', { name: String(targetYear) }))
fireEvent.click(dialog.getByRole('button', { name: 'Feb' }))
expect(screen.queryByRole('dialog', { name: 'Calendar' })).not.toBeInTheDocument()
expect(heading).toHaveTextContent(`February ${targetYear}`)
await waitFor(() =>
expect(
vi.mocked(fetch).mock.calls.some(([url]) => String(url).includes(`${targetYear}-`))
).toBe(true)
)
})
it('picks a month and year from the mini calendar and keeps year synced when stepping', async () => {
mount()
await waitFor(() => expect(screen.getByRole('button', { name: 'New event' })).toBeEnabled())
const rail = within(screen.getByRole('complementary', { name: 'Calendar sidebar' }))
const currentYear = new Date().getFullYear()
const prevYear = currentYear - 1
fireEvent.click(rail.getByRole('button', { name: new RegExp(String(currentYear)) }))
fireEvent.click(rail.getByRole('button', { name: 'Previous year' }))
fireEvent.click(rail.getByRole('button', { name: 'Jan' }))
expect(rail.getByRole('grid', { name: `January ${prevYear}` })).toBeInTheDocument()
fireEvent.click(rail.getByRole('button', { name: `January ${prevYear}` }))
fireEvent.click(rail.getByRole('button', { name: 'Previous month' }))
expect(rail.getByRole('button', { name: String(prevYear - 1) })).toBeInTheDocument()
})
describe('calendar models', () => {
it('clamps month navigation and covers the leap day once', () => {
expect(dateKey(addMonths(new Date(2024, 0, 31), 1))).toBe('2024-02-29')