@knitui/dates 0.5.0 → 0.6.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 (143) hide show
  1. package/lib/commonjs/DatesProvider/DatesProvider.js +33 -4
  2. package/lib/commonjs/DatesProvider/DatesProvider.js.map +1 -1
  3. package/lib/commonjs/Day/Day.js +45 -7
  4. package/lib/commonjs/Day/Day.js.map +1 -1
  5. package/lib/commonjs/Month/Month.js +266 -80
  6. package/lib/commonjs/Month/Month.js.map +1 -1
  7. package/lib/commonjs/MonthLevel/MonthLevel.js +19 -3
  8. package/lib/commonjs/MonthLevel/MonthLevel.js.map +1 -1
  9. package/lib/commonjs/MonthsList/MonthsList.js +160 -60
  10. package/lib/commonjs/MonthsList/MonthsList.js.map +1 -1
  11. package/lib/commonjs/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js +7 -1
  12. package/lib/commonjs/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js.map +1 -1
  13. package/lib/commonjs/TimeControlsList/TimeControl.js +15 -1
  14. package/lib/commonjs/TimeControlsList/TimeControl.js.map +1 -1
  15. package/lib/commonjs/TimeControlsList/TimeControlsList.js +16 -3
  16. package/lib/commonjs/TimeControlsList/TimeControlsList.js.map +1 -1
  17. package/lib/commonjs/WeekdaysRow/WeekdaysRow.js +23 -4
  18. package/lib/commonjs/WeekdaysRow/WeekdaysRow.js.map +1 -1
  19. package/lib/commonjs/YearsList/YearsList.js +155 -60
  20. package/lib/commonjs/YearsList/YearsList.js.map +1 -1
  21. package/lib/commonjs/YearsList/get-year-in-tab-order/get-year-in-tab-order.js +7 -1
  22. package/lib/commonjs/YearsList/get-year-in-tab-order/get-year-in-tab-order.js.map +1 -1
  23. package/lib/commonjs/hooks/use-dates-state/is-in-range/is-in-range.js +20 -0
  24. package/lib/commonjs/hooks/use-dates-state/is-in-range/is-in-range.js.map +1 -1
  25. package/lib/commonjs/hooks/use-dates-state/use-dates-state.js +25 -7
  26. package/lib/commonjs/hooks/use-dates-state/use-dates-state.js.map +1 -1
  27. package/lib/commonjs/internal/are-cell-props-equal.js +64 -0
  28. package/lib/commonjs/internal/are-cell-props-equal.js.map +1 -0
  29. package/lib/commonjs/internal/date-string-fast-path.js +98 -0
  30. package/lib/commonjs/internal/date-string-fast-path.js.map +1 -0
  31. package/lib/commonjs/internal/memoize-control-props.js +54 -0
  32. package/lib/commonjs/internal/memoize-control-props.js.map +1 -0
  33. package/lib/commonjs/internal/month-days-cache.js +71 -0
  34. package/lib/commonjs/internal/month-days-cache.js.map +1 -0
  35. package/lib/commonjs/utils/get-date-in-tab-order/get-date-in-tab-order.js +9 -1
  36. package/lib/commonjs/utils/get-date-in-tab-order/get-date-in-tab-order.js.map +1 -1
  37. package/lib/commonjs/utils/is-after-min-date/is-after-min-date.js +14 -1
  38. package/lib/commonjs/utils/is-after-min-date/is-after-min-date.js.map +1 -1
  39. package/lib/commonjs/utils/is-before-max-date/is-before-max-date.js +14 -1
  40. package/lib/commonjs/utils/is-before-max-date/is-before-max-date.js.map +1 -1
  41. package/lib/commonjs/utils/is-same-month/is-same-month.js +17 -1
  42. package/lib/commonjs/utils/is-same-month/is-same-month.js.map +1 -1
  43. package/lib/module/DatesProvider/DatesProvider.js +34 -5
  44. package/lib/module/DatesProvider/DatesProvider.js.map +1 -1
  45. package/lib/module/Day/Day.js +46 -8
  46. package/lib/module/Day/Day.js.map +1 -1
  47. package/lib/module/Month/Month.js +267 -82
  48. package/lib/module/Month/Month.js.map +1 -1
  49. package/lib/module/MonthLevel/MonthLevel.js +19 -3
  50. package/lib/module/MonthLevel/MonthLevel.js.map +1 -1
  51. package/lib/module/MonthsList/MonthsList.js +159 -60
  52. package/lib/module/MonthsList/MonthsList.js.map +1 -1
  53. package/lib/module/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js +7 -1
  54. package/lib/module/MonthsList/get-month-in-tab-order/get-month-in-tab-order.js.map +1 -1
  55. package/lib/module/TimeControlsList/TimeControl.js +15 -1
  56. package/lib/module/TimeControlsList/TimeControl.js.map +1 -1
  57. package/lib/module/TimeControlsList/TimeControlsList.js +16 -3
  58. package/lib/module/TimeControlsList/TimeControlsList.js.map +1 -1
  59. package/lib/module/WeekdaysRow/WeekdaysRow.js +23 -4
  60. package/lib/module/WeekdaysRow/WeekdaysRow.js.map +1 -1
  61. package/lib/module/YearsList/YearsList.js +154 -60
  62. package/lib/module/YearsList/YearsList.js.map +1 -1
  63. package/lib/module/YearsList/get-year-in-tab-order/get-year-in-tab-order.js +7 -1
  64. package/lib/module/YearsList/get-year-in-tab-order/get-year-in-tab-order.js.map +1 -1
  65. package/lib/module/hooks/use-dates-state/is-in-range/is-in-range.js +20 -0
  66. package/lib/module/hooks/use-dates-state/is-in-range/is-in-range.js.map +1 -1
  67. package/lib/module/hooks/use-dates-state/use-dates-state.js +25 -7
  68. package/lib/module/hooks/use-dates-state/use-dates-state.js.map +1 -1
  69. package/lib/module/internal/are-cell-props-equal.js +60 -0
  70. package/lib/module/internal/are-cell-props-equal.js.map +1 -0
  71. package/lib/module/internal/date-string-fast-path.js +91 -0
  72. package/lib/module/internal/date-string-fast-path.js.map +1 -0
  73. package/lib/module/internal/memoize-control-props.js +50 -0
  74. package/lib/module/internal/memoize-control-props.js.map +1 -0
  75. package/lib/module/internal/month-days-cache.js +68 -0
  76. package/lib/module/internal/month-days-cache.js.map +1 -0
  77. package/lib/module/utils/get-date-in-tab-order/get-date-in-tab-order.js +9 -1
  78. package/lib/module/utils/get-date-in-tab-order/get-date-in-tab-order.js.map +1 -1
  79. package/lib/module/utils/is-after-min-date/is-after-min-date.js +14 -1
  80. package/lib/module/utils/is-after-min-date/is-after-min-date.js.map +1 -1
  81. package/lib/module/utils/is-before-max-date/is-before-max-date.js +14 -1
  82. package/lib/module/utils/is-before-max-date/is-before-max-date.js.map +1 -1
  83. package/lib/module/utils/is-same-month/is-same-month.js +17 -1
  84. package/lib/module/utils/is-same-month/is-same-month.js.map +1 -1
  85. package/lib/typescript/DatesProvider/DatesProvider.d.ts.map +1 -1
  86. package/lib/typescript/Day/Day.d.ts +7 -7
  87. package/lib/typescript/Day/Day.d.ts.map +1 -1
  88. package/lib/typescript/Month/Month.d.ts +3 -2
  89. package/lib/typescript/Month/Month.d.ts.map +1 -1
  90. package/lib/typescript/MonthLevel/MonthLevel.d.ts.map +1 -1
  91. package/lib/typescript/MonthsList/MonthsList.d.ts +3 -2
  92. package/lib/typescript/MonthsList/MonthsList.d.ts.map +1 -1
  93. package/lib/typescript/MonthsList/get-month-in-tab-order/get-month-in-tab-order.d.ts +6 -0
  94. package/lib/typescript/MonthsList/get-month-in-tab-order/get-month-in-tab-order.d.ts.map +1 -1
  95. package/lib/typescript/TimeControlsList/TimeControl.d.ts +13 -0
  96. package/lib/typescript/TimeControlsList/TimeControl.d.ts.map +1 -1
  97. package/lib/typescript/TimeControlsList/TimeControlsList.d.ts.map +1 -1
  98. package/lib/typescript/WeekdaysRow/WeekdaysRow.d.ts.map +1 -1
  99. package/lib/typescript/YearsList/YearsList.d.ts +3 -2
  100. package/lib/typescript/YearsList/YearsList.d.ts.map +1 -1
  101. package/lib/typescript/YearsList/get-year-in-tab-order/get-year-in-tab-order.d.ts +6 -0
  102. package/lib/typescript/YearsList/get-year-in-tab-order/get-year-in-tab-order.d.ts.map +1 -1
  103. package/lib/typescript/hooks/use-dates-state/is-in-range/is-in-range.d.ts +12 -0
  104. package/lib/typescript/hooks/use-dates-state/is-in-range/is-in-range.d.ts.map +1 -1
  105. package/lib/typescript/hooks/use-dates-state/use-dates-state.d.ts.map +1 -1
  106. package/lib/typescript/internal/are-cell-props-equal.d.ts +21 -0
  107. package/lib/typescript/internal/are-cell-props-equal.d.ts.map +1 -0
  108. package/lib/typescript/internal/date-string-fast-path.d.ts +35 -0
  109. package/lib/typescript/internal/date-string-fast-path.d.ts.map +1 -0
  110. package/lib/typescript/internal/memoize-control-props.d.ts +12 -0
  111. package/lib/typescript/internal/memoize-control-props.d.ts.map +1 -0
  112. package/lib/typescript/internal/month-days-cache.d.ts +19 -0
  113. package/lib/typescript/internal/month-days-cache.d.ts.map +1 -0
  114. package/lib/typescript/utils/get-date-in-tab-order/get-date-in-tab-order.d.ts +9 -1
  115. package/lib/typescript/utils/get-date-in-tab-order/get-date-in-tab-order.d.ts.map +1 -1
  116. package/lib/typescript/utils/is-after-min-date/is-after-min-date.d.ts +6 -0
  117. package/lib/typescript/utils/is-after-min-date/is-after-min-date.d.ts.map +1 -1
  118. package/lib/typescript/utils/is-before-max-date/is-before-max-date.d.ts +6 -0
  119. package/lib/typescript/utils/is-before-max-date/is-before-max-date.d.ts.map +1 -1
  120. package/lib/typescript/utils/is-same-month/is-same-month.d.ts +13 -1
  121. package/lib/typescript/utils/is-same-month/is-same-month.d.ts.map +1 -1
  122. package/package.json +4 -4
  123. package/src/DatesProvider/DatesProvider.tsx +27 -5
  124. package/src/Day/Day.tsx +51 -10
  125. package/src/Month/Month.tsx +357 -80
  126. package/src/MonthLevel/MonthLevel.tsx +35 -16
  127. package/src/MonthsList/MonthsList.tsx +201 -55
  128. package/src/MonthsList/get-month-in-tab-order/get-month-in-tab-order.ts +12 -1
  129. package/src/TimeControlsList/TimeControl.tsx +16 -1
  130. package/src/TimeControlsList/TimeControlsList.tsx +15 -3
  131. package/src/WeekdaysRow/WeekdaysRow.tsx +29 -5
  132. package/src/YearsList/YearsList.tsx +196 -55
  133. package/src/YearsList/get-year-in-tab-order/get-year-in-tab-order.ts +12 -1
  134. package/src/hooks/use-dates-state/is-in-range/is-in-range.ts +22 -0
  135. package/src/hooks/use-dates-state/use-dates-state.ts +27 -11
  136. package/src/internal/are-cell-props-equal.ts +63 -0
  137. package/src/internal/date-string-fast-path.ts +94 -0
  138. package/src/internal/memoize-control-props.ts +54 -0
  139. package/src/internal/month-days-cache.ts +77 -0
  140. package/src/utils/get-date-in-tab-order/get-date-in-tab-order.ts +17 -2
  141. package/src/utils/is-after-min-date/is-after-min-date.ts +16 -1
  142. package/src/utils/is-before-max-date/is-before-max-date.ts +16 -1
  143. package/src/utils/is-same-month/is-same-month.ts +18 -1
@@ -16,6 +16,9 @@
16
16
  // hiding an outside day is `Day`'s `hidden` variant (`display: none`), and
17
17
  // `fullWidth` is a boolean VARIANT on the cell/frame, not a per-render spread.
18
18
  // ───────────────────────────────────────────────────────────────────────────
19
+ import * as React from "react";
20
+ import { useMemo } from "react";
21
+
19
22
  import dayjs from "dayjs";
20
23
 
21
24
  import { Box, Text } from "@knitui/components";
@@ -27,21 +30,24 @@ import {
27
30
  styled,
28
31
  withStaticProperties,
29
32
  } from "@knitui/core";
33
+ import { useCallbackRef } from "@knitui/hooks";
30
34
 
31
35
  import { type CalendarSize, CELL_FONT, CELL_SPACING, CELL_WIDTH } from "../cell-metrics";
32
36
  import { useDatesContext } from "../DatesProvider";
33
37
  import { Day, type DayProps, type RenderDay } from "../Day";
38
+ import { areCellPropsEqual } from "../internal/are-cell-props-equal";
34
39
  import { focusElement } from "../internal/focus-element";
35
40
  import {
36
41
  hasPreventDefault,
37
42
  hasStopPropagation,
38
43
  isEscapeKey,
39
44
  } from "../internal/has-prevent-default";
45
+ import { memoizeByDate } from "../internal/memoize-control-props";
46
+ import { getCachedMonthDays } from "../internal/month-days-cache";
40
47
  import type { ControlKeydownPayload, DateLabelFormat, DateStringValue, DayOfWeek } from "../types";
41
48
  import {
42
49
  type FocusableControl,
43
50
  getDateInTabOrder,
44
- getMonthDays,
45
51
  getWeekNumber,
46
52
  isAfterMinDate,
47
53
  isBeforeMaxDate,
@@ -50,6 +56,212 @@ import {
50
56
  } from "../utils";
51
57
  import { WeekdaysRow } from "../WeekdaysRow";
52
58
 
59
+ /**
60
+ * The facts about one day cell that depend ONLY on the grid shape + provider
61
+ * settings — no consumer callback, no selection. Derived once per grid shape and
62
+ * cached for as long as that shape holds; this is where all the per-cell dayjs
63
+ * work lives, so it is the layer that must never be invalidated by a consumer
64
+ * handing us a fresh closure. See the two `useMemo`s in `Month` below.
65
+ */
66
+ interface MonthBaseCellInfo {
67
+ /** The cell's date, `YYYY-MM-DD`. */
68
+ date: DateStringValue;
69
+ /** Day falls outside the displayed month. */
70
+ outside: boolean;
71
+ /** Day is removed from layout (`hideOutsideDates` + `outside`). */
72
+ hidden: boolean;
73
+ /** Day is one of the configured weekend days. */
74
+ weekend: boolean;
75
+ /**
76
+ * The locale-formatted fallback `aria-label` (`undefined` for a static grid) —
77
+ * what a cell announces when no `getDayAriaLabel` supplies one.
78
+ */
79
+ defaultAriaLabel: string | undefined;
80
+ }
81
+
82
+ /** One week row's base cells plus its (optional) ISO week number. */
83
+ interface MonthBaseRowInfo {
84
+ cells: MonthBaseCellInfo[];
85
+ weekNumber: number | undefined;
86
+ }
87
+
88
+ /**
89
+ * The SELECTION-INDEPENDENT facts about one day cell — {@link MonthBaseCellInfo}
90
+ * with the two consumer callbacks (`excludeDate`/`getDayAriaLabel`) applied over
91
+ * it. Derived once per grid shape + callback identity rather than once per
92
+ * render — see the two `useMemo`s in `Month` below.
93
+ */
94
+ interface MonthCellInfo {
95
+ /** The cell's date, `YYYY-MM-DD`. */
96
+ date: DateStringValue;
97
+ /** Day falls outside the displayed month. */
98
+ outside: boolean;
99
+ /** Day is removed from layout (`hideOutsideDates` + `outside`). */
100
+ hidden: boolean;
101
+ /** Day is one of the configured weekend days. */
102
+ weekend: boolean;
103
+ /** Day is out of bounds or excluded. */
104
+ disabled: boolean;
105
+ /** Resolved `aria-label` for the cell (`undefined` for a static grid). */
106
+ ariaLabel: string | undefined;
107
+ }
108
+
109
+ /** One week row's cells plus its (optional) ISO week number. */
110
+ interface MonthRowInfo {
111
+ cells: MonthCellInfo[];
112
+ weekNumber: number | undefined;
113
+ }
114
+
115
+ /** Props for the memoized day cell — see {@link MonthDayCell}. */
116
+ interface MonthDayCellProps {
117
+ /** Selection-independent facts about this cell (spread from `MonthCellInfo`). */
118
+ date: DateStringValue;
119
+ outside: boolean;
120
+ hidden: boolean;
121
+ weekend: boolean;
122
+ disabled: boolean | undefined;
123
+ ariaLabel: string | undefined;
124
+
125
+ /** Grid position, used for roving focus. */
126
+ rowIndex: number;
127
+ cellIndex: number;
128
+
129
+ /** Roving tabindex for this cell. */
130
+ tabIndex: number;
131
+
132
+ size: MonthSize;
133
+ isStatic: boolean;
134
+ fullWidth: boolean;
135
+ highlightToday: boolean;
136
+ renderDay: RenderDay | undefined;
137
+ stopPropagation: boolean | undefined;
138
+ preventFocus: boolean | undefined;
139
+
140
+ /** `styles.cell` props for the wrapper. */
141
+ cellProps: GetProps<typeof MonthCell> | undefined;
142
+
143
+ /** The raw `getDayProps(date)` result — the source of the consumer's own handlers. */
144
+ dayProps: Partial<DayProps> | undefined;
145
+
146
+ /** `styles.day` merged UNDER `dayProps` — spread onto the `Day` leaf. */
147
+ mergedDayProps: Partial<DayProps>;
148
+
149
+ /**
150
+ * The `__`-prefixed grid callbacks, pre-stabilised by `Month` with
151
+ * `useCallbackRef` — an unstable identity here would defeat the memo for every
152
+ * cell on every render.
153
+ */
154
+ onDayMouseEnter: (event: unknown, date: DateStringValue) => void;
155
+ onDayClick: (event: DayPressEvent, date: DateStringValue) => void;
156
+ onDayKeyDown: (event: DayKeyboardEvent, payload: ControlKeydownPayload) => void;
157
+ getDayRef: (rowIndex: number, cellIndex: number, control: FocusableControl) => void;
158
+ }
159
+
160
+ /**
161
+ * One day cell, memoized.
162
+ *
163
+ * The grid re-renders for reasons that concern only one or two cells — most
164
+ * sharply a web hover move, which updates `hoveredDate` and re-renders the whole
165
+ * `DatePicker` subtree per cell crossed. Without a memo boundary that meant 42
166
+ * `Day` leaves re-rendering (and 42 refs detaching/reattaching, re-running
167
+ * `__getDayRef` and rebuilding the `daysRefs` matrix) for a change that moved two
168
+ * cells' background colour.
169
+ *
170
+ * The boundary is deliberately here — an internal cell, the same pattern the kit
171
+ * uses for `Combobox`'s `OptionRow` and `TagsInput`'s chips — and NOT on the public
172
+ * `Day`, whose props contract stays exactly as it was. Tamagui's styled-context
173
+ * and theme reads happen inside `Day` and still propagate past a memo, so cells
174
+ * keep responding to theme/size changes.
175
+ */
176
+ const MonthDayCell = React.memo(function MonthDayCell({
177
+ date,
178
+ outside,
179
+ hidden,
180
+ weekend,
181
+ disabled,
182
+ ariaLabel,
183
+ rowIndex,
184
+ cellIndex,
185
+ tabIndex,
186
+ size,
187
+ isStatic,
188
+ fullWidth,
189
+ highlightToday,
190
+ renderDay,
191
+ stopPropagation,
192
+ preventFocus,
193
+ cellProps,
194
+ dayProps,
195
+ mergedDayProps,
196
+ onDayMouseEnter,
197
+ onDayClick,
198
+ onDayKeyDown,
199
+ getDayRef,
200
+ }: MonthDayCellProps) {
201
+ // Tamagui hover handlers are not part `Day`'s public prop type; attach them via
202
+ // a precisely-typed object spread, the kit's pattern for web-only affordances
203
+ // (see `@knitui/components` `hoverProps`). Native ignores it.
204
+ const hoverHandlers: { onHoverIn?: (event: unknown) => void } = {
205
+ onHoverIn: (event) => onDayMouseEnter(event, date),
206
+ };
207
+
208
+ return (
209
+ <MonthCell role="cell" fullWidth={fullWidth} {...cellProps}>
210
+ <Day
211
+ highlightToday={highlightToday}
212
+ renderDay={renderDay}
213
+ date={date}
214
+ size={size}
215
+ weekend={weekend}
216
+ outside={outside}
217
+ hidden={hidden}
218
+ aria-label={ariaLabel}
219
+ static={isStatic}
220
+ fullWidth={fullWidth}
221
+ disabled={disabled}
222
+ ref={(node) => {
223
+ if (node) {
224
+ getDayRef(rowIndex, cellIndex, {
225
+ focus: () => focusElement(node),
226
+ disabled,
227
+ isHidden: hidden,
228
+ isOutside: outside,
229
+ });
230
+ }
231
+ }}
232
+ // explicit beats sugar: the `day` slot sits UNDER per-item
233
+ // `getDayProps`, and the consumer's own handlers are preserved
234
+ // (called before ours).
235
+ {...mergedDayProps}
236
+ {...hoverHandlers}
237
+ onKeyDown={(event) => {
238
+ dayProps?.onKeyDown?.(event);
239
+ onDayKeyDown(event, { rowIndex, cellIndex, date });
240
+ // When nested inside an input-trigger dropdown (`__stopPropagation`,
241
+ // driven by `DateTimePicker`), keep the calendar's Escape from
242
+ // bubbling out and closing an outer picker — the cross-platform
243
+ // replacement for Mantine's `data-mantine-stop-propagation`. No-op
244
+ // on native (no DOM `stopPropagation`).
245
+ if (stopPropagation && isEscapeKey(event) && hasStopPropagation(event)) {
246
+ event.stopPropagation();
247
+ }
248
+ }}
249
+ onPress={(event) => {
250
+ dayProps?.onPress?.(event);
251
+ onDayClick(event, date);
252
+ }}
253
+ onPressIn={(event: DayPressInEvent) => {
254
+ dayProps?.onPressIn?.(event);
255
+ if (preventFocus && hasPreventDefault(event)) {
256
+ event.preventDefault();
257
+ }
258
+ }}
259
+ tabIndex={tabIndex}
260
+ />
261
+ </MonthCell>
262
+ );
263
+ }, areCellPropsEqual);
264
+
53
265
  /** Month grid size — the kit's shared control size scale (mirrors `Day`). */
54
266
  type MonthSize = CalendarSize;
55
267
 
@@ -367,21 +579,117 @@ const MonthComponent = MonthFrame.styleable<MonthProps>(function Month(props, re
367
579
  const minDateString = minDate == null ? undefined : toDateString(minDate);
368
580
  const maxDateString = maxDate == null ? undefined : toDateString(maxDate);
369
581
  const weekendDaysList = ctx.getWeekendDays(weekendDays);
582
+ const resolvedLocale = locale || ctx.locale;
583
+ // The weekend set is depended on by VALUE below, not by identity: a consumer
584
+ // inlining `weekendDays={[0, 6]}` hands us a fresh array every render, which
585
+ // would defeat the grid memo while never changing the answer.
586
+ const weekendDaysKey = weekendDaysList.join(",");
370
587
 
371
588
  // 7. Typed per-slot accessor (dev-warns unknown keys against the known set).
372
589
  const s = slotStyles<MonthStyles>(styles, MONTH_SLOT_KEYS, "Month");
373
590
 
374
- const dates = getMonthDays({
591
+ // Memoized on the three primitives the grid shape depends on, so navigating
592
+ // back to an already-visited month replays the cached weeks instead of walking
593
+ // the calendar day by day again (~70 dayjs instances + 35 `format` calls). Also
594
+ // gives `dates` a STABLE identity per month, which is what lets it be used as a
595
+ // `useMemo` dependency below.
596
+ const dates = getCachedMonthDays({
375
597
  month,
376
598
  firstDayOfWeek: ctx.getFirstDayOfWeek(firstDayOfWeek),
377
599
  consistentWeeks: ctx.consistentWeeks,
378
600
  });
379
601
 
602
+ // ── The per-cell derivation, hoisted out of the render loop ────────────────
603
+ // Everything here is SELECTION-INDEPENDENT: which days are outside the month,
604
+ // hidden, weekend, out of bounds, and what each announces. None of it can change
605
+ // when the selection or the hovered day changes — yet it was all re-derived for
606
+ // 42 cells on every render, including the renders caused by nothing but a hover
607
+ // moving one cell over. `dayjs(date)` + `format("D MMMM YYYY")` per cell was the
608
+ // bulk of it.
609
+ //
610
+ // The selection-DEPENDENT parts (`getDayProps`, the roving `tabIndex`) are
611
+ // deliberately left in the loop below: they read live state, so memoizing them
612
+ // would need a selection signature that is easy to get subtly wrong. Splitting
613
+ // at the selection boundary gets the same win with none of that risk.
614
+ //
615
+ // The derivation is split across TWO memos, at the consumer-callback boundary.
616
+ // `excludeDate`/`getDayAriaLabel` arrive straight from consumer props, and an app
617
+ // that writes `excludeDate={(d) => …}` inline hands us a new function on every
618
+ // render — which invalidated the single combined memo on every hover frame, so
619
+ // the 42 dayjs instances + 42 `format("D MMMM YYYY")` calls were paid anyway.
620
+ // Stabilising those identities (`useCallbackRef`) is NOT an option: a predicate
621
+ // legitimately closes over live state, and a memo that cannot see it change would
622
+ // silently show stale disabled days. So instead the EXPENSIVE half — everything
623
+ // that touches dayjs — is memoized WITHOUT them, and only the cheap half (calling
624
+ // the consumer's two functions and assembling the result) recomputes when their
625
+ // identity moves. Both memos still bail out entirely in the common case, so the
626
+ // stable-callback path is unchanged.
627
+ const baseRows = useMemo<MonthBaseRowInfo[]>(
628
+ () =>
629
+ dates.map((week) => ({
630
+ weekNumber: withWeekNumbers ? getWeekNumber(week) : undefined,
631
+ cells: week.map((date) => {
632
+ const outside = !isSameMonth(date, month);
633
+ // ONE dayjs per cell, shared by the weekend test and the fallback label.
634
+ // `weekendDaysList.some((wd) => wd === dayjs(date).day())` built an
635
+ // instance PER WEEKEND DAY TESTED — 2 with the default `[0, 6]`, i.e. 84
636
+ // per grid — and the label built a third. `.day()` is a plain field read
637
+ // and `.locale()` returns a clone, so sharing the instance is safe.
638
+ const day = dayjs(date);
639
+ const dayOfWeek = day.day();
640
+ return {
641
+ date,
642
+ outside,
643
+ hidden: hideOutsideDates ? outside : false,
644
+ weekend: weekendDaysList.some((wd) => wd === dayOfWeek),
645
+ // Computed up front rather than lazily behind `getDayAriaLabel`'s `||`:
646
+ // being independent of the consumer's getter is exactly what makes this
647
+ // layer cacheable, and the resolved label is identical either way.
648
+ defaultAriaLabel: isStatic
649
+ ? undefined
650
+ : day.locale(resolvedLocale).format("D MMMM YYYY"),
651
+ };
652
+ }),
653
+ })),
654
+ // `weekendDaysList` is depended on via `weekendDaysKey` (its value) rather than
655
+ // its identity — see the note where that key is built.
656
+ // eslint-disable-next-line react-hooks/exhaustive-deps
657
+ [dates, month, hideOutsideDates, weekendDaysKey, isStatic, resolvedLocale, withWeekNumbers],
658
+ );
659
+
660
+ const rowsInfo = useMemo<MonthRowInfo[]>(
661
+ () =>
662
+ baseRows.map((row) => ({
663
+ weekNumber: row.weekNumber,
664
+ cells: row.cells.map((cell) => ({
665
+ date: cell.date,
666
+ outside: cell.outside,
667
+ hidden: cell.hidden,
668
+ weekend: cell.weekend,
669
+ disabled:
670
+ excludeDate?.(cell.date) ||
671
+ !isBeforeMaxDate(cell.date, maxDateString) ||
672
+ !isAfterMinDate(cell.date, minDateString),
673
+ ariaLabel: getDayAriaLabel?.(cell.date) || cell.defaultAriaLabel,
674
+ })),
675
+ })),
676
+ [baseRows, excludeDate, minDateString, maxDateString, getDayAriaLabel],
677
+ );
678
+
679
+ // ONE per-render cache shared by `getDateInTabOrder` (which consults the getter
680
+ // twice per date) and the cell loop below (a third time) — so each date's props
681
+ // are derived exactly once instead of three times. `getDayProps` is typically
682
+ // `useDatesState`'s `getControlProps`, ~20 dayjs instances per call for a range
683
+ // picker, which made those redundant calls the single largest cost in the grid.
684
+ // Deliberately NOT memoized across renders: the getter closes over live
685
+ // selection/hover state (see `memoizeByDate`).
686
+ const resolveDayProps = memoizeByDate(getDayProps);
687
+
380
688
  const dateInTabOrder = getDateInTabOrder({
381
689
  dates,
382
690
  minDate: minDateString,
383
691
  maxDate: maxDateString,
384
- getDayProps,
692
+ getDayProps: resolveDayProps,
385
693
  excludeDate,
386
694
  hideOutsideDates,
387
695
  month,
@@ -389,84 +697,53 @@ const MonthComponent = MonthFrame.styleable<MonthProps>(function Month(props, re
389
697
 
390
698
  const cellGap = withCellSpacing ? CELL_SPACING : 0;
391
699
 
392
- const rows = dates.map((week, rowIndex) => {
393
- const cells = week.map((date, cellIndex) => {
394
- const day = dayjs(date);
395
- const outside = !isSameMonth(date, month);
396
- const hidden = hideOutsideDates ? outside : false;
397
- const weekend = weekendDaysList.some((wd) => wd === day.day());
398
- const disabled =
399
- excludeDate?.(date) ||
400
- !isBeforeMaxDate(date, maxDateString) ||
401
- !isAfterMinDate(date, minDateString);
402
- const ariaLabel =
403
- getDayAriaLabel?.(date) ||
404
- (isStatic ? undefined : day.locale(locale || ctx.locale).format("D MMMM YYYY"));
405
- const dayProps = getDayProps?.(date);
406
- const isDateInTabOrder = day.isSame(dateInTabOrder, "date");
407
- const tabIndex = __preventFocus || !isDateInTabOrder ? -1 : 0;
408
-
409
- // Tamagui hover handlers are not part of `Day`'s public prop type; attach
410
- // them via a precisely-typed object spread, the kit's pattern for web-only
411
- // affordances (see `@knitui/components` `hoverProps`). Native ignores it.
412
- const hoverHandlers: { onHoverIn?: (event: unknown) => void } = {
413
- onHoverIn: (event) => __onDayMouseEnter?.(event, date),
414
- };
700
+ // The cell memo can only bail out if every function prop it receives keeps a
701
+ // stable identity. All four of these arrive freshly built each render (the level
702
+ // group builds them per month, `DatePicker` per render), so they are wrapped
703
+ // once here rather than being stabilised at each of their many call sites.
704
+ const onDayMouseEnter = useCallbackRef(__onDayMouseEnter);
705
+ const onDayClick = useCallbackRef(__onDayClick);
706
+ const onDayKeyDown = useCallbackRef(__onDayKeyDown);
707
+ const getDayRef = useCallbackRef(__getDayRef);
708
+
709
+ const cellSlotProps = s.get("cell");
710
+
711
+ const rows = rowsInfo.map((row, rowIndex) => {
712
+ const cells = row.cells.map((info, cellIndex) => {
713
+ const dayProps = resolveDayProps?.(info.date);
714
+ // `dateInTabOrder` comes from the same `getMonthDays` output as `date`, so
715
+ // both are canonical `YYYY-MM-DD` — string equality is exactly the
716
+ // day-granularity `isSame` this used to spend a dayjs instance (plus its
717
+ // `startOf`/`endOf` clones) on, per cell, per render.
718
+ const isDateInTabOrder = dateInTabOrder !== undefined && info.date === dateInTabOrder;
415
719
 
416
720
  return (
417
- <MonthCell key={date} role="cell" fullWidth={fullWidth} {...s.get("cell")}>
418
- <Day
419
- highlightToday={highlightToday}
420
- renderDay={renderDay}
421
- date={date}
422
- size={size}
423
- weekend={weekend}
424
- outside={outside}
425
- hidden={hidden}
426
- aria-label={ariaLabel}
427
- static={isStatic}
428
- fullWidth={fullWidth}
429
- disabled={disabled}
430
- ref={(node) => {
431
- if (node) {
432
- __getDayRef?.(rowIndex, cellIndex, {
433
- focus: () => focusElement(node),
434
- disabled,
435
- isHidden: hidden,
436
- isOutside: outside,
437
- });
438
- }
439
- }}
440
- // explicit beats sugar: the `day` slot sits UNDER per-item
441
- // `getDayProps`, and the consumer's own handlers are preserved
442
- // (called before ours).
443
- {...s.merge("day", dayProps)}
444
- {...hoverHandlers}
445
- onKeyDown={(event) => {
446
- dayProps?.onKeyDown?.(event);
447
- __onDayKeyDown?.(event, { rowIndex, cellIndex, date });
448
- // When nested inside an input-trigger dropdown (`__stopPropagation`,
449
- // driven by `DateTimePicker`), keep the calendar's Escape from
450
- // bubbling out and closing an outer picker — the cross-platform
451
- // replacement for Mantine's `data-mantine-stop-propagation`. No-op
452
- // on native (no DOM `stopPropagation`).
453
- if (__stopPropagation && isEscapeKey(event) && hasStopPropagation(event)) {
454
- event.stopPropagation();
455
- }
456
- }}
457
- onPress={(event) => {
458
- dayProps?.onPress?.(event);
459
- __onDayClick?.(event, date);
460
- }}
461
- onPressIn={(event: DayPressInEvent) => {
462
- dayProps?.onPressIn?.(event);
463
- if (__preventFocus && hasPreventDefault(event)) {
464
- event.preventDefault();
465
- }
466
- }}
467
- tabIndex={tabIndex}
468
- />
469
- </MonthCell>
721
+ <MonthDayCell
722
+ key={info.date}
723
+ date={info.date}
724
+ outside={info.outside}
725
+ hidden={info.hidden}
726
+ weekend={info.weekend}
727
+ disabled={info.disabled}
728
+ ariaLabel={info.ariaLabel}
729
+ rowIndex={rowIndex}
730
+ cellIndex={cellIndex}
731
+ tabIndex={__preventFocus || !isDateInTabOrder ? -1 : 0}
732
+ size={size}
733
+ isStatic={isStatic}
734
+ fullWidth={fullWidth}
735
+ highlightToday={highlightToday}
736
+ renderDay={renderDay}
737
+ stopPropagation={__stopPropagation}
738
+ preventFocus={__preventFocus}
739
+ cellProps={cellSlotProps}
740
+ dayProps={dayProps}
741
+ mergedDayProps={s.merge("day", dayProps)}
742
+ onDayMouseEnter={onDayMouseEnter}
743
+ onDayClick={onDayClick}
744
+ onDayKeyDown={onDayKeyDown}
745
+ getDayRef={getDayRef}
746
+ />
470
747
  );
471
748
  });
472
749
 
@@ -481,7 +758,7 @@ const MonthComponent = MonthFrame.styleable<MonthProps>(function Month(props, re
481
758
  {withWeekNumbers && (
482
759
  <MonthWeekNumber size={size} role="rowheader" {...s.get("weekNumber")}>
483
760
  <MonthWeekNumberLabel size={size} {...s.get("weekNumberLabel")}>
484
- {getWeekNumber(week)}
761
+ {row.weekNumber}
485
762
  </MonthWeekNumberLabel>
486
763
  </MonthWeekNumber>
487
764
  )}
@@ -217,27 +217,46 @@ const MonthLevelComponent = MonthLevelFrame.styleable<MonthLevelProps>(
217
217
 
218
218
  // 9. Locale comes from `DatesProvider` (consumer `locale` prop wins).
219
219
  const ctx = useDatesContext();
220
+ const resolvedLocale = ctx.getLocale(locale);
220
221
 
221
222
  // 10. Auto-disable the controls at the `minDate`/`maxDate` bounds (an explicit
222
223
  // boolean override always wins).
223
- const _nextDisabled =
224
- typeof nextDisabled === "boolean"
225
- ? nextDisabled
226
- : maxDate
227
- ? !dayjs(month).endOf("month").isBefore(maxDate)
228
- : false;
224
+ //
225
+ // All three derivations are dayjs-heavy and none of them can change on a render
226
+ // caused by something else: 3 `dayjs(month)` constructions, an `endOf`/`startOf`
227
+ // clone each, plus a `.locale()` clone and a `format` for the label. The whole
228
+ // calendar re-renders per hovered day cell (`hoveredDate` lives above it), and
229
+ // this runs once PER COLUMN, so unmemoized it was paid on every hover frame.
230
+ // Memoized on exactly the inputs the three read — note `monthLabelFormat` and
231
+ // the `min`/`maxDate` bounds are depended on by IDENTITY, so a consumer's
232
+ // inline function or fresh `Date` still recomputes (correct, just no win).
233
+ const {
234
+ label,
235
+ nextDisabled: _nextDisabled,
236
+ previousDisabled: _previousDisabled,
237
+ } = React.useMemo(
238
+ () => ({
239
+ nextDisabled:
240
+ typeof nextDisabled === "boolean"
241
+ ? nextDisabled
242
+ : maxDate
243
+ ? !dayjs(month).endOf("month").isBefore(maxDate)
244
+ : false,
229
245
 
230
- const _previousDisabled =
231
- typeof previousDisabled === "boolean"
232
- ? previousDisabled
233
- : minDate
234
- ? !dayjs(month).startOf("month").isAfter(minDate)
235
- : false;
246
+ previousDisabled:
247
+ typeof previousDisabled === "boolean"
248
+ ? previousDisabled
249
+ : minDate
250
+ ? !dayjs(month).startOf("month").isAfter(minDate)
251
+ : false,
236
252
 
237
- const label =
238
- typeof monthLabelFormat === "function"
239
- ? monthLabelFormat(month)
240
- : dayjs(month).locale(ctx.getLocale(locale)).format(monthLabelFormat);
253
+ label:
254
+ typeof monthLabelFormat === "function"
255
+ ? monthLabelFormat(month)
256
+ : dayjs(month).locale(resolvedLocale).format(monthLabelFormat),
257
+ }),
258
+ [month, minDate, maxDate, nextDisabled, previousDisabled, resolvedLocale, monthLabelFormat],
259
+ );
241
260
 
242
261
  // 7. Route the header-named slots into `CalendarHeader`'s own `styles` map.
243
262
  const headerStyles: SlotStyles<CalendarHeaderStyles> = {