@klinking/colander 0.0.0

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 +49 -0
  2. package/dist/formats/date.d.cts +45 -0
  3. package/dist/formats/date.d.mts +45 -0
  4. package/dist/formats/date.mjs +2 -0
  5. package/dist/formats/object.cjs +49 -0
  6. package/dist/formats/object.d.cts +45 -0
  7. package/dist/formats/object.d.mts +45 -0
  8. package/dist/formats/object.mjs +2 -0
  9. package/dist/formats/plain-date-time.cjs +49 -0
  10. package/dist/formats/plain-date-time.d.cts +45 -0
  11. package/dist/formats/plain-date-time.d.mts +45 -0
  12. package/dist/formats/plain-date-time.mjs +2 -0
  13. package/dist/formats/plain-date.cjs +49 -0
  14. package/dist/formats/plain-date.d.cts +45 -0
  15. package/dist/formats/plain-date.d.mts +45 -0
  16. package/dist/formats/plain-date.mjs +2 -0
  17. package/dist/formats/plain-month-day.cjs +49 -0
  18. package/dist/formats/plain-month-day.d.cts +45 -0
  19. package/dist/formats/plain-month-day.d.mts +45 -0
  20. package/dist/formats/plain-month-day.mjs +2 -0
  21. package/dist/formats/plain-year-month.cjs +49 -0
  22. package/dist/formats/plain-year-month.d.cts +45 -0
  23. package/dist/formats/plain-year-month.d.mts +45 -0
  24. package/dist/formats/plain-year-month.mjs +2 -0
  25. package/dist/formats/zoned-date-time.cjs +49 -0
  26. package/dist/formats/zoned-date-time.d.cts +45 -0
  27. package/dist/formats/zoned-date-time.d.mts +45 -0
  28. package/dist/formats/zoned-date-time.mjs +2 -0
  29. package/dist/index.cjs +49 -0
  30. package/dist/index.d.cts +2 -0
  31. package/dist/index.d.mts +2 -0
  32. package/dist/index.mjs +2 -0
  33. package/dist/temporal-polyfill-DTx0VIX5.mjs +4556 -0
  34. package/dist/temporal-polyfill-DU8VfleL.cjs +4860 -0
  35. package/dist/weeks-view-context-BN6qR-83.d.mts +2022 -0
  36. package/dist/weeks-view-context-aHGgYkpt.d.cts +2022 -0
  37. package/package.json +89 -0
  38. package/src/calendar-context.ts +22 -0
  39. package/src/calendar-provider.test.tsx +123 -0
  40. package/src/calendar-provider.tsx +538 -0
  41. package/src/calendar-types.ts +212 -0
  42. package/src/compute-weeks-in-window.test.ts +83 -0
  43. package/src/compute-weeks-in-window.ts +40 -0
  44. package/src/context.ts +34 -0
  45. package/src/day-cell.tsx +635 -0
  46. package/src/drag-handle.tsx +137 -0
  47. package/src/formats/date.ts +110 -0
  48. package/src/formats/formats.test.ts +18 -0
  49. package/src/formats/object.ts +110 -0
  50. package/src/formats/plain-date-time.ts +110 -0
  51. package/src/formats/plain-date.ts +110 -0
  52. package/src/formats/plain-month-day.ts +110 -0
  53. package/src/formats/plain-year-month.ts +110 -0
  54. package/src/formats/zoned-date-time.ts +110 -0
  55. package/src/grid-header.tsx +141 -0
  56. package/src/grid.event-perf.test.tsx +282 -0
  57. package/src/grid.perf.test.tsx +467 -0
  58. package/src/grid.tsx +608 -0
  59. package/src/index.ts +116 -0
  60. package/src/keyboard.test.ts +498 -0
  61. package/src/keyboard.ts +142 -0
  62. package/src/month-separator.test.tsx +183 -0
  63. package/src/month-separator.tsx +238 -0
  64. package/src/month-view-context.ts +26 -0
  65. package/src/month-view-types.ts +89 -0
  66. package/src/month-view.test.tsx +1433 -0
  67. package/src/month-view.tsx +373 -0
  68. package/src/navigation.tsx +350 -0
  69. package/src/overflow.test.ts +124 -0
  70. package/src/overflow.ts +233 -0
  71. package/src/range-preview.tsx +90 -0
  72. package/src/resolve-first-week.test.ts +132 -0
  73. package/src/resolve-first-week.ts +145 -0
  74. package/src/root-selection.ts +621 -0
  75. package/src/selected-range.tsx +163 -0
  76. package/src/temporal-polyfill.test.ts +317 -0
  77. package/src/temporal-polyfill.ts +566 -0
  78. package/src/test-temporal.ts +17 -0
  79. package/src/types.ts +814 -0
  80. package/src/utils.bench.ts +183 -0
  81. package/src/utils.test.ts +983 -0
  82. package/src/utils.ts +493 -0
  83. package/src/view-context.ts +28 -0
  84. package/src/week-number.tsx +118 -0
  85. package/src/weeks-grid.tsx +265 -0
  86. package/src/weeks-keyboard.test.ts +222 -0
  87. package/src/weeks-keyboard.ts +156 -0
  88. package/src/weeks-navigation.tsx +209 -0
  89. package/src/weeks-view-context.ts +26 -0
  90. package/src/weeks-view-state.ts +421 -0
  91. package/src/weeks-view-types.ts +102 -0
  92. package/src/weeks-view.test.tsx +479 -0
  93. package/src/weeks-view.tsx +127 -0
@@ -0,0 +1,373 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
+ import { useCalendarStable, useCalendarState } from "./calendar-context";
4
+ import { CalendarProvider } from "./calendar-provider";
5
+ import type { CalendarProviderProps } from "./calendar-types";
6
+ import { MonthViewStableContext, MonthViewStateContext } from "./month-view-context";
7
+ import type {
8
+ MonthViewRootProps,
9
+ MonthViewStableContextValue,
10
+ MonthViewStateContextValue,
11
+ } from "./month-view-types";
12
+ import type { MonthData, RootState, ValueFormat } from "./types";
13
+ import {
14
+ calendarForLocale,
15
+ computeAdjacentMonth,
16
+ focusedDateForMonth,
17
+ getMonthWeeks,
18
+ resolveFocusTarget,
19
+ selectedToZdt,
20
+ toZonedDateTime,
21
+ } from "./utils";
22
+ import { ViewContext, type ViewContextValue } from "./view-context";
23
+
24
+ // ---------------------------------------------------------------------------
25
+ // MonthView.Root — placed inside CalendarProvider
26
+ // ---------------------------------------------------------------------------
27
+
28
+ function MonthViewRoot(props: MonthViewRootProps) {
29
+ const {
30
+ numberOfMonths: numberOfMonthsProp,
31
+ fixedWeeks: fixedWeeksProp,
32
+ outsideDays: outsideDaysProp,
33
+ overflowBehavior: overflowBehaviorProp,
34
+ month: monthProp,
35
+ defaultMonth: defaultMonthProp,
36
+ onMonthChange,
37
+ children,
38
+ } = props;
39
+
40
+ const numberOfMonths = Math.max(1, Math.min(numberOfMonthsProp ?? 1, 12));
41
+ const fixedWeeks = fixedWeeksProp ?? false;
42
+ const outsideDays = outsideDaysProp ?? "enabled";
43
+ const overflowBehavior = overflowBehaviorProp ?? "unbounded";
44
+
45
+ // Read calendar-level context
46
+ const calStable = useCalendarStable();
47
+ const calState = useCalendarState();
48
+
49
+ const T = calStable.temporal;
50
+ const { timeZone, locale, weekStartDay, isDateDisabled } = calStable;
51
+
52
+ // --- Month navigation state (controlled/uncontrolled) ---
53
+ const isMonthControlled = monthProp !== undefined;
54
+
55
+ const [internalMonth, setInternalMonth] = useState<{
56
+ year: number;
57
+ month: number;
58
+ }>(() => {
59
+ if (monthProp) return { year: monthProp.year, month: monthProp.month };
60
+ if (defaultMonthProp) return { year: defaultMonthProp.year, month: defaultMonthProp.month };
61
+ // Derive from selection or today
62
+ if (calState.selected) {
63
+ const zdt = toZonedDateTime(calState.selected, timeZone, T);
64
+ return { year: zdt.year, month: zdt.month };
65
+ }
66
+ const now = T.Now.zonedDateTimeISO(timeZone);
67
+ return { year: now.year, month: now.month };
68
+ });
69
+
70
+ const currentMonth = isMonthControlled
71
+ ? { year: monthProp.year, month: monthProp.month }
72
+ : internalMonth;
73
+
74
+ // --- Focus state ---
75
+ const [focusedDate, setFocusedDate] = useState<Temporal.PlainDate>(() => {
76
+ if (calState.selected) {
77
+ return toZonedDateTime(calState.selected, timeZone, T).toPlainDate();
78
+ }
79
+ return T.Now.plainDateISO();
80
+ });
81
+
82
+ const [gridHasFocus, setGridHasFocus] = useState(false);
83
+ const gridFocusedRef = useRef(false);
84
+
85
+ // --- Grid label IDs ---
86
+ 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;
94
+ }
95
+ if (prev[monthIndex] === id) return prev;
96
+ return { ...prev, [monthIndex]: id };
97
+ });
98
+ }, []);
99
+
100
+ // --- navigateToMonth: only shift if target not already visible ---
101
+ const navigateToMonth = useCallback(
102
+ (targetYear: number, targetMonth: number) => {
103
+ if (isMonthControlled) return; // controlled mode: parent manages
104
+ 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 };
114
+ });
115
+ },
116
+ [isMonthControlled, numberOfMonths],
117
+ );
118
+
119
+ // Sync month when focused date crosses a month boundary
120
+ useEffect(() => {
121
+ navigateToMonth(focusedDate.year, focusedDate.month);
122
+ }, [focusedDate, navigateToMonth]);
123
+
124
+ // --- Month navigation callbacks ---
125
+ const goNextMonth = useCallback(() => {
126
+ if (isMonthControlled) {
127
+ // Fire onMonthChange with the next month
128
+ const { year, month, firstDay } = computeAdjacentMonth(
129
+ { year: monthProp!.year, month: monthProp!.month },
130
+ "next",
131
+ T,
132
+ );
133
+ setFocusedDate((prev) => focusedDateForMonth(prev, { year, month }, firstDay));
134
+ // The effect on viewingYearMonth will fire onMonthChange
135
+ return;
136
+ }
137
+ setInternalMonth((m) => {
138
+ const { year, month, firstDay } = computeAdjacentMonth(m, "next", T);
139
+ setFocusedDate((prev) => focusedDateForMonth(prev, { year, month }, firstDay));
140
+ return { year, month };
141
+ });
142
+ }, [T, isMonthControlled, monthProp]);
143
+
144
+ const goPrevMonth = useCallback(() => {
145
+ if (isMonthControlled) {
146
+ const { year, month, firstDay } = computeAdjacentMonth(
147
+ { year: monthProp!.year, month: monthProp!.month },
148
+ "prev",
149
+ T,
150
+ );
151
+ setFocusedDate((prev) => focusedDateForMonth(prev, { year, month }, firstDay));
152
+ return;
153
+ }
154
+ setInternalMonth((m) => {
155
+ const { year, month, firstDay } = computeAdjacentMonth(m, "prev", T);
156
+ setFocusedDate((prev) => focusedDateForMonth(prev, { year, month }, firstDay));
157
+ return { year, month };
158
+ });
159
+ }, [T, isMonthControlled, monthProp]);
160
+
161
+ // --- Grid computation ---
162
+ const allMonths = useMemo<MonthData[]>(() => {
163
+ const opts = { weekStartDay, fixedWeeks };
164
+ const result: MonthData[] = [];
165
+ for (let i = 0; i < numberOfMonths; i++) {
166
+ const totalMonths = currentMonth.year * 12 + (currentMonth.month - 1) + i;
167
+ const y = Math.floor(totalMonths / 12);
168
+ const m = (totalMonths % 12) + 1;
169
+ result.push({ year: y, month: m, weeks: getMonthWeeks(y, m, T, opts) });
170
+ }
171
+ return result;
172
+ }, [currentMonth.year, currentMonth.month, T, weekStartDay, fixedWeeks, numberOfMonths]);
173
+
174
+ const weeks = allMonths[0].weeks;
175
+
176
+ // --- currentDateTime ---
177
+ const selectedZdt = useMemo(
178
+ () => selectedToZdt(calState.selected, timeZone, T),
179
+ [calState.selected, timeZone, T],
180
+ );
181
+
182
+ const currentDateTime = useMemo<Temporal.PlainDateTime>(
183
+ () =>
184
+ T.PlainDateTime.from(
185
+ {
186
+ year: currentMonth.year,
187
+ month: currentMonth.month,
188
+ day: focusedDate.day,
189
+ hour: selectedZdt?.hour ?? 0,
190
+ minute: selectedZdt?.minute ?? 0,
191
+ second: selectedZdt?.second ?? 0,
192
+ },
193
+ { overflow: "constrain" },
194
+ ),
195
+ [currentMonth, focusedDate.day, selectedZdt, T],
196
+ );
197
+
198
+ // --- tabTargetDate ---
199
+ const selectedPlain = selectedZdt?.toPlainDate();
200
+
201
+ const isDateDisabledFn = useCallback(
202
+ (date: Temporal.PlainDate): boolean => {
203
+ return isDateDisabled?.(date) ?? false;
204
+ },
205
+ [isDateDisabled],
206
+ );
207
+
208
+ const tabTargetDate = useMemo(
209
+ () =>
210
+ resolveFocusTarget(
211
+ focusedDate,
212
+ selectedPlain,
213
+ weeks,
214
+ currentMonth,
215
+ isDateDisabledFn,
216
+ T,
217
+ gridHasFocus,
218
+ ),
219
+ [focusedDate, selectedPlain, weeks, currentMonth, isDateDisabledFn, T, gridHasFocus],
220
+ );
221
+
222
+ // --- viewingYearMonth (for onMonthChange callback) ---
223
+ const localeCalendar = useMemo(() => calendarForLocale(locale), [locale]);
224
+
225
+ const viewingYearMonth = useMemo(
226
+ () =>
227
+ T.PlainYearMonth.from({
228
+ year: currentMonth.year,
229
+ month: currentMonth.month,
230
+ calendar: localeCalendar,
231
+ }),
232
+ [currentMonth, T, localeCalendar],
233
+ );
234
+
235
+ // --- Fire onMonthChange (not on mount) ---
236
+ const onMonthChangeRef = useRef(onMonthChange);
237
+ onMonthChangeRef.current = onMonthChange;
238
+ const mountedRef = useRef(false);
239
+
240
+ useEffect(() => {
241
+ if (!mountedRef.current) {
242
+ mountedRef.current = true;
243
+ return;
244
+ }
245
+ onMonthChangeRef.current?.(viewingYearMonth);
246
+ }, [viewingYearMonth]);
247
+
248
+ // --- rootState (for render functions) ---
249
+ const { selected, selectedDates, rangeStart, rangeEnd } = calState;
250
+
251
+ const rootState = useMemo<RootState>(
252
+ () => ({
253
+ hasSelection: selectedDates.length > 0,
254
+ selected: selected?.value,
255
+ selectedDates: selectedDates as any,
256
+ rangeStart: rangeStart as any,
257
+ rangeEnd: rangeEnd as any,
258
+ focused: focusedDate,
259
+ viewing: viewingYearMonth,
260
+ timeZone,
261
+ locale,
262
+ readOnly: calStable.readOnly,
263
+ }),
264
+ [
265
+ selectedDates,
266
+ selected,
267
+ rangeStart,
268
+ rangeEnd,
269
+ focusedDate,
270
+ viewingYearMonth,
271
+ timeZone,
272
+ locale,
273
+ calStable.readOnly,
274
+ ],
275
+ );
276
+
277
+ // --- Context values ---
278
+ const stableCtx = useMemo<MonthViewStableContextValue>(
279
+ () => ({
280
+ numberOfMonths,
281
+ fixedWeeks,
282
+ outsideDays,
283
+ overflowBehavior,
284
+ goNextMonth,
285
+ goPrevMonth,
286
+ setGridLabelId,
287
+ gridFocusedRef,
288
+ }),
289
+ [
290
+ numberOfMonths,
291
+ fixedWeeks,
292
+ outsideDays,
293
+ overflowBehavior,
294
+ goNextMonth,
295
+ goPrevMonth,
296
+ setGridLabelId,
297
+ ],
298
+ );
299
+
300
+ const stateCtx = useMemo<MonthViewStateContextValue>(
301
+ () => ({
302
+ currentMonth,
303
+ weeks,
304
+ allMonths,
305
+ currentDateTime,
306
+ gridLabelIds,
307
+ rootState,
308
+ }),
309
+ [currentMonth, weeks, allMonths, currentDateTime, gridLabelIds, rootState],
310
+ );
311
+
312
+ const viewCtx = useMemo<ViewContextValue>(
313
+ () => ({
314
+ viewType: "month" as const,
315
+ focusedDate,
316
+ setFocusedDate,
317
+ tabTargetDate,
318
+ gridHasFocus,
319
+ setGridHasFocus,
320
+ }),
321
+ [focusedDate, tabTargetDate, gridHasFocus],
322
+ );
323
+
324
+ return (
325
+ <MonthViewStableContext.Provider value={stableCtx}>
326
+ <MonthViewStateContext.Provider value={stateCtx}>
327
+ <ViewContext.Provider value={viewCtx}>{children}</ViewContext.Provider>
328
+ </MonthViewStateContext.Provider>
329
+ </MonthViewStableContext.Provider>
330
+ );
331
+ }
332
+
333
+ // ---------------------------------------------------------------------------
334
+ // MonthView convenience wrapper — composes CalendarProvider + MonthView.Root
335
+ // ---------------------------------------------------------------------------
336
+
337
+ type MonthViewProps<F extends ValueFormat = "PlainDate"> = CalendarProviderProps<F> &
338
+ MonthViewRootProps;
339
+
340
+ function MonthView<F extends ValueFormat = "PlainDate">(props: MonthViewProps<F>) {
341
+ const {
342
+ numberOfMonths,
343
+ fixedWeeks,
344
+ outsideDays,
345
+ overflowBehavior,
346
+ month,
347
+ defaultMonth,
348
+ onMonthChange,
349
+ children,
350
+ ...calendarProps
351
+ } = props as MonthViewProps<F> & { children?: React.ReactNode };
352
+
353
+ return (
354
+ <CalendarProvider {...(calendarProps as CalendarProviderProps<F>)}>
355
+ <MonthViewRoot
356
+ numberOfMonths={numberOfMonths}
357
+ fixedWeeks={fixedWeeks}
358
+ outsideDays={outsideDays}
359
+ overflowBehavior={overflowBehavior}
360
+ month={month}
361
+ defaultMonth={defaultMonth}
362
+ onMonthChange={onMonthChange}
363
+ >
364
+ {children}
365
+ </MonthViewRoot>
366
+ </CalendarProvider>
367
+ );
368
+ }
369
+
370
+ MonthView.Root = MonthViewRoot;
371
+
372
+ export { MonthView, MonthViewRoot };
373
+ export type { MonthViewProps };