@klinking/colander 0.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/dist/formats/date.cjs +51 -48
  2. package/dist/formats/date.d.cts +10 -37
  3. package/dist/formats/date.d.mts +10 -37
  4. package/dist/formats/date.mjs +6 -2
  5. package/dist/formats/object.cjs +51 -48
  6. package/dist/formats/object.d.cts +10 -37
  7. package/dist/formats/object.d.mts +10 -37
  8. package/dist/formats/object.mjs +6 -2
  9. package/dist/formats/plain-date-time.cjs +51 -48
  10. package/dist/formats/plain-date-time.d.cts +10 -37
  11. package/dist/formats/plain-date-time.d.mts +10 -37
  12. package/dist/formats/plain-date-time.mjs +6 -2
  13. package/dist/formats/plain-date.cjs +51 -48
  14. package/dist/formats/plain-date.d.cts +10 -37
  15. package/dist/formats/plain-date.d.mts +10 -37
  16. package/dist/formats/plain-date.mjs +6 -2
  17. package/dist/formats/plain-month-day.cjs +51 -48
  18. package/dist/formats/plain-month-day.d.cts +10 -37
  19. package/dist/formats/plain-month-day.d.mts +10 -37
  20. package/dist/formats/plain-month-day.mjs +6 -2
  21. package/dist/formats/plain-year-month.cjs +51 -48
  22. package/dist/formats/plain-year-month.d.cts +10 -37
  23. package/dist/formats/plain-year-month.d.mts +10 -37
  24. package/dist/formats/plain-year-month.mjs +6 -2
  25. package/dist/formats/zoned-date-time.cjs +51 -48
  26. package/dist/formats/zoned-date-time.d.cts +10 -37
  27. package/dist/formats/zoned-date-time.d.mts +10 -37
  28. package/dist/formats/zoned-date-time.mjs +6 -2
  29. package/dist/index.cjs +47 -48
  30. package/dist/index.d.cts +11 -2
  31. package/dist/index.d.mts +11 -2
  32. package/dist/index.mjs +2 -2
  33. package/dist/{temporal-polyfill-DTx0VIX5.mjs → weeks-navigation-B-Nch3LS.mjs} +357 -562
  34. package/dist/{temporal-polyfill-DU8VfleL.cjs → weeks-navigation-FxcECgCR.cjs} +357 -568
  35. package/dist/{weeks-view-context-BN6qR-83.d.mts → weeks-view-context-BZeWx9N8.d.cts} +275 -258
  36. package/dist/{weeks-view-context-aHGgYkpt.d.cts → weeks-view-context-MVe1b-BS.d.mts} +275 -258
  37. package/package.json +20 -20
  38. package/src/calendar-context.ts +12 -5
  39. package/src/calendar-provider.test.tsx +18 -4
  40. package/src/calendar-provider.tsx +130 -35
  41. package/src/calendar-types.ts +21 -4
  42. package/src/compute-weeks-in-window.test.ts +9 -7
  43. package/src/context.ts +1 -1
  44. package/src/day-cell.test.tsx +109 -0
  45. package/src/day-cell.tsx +132 -49
  46. package/src/drag-handle.tsx +23 -24
  47. package/src/formats/date.ts +120 -56
  48. package/src/formats/formats.test.ts +8 -4
  49. package/src/formats/object.ts +120 -56
  50. package/src/formats/plain-date-time.ts +120 -56
  51. package/src/formats/plain-date.ts +120 -56
  52. package/src/formats/plain-month-day.ts +120 -56
  53. package/src/formats/plain-year-month.ts +120 -56
  54. package/src/formats/zoned-date-time.ts +120 -56
  55. package/src/grid-header.tsx +24 -18
  56. package/src/grid.event-perf.test.tsx +30 -13
  57. package/src/grid.perf.test.tsx +97 -33
  58. package/src/grid.tsx +75 -43
  59. package/src/index.ts +16 -4
  60. package/src/keyboard.test.ts +110 -42
  61. package/src/keyboard.ts +18 -7
  62. package/src/month-separator.test.tsx +3 -1
  63. package/src/month-separator.tsx +30 -9
  64. package/src/month-view-context.ts +8 -3
  65. package/src/month-view-types.ts +14 -10
  66. package/src/month-view.test.tsx +687 -68
  67. package/src/month-view.tsx +176 -71
  68. package/src/navigation.tsx +77 -47
  69. package/src/overflow.test.ts +15 -13
  70. package/src/overflow.ts +10 -2
  71. package/src/range-preview.tsx +37 -12
  72. package/src/resolve-first-week.test.ts +14 -10
  73. package/src/resolve-first-week.ts +6 -2
  74. package/src/root-selection.ts +185 -101
  75. package/src/selected-range.tsx +47 -22
  76. package/src/test-temporal.ts +0 -2
  77. package/src/types.ts +126 -103
  78. package/src/utils.bench.ts +30 -6
  79. package/src/utils.test.ts +203 -55
  80. package/src/utils.ts +156 -54
  81. package/src/view-context.ts +4 -1
  82. package/src/week-number.tsx +5 -8
  83. package/src/weeks-grid.tsx +45 -19
  84. package/src/weeks-keyboard.test.ts +6 -7
  85. package/src/weeks-keyboard.ts +8 -3
  86. package/src/weeks-navigation.tsx +21 -9
  87. package/src/weeks-view-context.ts +8 -3
  88. package/src/weeks-view-state.ts +100 -33
  89. package/src/weeks-view-types.ts +14 -8
  90. package/src/weeks-view.test.tsx +127 -19
  91. package/src/weeks-view.tsx +30 -12
  92. package/src/temporal-polyfill.test.ts +0 -317
  93. package/src/temporal-polyfill.ts +0 -566
@@ -3,7 +3,10 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
3
  import { useCalendarStable, useCalendarState } from "./calendar-context";
4
4
  import { CalendarProvider } from "./calendar-provider";
5
5
  import type { CalendarProviderProps } from "./calendar-types";
6
- import { MonthViewStableContext, MonthViewStateContext } from "./month-view-context";
6
+ import {
7
+ MonthViewStableContext,
8
+ MonthViewStateContext,
9
+ } from "./month-view-context";
7
10
  import type {
8
11
  MonthViewRootProps,
9
12
  MonthViewStableContextValue,
@@ -11,12 +14,12 @@ import type {
11
14
  } from "./month-view-types";
12
15
  import type { MonthData, RootState, ValueFormat } from "./types";
13
16
  import {
14
- calendarForLocale,
15
17
  computeAdjacentMonth,
16
18
  focusedDateForMonth,
17
19
  getMonthWeeks,
18
20
  resolveFocusTarget,
19
21
  selectedToZdt,
22
+ shiftWindowToMonth,
20
23
  toZonedDateTime,
21
24
  } from "./utils";
22
25
  import { ViewContext, type ViewContextValue } from "./view-context";
@@ -47,7 +50,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
47
50
  const calState = useCalendarState();
48
51
 
49
52
  const T = calStable.temporal;
50
- const { timeZone, locale, weekStartDay, isDateDisabled } = calStable;
53
+ const { timeZone, weekStartDay, isDateDisabled } = calStable;
51
54
 
52
55
  // --- Month navigation state (controlled/uncontrolled) ---
53
56
  const isMonthControlled = monthProp !== undefined;
@@ -57,7 +60,8 @@ function MonthViewRoot(props: MonthViewRootProps) {
57
60
  month: number;
58
61
  }>(() => {
59
62
  if (monthProp) return { year: monthProp.year, month: monthProp.month };
60
- if (defaultMonthProp) return { year: defaultMonthProp.year, month: defaultMonthProp.month };
63
+ if (defaultMonthProp)
64
+ return { year: defaultMonthProp.year, month: defaultMonthProp.month };
61
65
  // Derive from selection or today
62
66
  if (calState.selected) {
63
67
  const zdt = toZonedDateTime(calState.selected, timeZone, T);
@@ -84,62 +88,151 @@ function MonthViewRoot(props: MonthViewRootProps) {
84
88
 
85
89
  // --- Grid label IDs ---
86
90
  const [gridLabelIds, setGridLabelIds] = useState<Record<number, string>>({});
87
- const setGridLabelId = useCallback((monthIndex: number, id: string | undefined) => {
88
- setGridLabelIds((prev) => {
89
- if (id === undefined) {
90
- if (!(monthIndex in prev)) return prev;
91
- const next = { ...prev };
92
- delete next[monthIndex];
93
- return next;
91
+ const setGridLabelId = useCallback(
92
+ (monthIndex: number, id: string | undefined) => {
93
+ setGridLabelIds((prev) => {
94
+ if (id === undefined) {
95
+ if (!(monthIndex in prev)) return prev;
96
+ const next = { ...prev };
97
+ delete next[monthIndex];
98
+ return next;
99
+ }
100
+ if (prev[monthIndex] === id) return prev;
101
+ return { ...prev, [monthIndex]: id };
102
+ });
103
+ },
104
+ [],
105
+ );
106
+
107
+ // Always-current ref so callbacks/effects don't re-subscribe on every
108
+ // onMonthChange identity change.
109
+ const onMonthChangeRef = useRef(onMonthChange);
110
+ onMonthChangeRef.current = onMonthChange;
111
+
112
+ // Request the parent move the view to `{year, month}` (controlled mode).
113
+ // Built in ISO — `year`/`month` are ISO numbers, and the locale calendar
114
+ // only affects display (see MonthYearString). Passing `calendar` here would
115
+ // reinterpret the ISO numbers as locale-calendar fields (e.g. Buddhist),
116
+ // shifting the value by centuries.
117
+ const notifyMonth = useCallback(
118
+ (year: number, month: number) => {
119
+ onMonthChangeRef.current?.(T.PlainYearMonth.from({ year, month }));
120
+ },
121
+ [T],
122
+ );
123
+
124
+ // Whether `{year, month}` falls within the currently displayed window.
125
+ const isMonthVisible = useCallback(
126
+ (targetYear: number, targetMonth: number) => {
127
+ for (let i = 0; i < numberOfMonths; i++) {
128
+ const totalMonths =
129
+ currentMonth.year * 12 + (currentMonth.month - 1) + i;
130
+ const y = Math.floor(totalMonths / 12);
131
+ const m = (totalMonths % 12) + 1;
132
+ if (targetYear === y && targetMonth === m) return true;
94
133
  }
95
- if (prev[monthIndex] === id) return prev;
96
- return { ...prev, [monthIndex]: id };
97
- });
98
- }, []);
134
+ return false;
135
+ },
136
+ [currentMonth.year, currentMonth.month, numberOfMonths],
137
+ );
99
138
 
100
- // --- navigateToMonth: only shift if target not already visible ---
139
+ // --- navigateToMonth: shift the window by the minimum needed to reveal the
140
+ // target month (keeps keyboard crossing symmetric with the Prev/Next
141
+ // buttons, which step one month at a time). ---
101
142
  const navigateToMonth = useCallback(
102
143
  (targetYear: number, targetMonth: number) => {
103
144
  if (isMonthControlled) return; // controlled mode: parent manages
104
145
  setInternalMonth((prev) => {
105
- for (let i = 0; i < numberOfMonths; i++) {
106
- const totalMonths = prev.year * 12 + (prev.month - 1) + i;
107
- const y = Math.floor(totalMonths / 12);
108
- const m = (totalMonths % 12) + 1;
109
- if (targetYear === y && targetMonth === m) {
110
- return prev; // Already visible
111
- }
112
- }
113
- return { year: targetYear, month: targetMonth };
146
+ const next = shiftWindowToMonth(
147
+ prev,
148
+ { year: targetYear, month: targetMonth },
149
+ numberOfMonths,
150
+ );
151
+ if (next.year === prev.year && next.month === prev.month) return prev;
152
+ return next;
114
153
  });
115
154
  },
116
155
  [isMonthControlled, numberOfMonths],
117
156
  );
118
157
 
119
- // Sync month when focused date crosses a month boundary
158
+ // Button navigation in controlled mode notifies the parent directly, then
159
+ // moves focus into the target month. This ref holds that focus value so the
160
+ // focus-sync effect skips re-notifying for it. Keyed by the value (not a
161
+ // one-shot flag) so it can't strand and suppress a later keyboard crossing
162
+ // if the button's focus move is a no-op or the parent ignores the change.
163
+ const skipFocusSyncForRef = useRef<Temporal.PlainDate | null>(null);
164
+ const prevFocusedRef = useRef(focusedDate);
165
+
166
+ // Clear a stale skip target whenever the parent actually moves the view.
167
+ useEffect(() => {
168
+ skipFocusSyncForRef.current = null;
169
+ }, [monthProp?.year, monthProp?.month]);
170
+
171
+ // Sync month when the focused date crosses a month boundary. Uncontrolled:
172
+ // shift internal state. Controlled: ask the parent to move the view, but
173
+ // only when focus actually moved out of the visible window (keyboard nav) —
174
+ // never as an echo of an external `month` update or a button click.
120
175
  useEffect(() => {
176
+ const prevFocused = prevFocusedRef.current;
177
+ prevFocusedRef.current = focusedDate;
178
+ if (isMonthControlled) {
179
+ if (skipFocusSyncForRef.current === focusedDate) {
180
+ skipFocusSyncForRef.current = null;
181
+ return;
182
+ }
183
+ if (prevFocused === focusedDate) return;
184
+ if (isMonthVisible(focusedDate.year, focusedDate.month)) return;
185
+ // Emit the minimally-shifted first-visible month (what the parent binds
186
+ // to `month`), not the focused month — so a controlled window scrolls by
187
+ // the minimum, matching uncontrolled mode and the nav buttons.
188
+ const next = shiftWindowToMonth(
189
+ { year: currentMonth.year, month: currentMonth.month },
190
+ { year: focusedDate.year, month: focusedDate.month },
191
+ numberOfMonths,
192
+ );
193
+ notifyMonth(next.year, next.month);
194
+ return;
195
+ }
121
196
  navigateToMonth(focusedDate.year, focusedDate.month);
122
- }, [focusedDate, navigateToMonth]);
197
+ }, [
198
+ focusedDate,
199
+ navigateToMonth,
200
+ isMonthControlled,
201
+ isMonthVisible,
202
+ notifyMonth,
203
+ currentMonth.year,
204
+ currentMonth.month,
205
+ numberOfMonths,
206
+ ]);
123
207
 
124
208
  // --- Month navigation callbacks ---
125
209
  const goNextMonth = useCallback(() => {
126
210
  if (isMonthControlled) {
127
- // Fire onMonthChange with the next month
128
211
  const { year, month, firstDay } = computeAdjacentMonth(
129
212
  { year: monthProp!.year, month: monthProp!.month },
130
213
  "next",
131
214
  T,
132
215
  );
133
- setFocusedDate((prev) => focusedDateForMonth(prev, { year, month }, firstDay));
134
- // The effect on viewingYearMonth will fire onMonthChange
216
+ // Notify the parent directly; the follow-on focus move is suppressed in
217
+ // the focus-sync effect (keyed on the value) so onMonthChange fires once.
218
+ const nextFocus = focusedDateForMonth(
219
+ focusedDate,
220
+ { year, month },
221
+ firstDay,
222
+ );
223
+ skipFocusSyncForRef.current = nextFocus;
224
+ notifyMonth(year, month);
225
+ setFocusedDate(nextFocus);
135
226
  return;
136
227
  }
137
228
  setInternalMonth((m) => {
138
229
  const { year, month, firstDay } = computeAdjacentMonth(m, "next", T);
139
- setFocusedDate((prev) => focusedDateForMonth(prev, { year, month }, firstDay));
230
+ setFocusedDate((prev) =>
231
+ focusedDateForMonth(prev, { year, month }, firstDay),
232
+ );
140
233
  return { year, month };
141
234
  });
142
- }, [T, isMonthControlled, monthProp]);
235
+ }, [T, isMonthControlled, monthProp, notifyMonth, focusedDate]);
143
236
 
144
237
  const goPrevMonth = useCallback(() => {
145
238
  if (isMonthControlled) {
@@ -148,15 +241,24 @@ function MonthViewRoot(props: MonthViewRootProps) {
148
241
  "prev",
149
242
  T,
150
243
  );
151
- setFocusedDate((prev) => focusedDateForMonth(prev, { year, month }, firstDay));
244
+ const nextFocus = focusedDateForMonth(
245
+ focusedDate,
246
+ { year, month },
247
+ firstDay,
248
+ );
249
+ skipFocusSyncForRef.current = nextFocus;
250
+ notifyMonth(year, month);
251
+ setFocusedDate(nextFocus);
152
252
  return;
153
253
  }
154
254
  setInternalMonth((m) => {
155
255
  const { year, month, firstDay } = computeAdjacentMonth(m, "prev", T);
156
- setFocusedDate((prev) => focusedDateForMonth(prev, { year, month }, firstDay));
256
+ setFocusedDate((prev) =>
257
+ focusedDateForMonth(prev, { year, month }, firstDay),
258
+ );
157
259
  return { year, month };
158
260
  });
159
- }, [T, isMonthControlled, monthProp]);
261
+ }, [T, isMonthControlled, monthProp, notifyMonth, focusedDate]);
160
262
 
161
263
  // --- Grid computation ---
162
264
  const allMonths = useMemo<MonthData[]>(() => {
@@ -169,9 +271,21 @@ function MonthViewRoot(props: MonthViewRootProps) {
169
271
  result.push({ year: y, month: m, weeks: getMonthWeeks(y, m, T, opts) });
170
272
  }
171
273
  return result;
172
- }, [currentMonth.year, currentMonth.month, T, weekStartDay, fixedWeeks, numberOfMonths]);
274
+ }, [
275
+ currentMonth.year,
276
+ currentMonth.month,
277
+ T,
278
+ weekStartDay,
279
+ fixedWeeks,
280
+ numberOfMonths,
281
+ ]);
173
282
 
174
- const weeks = allMonths[0].weeks;
283
+ // numberOfMonths >= 1, so allMonths always has at least one entry
284
+ const firstMonth = allMonths[0];
285
+ if (firstMonth === undefined) {
286
+ throw new Error("MonthView: numberOfMonths must be at least 1.");
287
+ }
288
+ const weeks = firstMonth.weeks;
175
289
 
176
290
  // --- currentDateTime ---
177
291
  const selectedZdt = useMemo(
@@ -216,25 +330,32 @@ function MonthViewRoot(props: MonthViewRootProps) {
216
330
  T,
217
331
  gridHasFocus,
218
332
  ),
219
- [focusedDate, selectedPlain, weeks, currentMonth, isDateDisabledFn, T, gridHasFocus],
333
+ [
334
+ focusedDate,
335
+ selectedPlain,
336
+ weeks,
337
+ currentMonth,
338
+ isDateDisabledFn,
339
+ T,
340
+ gridHasFocus,
341
+ ],
220
342
  );
221
343
 
222
- // --- viewingYearMonth (for onMonthChange callback) ---
223
- const localeCalendar = useMemo(() => calendarForLocale(locale), [locale]);
224
-
344
+ // --- viewingYearMonth (for the render-prop rootState) ---
345
+ // ISO — the locale calendar only affects display, not this value.
225
346
  const viewingYearMonth = useMemo(
226
347
  () =>
227
348
  T.PlainYearMonth.from({
228
349
  year: currentMonth.year,
229
350
  month: currentMonth.month,
230
- calendar: localeCalendar,
231
351
  }),
232
- [currentMonth, T, localeCalendar],
352
+ [currentMonth, T],
233
353
  );
234
354
 
235
- // --- Fire onMonthChange (not on mount) ---
236
- const onMonthChangeRef = useRef(onMonthChange);
237
- onMonthChangeRef.current = onMonthChange;
355
+ // --- Fire onMonthChange when uncontrolled navigation moves the view (not on
356
+ // mount). In controlled mode the parent owns `month`, so notification flows
357
+ // from button clicks (goNext/goPrevMonth) and the focus-sync effect instead;
358
+ // firing here would just echo the parent's own update back to it. ---
238
359
  const mountedRef = useRef(false);
239
360
 
240
361
  useEffect(() => {
@@ -242,36 +363,18 @@ function MonthViewRoot(props: MonthViewRootProps) {
242
363
  mountedRef.current = true;
243
364
  return;
244
365
  }
366
+ if (isMonthControlled) return;
245
367
  onMonthChangeRef.current?.(viewingYearMonth);
246
- }, [viewingYearMonth]);
368
+ }, [viewingYearMonth, isMonthControlled]);
247
369
 
248
370
  // --- rootState (for render functions) ---
249
- const { selected, selectedDates, rangeStart, rangeEnd } = calState;
250
-
251
371
  const rootState = useMemo<RootState>(
252
372
  () => ({
253
- hasSelection: selectedDates.length > 0,
254
- selected: selected?.value,
255
- selectedDates: selectedDates as any,
256
- rangeStart: rangeStart as any,
257
- rangeEnd: rangeEnd as any,
373
+ ...calState.baseRootState,
258
374
  focused: focusedDate,
259
375
  viewing: viewingYearMonth,
260
- timeZone,
261
- locale,
262
- readOnly: calStable.readOnly,
263
376
  }),
264
- [
265
- selectedDates,
266
- selected,
267
- rangeStart,
268
- rangeEnd,
269
- focusedDate,
270
- viewingYearMonth,
271
- timeZone,
272
- locale,
273
- calStable.readOnly,
274
- ],
377
+ [calState.baseRootState, focusedDate, viewingYearMonth],
275
378
  );
276
379
 
277
380
  // --- Context values ---
@@ -334,10 +437,12 @@ function MonthViewRoot(props: MonthViewRootProps) {
334
437
  // MonthView convenience wrapper — composes CalendarProvider + MonthView.Root
335
438
  // ---------------------------------------------------------------------------
336
439
 
337
- type MonthViewProps<F extends ValueFormat = "PlainDate"> = CalendarProviderProps<F> &
338
- MonthViewRootProps;
440
+ type MonthViewProps<F extends ValueFormat = "PlainDate"> =
441
+ CalendarProviderProps<F> & MonthViewRootProps;
339
442
 
340
- function MonthView<F extends ValueFormat = "PlainDate">(props: MonthViewProps<F>) {
443
+ function MonthView<F extends ValueFormat = "PlainDate">(
444
+ props: MonthViewProps<F>,
445
+ ) {
341
446
  const {
342
447
  numberOfMonths,
343
448
  fixedWeeks,
@@ -5,7 +5,6 @@ import { useCalendarStable, useCalendarState } from "./calendar-context";
5
5
  import { useMonthViewStable, useMonthViewState } from "./month-view-context";
6
6
  import type { StateAttributesMapping } from "./types";
7
7
  import type {
8
- ValueFormat,
9
8
  DateStringState,
10
9
  DateStringProps,
11
10
  TimeStringState,
@@ -16,7 +15,7 @@ import type {
16
15
  NextMonthButtonProps,
17
16
  NavButtonState,
18
17
  } from "./types";
19
- import { selectedToZdt, zdtToNativeDate, calendarForLocale } from "./utils";
18
+ import { selectedToZdt, zdtToNativeDate } from "./utils";
20
19
 
21
20
  const dateStringStateAttributesMapping = {
22
21
  root: () => null,
@@ -38,7 +37,10 @@ const monthYearStringStateAttributesMapping = {
38
37
  year: () => null,
39
38
  } as const satisfies StateAttributesMapping<MonthYearStringState>;
40
39
 
41
- function DateStringFn(props: DateStringProps, ref: React.ForwardedRef<HTMLSpanElement>) {
40
+ function DateStringFn(
41
+ props: DateStringProps,
42
+ ref: React.ForwardedRef<HTMLSpanElement>,
43
+ ) {
42
44
  const { render, locales, options, ...otherProps } = props;
43
45
  const { locale, temporal: T, timeZone } = useCalendarStable();
44
46
  const { selected } = useCalendarState();
@@ -57,7 +59,7 @@ function DateStringFn(props: DateStringProps, ref: React.ForwardedRef<HTMLSpanEl
57
59
 
58
60
  const state = useMemo<DateStringState>(
59
61
  () => ({
60
- root: rootState as any,
62
+ root: rootState,
61
63
  month,
62
64
  year,
63
65
  day,
@@ -84,11 +86,14 @@ function DateStringFn(props: DateStringProps, ref: React.ForwardedRef<HTMLSpanEl
84
86
  * Displays the currently selected (or current) date as localized text.
85
87
  * Renders a `<span>` with `aria-live="polite"`.
86
88
  */
87
- export const DateString = forwardRef(DateStringFn) as <F extends ValueFormat = ValueFormat>(
88
- props: DateStringProps<F> & React.RefAttributes<HTMLSpanElement>,
89
+ export const DateString = forwardRef(DateStringFn) as (
90
+ props: DateStringProps & React.RefAttributes<HTMLSpanElement>,
89
91
  ) => React.ReactElement | null;
90
92
 
91
- function TimeStringFn(props: TimeStringProps, ref: React.ForwardedRef<HTMLSpanElement>) {
93
+ function TimeStringFn(
94
+ props: TimeStringProps,
95
+ ref: React.ForwardedRef<HTMLSpanElement>,
96
+ ) {
92
97
  const { render, locales, options, ...otherProps } = props;
93
98
  const { locale, temporal: T, timeZone } = useCalendarStable();
94
99
  const { selected } = useCalendarState();
@@ -100,7 +105,10 @@ function TimeStringFn(props: TimeStringProps, ref: React.ForwardedRef<HTMLSpanEl
100
105
  : zdtToNativeDate(T.Now.zonedDateTimeISO(timeZone));
101
106
 
102
107
  const mergedOptions: Intl.DateTimeFormatOptions = { timeZone, ...options };
103
- const formatted = displayDate.toLocaleTimeString(locales ?? locale, mergedOptions);
108
+ const formatted = displayDate.toLocaleTimeString(
109
+ locales ?? locale,
110
+ mergedOptions,
111
+ );
104
112
 
105
113
  const nowZdt = selZdt ?? T.Now.zonedDateTimeISO(timeZone);
106
114
  const hour = nowZdt.hour;
@@ -109,7 +117,7 @@ function TimeStringFn(props: TimeStringProps, ref: React.ForwardedRef<HTMLSpanEl
109
117
 
110
118
  const state = useMemo<TimeStringState>(
111
119
  () => ({
112
- root: rootState as any,
120
+ root: rootState,
113
121
  hour,
114
122
  minute,
115
123
  second,
@@ -137,16 +145,29 @@ function TimeStringFn(props: TimeStringProps, ref: React.ForwardedRef<HTMLSpanEl
137
145
  * Falls back to the current time when nothing is selected.
138
146
  * Renders a `<span>` with `aria-live="polite"`.
139
147
  */
140
- export const TimeString = forwardRef(TimeStringFn) as <F extends ValueFormat = ValueFormat>(
141
- props: TimeStringProps<F> & React.RefAttributes<HTMLSpanElement>,
148
+ export const TimeString = forwardRef(TimeStringFn) as (
149
+ props: TimeStringProps & React.RefAttributes<HTMLSpanElement>,
142
150
  ) => React.ReactElement | null;
143
151
 
144
- function MonthYearStringFn(props: MonthYearStringProps, ref: React.ForwardedRef<HTMLSpanElement>) {
145
- const { render, locales, options, monthIndex: monthIndexProp, ...otherProps } = props;
152
+ function MonthYearStringFn(
153
+ props: MonthYearStringProps,
154
+ ref: React.ForwardedRef<HTMLSpanElement>,
155
+ ) {
156
+ const {
157
+ render,
158
+ locales,
159
+ options,
160
+ monthIndex: monthIndexProp,
161
+ ...otherProps
162
+ } = props;
146
163
  const monthIndex = monthIndexProp ?? 0;
147
- const { locale } = useCalendarStable();
164
+ const { locale, temporal: T } = useCalendarStable();
148
165
  const monthViewStable = useMonthViewStable();
149
- const { currentMonth: currentDateTime, allMonths, rootState } = useMonthViewState();
166
+ const {
167
+ currentMonth: currentDateTime,
168
+ allMonths,
169
+ rootState,
170
+ } = useMonthViewState();
150
171
  const { setGridLabelId } = monthViewStable;
151
172
 
152
173
  const id = useId();
@@ -160,16 +181,23 @@ function MonthYearStringFn(props: MonthYearStringProps, ref: React.ForwardedRef<
160
181
  const displayYear = monthData?.year ?? currentDateTime.year;
161
182
  const displayMonth = monthData?.month ?? currentDateTime.month;
162
183
 
163
- const displayDate = new Date(displayYear, displayMonth - 1, 1);
164
184
  const defaultOptions: Intl.DateTimeFormatOptions = options ?? {
165
185
  month: "long",
166
186
  year: "numeric",
167
187
  };
168
- const formatted = displayDate.toLocaleDateString(locales ?? locale, defaultOptions);
188
+ // Format through Temporal
189
+ // `displayYear`/`displayMonth` are ISO; formatting an ISO
190
+ // `PlainDate` (not `PlainYearMonth`, which throws on calendar mismatch)
191
+ // lets Intl render it in the locale's calendar (e.g. Buddhist for th-TH).
192
+ const formatted = T.PlainDate.from({
193
+ year: displayYear,
194
+ month: displayMonth,
195
+ day: 1,
196
+ }).toLocaleString(locales ?? locale, defaultOptions);
169
197
 
170
198
  const state = useMemo<MonthYearStringState>(
171
199
  () => ({
172
- root: rootState as any,
200
+ root: rootState,
173
201
  month: displayMonth,
174
202
  year: displayYear,
175
203
  }),
@@ -204,14 +232,17 @@ function MonthYearStringFn(props: MonthYearStringProps, ref: React.ForwardedRef<
204
232
  * referenced by the calendar grid's `aria-labelledby` and serves as its
205
233
  * accessible name.
206
234
  */
207
- export const MonthYearString = forwardRef(MonthYearStringFn) as <
208
- F extends ValueFormat = ValueFormat,
209
- >(
210
- props: MonthYearStringProps<F> & React.RefAttributes<HTMLSpanElement>,
235
+ export const MonthYearString = forwardRef(MonthYearStringFn) as (
236
+ props: MonthYearStringProps & React.RefAttributes<HTMLSpanElement>,
211
237
  ) => React.ReactElement | null;
212
238
 
213
- function useNavButton<F extends ValueFormat = ValueFormat>(direction: "prev" | "next") {
214
- const { disabled: globalDisabled, minValue, maxValue, locale, temporal: T } = useCalendarStable();
239
+ function useNavButton(direction: "prev" | "next") {
240
+ const {
241
+ disabled: globalDisabled,
242
+ minValue,
243
+ maxValue,
244
+ temporal: T,
245
+ } = useCalendarStable();
215
246
  const monthViewStable = useMonthViewStable();
216
247
  const monthViewState = useMonthViewState();
217
248
  const {
@@ -219,12 +250,18 @@ function useNavButton<F extends ValueFormat = ValueFormat>(direction: "prev" | "
219
250
  goPrevMonth: goToPrevMonth,
220
251
  numberOfMonths,
221
252
  } = monthViewStable;
222
- const { currentMonth: currentDateTime, allMonths, rootState } = monthViewState;
253
+ const {
254
+ currentMonth: currentDateTime,
255
+ allMonths,
256
+ rootState,
257
+ } = monthViewState;
223
258
 
224
259
  // For "next", compute destination from the last visible month
225
260
  // For "prev", compute destination from the first visible month
226
261
  const refMonth =
227
- direction === "next" ? (allMonths[numberOfMonths - 1] ?? currentDateTime) : currentDateTime;
262
+ direction === "next"
263
+ ? (allMonths[numberOfMonths - 1] ?? currentDateTime)
264
+ : currentDateTime;
228
265
 
229
266
  const destMonth =
230
267
  direction === "prev"
@@ -251,28 +288,25 @@ function useNavButton<F extends ValueFormat = ValueFormat>(direction: "prev" | "
251
288
  if (!boundValue) return false;
252
289
  if (direction === "prev") {
253
290
  return (
254
- destYear < boundValue.year || (destYear === boundValue.year && destMonth < boundValue.month)
291
+ destYear < boundValue.year ||
292
+ (destYear === boundValue.year && destMonth < boundValue.month)
255
293
  );
256
294
  }
257
295
  return (
258
- destYear > boundValue.year || (destYear === boundValue.year && destMonth > boundValue.month)
296
+ destYear > boundValue.year ||
297
+ (destYear === boundValue.year && destMonth > boundValue.month)
259
298
  );
260
299
  }, [globalDisabled, destYear, destMonth, boundValue, direction]);
261
300
 
262
- const localeCalendar = useMemo(() => calendarForLocale(locale), [locale]);
263
-
301
+ // ISO — `destYear`/`destMonth` are ISO numbers; the locale calendar only
302
+ // affects display, so it must not be injected into this value.
264
303
  const target = useMemo(
265
- () =>
266
- T.PlainYearMonth.from({
267
- year: destYear,
268
- month: destMonth,
269
- calendar: localeCalendar,
270
- }),
271
- [destYear, destMonth, T, localeCalendar],
304
+ () => T.PlainYearMonth.from({ year: destYear, month: destMonth }),
305
+ [destYear, destMonth, T],
272
306
  );
273
307
 
274
- const state = useMemo<NavButtonState<F>>(
275
- () => ({ root: rootState as any, direction, disabled: isDisabled, target }),
308
+ const state = useMemo<NavButtonState>(
309
+ () => ({ root: rootState, direction, disabled: isDisabled, target }),
276
310
  [rootState, direction, isDisabled, target],
277
311
  );
278
312
 
@@ -316,10 +350,8 @@ function PrevMonthButtonFn(
316
350
  * Button that navigates to the previous month. Automatically disabled
317
351
  * when the previous month falls before `min`. Exposes `data-direction="prev"`.
318
352
  */
319
- export const PrevMonthButton = forwardRef(PrevMonthButtonFn) as <
320
- F extends ValueFormat = ValueFormat,
321
- >(
322
- props: PrevMonthButtonProps<F> & React.RefAttributes<HTMLButtonElement>,
353
+ export const PrevMonthButton = forwardRef(PrevMonthButtonFn) as (
354
+ props: PrevMonthButtonProps & React.RefAttributes<HTMLButtonElement>,
323
355
  ) => React.ReactElement | null;
324
356
 
325
357
  function NextMonthButtonFn(
@@ -343,8 +375,6 @@ function NextMonthButtonFn(
343
375
  * Button that navigates to the next month. Automatically disabled
344
376
  * when the next month falls after `max`. Exposes `data-direction="next"`.
345
377
  */
346
- export const NextMonthButton = forwardRef(NextMonthButtonFn) as <
347
- F extends ValueFormat = ValueFormat,
348
- >(
349
- props: NextMonthButtonProps<F> & React.RefAttributes<HTMLButtonElement>,
378
+ export const NextMonthButton = forwardRef(NextMonthButtonFn) as (
379
+ props: NextMonthButtonProps & React.RefAttributes<HTMLButtonElement>,
350
380
  ) => React.ReactElement | null;
@@ -67,19 +67,21 @@ describe("applyOverflow", () => {
67
67
  useMinMax: false,
68
68
  expected: { firstWeek: "2026-06-01", weekCount: 8 },
69
69
  },
70
- ])("$description", ({ targetFirstWeek, weekCount, behavior, useMinMax, expected }) => {
71
- const result = applyOverflow({
72
- targetFirstWeek,
73
- weekCount,
74
- behavior,
75
- min: useMinMax ? min : undefined,
76
- max: useMinMax ? max : undefined,
77
- weekStartDay: 0,
78
- T,
79
- });
80
- expect(result.firstWeek.toString()).toBe(expected.firstWeek);
81
- expect(result.weekCount).toBe(expected.weekCount);
82
- });
70
+ ])(
71
+ "$description",
72
+ ({ targetFirstWeek, weekCount, behavior, useMinMax, expected }) => {
73
+ const result = applyOverflow({
74
+ targetFirstWeek,
75
+ weekCount,
76
+ behavior,
77
+ ...(useMinMax && { min, max }),
78
+ weekStartDay: 0,
79
+ T,
80
+ });
81
+ expect(result.firstWeek.toString()).toBe(expected.firstWeek);
82
+ expect(result.weekCount).toBe(expected.weekCount);
83
+ },
84
+ );
83
85
  });
84
86
 
85
87
  describe("canShift", () => {
package/src/overflow.ts CHANGED
@@ -1,7 +1,12 @@
1
1
  import type { Temporal } from "@js-temporal/polyfill";
2
2
  import type { TemporalNamespace, WeekStartDay } from "./types";
3
3
 
4
- export type OverflowBehavior = "unbounded" | "stop" | "stop-shrink" | "snap" | "snap-shrink";
4
+ export type OverflowBehavior =
5
+ | "unbounded"
6
+ | "stop"
7
+ | "stop-shrink"
8
+ | "snap"
9
+ | "snap-shrink";
5
10
 
6
11
  export type MonthOverflowBehavior = "unbounded" | "stop";
7
12
 
@@ -19,7 +24,10 @@ function weekHasValidDay(
19
24
  // Week spans [weekStart, weekStart+6]. Overlaps with [min, max] iff
20
25
  // weekStart <= max AND weekStart+6 >= min.
21
26
  const weekEnd = weekStart.add({ days: 6 });
22
- return T.PlainDate.compare(weekStart, max) <= 0 && T.PlainDate.compare(weekEnd, min) >= 0;
27
+ return (
28
+ T.PlainDate.compare(weekStart, max) <= 0 &&
29
+ T.PlainDate.compare(weekEnd, min) >= 0
30
+ );
23
31
  }
24
32
 
25
33
  /**