@ankhorage/zora 28.9.0 → 28.10.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 (44) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +1 -1
  3. package/dist/capabilities/index.js +52 -5
  4. package/dist/capabilities/index.js.map +4 -4
  5. package/dist/features/calendar/adapters/inbound/Calendar.d.ts +1 -1
  6. package/dist/features/calendar/adapters/inbound/Calendar.d.ts.map +1 -1
  7. package/dist/features/calendar/adapters/inbound/Calendar.js +207 -75
  8. package/dist/features/calendar/adapters/inbound/Calendar.js.map +1 -1
  9. package/dist/features/calendar/calendarMeta.d.ts +106 -4
  10. package/dist/features/calendar/calendarMeta.d.ts.map +1 -1
  11. package/dist/features/calendar/calendarMeta.js +44 -4
  12. package/dist/features/calendar/calendarMeta.js.map +1 -1
  13. package/dist/features/calendar/public.d.ts +1 -1
  14. package/dist/features/calendar/public.d.ts.map +1 -1
  15. package/dist/features/calendar/public.js.map +1 -1
  16. package/dist/features/data-table/dataTableMeta.d.ts +10 -0
  17. package/dist/features/data-table/dataTableMeta.d.ts.map +1 -1
  18. package/dist/features/data-table/dataTableMeta.js +7 -0
  19. package/dist/features/data-table/dataTableMeta.js.map +1 -1
  20. package/dist/types/calendar.d.ts +40 -19
  21. package/dist/types/calendar.d.ts.map +1 -1
  22. package/dist/types/calendar.js.map +1 -1
  23. package/package.json +79 -4
  24. package/src/features/calendar/adapters/inbound/Calendar.interaction.test.tsx +95 -0
  25. package/src/features/calendar/adapters/inbound/Calendar.tsx +425 -135
  26. package/src/features/calendar/calendarMeta.ts +44 -4
  27. package/src/features/calendar/public.test.ts +8 -5
  28. package/src/features/calendar/public.ts +4 -0
  29. package/src/features/data-table/adapters/inbound/DataTable.test.tsx +52 -1
  30. package/src/features/data-table/dataTableMeta.test.ts +8 -1
  31. package/src/features/data-table/dataTableMeta.ts +7 -0
  32. package/src/features/registry/componentMeta.test.ts +1 -0
  33. package/src/types/calendar.ts +44 -19
  34. package/web-dist/chunks/Accordion-59x6e7bs.js +200 -0
  35. package/web-dist/chunks/Accordion-q2yxtdzc.js +426 -0
  36. package/web-dist/components/calendar/Calendar.d.ts +2 -2
  37. package/web-dist/components/calendar/Calendar.dependency-2.d.ts +165 -0
  38. package/web-dist/components/calendar/Calendar.js +2 -1
  39. package/web-dist/components/calendar/Calendar.types-0.d.ts +45 -19
  40. package/web-dist/components/calendar/index.d.ts +1 -1
  41. package/web-dist/components/date-picker/DatePicker.js +2 -1
  42. package/web-dist/components/time-grid/TimeGrid.js +4 -198
  43. package/web-dist/manifest.json +6 -2
  44. package/web-dist/chunks/Accordion-v3bmbnfh.js +0 -237
@@ -1,107 +1,219 @@
1
1
  import type { GridRectItem } from '@ankhorage/grid-view';
2
2
  import React from 'react';
3
3
 
4
- import type { CalendarProps } from '../../../../types/calendar';
4
+ import type { CalendarProps, CalendarView } from '../../../../types/calendar';
5
+ import type {
6
+ TemporalDate,
7
+ TemporalEvent,
8
+ TemporalTimedEvent,
9
+ TemporalTimeZone,
10
+ } from '../../../../types/temporal';
5
11
  import { Button } from '../../../button/public';
6
12
  import { GridView } from '../../../grid-view/public';
7
13
  import { View } from '../../../layout/public';
8
14
  import { withZoraThemeScope } from '../../../theme/adapters/inbound/withZoraThemeScope';
15
+ import { TimeGrid } from '../../../time-grid/public';
9
16
  import { Text } from '../../../typography/public';
10
17
  import { createCalendarMonth } from '../../application/createCalendarMonth';
11
18
  import { isCalendarDateDisabled } from '../../application/isCalendarDateDisabled';
12
19
 
13
20
  const GRID_COLUMNS = 7;
14
21
  const GRID_ROWS = 6;
22
+ const MINUTES_PER_DAY = 24 * 60;
15
23
 
16
- /*** Renders a locale-aware, accessible month calendar using canonical GridView geometry. */
24
+ /*** Renders controlled month, week, and day Calendar views through GridView and TimeGrid. */
17
25
  export const Calendar = withZoraThemeScope(CalendarInner);
18
26
 
19
- /*** Connects controlled Calendar state to the shared month grid presentation. */
20
- function CalendarInner({
21
- defaultDisplayedMonth,
22
- density = 'regular',
23
- disabledDates = [],
24
- displayedMonth: controlledDisplayedMonth,
25
- interactionPolicy,
26
- isDateDisabled,
27
- locale,
28
- maxDate,
29
- minDate,
30
- onDisplayedMonthChange,
31
- onSelectedDateChange,
32
- selectedDate,
33
- testID,
34
- weekStartsOn = 0,
35
- }: CalendarProps) {
36
- const [uncontrolledDisplayedMonth, setUncontrolledDisplayedMonth] = React.useState(() =>
37
- startOfLocalMonth(defaultDisplayedMonth ?? selectedDate ?? minDate ?? new Date()),
38
- );
39
- const displayedMonth = startOfLocalMonth(controlledDisplayedMonth ?? uncontrolledDisplayedMonth);
40
- const cellSize = density === 'compact' ? 42 : 48;
41
- const gridWidth = cellSize * GRID_COLUMNS;
42
- const gridHeight = cellSize * GRID_ROWS;
43
- const cells = React.useMemo(
44
- () => createCalendarMonth(displayedMonth, weekStartsOn),
45
- [displayedMonth, weekStartsOn],
46
- );
47
- const gridItems = React.useMemo<readonly GridRectItem[]>(
27
+ /*** Connects serializable manifest state and the DatePicker-compatible code API to Calendar views. */
28
+ function CalendarInner(props: CalendarProps) {
29
+ const {
30
+ defaultDisplayedDate,
31
+ defaultDisplayedMonth,
32
+ defaultView = 'month',
33
+ density = 'regular',
34
+ displayedDate: controlledDisplayedDate,
35
+ displayedMonth: controlledDisplayedMonth,
36
+ events = [],
37
+ locale,
38
+ onDateChange,
39
+ onDisplayedMonthChange,
40
+ onEventActivate,
41
+ onViewChange,
42
+ timeZone,
43
+ view: controlledView,
44
+ weekStartsOn = 0,
45
+ } = props;
46
+ const [uncontrolledDate, setUncontrolledDate] = React.useState(
48
47
  () =>
49
- cells.map((cell, index) => ({
50
- height: cellSize,
51
- id: cell.id,
52
- width: cellSize,
53
- x: (index % GRID_COLUMNS) * cellSize,
54
- y: Math.floor(index / GRID_COLUMNS) * cellSize,
55
- })),
56
- [cellSize, cells],
48
+ parseCalendarDate(defaultDisplayedDate) ??
49
+ startOfLocalDay(defaultDisplayedMonth ?? props.selectedDate ?? props.minDate ?? new Date()),
57
50
  );
58
- const cellsById = React.useMemo(() => new Map(cells.map((cell) => [cell.id, cell])), [cells]);
59
- const disabledDateIds = React.useMemo(
60
- () => new Set(disabledDates.map(formatCalendarDate)),
61
- [disabledDates],
62
- );
63
- const weekdays = React.useMemo(
64
- () => createWeekdayLabels(locale, weekStartsOn),
65
- [locale, weekStartsOn],
66
- );
67
- const setDisplayedMonth = React.useCallback(
68
- (month: Date) => {
69
- const nextMonth = startOfLocalMonth(month);
70
- if (controlledDisplayedMonth === undefined) setUncontrolledDisplayedMonth(nextMonth);
71
- onDisplayedMonthChange?.(nextMonth);
51
+ const [uncontrolledView, setUncontrolledView] = React.useState<CalendarView>(defaultView);
52
+ const displayedDate =
53
+ parseCalendarDate(controlledDisplayedDate) ??
54
+ startOfLocalDay(controlledDisplayedMonth ?? uncontrolledDate);
55
+ const view = controlledView ?? uncontrolledView;
56
+ const setDisplayedDate = React.useCallback(
57
+ (value: Date) => {
58
+ const next = startOfLocalDay(value);
59
+ if (controlledDisplayedDate === undefined && controlledDisplayedMonth === undefined)
60
+ setUncontrolledDate(next);
61
+ onDisplayedMonthChange?.(startOfLocalMonth(next));
62
+ onDateChange?.({ date: formatCalendarDate(next) });
72
63
  },
73
- [controlledDisplayedMonth, onDisplayedMonthChange],
64
+ [controlledDisplayedDate, controlledDisplayedMonth, onDateChange, onDisplayedMonthChange],
65
+ );
66
+ const setView = React.useCallback(
67
+ (next: CalendarView) => {
68
+ if (controlledView === undefined) setUncontrolledView(next);
69
+ onViewChange?.({ view: next });
70
+ },
71
+ [controlledView, onViewChange],
72
+ );
73
+ const navigate = React.useCallback(
74
+ (offset: number) =>
75
+ setDisplayedDate(
76
+ view === 'month'
77
+ ? addMonths(displayedDate, offset)
78
+ : addDays(displayedDate, offset * (view === 'week' ? 7 : 1)),
79
+ ),
80
+ [displayedDate, setDisplayedDate, view],
74
81
  );
75
- const previousMonth = addMonths(displayedMonth, -1);
76
- const nextMonth = addMonths(displayedMonth, 1);
77
-
78
82
  return (
79
- <View accessibilityLabel="Calendar" gap={density === 'compact' ? 's' : 'm'} testID={testID}>
83
+ <View
84
+ accessibilityLabel="Calendar"
85
+ gap={density === 'compact' ? 's' : 'm'}
86
+ testID={props.testID}
87
+ >
80
88
  <View align="center" direction="row" justify="space-between">
81
89
  <Button
82
- disabled={!monthIntersectsRange(previousMonth, minDate, maxDate)}
83
- interactionPolicy={interactionPolicy}
84
- onPress={() => setDisplayedMonth(previousMonth)}
90
+ interactionPolicy={props.interactionPolicy}
91
+ onPress={() => navigate(-1)}
85
92
  size="s"
93
+ testID={withTestId(props.testID, 'previous')}
86
94
  variant="ghost"
87
95
  >
88
96
  Previous
89
97
  </Button>
90
98
  <Text accessibilityRole="header" align="center" variant="label" weight="semiBold">
91
- {formatMonthLabel(displayedMonth, locale)}
99
+ {formatViewLabel(displayedDate, locale, view)}
92
100
  </Text>
93
101
  <Button
94
- disabled={!monthIntersectsRange(nextMonth, minDate, maxDate)}
95
- interactionPolicy={interactionPolicy}
96
- onPress={() => setDisplayedMonth(nextMonth)}
102
+ interactionPolicy={props.interactionPolicy}
103
+ onPress={() => navigate(1)}
97
104
  size="s"
105
+ testID={withTestId(props.testID, 'next')}
98
106
  variant="ghost"
99
107
  >
100
108
  Next
101
109
  </Button>
102
110
  </View>
111
+ <View direction="row" justify="space-between">
112
+ {(['day', 'week', 'month'] as const).map((candidate) => (
113
+ <Button
114
+ key={candidate}
115
+ interactionPolicy={props.interactionPolicy}
116
+ onPress={() => setView(candidate)}
117
+ size="s"
118
+ testID={withTestId(props.testID, `view-${candidate}`)}
119
+ variant={view === candidate ? 'solid' : 'ghost'}
120
+ >
121
+ {candidate}
122
+ </Button>
123
+ ))}
124
+ <Button
125
+ interactionPolicy={props.interactionPolicy}
126
+ onPress={() => setDisplayedDate(new Date())}
127
+ size="s"
128
+ testID={withTestId(props.testID, 'today')}
129
+ variant="ghost"
130
+ >
131
+ Today
132
+ </Button>
133
+ </View>
134
+ {view === 'month' ? (
135
+ <CalendarMonth
136
+ {...props}
137
+ activateEvent={(id) => onEventActivate?.({ eventId: id })}
138
+ cellSize={density === 'compact' ? 42 : 48}
139
+ displayedDate={displayedDate}
140
+ events={events}
141
+ timeZone={timeZone}
142
+ weekStartsOn={weekStartsOn}
143
+ onSelectDate={(date) => {
144
+ setDisplayedDate(date);
145
+ props.onSelectedDateChange?.(date);
146
+ }}
147
+ />
148
+ ) : (
149
+ <CalendarTimeView
150
+ activateEvent={(id) => onEventActivate?.({ eventId: id })}
151
+ displayedDate={displayedDate}
152
+ events={events}
153
+ interactionPolicy={props.interactionPolicy}
154
+ locale={locale}
155
+ testID={props.testID}
156
+ timeZone={timeZone}
157
+ view={view}
158
+ weekStartsOn={weekStartsOn}
159
+ />
160
+ )}
161
+ </View>
162
+ );
163
+ }
164
+
165
+ /*** Renders the canonical GridView month geometry with stable event activation. */
166
+ function CalendarMonth({
167
+ activateEvent,
168
+ cellSize,
169
+ displayedDate,
170
+ events,
171
+ onSelectDate,
172
+ timeZone,
173
+ weekStartsOn,
174
+ ...props
175
+ }: Omit<CalendarProps, 'displayedDate'> & {
176
+ readonly activateEvent: (eventId: string) => void;
177
+ readonly cellSize: number;
178
+ readonly displayedDate: Date;
179
+ readonly events: readonly TemporalEvent[];
180
+ readonly onSelectDate: (date: Date) => void;
181
+ readonly timeZone: TemporalTimeZone | undefined;
182
+ readonly weekStartsOn: number;
183
+ }) {
184
+ const cells = React.useMemo(
185
+ () => createCalendarMonth(displayedDate, weekStartsOn),
186
+ [displayedDate, weekStartsOn],
187
+ );
188
+ const gridItems = React.useMemo<readonly GridRectItem[]>(
189
+ () =>
190
+ cells.map((cell, index) => ({
191
+ height: cellSize,
192
+ id: cell.id,
193
+ width: cellSize,
194
+ x: (index % GRID_COLUMNS) * cellSize,
195
+ y: Math.floor(index / GRID_COLUMNS) * cellSize,
196
+ })),
197
+ [cellSize, cells],
198
+ );
199
+ const cellsById = React.useMemo(() => new Map(cells.map((cell) => [cell.id, cell])), [cells]);
200
+ const disabledDateIds = React.useMemo(
201
+ () => new Set((props.disabledDates ?? []).map(formatCalendarDate)),
202
+ [props.disabledDates],
203
+ );
204
+ const eventsByDate = React.useMemo(
205
+ () =>
206
+ createEventsByDate(
207
+ events,
208
+ cells.map((cell) => cell.date),
209
+ timeZone,
210
+ ),
211
+ [cells, events, timeZone],
212
+ );
213
+ return (
214
+ <>
103
215
  <View direction="row">
104
- {weekdays.map((weekday) => (
216
+ {createWeekdayLabels(props.locale, weekStartsOn).map((weekday) => (
105
217
  <View key={weekday.id} width={cellSize}>
106
218
  <Text align="center" emphasis="muted" variant="caption" weight="semiBold">
107
219
  {weekday.label}
@@ -110,110 +222,288 @@ function CalendarInner({
110
222
  ))}
111
223
  </View>
112
224
  <GridView
113
- contentHeight={gridHeight}
114
- contentWidth={gridWidth}
115
- height={gridHeight}
116
- interactionPolicy={interactionPolicy}
225
+ contentHeight={cellSize * GRID_ROWS}
226
+ contentWidth={cellSize * GRID_COLUMNS}
227
+ height={cellSize * GRID_ROWS}
228
+ interactionPolicy={props.interactionPolicy}
117
229
  items={gridItems}
118
230
  overscanPixels={0}
119
231
  renderItem={(item) => {
120
232
  const cell = cellsById.get(item.id);
121
233
  if (!cell) return null;
122
234
  const selected =
123
- selectedDate !== null &&
124
- selectedDate !== undefined &&
125
- isSameLocalDay(selectedDate, cell.date);
235
+ props.selectedDate !== null &&
236
+ props.selectedDate !== undefined &&
237
+ isSameLocalDay(props.selectedDate, cell.date);
126
238
  const disabled = isCalendarDateDisabled({
127
239
  date: cell.date,
128
240
  disabledDateIds,
129
- isDateDisabled,
130
- maxDate,
131
- minDate,
241
+ isDateDisabled: props.isDateDisabled,
242
+ maxDate: props.maxDate,
243
+ minDate: props.minDate,
132
244
  });
133
- const selectCell = () => {
134
- if (!cell.inDisplayedMonth) setDisplayedMonth(cell.date);
135
- onSelectedDateChange?.(cell.date);
136
- };
245
+ const event = eventsByDate.get(cell.id)?.[0];
137
246
  return (
138
- <Button
139
- accessibilityState={{ disabled, selected }}
140
- color={selected ? 'primary' : 'neutral'}
141
- disabled={disabled}
142
- interactionPolicy={interactionPolicy}
143
- onPress={selectCell}
144
- size="s"
145
- testID={testID ? `${testID}-day-${cell.id}` : undefined}
146
- variant={selected ? 'solid' : cell.inDisplayedMonth ? 'ghost' : 'outline'}
147
- >
148
- {cell.date.getDate()}
149
- </Button>
247
+ <View gap="xxs">
248
+ <Button
249
+ accessibilityState={{ disabled, selected }}
250
+ color={selected ? 'primary' : 'neutral'}
251
+ disabled={disabled}
252
+ interactionPolicy={props.interactionPolicy}
253
+ onPress={() => onSelectDate(cell.date)}
254
+ size="s"
255
+ testID={withTestId(props.testID, `day-${cell.id}`)}
256
+ variant={selected ? 'solid' : cell.inDisplayedMonth ? 'ghost' : 'outline'}
257
+ >
258
+ {cell.date.getDate()}
259
+ </Button>
260
+ {props.density !== 'compact' && event ? (
261
+ <Button
262
+ interactionPolicy={props.interactionPolicy}
263
+ onPress={() => activateEvent(event.id)}
264
+ size="s"
265
+ testID={withTestId(props.testID, `event-${event.id}`)}
266
+ variant="outline"
267
+ >
268
+ {event.title}
269
+ </Button>
270
+ ) : null}
271
+ </View>
150
272
  );
151
273
  }}
152
- testID={testID ? `${testID}-grid` : undefined}
274
+ testID={withTestId(props.testID, 'grid')}
153
275
  viewportInteraction="locked"
154
- width={gridWidth}
276
+ width={cellSize * GRID_COLUMNS}
155
277
  />
156
- </View>
278
+ </>
157
279
  );
158
280
  }
159
281
 
160
- /*** Produces the ordered weekday labels for one local calendar week. */
282
+ /*** Renders day and week event lanes through the existing TimeGrid foundation. */
283
+ function CalendarTimeView({
284
+ activateEvent,
285
+ displayedDate,
286
+ events,
287
+ interactionPolicy,
288
+ locale,
289
+ testID,
290
+ timeZone,
291
+ view,
292
+ weekStartsOn,
293
+ }: {
294
+ readonly activateEvent: (eventId: string) => void;
295
+ readonly displayedDate: Date;
296
+ readonly events: readonly TemporalEvent[];
297
+ readonly interactionPolicy: CalendarProps['interactionPolicy'];
298
+ readonly locale: string | undefined;
299
+ readonly testID: string | undefined;
300
+ readonly timeZone: TemporalTimeZone | undefined;
301
+ readonly view: Exclude<CalendarView, 'month'>;
302
+ readonly weekStartsOn: number;
303
+ }) {
304
+ const dates = view === 'day' ? [displayedDate] : createWeek(displayedDate, weekStartsOn);
305
+ const eventById = new Map(events.map((event) => [event.id, event]));
306
+ return (
307
+ <TimeGrid
308
+ contentWidth={MINUTES_PER_DAY}
309
+ height={Math.min(56 * dates.length, 336)}
310
+ interactionPolicy={interactionPolicy}
311
+ intervals={createTimeGridIntervals(events, dates, timeZone)}
312
+ lanes={dates.map((date) => ({ height: 56, id: formatCalendarDate(date) }))}
313
+ onIntervalPress={(interval) => activateEvent(interval.id)}
314
+ overscanPixels={0}
315
+ renderInterval={(interval) => (
316
+ <Text variant="caption">{eventById.get(interval.id)?.title}</Text>
317
+ )}
318
+ renderLaneLabel={(lane) => (
319
+ <Text variant="caption">
320
+ {formatDayLabel(parseCalendarDate(lane.id) ?? displayedDate, locale)}
321
+ </Text>
322
+ )}
323
+ testID={withTestId(testID, view)}
324
+ width={320}
325
+ />
326
+ );
327
+ }
328
+
329
+ /*** Produces weekday labels for one local calendar week. */
161
330
  function createWeekdayLabels(locale: string | undefined, weekStartsOn: number) {
162
331
  const formatter = new Intl.DateTimeFormat(locale, { weekday: 'short' });
163
- const sunday = new Date(2023, 0, 1);
164
- return Array.from({ length: GRID_COLUMNS }, (_, index) => {
165
- const day = new Date(
166
- sunday.getFullYear(),
167
- sunday.getMonth(),
168
- sunday.getDate() + ((weekStartsOn + index) % GRID_COLUMNS),
169
- );
170
- return { id: String(index), label: formatter.format(day) };
171
- });
332
+ return Array.from({ length: GRID_COLUMNS }, (_, index) => ({
333
+ id: String(index),
334
+ label: formatter.format(addDays(new Date(2023, 0, 1), (weekStartsOn + index) % GRID_COLUMNS)),
335
+ }));
172
336
  }
173
337
 
174
- /*** Moves a local month without carrying a day-of-month overflow into the target month. */
175
- function addMonths(month: Date, count: number): Date {
176
- return new Date(month.getFullYear(), month.getMonth() + count, 1);
338
+ /*** Creates the visible local dates for a week view. */
339
+ function createWeek(date: Date, weekStartsOn: number): readonly Date[] {
340
+ const first = addDays(date, -((date.getDay() - weekStartsOn + GRID_COLUMNS) % GRID_COLUMNS));
341
+ return Array.from({ length: GRID_COLUMNS }, (_, index) => addDays(first, index));
177
342
  }
178
343
 
179
- /*** Normalizes a value to the first local day of its month. */
180
- function startOfLocalMonth(value: Date): Date {
181
- return new Date(value.getFullYear(), value.getMonth(), 1);
344
+ /*** Indexes events by each displayed local date they intersect in the Calendar timezone. */
345
+ function createEventsByDate(
346
+ events: readonly TemporalEvent[],
347
+ dates: readonly Date[],
348
+ timeZone: TemporalTimeZone | undefined,
349
+ ): ReadonlyMap<string, readonly TemporalEvent[]> {
350
+ return new Map(
351
+ dates.map((date) => [
352
+ formatCalendarDate(date),
353
+ events.filter((event) => eventIntersectsDate(event, formatCalendarDate(date), timeZone)),
354
+ ]),
355
+ );
182
356
  }
183
357
 
184
- /*** Formats a Date as a timezone-independent local calendar identifier. */
185
- function formatCalendarDate(value: Date): string {
186
- const year = String(value.getFullYear()).padStart(4, '0');
187
- const month = String(value.getMonth() + 1).padStart(2, '0');
188
- const day = String(value.getDate()).padStart(2, '0');
189
- return `${year}-${month}-${day}`;
358
+ /*** Maps timed events into TimeGrid lane-local minute coordinates. */
359
+ function createTimeGridIntervals(
360
+ events: readonly TemporalEvent[],
361
+ dates: readonly Date[],
362
+ timeZone: TemporalTimeZone | undefined,
363
+ ) {
364
+ return dates.flatMap((date) =>
365
+ events.flatMap((event) => {
366
+ const dateId = formatCalendarDate(date);
367
+ if (!isTemporalTimedEvent(event)) return [];
368
+ if (!eventIntersectsDate(event, dateId, timeZone)) return [];
369
+ const zone = timeZone ?? event.range.timeZone;
370
+ const start = getZonedDateTimeParts(event.range.start, zone);
371
+ const end = getZonedDateTimeParts(resolveTimedEnd(event), zone);
372
+ const startMinute = start.date === dateId ? start.hour * 60 + start.minute : 0;
373
+ const endMinute = end.date === dateId ? end.hour * 60 + end.minute : MINUTES_PER_DAY;
374
+ return [
375
+ {
376
+ extent: Math.max(1, endMinute - startMinute),
377
+ id: event.id,
378
+ laneId: dateId,
379
+ start: startMinute,
380
+ },
381
+ ];
382
+ }),
383
+ );
190
384
  }
191
385
 
192
- /*** Compares local calendar dates using their canonical date-only identifiers. */
193
- function compareLocalDates(left: Date, right: Date): number {
194
- return formatCalendarDate(left).localeCompare(formatCalendarDate(right));
386
+ /*** Reports whether an event overlaps one local Calendar date. */
387
+ function eventIntersectsDate(
388
+ event: TemporalEvent,
389
+ date: TemporalDate,
390
+ timeZone: TemporalTimeZone | undefined,
391
+ ): boolean {
392
+ if (!isTemporalTimedEvent(event))
393
+ return (
394
+ event.range.start <= date &&
395
+ date < (event.range.end ?? addDaysToDate(event.range.start, event.range.duration))
396
+ );
397
+ const zone = timeZone ?? event.range.timeZone;
398
+ return (
399
+ getZonedDateTimeParts(event.range.start, zone).date <= date &&
400
+ date <= getZonedDateTimeParts(resolveTimedEnd(event), zone).date
401
+ );
195
402
  }
196
403
 
197
- /*** Reports whether a candidate month contains at least one date inside the configured range. */
198
- function monthIntersectsRange(
199
- month: Date,
200
- minDate: Date | undefined,
201
- maxDate: Date | undefined,
202
- ): boolean {
203
- const first = startOfLocalMonth(month);
204
- const last = new Date(first.getFullYear(), first.getMonth() + 1, 0);
404
+ /*** Narrows a serializable Calendar event to the instant-based timed variant. */
405
+ function isTemporalTimedEvent(event: TemporalEvent): event is TemporalTimedEvent {
406
+ return event.range.allDay === false;
407
+ }
408
+
409
+ /*** Resolves the exclusive timed end, including the existing duration alternative. */
410
+ function resolveTimedEnd(event: TemporalTimedEvent): string {
205
411
  return (
206
- (!minDate || compareLocalDates(last, minDate) >= 0) &&
207
- (!maxDate || compareLocalDates(first, maxDate) <= 0)
412
+ event.range.end ??
413
+ new Date(
414
+ Date.parse(event.range.start) + parseDurationMinutes(event.range.duration) * 60_000,
415
+ ).toISOString()
416
+ );
417
+ }
418
+
419
+ /*** Reads a UTC instant as a local date and wall-clock time in an IANA timezone. */
420
+ function getZonedDateTimeParts(
421
+ instant: string,
422
+ timeZone: TemporalTimeZone,
423
+ ): { date: TemporalDate; hour: number; minute: number } {
424
+ const parts = new Intl.DateTimeFormat('en-CA', {
425
+ day: '2-digit',
426
+ hour: '2-digit',
427
+ hourCycle: 'h23',
428
+ minute: '2-digit',
429
+ month: '2-digit',
430
+ timeZone,
431
+ year: 'numeric',
432
+ }).formatToParts(new Date(instant));
433
+ const values = Object.fromEntries(parts.map((part) => [part.type, part.value]));
434
+ return {
435
+ date: `${values.year}-${values.month}-${values.day}` as TemporalDate,
436
+ hour: Number(values.hour),
437
+ minute: Number(values.minute),
438
+ };
439
+ }
440
+
441
+ /*** Parses the bounded hour-and-minute duration form used for Calendar placement. */
442
+ function parseDurationMinutes(duration: string | undefined): number {
443
+ const match = /^PT(?:(\d+)H)?(?:(\d+)M)?$/.exec(duration ?? 'PT60M');
444
+ return match ? Number(match[1] ?? 0) * 60 + Number(match[2] ?? 0) : 60;
445
+ }
446
+
447
+ /*** Adds a date-only duration without introducing a timezone. */
448
+ function addDaysToDate(start: TemporalDate, duration: string | undefined): TemporalDate {
449
+ return formatCalendarDate(
450
+ addDays(
451
+ parseCalendarDate(start) ?? new Date(),
452
+ Math.max(1, Math.ceil(parseDurationMinutes(duration) / MINUTES_PER_DAY)),
453
+ ),
208
454
  );
209
455
  }
210
456
 
211
- /*** Reports whether two values identify the same local calendar day. */
457
+ /*** Parses a local YYYY-MM-DD date without UTC conversion. */
458
+ function parseCalendarDate(value: string | undefined): Date | undefined {
459
+ if (!value || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return undefined;
460
+ const [year, month, day] = value.split('-').map(Number);
461
+ return year === undefined || month === undefined || day === undefined
462
+ ? undefined
463
+ : new Date(year, month - 1, day);
464
+ }
465
+
466
+ /*** Moves a local calendar day. */
467
+ function addDays(date: Date, count: number): Date {
468
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate() + count);
469
+ }
470
+ /*** Moves a local month without day overflow. */
471
+ function addMonths(month: Date, count: number): Date {
472
+ const year = month.getFullYear();
473
+ const targetMonth = month.getMonth() + count;
474
+ const lastDay = new Date(year, targetMonth + 1, 0).getDate();
475
+ return new Date(year, targetMonth, Math.min(month.getDate(), lastDay));
476
+ }
477
+ /*** Normalizes a value to the first local day of its month. */
478
+ function startOfLocalMonth(value: Date): Date {
479
+ return new Date(value.getFullYear(), value.getMonth(), 1);
480
+ }
481
+ /*** Normalizes a value to its local calendar date. */
482
+ function startOfLocalDay(value: Date): Date {
483
+ return new Date(value.getFullYear(), value.getMonth(), value.getDate());
484
+ }
485
+ /*** Formats a Date as a timezone-independent local calendar identifier. */
486
+ function formatCalendarDate(value: Date): TemporalDate {
487
+ return `${String(value.getFullYear()).padStart(4, '0')}-${String(value.getMonth() + 1).padStart(2, '0')}-${String(value.getDate()).padStart(2, '0')}` as TemporalDate;
488
+ }
489
+ /*** Compares local calendar dates using their canonical date-only identifiers. */
212
490
  function isSameLocalDay(left: Date, right: Date): boolean {
213
491
  return formatCalendarDate(left) === formatCalendarDate(right);
214
492
  }
215
-
216
- /*** Formats a display month with the caller's locale. */
217
- function formatMonthLabel(value: Date, locale: string | undefined): string {
218
- return new Intl.DateTimeFormat(locale, { month: 'long', year: 'numeric' }).format(value);
493
+ /*** Formats the controlled view title. */
494
+ function formatViewLabel(value: Date, locale: string | undefined, view: CalendarView): string {
495
+ return new Intl.DateTimeFormat(
496
+ locale,
497
+ view === 'month'
498
+ ? { month: 'long', year: 'numeric' }
499
+ : { day: 'numeric', month: 'short', year: 'numeric' },
500
+ ).format(value);
501
+ }
502
+ /*** Formats a compact lane label for day and week views. */
503
+ function formatDayLabel(value: Date, locale: string | undefined): string {
504
+ return new Intl.DateTimeFormat(locale, { day: 'numeric', weekday: 'short' }).format(value);
505
+ }
506
+ /*** Resolves a test identifier suffix only when a test root is provided. */
507
+ function withTestId(testID: string | undefined, suffix: string): string | undefined {
508
+ return testID === undefined ? undefined : `${testID}-${suffix}`;
219
509
  }