@klinking/colander 3.1.0-alpha.5 → 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 (48) 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-BLZZm1fq.d.mts → index-DnFCKyyw.d.cts} +44 -10
  30. package/dist/{weeks-view-context-CkTcdZpj.d.cts → index-q3cQKJdA.d.mts} +44 -10
  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-CEV8w_69.mjs → weeks-navigation-Dn3UtG76.mjs} +364 -389
  36. package/dist/{weeks-navigation-q3beZn2n.cjs → weeks-navigation-SuKJZJCM.cjs} +364 -389
  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/range-preview.test.ts +120 -0
  43. package/src/root-selection.ts +99 -97
  44. package/src/weeks-navigation.tsx +7 -32
  45. package/src/weeks-view-state.ts +24 -81
  46. package/src/weeks-view-types.ts +4 -1
  47. package/src/weeks-window.test.ts +43 -0
  48. package/src/weeks-window.ts +64 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@klinking/colander",
3
- "version": "3.1.0-alpha.5",
3
+ "version": "3.1.0-alpha.6",
4
4
  "homepage": "https://github.com/klink-ing/colander#readme",
5
5
  "bugs": "https://github.com/klink-ing/colander/issues",
6
6
  "license": "MIT",
@@ -12,8 +12,10 @@ import type {
12
12
  import {
13
13
  isDateRange,
14
14
  tagRaw,
15
+ computeRangePreview,
15
16
  computeSelectionUpdate,
16
17
  computeSetRangeUpdate,
18
+ decodeRange,
17
19
  truncateDatesForMode,
18
20
  type SelectionResult,
19
21
  } from "./root-selection";
@@ -180,14 +182,7 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
180
182
  }
181
183
  if (isRange) {
182
184
  if (rangeValue === null) return [];
183
- if (rangeValue) {
184
- const start =
185
- rangeValue.start != null ? rawToPlain(rangeValue.start) : null;
186
- const end = rangeValue.end != null ? rawToPlain(rangeValue.end) : null;
187
- if (start === null && end === null) return [];
188
- if (start === null && end !== null) return [end, null];
189
- return [start, end];
190
- }
185
+ if (rangeValue) return decodeRange(rangeValue, rawToPlain);
191
186
  return undefined;
192
187
  }
193
188
  if (singleValue === null) return [];
@@ -237,15 +232,7 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
237
232
  if (multipleDefault) return sortDates(multipleDefault.map(rawToPlain));
238
233
  return [];
239
234
  }
240
- if (rangeDefault) {
241
- const start =
242
- rangeDefault.start != null ? rawToPlain(rangeDefault.start) : null;
243
- const end =
244
- rangeDefault.end != null ? rawToPlain(rangeDefault.end) : null;
245
- if (start === null && end === null) return [];
246
- if (start === null && end !== null) return [end, null];
247
- return [start, end];
248
- }
235
+ if (rangeDefault) return decodeRange(rangeDefault, rawToPlain);
249
236
  if (singleDefault != null) return [rawToPlain(singleDefault)];
250
237
  return [];
251
238
  }, [
@@ -366,52 +353,27 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
366
353
  >(() => {
367
354
  if (previewRangeProp !== undefined) {
368
355
  if (!previewRangeProp) return [undefined, undefined];
369
- const rawToPlainLocal = (
370
- raw: RawValueForFormat<F>,
371
- ): Temporal.PlainDate => {
372
- const tagged = {
373
- format: resolvedFormat,
374
- value: raw,
375
- } as DateValueObject;
376
- return toZonedDateTime(tagged, timeZone, T).toPlainDate();
377
- };
378
356
  const s =
379
357
  previewRangeProp.start != null
380
- ? rawToPlainLocal(previewRangeProp.start)
358
+ ? rawToPlain(previewRangeProp.start)
381
359
  : undefined;
382
360
  const e =
383
361
  previewRangeProp.end != null
384
- ? rawToPlainLocal(previewRangeProp.end)
362
+ ? rawToPlain(previewRangeProp.end)
385
363
  : undefined;
386
364
  return [s, e];
387
365
  }
388
366
  if (!hoveredDate || !isRange) return [undefined, undefined];
389
- const result = computeSelectionUpdate<F>({
390
- date: hoveredDate,
367
+ const preview = computeRangePreview({
368
+ hoveredDate,
391
369
  readOnly,
392
370
  isDateDisabled,
393
- isMultiple: false,
394
- isRange: true,
395
371
  committedDates,
396
- committedStart,
397
- committedEnd,
398
- selectedZdt: undefined,
399
372
  rangeMode,
400
373
  allowRangeReversal,
401
- sortDates,
402
- currentSingleFormatted,
403
- currentRangeFormatted,
404
- currentMultipleFormatted,
405
- resolvedFormat,
406
- timeZone,
407
374
  T,
408
375
  });
409
- if (result.skip || result.newDates.length === 0)
410
- return [undefined, undefined];
411
- const ps = result.newDates[0];
412
- const pe = result.newDates.length > 1 ? result.newDates[1] : ps;
413
- if (ps == null || pe == null) return [undefined, undefined];
414
- return [ps, pe];
376
+ return preview ?? [undefined, undefined];
415
377
  }, [
416
378
  previewRangeProp,
417
379
  hoveredDate,
@@ -419,16 +381,9 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
419
381
  readOnly,
420
382
  isDateDisabled,
421
383
  committedDates,
422
- committedStart,
423
- committedEnd,
424
384
  rangeMode,
425
385
  allowRangeReversal,
426
- sortDates,
427
- currentSingleFormatted,
428
- currentRangeFormatted,
429
- currentMultipleFormatted,
430
- resolvedFormat,
431
- timeZone,
386
+ rawToPlain,
432
387
  T,
433
388
  ]);
434
389
 
@@ -0,0 +1,214 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { describe, expect, it } from "vitest";
3
+ import { computeDayCellState, dayCellStateEqual } from "./day-cell-state";
4
+ import type { DayCellInputs } from "./day-cell-state";
5
+ import type { OutsideDays } from "./types";
6
+
7
+ const T = Temporal;
8
+ const d = (iso: string) => T.PlainDate.from(iso);
9
+
10
+ const inputs = (overrides: Partial<DayCellInputs> = {}): DayCellInputs => ({
11
+ T,
12
+ today: d("2026-03-10"),
13
+ orientation: "horizontal",
14
+ rootState: {} as DayCellInputs["rootState"],
15
+ selectionMode: "single",
16
+ selectedDates: [],
17
+ rangeStart: undefined,
18
+ rangeEnd: undefined,
19
+ previewStart: undefined,
20
+ previewEnd: undefined,
21
+ disabled: false,
22
+ isDateDisabled: undefined,
23
+ focusedDate: d("2026-03-15"),
24
+ tabTargetDate: d("2026-03-15"),
25
+ outsideDays: "enabled",
26
+ ...overrides,
27
+ });
28
+
29
+ const march = { columnIndex: 0, month: { year: 2026, month: 3 } };
30
+
31
+ describe("computeDayCellState", () => {
32
+ it.each([
33
+ {
34
+ description:
35
+ "today, selected, focused and tab target in the viewed month",
36
+ date: "2026-03-10",
37
+ overrides: {
38
+ selectedDates: [d("2026-03-10")],
39
+ focusedDate: d("2026-03-10"),
40
+ tabTargetDate: d("2026-03-10"),
41
+ },
42
+ expected: {
43
+ today: true,
44
+ selected: true,
45
+ focused: true,
46
+ isTabTarget: true,
47
+ outsideMonth: false,
48
+ disabled: false,
49
+ },
50
+ },
51
+ {
52
+ description: "isDateDisabled disables the cell",
53
+ date: "2026-03-12",
54
+ overrides: { isDateDisabled: (x: Temporal.PlainDate) => x.day === 12 },
55
+ expected: { disabled: true, selected: false },
56
+ },
57
+ {
58
+ description: "range mode never reports selected, only range state",
59
+ date: "2026-03-12",
60
+ overrides: {
61
+ selectionMode: "range" as const,
62
+ selectedDates: [d("2026-03-12")],
63
+ rangeStart: d("2026-03-11"),
64
+ rangeEnd: d("2026-03-13"),
65
+ },
66
+ expected: {
67
+ selected: false,
68
+ inRange: true,
69
+ rangeBoundary: false,
70
+ rangeIndex: 1,
71
+ rangeLength: 3,
72
+ rangeHasStart: true,
73
+ rangeHasEnd: true,
74
+ },
75
+ },
76
+ {
77
+ description: "half-open range: only the start is set",
78
+ date: "2026-03-11",
79
+ overrides: {
80
+ selectionMode: "range" as const,
81
+ rangeStart: d("2026-03-11"),
82
+ },
83
+ expected: {
84
+ rangeStart: true,
85
+ rangeEnd: false,
86
+ inRange: true,
87
+ rangeIndex: 0,
88
+ rangeLength: 1,
89
+ rangeHasStart: true,
90
+ rangeHasEnd: false,
91
+ },
92
+ },
93
+ {
94
+ description: "preview range marks its boundary",
95
+ date: "2026-03-14",
96
+ overrides: {
97
+ selectionMode: "range" as const,
98
+ previewStart: d("2026-03-12"),
99
+ previewEnd: d("2026-03-14"),
100
+ },
101
+ expected: {
102
+ rangePreviewEnd: true,
103
+ rangePreviewBoundary: true,
104
+ rangePreviewInRange: true,
105
+ rangePreviewIndex: 2,
106
+ rangePreviewLength: 3,
107
+ },
108
+ },
109
+ ])("$description", ({ date, overrides, expected }) => {
110
+ const state = computeDayCellState(d(date), march, inputs(overrides));
111
+ expect(state).toMatchObject(expected);
112
+ });
113
+
114
+ it.each([
115
+ {
116
+ description: "enabled: outside day is a normal cell",
117
+ outsideDays: "enabled" as OutsideDays,
118
+ expected: {
119
+ outsideMonth: true,
120
+ hidden: false,
121
+ disabled: false,
122
+ inRange: true,
123
+ },
124
+ },
125
+ {
126
+ description: "readOnly: outside day is disabled but keeps range state",
127
+ outsideDays: "readOnly" as OutsideDays,
128
+ expected: {
129
+ outsideMonth: true,
130
+ hidden: false,
131
+ disabled: true,
132
+ inRange: true,
133
+ },
134
+ },
135
+ {
136
+ description: "disabled: outside day is disabled and drops range state",
137
+ outsideDays: "disabled" as OutsideDays,
138
+ expected: {
139
+ outsideMonth: true,
140
+ hidden: false,
141
+ disabled: true,
142
+ inRange: false,
143
+ },
144
+ },
145
+ {
146
+ description: "hidden: outside day is hidden, not today, no range state",
147
+ outsideDays: "hidden" as OutsideDays,
148
+ expected: {
149
+ outsideMonth: true,
150
+ hidden: true,
151
+ disabled: true,
152
+ today: false,
153
+ inRange: false,
154
+ },
155
+ },
156
+ ])("outside day, $description", ({ outsideDays, expected }) => {
157
+ const state = computeDayCellState(
158
+ d("2026-04-02"),
159
+ march,
160
+ inputs({
161
+ outsideDays,
162
+ selectionMode: "range",
163
+ rangeStart: d("2026-04-01"),
164
+ rangeEnd: d("2026-04-03"),
165
+ selectedDates: [d("2026-04-02")],
166
+ focusedDate: d("2026-04-02"),
167
+ tabTargetDate: d("2026-04-02"),
168
+ }),
169
+ );
170
+ expect(state).toMatchObject({
171
+ ...expected,
172
+ selected: false,
173
+ focused: outsideDays === "enabled",
174
+ isTabTarget: outsideDays === "enabled",
175
+ });
176
+ });
177
+ });
178
+
179
+ describe("dayCellStateEqual", () => {
180
+ const state = () => computeDayCellState(d("2026-03-12"), march, inputs());
181
+
182
+ it.each([
183
+ { description: "identical states", change: {}, expected: true },
184
+ {
185
+ description: "root/orientation differ",
186
+ change: { orientation: "vertical" as const },
187
+ expected: true,
188
+ },
189
+ {
190
+ description: "selected differs",
191
+ change: { selected: true },
192
+ expected: false,
193
+ },
194
+ {
195
+ description: "tab target differs",
196
+ change: { isTabTarget: true },
197
+ expected: false,
198
+ },
199
+ {
200
+ description: "range index differs",
201
+ change: { rangeIndex: 2 },
202
+ expected: false,
203
+ },
204
+ {
205
+ description: "preview length differs",
206
+ change: { rangePreviewLength: 4 },
207
+ expected: false,
208
+ },
209
+ ])("$description", ({ change, expected }) => {
210
+ expect(dayCellStateEqual(state(), { ...state(), ...change })).toBe(
211
+ expected,
212
+ );
213
+ });
214
+ });
@@ -0,0 +1,314 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import { useContext } from "react";
3
+ import { useCalendarStable, useCalendarState } from "./calendar-context";
4
+ import { WeekDataContext, GridContext } from "./context";
5
+ import type { GridOrientation } from "./context";
6
+ import type {
7
+ DayCellTemplateState,
8
+ OutsideDays,
9
+ TemporalNamespace,
10
+ } from "./types";
11
+ import { isInRange as isInRangeUtil } from "./utils";
12
+ import { useViewContext, useViewStable, useViewState } from "./view-context";
13
+
14
+ /** A day cell's state: the public {@link DayCellTemplateState} plus the roving-tabindex flag. */
15
+ export type DayCellState = DayCellTemplateState & { isTabTarget: boolean };
16
+
17
+ type RangeState = Pick<
18
+ DayCellTemplateState,
19
+ | "rangeStart"
20
+ | "rangeEnd"
21
+ | "rangeBoundary"
22
+ | "inRange"
23
+ | "rangeIndex"
24
+ | "rangeLength"
25
+ | "rangeHasStart"
26
+ | "rangeHasEnd"
27
+ | "rangePreviewStart"
28
+ | "rangePreviewEnd"
29
+ | "rangePreviewBoundary"
30
+ | "rangePreviewInRange"
31
+ | "rangePreviewIndex"
32
+ | "rangePreviewLength"
33
+ | "rangePreviewHasStart"
34
+ | "rangePreviewHasEnd"
35
+ >;
36
+
37
+ const NO_RANGE: RangeState = {
38
+ rangeStart: false,
39
+ rangeEnd: false,
40
+ rangeBoundary: false,
41
+ inRange: false,
42
+ rangeIndex: false,
43
+ rangeLength: false,
44
+ rangeHasStart: false,
45
+ rangeHasEnd: false,
46
+ rangePreviewStart: false,
47
+ rangePreviewEnd: false,
48
+ rangePreviewBoundary: false,
49
+ rangePreviewInRange: false,
50
+ rangePreviewIndex: false,
51
+ rangePreviewLength: false,
52
+ rangePreviewHasStart: false,
53
+ rangePreviewHasEnd: false,
54
+ };
55
+
56
+ /** Everything that is the same for every cell of one render. */
57
+ export interface DayCellInputs {
58
+ T: TemporalNamespace;
59
+ today: Temporal.PlainDate;
60
+ orientation: GridOrientation;
61
+ rootState: DayCellTemplateState["root"];
62
+ selectionMode: "single" | "range" | "multiple";
63
+ selectedDates: Temporal.PlainDate[];
64
+ rangeStart: Temporal.PlainDate | undefined;
65
+ rangeEnd: Temporal.PlainDate | undefined;
66
+ previewStart: Temporal.PlainDate | undefined;
67
+ previewEnd: Temporal.PlainDate | undefined;
68
+ disabled: boolean;
69
+ isDateDisabled: ((date: Temporal.PlainDate) => boolean) | undefined;
70
+ focusedDate: Temporal.PlainDate;
71
+ tabTargetDate: Temporal.PlainDate;
72
+ outsideDays: OutsideDays;
73
+ }
74
+
75
+ /** Where one cell sits: its column and the month the grid it is in belongs to. */
76
+ export interface DayCellPosition {
77
+ columnIndex: number;
78
+ month: { year: number; month: number };
79
+ }
80
+
81
+ function computeRangeState(
82
+ date: Temporal.PlainDate,
83
+ inputs: DayCellInputs,
84
+ ): RangeState {
85
+ const { T, selectionMode, rangeStart, rangeEnd, previewStart, previewEnd } =
86
+ inputs;
87
+ if (selectionMode !== "range") return NO_RANGE;
88
+
89
+ const isRangeStart = rangeStart
90
+ ? T.PlainDate.compare(date, rangeStart) === 0
91
+ : false;
92
+ const isRangeEnd = rangeEnd
93
+ ? T.PlainDate.compare(date, rangeEnd) === 0
94
+ : false;
95
+ const inRange = isInRangeUtil(date, rangeStart, rangeEnd, T) !== false;
96
+ const effectiveStart = rangeStart ?? rangeEnd;
97
+ const effectiveEnd = rangeEnd ?? rangeStart;
98
+
99
+ const isPreviewStart = previewStart
100
+ ? T.PlainDate.compare(date, previewStart) === 0
101
+ : false;
102
+ const isPreviewEnd = previewEnd
103
+ ? T.PlainDate.compare(date, previewEnd) === 0
104
+ : false;
105
+ const inPreview =
106
+ previewStart && previewEnd
107
+ ? isInRangeUtil(date, previewStart, previewEnd, T) !== false
108
+ : false;
109
+
110
+ return {
111
+ rangeStart: isRangeStart,
112
+ rangeEnd: isRangeEnd,
113
+ rangeBoundary: isRangeStart || isRangeEnd,
114
+ inRange,
115
+ rangeIndex:
116
+ inRange && effectiveStart ? date.since(effectiveStart).days : false,
117
+ rangeLength:
118
+ inRange && effectiveStart && effectiveEnd
119
+ ? effectiveEnd.since(effectiveStart).days + 1
120
+ : false,
121
+ rangeHasStart: inRange && rangeStart !== undefined,
122
+ rangeHasEnd: inRange && rangeEnd !== undefined,
123
+ rangePreviewStart: isPreviewStart,
124
+ rangePreviewEnd: isPreviewEnd,
125
+ rangePreviewBoundary: isPreviewStart || isPreviewEnd,
126
+ rangePreviewInRange: inPreview,
127
+ rangePreviewIndex:
128
+ inPreview && previewStart ? date.since(previewStart).days : false,
129
+ rangePreviewLength:
130
+ inPreview && previewStart && previewEnd
131
+ ? previewEnd.since(previewStart).days + 1
132
+ : false,
133
+ rangePreviewHasStart: inPreview && previewStart !== undefined,
134
+ rangePreviewHasEnd: inPreview && previewEnd !== undefined,
135
+ };
136
+ }
137
+
138
+ /** Derives the state of one day cell. Pure function, no hooks. */
139
+ export function computeDayCellState(
140
+ date: Temporal.PlainDate,
141
+ position: DayCellPosition,
142
+ inputs: DayCellInputs,
143
+ ): DayCellState {
144
+ const { T, outsideDays } = inputs;
145
+ const { columnIndex, month } = position;
146
+ const isCurrentMonth = date.year === month.year && date.month === month.month;
147
+ const base = {
148
+ root: inputs.rootState,
149
+ date,
150
+ columnIndex,
151
+ orientation: inputs.orientation,
152
+ };
153
+
154
+ if (!isCurrentMonth && outsideDays === "hidden") {
155
+ return {
156
+ ...base,
157
+ ...NO_RANGE,
158
+ outsideMonth: true,
159
+ hidden: true,
160
+ selected: false,
161
+ today: false,
162
+ disabled: true,
163
+ focused: false,
164
+ isTabTarget: false,
165
+ };
166
+ }
167
+
168
+ const isToday = T.PlainDate.compare(date, inputs.today) === 0;
169
+
170
+ if (!isCurrentMonth && outsideDays !== "enabled") {
171
+ // `readOnly` outside days keep their range state; `disabled` ones don't.
172
+ const suppressRange = outsideDays === "disabled";
173
+ return {
174
+ ...base,
175
+ ...(suppressRange ? NO_RANGE : computeRangeState(date, inputs)),
176
+ outsideMonth: true,
177
+ hidden: false,
178
+ selected: false,
179
+ today: isToday,
180
+ disabled: true,
181
+ focused: false,
182
+ isTabTarget: false,
183
+ };
184
+ }
185
+
186
+ return {
187
+ ...base,
188
+ ...computeRangeState(date, inputs),
189
+ outsideMonth: !isCurrentMonth,
190
+ hidden: false,
191
+ selected:
192
+ inputs.selectionMode !== "range"
193
+ ? inputs.selectedDates.some((d) => T.PlainDate.compare(d, date) === 0)
194
+ : false,
195
+ today: isToday,
196
+ disabled: inputs.disabled || (inputs.isDateDisabled?.(date) ?? false),
197
+ focused: T.PlainDate.compare(date, inputs.focusedDate) === 0,
198
+ isTabTarget: T.PlainDate.compare(date, inputs.tabTargetDate) === 0,
199
+ };
200
+ }
201
+
202
+ /**
203
+ * Every field of {@link DayCellState} that decides whether a cell re-renders.
204
+ * `root`, `date`, `columnIndex` and `orientation` are not listed: the memo
205
+ * compares `date`/`columnIndex` as props, and the other two never change a
206
+ * cell on their own. Adding a field to the state fails to compile until it is
207
+ * listed here.
208
+ */
209
+ const COMPARED_STATE: Record<
210
+ Exclude<keyof DayCellState, "root" | "date" | "columnIndex" | "orientation">,
211
+ true
212
+ > = {
213
+ outsideMonth: true,
214
+ hidden: true,
215
+ selected: true,
216
+ today: true,
217
+ disabled: true,
218
+ focused: true,
219
+ rangeStart: true,
220
+ rangeEnd: true,
221
+ rangeBoundary: true,
222
+ inRange: true,
223
+ rangeIndex: true,
224
+ rangeLength: true,
225
+ rangeHasStart: true,
226
+ rangeHasEnd: true,
227
+ rangePreviewStart: true,
228
+ rangePreviewEnd: true,
229
+ rangePreviewBoundary: true,
230
+ rangePreviewInRange: true,
231
+ rangePreviewIndex: true,
232
+ rangePreviewLength: true,
233
+ rangePreviewHasStart: true,
234
+ rangePreviewHasEnd: true,
235
+ isTabTarget: true,
236
+ };
237
+
238
+ const COMPARED_KEYS = Object.keys(
239
+ COMPARED_STATE,
240
+ ) as (keyof typeof COMPARED_STATE)[];
241
+
242
+ /** Whether two cell states render the same cell. */
243
+ export function dayCellStateEqual(a: DayCellState, b: DayCellState): boolean {
244
+ for (const key of COMPARED_KEYS) {
245
+ if (a[key] !== b[key]) return false;
246
+ }
247
+ return true;
248
+ }
249
+
250
+ /**
251
+ * Reads the contexts a day cell depends on and returns a function that derives
252
+ * the state of any cell. `columnIndex` defaults to the date's column in a
253
+ * week starting on the calendar's `weekStartDay`.
254
+ */
255
+ export function useDayCellDeriver(): (
256
+ date: Temporal.PlainDate,
257
+ columnIndex?: number,
258
+ ) => DayCellState {
259
+ const { orientation } = useContext(GridContext);
260
+ const weekData = useContext(WeekDataContext);
261
+ const {
262
+ disabled,
263
+ isDateDisabled,
264
+ selectionMode,
265
+ temporal: T,
266
+ today,
267
+ weekStartDay,
268
+ } = useCalendarStable();
269
+ const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } =
270
+ useCalendarState();
271
+ const viewCtx = useViewContext();
272
+ const { focusedDate, tabTargetDate } = viewCtx;
273
+ const { rootState, currentDateTime } = useViewState();
274
+ const { outsideDays } = useViewStable();
275
+
276
+ const inputs: DayCellInputs = {
277
+ T,
278
+ today: today(),
279
+ orientation,
280
+ rootState,
281
+ selectionMode,
282
+ selectedDates,
283
+ rangeStart,
284
+ rangeEnd,
285
+ previewStart,
286
+ previewEnd,
287
+ disabled,
288
+ isDateDisabled,
289
+ focusedDate,
290
+ tabTargetDate,
291
+ outsideDays,
292
+ };
293
+
294
+ // A weeks view has no "current month": every cell is in its own month, so
295
+ // `outsideMonth` is always false. Otherwise a cell belongs to the month of
296
+ // the grid it sits in (set per grid in multi-month mode).
297
+ const isWeeksView = viewCtx.viewType === "weeks";
298
+ const gridMonth = weekData?.gridMonth ?? currentDateTime;
299
+
300
+ return (date, columnIndex) => {
301
+ const daysInWeek = date.daysInWeek;
302
+ return computeDayCellState(
303
+ date,
304
+ {
305
+ columnIndex:
306
+ columnIndex ??
307
+ ((date.dayOfWeek % daysInWeek) - weekStartDay + daysInWeek) %
308
+ daysInWeek,
309
+ month: isWeeksView ? { year: date.year, month: date.month } : gridMonth,
310
+ },
311
+ inputs,
312
+ );
313
+ };
314
+ }