@klinking/colander 3.1.0-alpha.4 → 3.1.0-alpha.6

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 (55) hide show
  1. package/dist/formats/date.cjs +1 -1
  2. package/dist/formats/date.d.cts +1 -1
  3. package/dist/formats/date.d.mts +1 -1
  4. package/dist/formats/date.mjs +1 -1
  5. package/dist/formats/object.cjs +1 -1
  6. package/dist/formats/object.d.cts +1 -1
  7. package/dist/formats/object.d.mts +1 -1
  8. package/dist/formats/object.mjs +1 -1
  9. package/dist/formats/plain-date-time.cjs +1 -1
  10. package/dist/formats/plain-date-time.d.cts +1 -1
  11. package/dist/formats/plain-date-time.d.mts +1 -1
  12. package/dist/formats/plain-date-time.mjs +1 -1
  13. package/dist/formats/plain-date.cjs +1 -1
  14. package/dist/formats/plain-date.d.cts +1 -1
  15. package/dist/formats/plain-date.d.mts +1 -1
  16. package/dist/formats/plain-date.mjs +1 -1
  17. package/dist/formats/plain-month-day.cjs +1 -1
  18. package/dist/formats/plain-month-day.d.cts +1 -1
  19. package/dist/formats/plain-month-day.d.mts +1 -1
  20. package/dist/formats/plain-month-day.mjs +1 -1
  21. package/dist/formats/plain-year-month.cjs +1 -1
  22. package/dist/formats/plain-year-month.d.cts +1 -1
  23. package/dist/formats/plain-year-month.d.mts +1 -1
  24. package/dist/formats/plain-year-month.mjs +1 -1
  25. package/dist/formats/zoned-date-time.cjs +1 -1
  26. package/dist/formats/zoned-date-time.d.cts +1 -1
  27. package/dist/formats/zoned-date-time.d.mts +1 -1
  28. package/dist/formats/zoned-date-time.mjs +1 -1
  29. package/dist/{weeks-view-context-CUCy8njs.d.cts → index-DnFCKyyw.d.cts} +71 -8
  30. package/dist/{weeks-view-context-E1NwyNgQ.d.mts → index-q3cQKJdA.d.mts} +71 -8
  31. package/dist/index.cjs +1 -1
  32. package/dist/index.d.cts +2 -11
  33. package/dist/index.d.mts +2 -11
  34. package/dist/index.mjs +1 -1
  35. package/dist/{weeks-navigation-elCpdfQ3.mjs → weeks-navigation-Dn3UtG76.mjs} +578 -589
  36. package/dist/{weeks-navigation-DV538__x.cjs → weeks-navigation-SuKJZJCM.cjs} +578 -589
  37. package/package.json +1 -1
  38. package/src/calendar-provider.tsx +10 -55
  39. package/src/day-cell-state.test.ts +214 -0
  40. package/src/day-cell-state.ts +314 -0
  41. package/src/day-cell.tsx +29 -383
  42. package/src/month-view-navigation.ts +180 -0
  43. package/src/month-view-types.ts +3 -0
  44. package/src/month-view.bounds.test.tsx +136 -0
  45. package/src/month-view.tsx +37 -205
  46. package/src/month-window.test.ts +207 -0
  47. package/src/month-window.ts +93 -0
  48. package/src/navigation.tsx +15 -74
  49. package/src/range-preview.test.ts +120 -0
  50. package/src/root-selection.ts +99 -97
  51. package/src/weeks-navigation.tsx +7 -32
  52. package/src/weeks-view-state.ts +24 -81
  53. package/src/weeks-view-types.ts +4 -1
  54. package/src/weeks-window.test.ts +43 -0
  55. package/src/weeks-window.ts +64 -0
@@ -2,7 +2,7 @@ import type { Temporal } from "@js-temporal/polyfill";
2
2
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
3
  import { useCalendarStable, useCalendarState } from "./calendar-context";
4
4
  import { computeWeeksInWindow } from "./compute-weeks-in-window";
5
- import { applyOutOfRange, type OutOfRangeBehavior } from "./out-of-range";
5
+ import type { OutOfRangeBehavior } from "./out-of-range";
6
6
  import {
7
7
  resolveFirstWeekSpec,
8
8
  resolveFirstWeek,
@@ -21,6 +21,7 @@ import type {
21
21
  WeeksViewStateContextValue,
22
22
  WindowInfo,
23
23
  } from "./weeks-view-types";
24
+ import { createWeekWindowPolicy } from "./weeks-window";
24
25
 
25
26
  /**
26
27
  * Core state management hook for WeeksView.Root.
@@ -79,26 +80,22 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
79
80
  );
80
81
 
81
82
  // --- Apply out-of-range behavior ---
82
- const outOfRangeResult = useMemo(
83
+ const windowPolicy = useMemo(
83
84
  () =>
84
- applyOutOfRange({
85
- targetFirstWeek: resolvedFirstWeek,
85
+ createWeekWindowPolicy({
86
86
  weekCount,
87
87
  behavior: outOfRangeBehavior,
88
- ...(minValue !== undefined && { min: minValue }),
89
- ...(maxValue !== undefined && { max: maxValue }),
88
+ min: minValue,
89
+ max: maxValue,
90
90
  weekStartDay,
91
91
  T,
92
92
  }),
93
- [
94
- resolvedFirstWeek,
95
- weekCount,
96
- outOfRangeBehavior,
97
- minValue,
98
- maxValue,
99
- weekStartDay,
100
- T,
101
- ],
93
+ [weekCount, outOfRangeBehavior, minValue, maxValue, weekStartDay, T],
94
+ );
95
+
96
+ const outOfRangeResult = useMemo(
97
+ () => windowPolicy.resolve(resolvedFirstWeek),
98
+ [windowPolicy, resolvedFirstWeek],
102
99
  );
103
100
 
104
101
  // Recompute weeks if the out-of-range behavior adjusted the window
@@ -312,56 +309,20 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
312
309
 
313
310
  const goNext = useCallback(
314
311
  (shiftBy?: number) => {
315
- const shift = shiftBy ?? weekCount;
316
- const target = resolvedFirstWeek.add({ weeks: shift });
317
- const adjusted = applyOutOfRange({
318
- targetFirstWeek: target,
319
- weekCount,
320
- behavior: outOfRangeBehavior,
321
- ...(minValue !== undefined && { min: minValue }),
322
- ...(maxValue !== undefined && { max: maxValue }),
323
- weekStartDay,
324
- T,
325
- });
326
- updateFirstWeek(adjusted.firstWeek);
312
+ const target = resolvedFirstWeek.add({ weeks: shiftBy ?? weekCount });
313
+ updateFirstWeek(windowPolicy.resolve(target).firstWeek);
327
314
  },
328
- [
329
- resolvedFirstWeek,
330
- weekCount,
331
- outOfRangeBehavior,
332
- minValue,
333
- maxValue,
334
- weekStartDay,
335
- T,
336
- updateFirstWeek,
337
- ],
315
+ [resolvedFirstWeek, weekCount, windowPolicy, updateFirstWeek],
338
316
  );
339
317
 
340
318
  const goPrev = useCallback(
341
319
  (shiftBy?: number) => {
342
- const shift = shiftBy ?? weekCount;
343
- const target = resolvedFirstWeek.subtract({ weeks: shift });
344
- const adjusted = applyOutOfRange({
345
- targetFirstWeek: target,
346
- weekCount,
347
- behavior: outOfRangeBehavior,
348
- ...(minValue !== undefined && { min: minValue }),
349
- ...(maxValue !== undefined && { max: maxValue }),
350
- weekStartDay,
351
- T,
320
+ const target = resolvedFirstWeek.subtract({
321
+ weeks: shiftBy ?? weekCount,
352
322
  });
353
- updateFirstWeek(adjusted.firstWeek);
323
+ updateFirstWeek(windowPolicy.resolve(target).firstWeek);
354
324
  },
355
- [
356
- resolvedFirstWeek,
357
- weekCount,
358
- outOfRangeBehavior,
359
- minValue,
360
- maxValue,
361
- weekStartDay,
362
- T,
363
- updateFirstWeek,
364
- ],
325
+ [resolvedFirstWeek, weekCount, windowPolicy, updateFirstWeek],
365
326
  );
366
327
 
367
328
  // --- scrollToWeek ---
@@ -370,35 +331,15 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
370
331
  target: FirstWeekSpec,
371
332
  options?: { snap?: "start" | "center" | "end" | "nearest" },
372
333
  ) => {
373
- const resolvedTarget = resolveSpec(target);
374
334
  const newFirstWeek = resolveFirstWeek(
375
335
  resolvedFirstWeek,
376
336
  weekCount,
377
- resolvedTarget,
337
+ resolveSpec(target),
378
338
  options,
379
339
  );
380
- const adjusted = applyOutOfRange({
381
- targetFirstWeek: newFirstWeek,
382
- weekCount,
383
- behavior: outOfRangeBehavior,
384
- ...(minValue !== undefined && { min: minValue }),
385
- ...(maxValue !== undefined && { max: maxValue }),
386
- weekStartDay,
387
- T,
388
- });
389
- updateFirstWeek(adjusted.firstWeek);
340
+ updateFirstWeek(windowPolicy.resolve(newFirstWeek).firstWeek);
390
341
  },
391
- [
392
- resolvedFirstWeek,
393
- weekCount,
394
- resolveSpec,
395
- outOfRangeBehavior,
396
- minValue,
397
- maxValue,
398
- weekStartDay,
399
- T,
400
- updateFirstWeek,
401
- ],
342
+ [resolvedFirstWeek, weekCount, resolveSpec, windowPolicy, updateFirstWeek],
402
343
  );
403
344
 
404
345
  // --- Fire onWindowChange ---
@@ -425,6 +366,7 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
425
366
  weekCount,
426
367
  scrollBy,
427
368
  outOfRangeBehavior,
369
+ windowPolicy,
428
370
  goNext,
429
371
  goPrev,
430
372
  scrollToWeek,
@@ -435,6 +377,7 @@ export function useWeeksViewRootState(props: WeeksViewRootProps) {
435
377
  weekCount,
436
378
  scrollBy,
437
379
  outOfRangeBehavior,
380
+ windowPolicy,
438
381
  goNext,
439
382
  goPrev,
440
383
  scrollToWeek,
@@ -3,6 +3,7 @@ import type { WeekDescriptor } from "./compute-weeks-in-window";
3
3
  import type { OutOfRangeBehavior } from "./out-of-range";
4
4
  import type { FirstWeekSpec, ScrollToWeekSnap } from "./resolve-first-week";
5
5
  import type { RootState } from "./types";
6
+ import type { WeekWindowPolicy } from "./weeks-window";
6
7
 
7
8
  // ---------------------------------------------------------------------------
8
9
  // WeeksView.Root props
@@ -63,7 +64,7 @@ export interface WindowInfo {
63
64
  windowStart: Temporal.PlainDate;
64
65
  /** The last date of the visible window. */
65
66
  windowEnd: Temporal.PlainDate;
66
- /** Number of week rows in the window (the prop value, not actual when shrunk). */
67
+ /** Number of week rows actually shown (fewer than the `weekCount` prop when shrunk). */
67
68
  weekCount: number;
68
69
  /** Total number of calendar days in the window. */
69
70
  dayCount: number;
@@ -87,6 +88,8 @@ export interface WeeksViewStableContextValue {
87
88
  scrollBy: "row" | "page";
88
89
  /** How navigation behaves at bounds. */
89
90
  outOfRangeBehavior: OutOfRangeBehavior;
91
+ /** Where the window may sit given `min`/`max` and `outOfRangeBehavior`. */
92
+ windowPolicy: WeekWindowPolicy;
90
93
  /** Navigate to the next week(s). Optionally override the shift amount (in weeks). */
91
94
  goNext: (shiftBy?: number) => void;
92
95
  /** Navigate to the previous week(s). Optionally override the shift amount (in weeks). */
@@ -0,0 +1,43 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { describe, it, expect } from "vitest";
3
+ import { createWeekWindowPolicy } from "./weeks-window";
4
+
5
+ const T = Temporal;
6
+ const pd = (s: string) => T.PlainDate.from(s);
7
+
8
+ describe("createWeekWindowPolicy", () => {
9
+ const policy = createWeekWindowPolicy({
10
+ weekCount: 4,
11
+ behavior: "snap",
12
+ min: pd("2026-03-01"),
13
+ max: pd("2026-04-30"),
14
+ weekStartDay: 0,
15
+ T,
16
+ });
17
+
18
+ it("resolve snaps an overshooting first week back to the max edge", () => {
19
+ const { firstWeek, weekCount } = policy.resolve(pd("2026-05-10"));
20
+ expect({ firstWeek: firstWeek.toString(), weekCount }).toEqual({
21
+ firstWeek: "2026-04-05",
22
+ weekCount: 4,
23
+ });
24
+ });
25
+
26
+ it.each([
27
+ {
28
+ description:
29
+ "next is blocked once the window already sits at the max edge",
30
+ from: "2026-04-05",
31
+ direction: 1 as const,
32
+ expected: false,
33
+ },
34
+ {
35
+ description: "prev is allowed away from the max edge",
36
+ from: "2026-04-05",
37
+ direction: -1 as const,
38
+ expected: true,
39
+ },
40
+ ])("canShift: $description", ({ from, direction, expected }) => {
41
+ expect(policy.canShift(pd(from), direction)).toBe(expected);
42
+ });
43
+ });
@@ -0,0 +1,64 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import {
3
+ applyOutOfRange,
4
+ canShift,
5
+ type OutOfRangeBehavior,
6
+ } from "./out-of-range";
7
+ import type { TemporalNamespace, WeekStartDay } from "./types";
8
+
9
+ /** Inputs that determine how a weeks window may move relative to `min`/`max`. */
10
+ export interface WeekWindowPolicyInput {
11
+ weekCount: number;
12
+ behavior: OutOfRangeBehavior;
13
+ min: Temporal.PlainDate | undefined;
14
+ max: Temporal.PlainDate | undefined;
15
+ weekStartDay: WeekStartDay;
16
+ T: TemporalNamespace;
17
+ }
18
+
19
+ /**
20
+ * The rules for where a WeeksView window may sit. Built once from the bounds and
21
+ * shared by every way of moving the window (buttons, keyboard, `scrollToWeek`).
22
+ */
23
+ export interface WeekWindowPolicy {
24
+ /** Applies the out-of-range behavior to a requested first week. */
25
+ resolve(requestedFirstWeek: Temporal.PlainDate): {
26
+ firstWeek: Temporal.PlainDate;
27
+ weekCount: number;
28
+ };
29
+ /** Whether a Prev/Next step of `shiftBy` weeks (default `weekCount`) is allowed. */
30
+ canShift(
31
+ currentFirstWeek: Temporal.PlainDate,
32
+ direction: 1 | -1,
33
+ shiftBy?: number,
34
+ ): boolean;
35
+ }
36
+
37
+ export function createWeekWindowPolicy({
38
+ weekCount,
39
+ behavior,
40
+ min,
41
+ max,
42
+ weekStartDay,
43
+ T,
44
+ }: WeekWindowPolicyInput): WeekWindowPolicy {
45
+ const bounds = {
46
+ weekCount,
47
+ behavior,
48
+ ...(min !== undefined && { min }),
49
+ ...(max !== undefined && { max }),
50
+ weekStartDay,
51
+ T,
52
+ };
53
+ return {
54
+ resolve: (requestedFirstWeek) =>
55
+ applyOutOfRange({ ...bounds, targetFirstWeek: requestedFirstWeek }),
56
+ canShift: (currentFirstWeek, direction, shiftBy) =>
57
+ canShift({
58
+ ...bounds,
59
+ currentFirstWeek,
60
+ direction,
61
+ ...(shiftBy !== undefined && { shiftBy }),
62
+ }),
63
+ };
64
+ }