Skip to content

fix(Calendar): open on a month inside the min/max window - #5306

Merged
cixzhang merged 1 commit into
mainfrom
fix/calendar-initial-month-clamp
Aug 24, 2026
Merged

cixzhang merged 1 commit into
mainfrom
fix/calendar-initial-month-clamp

Conversation

@imdreamrunner

Copy link
Copy Markdown
Contributor

What this does

Calendar picks its initial visible month from focusDate ?? value ?? today, with no regard for min/max. When the allowed window doesn't contain today — a closed audit period, a booking window that opens next season, a birth-date range — it opens on today's month with every day disabled, and the prev/next arrows are the only way in, one click per month.

The fallback is now today clamped into the window: today when today is inside it, otherwise whichever bound is nearest. An explicit focusDate or a selected value still wins untouched.

// today is 2026-08-21
<Calendar min="2019-01-01" max="2019-04-30" />   // before: August 2026, all disabled
                                                 // after:  April 2019
<Calendar min="2027-03-04" max="2027-06-30" />   // before: August 2026, all disabled
                                                 // after:  March 2027

DateInput, DateRangeInput, and DateTimeInput all mount Calendar without a focusDate, so they inherit the fix.

What changed

  • New getInitialFocusDate() helper (packages/core/src/Calendar/getInitialFocusDate.ts) holding the precedence and the clamp, with unit tests; Calendar.tsx calls it in place of its inline initializer.
  • With numberOfMonths={2}, clamping back to max lands max in the right-hand pane — opening on [max, max + 1] would spend half the calendar on a month that is entirely out of bounds. The shift never crosses min's month.
  • Inverted bounds (min after max) resolve to min, deterministically rather than off a contradiction.
  • Docs: focusDate's description now states the fallback (en, zh, dense).
  • New Storybook story Core/Calendar → Min/max window away from today — the existing min/max stories all pin focusDate, so none of them showed this.

Test plan

  • packages/core/src/Calendar/getInitialFocusDate.test.ts — 13 unit tests over the precedence, both clamp directions, inclusivity of the bounds, the two-month shift and its min guard, and inverted bounds.
  • 6 render tests in Calendar.test.tsx (fake-timed to 2026-08-21) asserting the visible-month label and that the landed month's days are selectable. Negative control: 3 of them fail with the clamp neutralized, and the focusDate/value precedence ones correctly still pass.
  • Green locally: pnpm lint:strict (0 errors), pnpm vitest run packages/core packages/lab (7521 tests), pnpm build, pnpm -F @astryxdesign/storybook build, and the core/lab/charts/cli/storybook typechecks plus lab:readiness:check.

Related: #4941 asks for a month/year picker so distant dates are navigable. This is the narrower defect underneath it — the calendar not starting inside its own window — and the two don't overlap.

@imdreamrunner

With no focusDate and no selected value, Calendar initialized its visible
month to today regardless of min/max. A window that does not contain today
— a closed audit period, a booking window that opens next season — therefore
opened on a grid where every day was disabled, and the prev/next arrows were
the only way in, one month per click.

The initial month is now today clamped into the window: today when it is
inside, otherwise the nearest bound. An explicit focusDate or a selected
value still wins untouched, so callers that already say where to look are
unaffected. With numberOfMonths={2} a past window puts max in the right-hand
pane rather than spending the second pane entirely out of bounds.

DateInput, DateRangeInput, and DateTimeInput inherit the fix — they all
mount Calendar without a focusDate.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 21, 2026
@vercel

vercel Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 21, 2026 10:14pm

Request Review

@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

Calendar (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 1485 -
Complexity N/A Very High (136) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.8KB 1.2KB

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

github-actions Bot added a commit that referenced this pull request Aug 21, 2026

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is good. Clamp and the two-month shift both check out. Nit: bounds that arrive after mount — a check-out min — still open on today's disabled month.

[Reviewed by Robohands]

// Focus date state (which month is visible). Falls back to today, clamped
// into the min/max window so a window that doesn't contain today doesn't
// open on an all-disabled month.
const [internalFocusDate, setInternalFocusDate] = useState<PlainDate>(() =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mount-only: a check-out min chosen later still opens on today, all disabled.

type: 'ISODateString',
description: 'Controlled visible month.',
description:
'Controlled visible month. Unset, the calendar opens on the selected date, else on today clamped into the min/max window.',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth saying "on mount" — the clamp doesn't re-run when bounds change.

@cixzhang
cixzhang merged commit 27646f7 into main Aug 24, 2026
20 checks passed
@github-actions
github-actions Bot deleted the fix/calendar-initial-month-clamp branch August 24, 2026 04:19
AKnassa added a commit to AKnassa/astryx that referenced this pull request Sep 26, 2026
No textual conflicts. Calendar.tsx, Calendar.test.tsx, Calendar.doc.mjs
and styles.ts auto-merged cleanly alongside upstream's Calendar work
(meta#5306 min/max opening month, meta#5453 320px reflow, meta#5120/meta#5303 locale,
meta#6121 theme selectors).

Merge fallout fixed here:

- astracat F4: meta#6121 moved generated variant selectors from bare
  classes to data attributes, so the calendar-picker defineTheme
  assertion now expects `.astryx-calendar-picker[data-picker="year"]`
  instead of the stale `.astryx-calendar-picker.year`, matching how
  meta#6121 updated the sibling calendar-nav assertion. Red before: "expected
  ... to contain '.astryx-calendar-picker.year'" (Calendar.test.tsx:1885).

- Provider locale: meta#5120/meta#5303 moved the static caption onto the
  InternationalizationProvider locale and added the
  @astryx/no-raw-intl-locale lint rule, which the picker option labels
  (still undefined-locale) now fail. They pass `locale` like the caption
  again, so the two caption modes keep agreeing on month names. New test
  "labels the month picker in the provider locale, like the static
  caption" was red first (es-ES rendered "March", expected "marzo").

Calendar.test.tsx 112/112 green after both.

This branch was successfully deployed

1 active deployment
Preview — 03e802e7 Deployed Aug 21, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants