@super-calendar/dom 2.16.0 → 2.17.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.
package/src/MonthView.tsx CHANGED
@@ -12,6 +12,7 @@ import {
12
12
  type CSSProperties,
13
13
  type KeyboardEvent as ReactKeyboardEvent,
14
14
  type ReactElement,
15
+ type PointerEvent as ReactPointerEvent,
15
16
  useEffect,
16
17
  useMemo,
17
18
  useRef,
@@ -20,6 +21,7 @@ import {
20
21
  import {
21
22
  bandRounding,
22
23
  buildMonthGrid,
24
+ buildMultiWeekGrid,
23
25
  type CalendarEvent,
24
26
  compareDayEvents,
25
27
  dayBadgeKind,
@@ -32,8 +34,8 @@ import {
32
34
  layoutMonthWeek,
33
35
  monthCreateRange,
34
36
  monthDropBounds,
35
- type MonthGridDay,
36
- type MonthGridWeek,
37
+ type MonthGridWeekday,
38
+ type MultiWeekGridDay,
37
39
  overlapsOtherEvents,
38
40
  rangeBandKind,
39
41
  type WeekdayFormat,
@@ -44,6 +46,7 @@ import {
44
46
  dayAccessibilityLabel,
45
47
  resolveCalendarLabels,
46
48
  } from "@super-calendar/core";
49
+ import { holdDateDrag } from "./touch";
47
50
  import { createSlots, dataState, type SlotDefault, type SlotStyleProps } from "./slots";
48
51
  import { type DomCalendarTheme, mergeDomTheme } from "./theme";
49
52
 
@@ -77,12 +80,22 @@ const CELL_PAD = 4;
77
80
  // Opacity of an event chip while it is being dragged to another day.
78
81
  const DRAGGED_CHIP_OPACITY = 0.4;
79
82
 
83
+ // A day as the shared rows render it. Month days carry `isCurrentMonth`;
84
+ // multi-week days omit it because every one of them is inside the period.
85
+ type RowDay = MultiWeekGridDay & { isCurrentMonth?: boolean };
86
+ type RowWeek = { id: string; days: RowDay[] };
87
+
88
+ // A leading or trailing day of a neighbouring month in the month grid.
89
+ const isOutsidePeriod = (day: RowDay) => day.isCurrentMonth === false;
90
+
80
91
  /** Props passed to a custom month event chip renderer. */
81
92
  export interface DomMonthEventArgs<T = unknown> {
82
93
  /** The event to render. */
83
94
  event: CalendarEvent<T>;
84
95
  /** Call to fire the view's `onPressEvent` for this chip. */
85
96
  onPress: () => void;
97
+ /** The current row view. Supplied by the calendar; optional for existing standalone chip callers. */
98
+ mode?: "month" | "multiweek";
86
99
  }
87
100
 
88
101
  /** A component that renders a single month-grid event chip. */
@@ -91,9 +104,13 @@ export type DomMonthEvent<T = unknown> = ComponentType<DomMonthEventArgs<T>>;
91
104
  // An in-progress month drag: either a day span being swept out for a new event,
92
105
  // or an existing event being carried to another day. Days are held as start-of-day
93
106
  // timestamps, so the reducer-ish updates compare cheaply.
94
- type MonthDrag<T> =
107
+ type MonthDrag<T> = (
95
108
  | { kind: "create"; anchor: number; hover: number }
96
- | { kind: "move"; event: CalendarEvent<T>; from: number; to: number };
109
+ | { kind: "move"; event: CalendarEvent<T>; from: number; to: number }
110
+ ) & {
111
+ // The rows the drag began in, so a drag never commits into a different window.
112
+ windowKey: string;
113
+ };
97
114
 
98
115
  /** Props for {@link MonthView}. */
99
116
  export interface MonthViewProps<T = unknown>
@@ -214,7 +231,7 @@ export interface MonthViewProps<T = unknown>
214
231
  // always applied) and `themed` (colour/type/spacing, dropped when a class is set).
215
232
 
216
233
  function dayCellDefault(
217
- day: MonthGridDay,
234
+ day: RowDay,
218
235
  theme: DomCalendarTheme,
219
236
  highlightWeekends: boolean,
220
237
  ): SlotDefault {
@@ -254,7 +271,7 @@ function dayCellDefault(
254
271
  * event chips (and matches the native renderer's month grid).
255
272
  */
256
273
  function rangeBandDefault(
257
- day: MonthGridDay,
274
+ day: RowDay,
258
275
  theme: DomCalendarTheme,
259
276
  fillCell: boolean,
260
277
  eventsMode: boolean,
@@ -294,7 +311,7 @@ function rangeBandDefault(
294
311
  return { base, themed: { background: theme.rangeBackground } };
295
312
  }
296
313
 
297
- function badgeDefault(day: MonthGridDay, theme: DomCalendarTheme, hovered: boolean): SlotDefault {
314
+ function badgeDefault(day: RowDay, theme: DomCalendarTheme, hovered: boolean): SlotDefault {
298
315
  const badge = dayBadgeKind(day, day.isToday);
299
316
  const filled = badge !== "none";
300
317
  const background = filled
@@ -325,7 +342,7 @@ function badgeDefault(day: MonthGridDay, theme: DomCalendarTheme, hovered: boole
325
342
  // --- Calendar (events) layout styles -------------------------------------
326
343
 
327
344
  function eventCellDefault(
328
- day: MonthGridDay,
345
+ day: RowDay,
329
346
  theme: DomCalendarTheme,
330
347
  height: number,
331
348
  highlightWeekends: boolean,
@@ -361,7 +378,7 @@ function eventCellDefault(
361
378
  };
362
379
  }
363
380
 
364
- function compactBadgeDefault(day: MonthGridDay, theme: DomCalendarTheme): SlotDefault {
381
+ function compactBadgeDefault(day: RowDay, theme: DomCalendarTheme): SlotDefault {
365
382
  const badge = dayBadgeKind(day, day.isToday);
366
383
  const filled = badge !== "none";
367
384
  return {
@@ -423,7 +440,11 @@ function chipDefault(theme: DomCalendarTheme, chipHeight: number): SlotDefault {
423
440
  };
424
441
  }
425
442
 
426
- function moreButtonDefault(theme: DomCalendarTheme, chipHeight: number): SlotDefault {
443
+ function moreButtonDefault(
444
+ theme: DomCalendarTheme,
445
+ chipHeight: number,
446
+ multiWeek = false,
447
+ ): SlotDefault {
427
448
  return {
428
449
  base: {
429
450
  position: "relative",
@@ -435,6 +456,7 @@ function moreButtonDefault(theme: DomCalendarTheme, chipHeight: number): SlotDef
435
456
  height: chipHeight,
436
457
  lineHeight: `${chipHeight}px`,
437
458
  fontFamily: "inherit",
459
+ ...(multiWeek ? { whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" } : {}),
438
460
  },
439
461
  themed: {
440
462
  padding: "0 6px",
@@ -456,7 +478,7 @@ interface MonthViewInternalProps<T = unknown> extends MonthViewProps<T> {
456
478
  // A multi-day event lives in each covered day's bucket, so dedupe by reference;
457
479
  // this scans only the row's days, not every event in the month.
458
480
  function collectRowEvents<T>(
459
- week: MonthGridWeek,
481
+ week: RowWeek,
460
482
  eventsByDay: ReadonlyMap<string, CalendarEvent<T>[]>,
461
483
  ): CalendarEvent<T>[] {
462
484
  const seen = new Set<CalendarEvent<T>>();
@@ -474,6 +496,38 @@ function collectRowEvents<T>(
474
496
  return out;
475
497
  }
476
498
 
499
+ /**
500
+ * Which rows a {@link DateRows} grid shows. Internal to the package: the public
501
+ * views adapt their own props to it.
502
+ */
503
+ export type DateRowsSource =
504
+ | {
505
+ kind: "month";
506
+ /** Any day within the month to render. */
507
+ date: Date;
508
+ showAdjacentMonths: boolean;
509
+ }
510
+ | {
511
+ kind: "multiweek";
512
+ /** Any day within the first week to render. */
513
+ date: Date;
514
+ numberOfWeeks: number;
515
+ };
516
+
517
+ // The localized first and last displayed days, e.g. "Nov 1, 2026 – Nov 14, 2026".
518
+ function rangeTitle(start: Date, endExclusive: Date, locale?: Locale): string {
519
+ const options = locale ? { locale } : undefined;
520
+ return `${format(start, "PP", options)} – ${format(addDays(endExclusive, -1), "PP", options)}`;
521
+ }
522
+
523
+ /** Props for the private {@link DateRows} grid shared by the public row views. */
524
+ export interface DateRowsProps<T = unknown> extends Omit<
525
+ MonthViewInternalProps<T>,
526
+ "date" | "showAdjacentMonths"
527
+ > {
528
+ source: DateRowsSource;
529
+ }
530
+
477
531
  /**
478
532
  * A single static month grid, rendered with plain DOM elements.
479
533
  *
@@ -484,6 +538,18 @@ function collectRowEvents<T>(
484
538
  */
485
539
  export function MonthView<T = unknown>({
486
540
  date,
541
+ showAdjacentMonths = true,
542
+ ...rest
543
+ }: MonthViewInternalProps<T>): ReactElement {
544
+ return <DateRows<T> {...rest} source={{ kind: "month", date, showAdjacentMonths }} />;
545
+ }
546
+
547
+ /**
548
+ * The day-row grid behind the public row views: week rows of day cells with
549
+ * spanning event bars, overflow, selection, drag, and keyboard handling.
550
+ */
551
+ export function DateRows<T = unknown>({
552
+ source,
487
553
  weekStartsOn = 0,
488
554
  hiddenDays,
489
555
  weekdayFormat = "short",
@@ -500,7 +566,6 @@ export function MonthView<T = unknown>({
500
566
  onPressMore,
501
567
  selectedRange,
502
568
  selectedDates,
503
- showAdjacentMonths = true,
504
569
  highlightWeekends = true,
505
570
  fillCellOnSelection = false,
506
571
  showTitle = true,
@@ -523,14 +588,22 @@ export function MonthView<T = unknown>({
523
588
  style,
524
589
  classNames,
525
590
  styles,
526
- }: MonthViewInternalProps<T>): ReactElement {
591
+ }: DateRowsProps<T>): ReactElement {
592
+ const { date } = source;
593
+ const multiWeek = source.kind === "multiweek";
594
+ // Multi-week rows only contain days of the requested period, so nothing is blanked.
595
+ const showAdjacentMonths = source.kind === "month" ? source.showAdjacentMonths : true;
596
+ const numberOfWeeks = source.kind === "multiweek" ? source.numberOfWeeks : undefined;
597
+ // The mode reported to custom renderers and accessibility labels.
598
+ const viewMode: "month" | "multiweek" = multiWeek ? "multiweek" : "month";
527
599
  const theme = useMemo(() => mergeDomTheme(themeOverrides), [themeOverrides]);
528
600
  const labels = useMemo(() => resolveCalendarLabels(labelsProp), [labelsProp]);
529
601
  const slot = createSlots<MonthViewSlot>({ classNames, styles });
530
602
 
531
603
  // Calendar layout (date in the corner + event chips) is on whenever `events`
532
- // is provided; otherwise the compact picker badge layout is used.
533
- const eventsMode = events !== undefined;
604
+ // is provided; otherwise the compact picker badge layout is used. Multi-week
605
+ // rows always use the calendar layout.
606
+ const eventsMode = events !== undefined || multiWeek;
534
607
  // The day cells form a roving tab stop (arrow keys + Enter) in the picker
535
608
  // always, and in events mode only when the consumer opts in. Otherwise events
536
609
  // mode tabs through the event chips alone.
@@ -586,23 +659,19 @@ export function MonthView<T = unknown>({
586
659
  }, [moreOpenFor]);
587
660
  // Reserve `maxVisibleEventCount` rows below the date so every cell is uniform;
588
661
  // when a day overflows, the last row becomes the "+N more" affordance.
589
- const eventRowHeight = CELL_PAD * 2 + DATE_ROW + maxVisibleEventCount * (chipHeight + chipGap);
590
- const { weeks, weekdays } = useMemo(
591
- () =>
592
- buildMonthGrid(date, {
593
- weekStartsOn,
594
- hiddenDays,
595
- weekdayFormat,
596
- selectedRange,
597
- selectedDates,
598
- minDate,
599
- maxDate,
600
- isDateDisabled,
601
- locale,
602
- }),
603
- [
604
- date,
662
+ const eventRowHeight =
663
+ CELL_PAD * 2 +
664
+ DATE_ROW +
665
+ (multiWeek ? Math.max(1, maxVisibleEventCount) : maxVisibleEventCount) * (chipHeight + chipGap);
666
+ const { weeks, weekdays, title, period } = useMemo((): {
667
+ weeks: RowWeek[];
668
+ weekdays: MonthGridWeekday[];
669
+ title: string;
670
+ period: { start: Date; endExclusive: Date } | null;
671
+ } => {
672
+ const options = {
605
673
  weekStartsOn,
674
+ hiddenDays,
606
675
  weekdayFormat,
607
676
  selectedRange,
608
677
  selectedDates,
@@ -610,8 +679,34 @@ export function MonthView<T = unknown>({
610
679
  maxDate,
611
680
  isDateDisabled,
612
681
  locale,
613
- ],
614
- );
682
+ };
683
+ if (numberOfWeeks === undefined) {
684
+ return {
685
+ ...buildMonthGrid(date, options),
686
+ title: format(date, "MMMM yyyy", locale ? { locale } : undefined),
687
+ period: null,
688
+ };
689
+ }
690
+ const grid = buildMultiWeekGrid(date, { ...options, numberOfWeeks });
691
+ return {
692
+ weeks: grid.weeks,
693
+ weekdays: grid.weekdays,
694
+ title: rangeTitle(grid.visibleRange.start, grid.visibleRange.endExclusive, locale),
695
+ period: grid.period,
696
+ };
697
+ }, [
698
+ date,
699
+ numberOfWeeks,
700
+ weekStartsOn,
701
+ hiddenDays,
702
+ weekdayFormat,
703
+ selectedRange,
704
+ selectedDates,
705
+ minDate,
706
+ maxDate,
707
+ isDateDisabled,
708
+ locale,
709
+ ]);
615
710
 
616
711
  // Lay out each week row's spanning bars once per data change, not per render, so
617
712
  // hover / popover / drag-create state changes don't relay out every row.
@@ -628,32 +723,111 @@ export function MonthView<T = unknown>({
628
723
  [eventsMode, weeks, eventsByDay],
629
724
  );
630
725
 
726
+ // Multi-week rows rove over the rendered, enabled days only (oldest first), so
727
+ // focus skips hidden and disabled days and never leaves the displayed weeks.
728
+ const focusableDays = useMemo(
729
+ () =>
730
+ period
731
+ ? weeks
732
+ .flatMap((week) => week.days)
733
+ .filter((day) => !day.isDisabled)
734
+ .map((day) => startOfDay(day.date))
735
+ .sort((a, b) => a.getTime() - b.getTime())
736
+ : null,
737
+ [period, weeks],
738
+ );
739
+ const focusableKeys = useMemo(
740
+ () => (focusableDays ? new Set(focusableDays.map((d) => format(d, "yyyy-MM-dd"))) : null),
741
+ [focusableDays],
742
+ );
743
+
631
744
  // Roving tabindex: only one day is in the tab order; arrow keys move focus
632
745
  // within the month, so the grid is a single, sensible tab stop.
633
- const initialFocus = useMemo(() => {
746
+ const initialFocus = useMemo((): Date | null => {
747
+ if (focusableDays && focusableKeys) {
748
+ // The selected day, then today, then the first enabled day; none when every
749
+ // displayed day is disabled.
750
+ const rendered = (d?: Date | null): d is Date =>
751
+ !!d && focusableKeys.has(format(d, "yyyy-MM-dd"));
752
+ if (rendered(selectedRange?.start)) return startOfDay(selectedRange.start);
753
+ const picked = selectedDates?.find(rendered);
754
+ if (picked) return startOfDay(picked);
755
+ const today = new Date();
756
+ if (rendered(today)) return startOfDay(today);
757
+ return focusableDays[0] ?? null;
758
+ }
634
759
  const inMonth = (d?: Date | null) => !!d && isSameMonth(d, date);
635
760
  if (inMonth(selectedRange?.start)) return startOfDay(selectedRange!.start);
636
761
  const picked = selectedDates?.find(inMonth);
637
762
  if (picked) return startOfDay(picked);
638
763
  const today = new Date();
639
764
  return isSameMonth(today, date) ? startOfDay(today) : startOfMonth(date);
640
- }, [date, selectedRange, selectedDates]);
765
+ }, [focusableDays, focusableKeys, date, selectedRange, selectedDates]);
641
766
 
642
767
  const [focusedDate, setFocusedDate] = useState(initialFocus);
643
768
  const initialFocusRef = useRef(initialFocus);
644
769
  initialFocusRef.current = initialFocus;
645
- const monthKey = format(date, "yyyy-MM");
646
- const focusedMonth = useRef(monthKey);
770
+ // The displayed window: the month, or the first week and count of multi-week rows.
771
+ const windowKey = period
772
+ ? `${format(period.start, "yyyy-MM-dd")}+${numberOfWeeks}:${weekdays.map((day) => day.date.getDay()).join(",")}`
773
+ : format(date, "yyyy-MM");
774
+ const windowKeyRef = useRef(windowKey);
775
+ windowKeyRef.current = windowKey;
776
+ const focusedWindow = useRef(windowKey);
777
+ const gridRef = useRef<HTMLDivElement>(null);
778
+ const restoreDayFocus = useRef(false);
779
+ if (multiWeek && windowKey !== focusedWindow.current) {
780
+ const active = gridRef.current?.ownerDocument.activeElement;
781
+ // React replaces keyed week rows when paging. Remember whether the removed
782
+ // day owned focus, so repeated PageUp/PageDown stays inside the calendar.
783
+ restoreDayFocus.current =
784
+ !!active?.hasAttribute("data-day") && !!gridRef.current?.contains(active);
785
+ }
647
786
  useEffect(() => {
648
787
  // A parent can focus a day before the initial passive effect runs.
649
- if (monthKey === focusedMonth.current) return;
650
- focusedMonth.current = monthKey;
788
+ if (windowKey === focusedWindow.current) return;
789
+ focusedWindow.current = windowKey;
651
790
  setFocusedDate(initialFocusRef.current);
652
- }, [monthKey]);
791
+ if (restoreDayFocus.current) {
792
+ const active = gridRef.current?.ownerDocument.activeElement;
793
+ if (
794
+ active === gridRef.current?.ownerDocument.body ||
795
+ (active && gridRef.current?.contains(active))
796
+ ) {
797
+ const next = initialFocusRef.current
798
+ ? gridRef.current?.querySelector<HTMLElement>(
799
+ `[data-day="${format(initialFocusRef.current, "yyyy-MM-dd")}"]`,
800
+ )
801
+ : gridRef.current;
802
+ next?.focus();
803
+ }
804
+ }
805
+ restoreDayFocus.current = false;
806
+ }, [windowKey]);
653
807
 
654
808
  const [hoveredKey, setHoveredKey] = useState<string | null>(null);
655
- const gridRef = useRef<HTMLDivElement>(null);
656
- const focusKey = format(focusedDate, "yyyy-MM-dd");
809
+ const storedFocusKey = focusedDate ? format(focusedDate, "yyyy-MM-dd") : null;
810
+ // A multi-week focus target that hidden days or new constraints removed falls
811
+ // back to the initial target, without moving DOM focus.
812
+ const focusKey =
813
+ focusableKeys && (storedFocusKey === null || !focusableKeys.has(storedFocusKey))
814
+ ? initialFocus && format(initialFocus, "yyyy-MM-dd")
815
+ : storedFocusKey;
816
+
817
+ useEffect(() => {
818
+ if (!multiWeek) return;
819
+ const active = gridRef.current?.ownerDocument.activeElement;
820
+ if (!active?.hasAttribute("data-day") || !gridRef.current?.contains(active)) return;
821
+ if (
822
+ active.getAttribute("data-day") === focusKey &&
823
+ active.getAttribute("aria-disabled") !== "true"
824
+ )
825
+ return;
826
+ const next = focusKey
827
+ ? gridRef.current.querySelector<HTMLElement>(`[data-day="${focusKey}"]`)
828
+ : gridRef.current;
829
+ next?.focus();
830
+ }, [multiWeek, focusKey]);
657
831
 
658
832
  // Dragging (events mode): press a day and drag across others to sketch an
659
833
  // all-day span, or press an event chip and drop it on another day. Both are
@@ -689,12 +863,12 @@ export function MonthView<T = unknown>({
689
863
  const beginCreate = (day: Date) => {
690
864
  if (!onCreateEvent && !onSelectDrag) return;
691
865
  const t = startOfDay(day).getTime();
692
- setDrag({ kind: "create", anchor: t, hover: t });
866
+ setDrag({ kind: "create", anchor: t, hover: t, windowKey });
693
867
  };
694
868
  const beginMove = (event: CalendarEvent<T>, day: Date) => {
695
869
  if (!canMove(event)) return;
696
870
  const t = startOfDay(day).getTime();
697
- setDrag({ kind: "move", event, from: t, to: t });
871
+ setDrag({ kind: "move", event, from: t, to: t, windowKey });
698
872
  onDragStart?.(event);
699
873
  };
700
874
  const extendDrag = (day: Date) => {
@@ -711,6 +885,87 @@ export function MonthView<T = unknown>({
711
885
  onSelectDrag?.(new Date(lo), new Date(hi));
712
886
  }
713
887
  };
888
+ const heldTouch = useRef<(() => void) | null>(null);
889
+ const heldOrigin = useRef<{ day: Date; event?: CalendarEvent<T> } | null>(null);
890
+ const dayLookup = useMemo(
891
+ () => new Map(weeks.flatMap((week) => week.days).map((day) => [day.id, day])),
892
+ [weeks],
893
+ );
894
+ const touchHandlers = useRef({ beginCreate, beginMove, extendDrag, dayLookup });
895
+ touchHandlers.current = { beginCreate, beginMove, extendDrag, dayLookup };
896
+ const pressRow = (
897
+ event: ReactPointerEvent<HTMLElement>,
898
+ day: Date,
899
+ moving?: CalendarEvent<T>,
900
+ ) => {
901
+ if (event.button !== 0 || event.isPrimary === false || heldTouch.current) return;
902
+ beginPress();
903
+ const start = () => {
904
+ if (moving) touchHandlers.current.beginMove(moving, day);
905
+ else touchHandlers.current.beginCreate(day);
906
+ };
907
+ if (!multiWeek || !event.pointerType || event.pointerType === "mouse") {
908
+ start();
909
+ return;
910
+ }
911
+ heldOrigin.current = { day, event: moving };
912
+ heldTouch.current = holdDateDrag(
913
+ {
914
+ box: event.currentTarget,
915
+ pointerId: event.pointerId,
916
+ pointerType: event.pointerType,
917
+ clientX: event.clientX,
918
+ clientY: event.clientY,
919
+ },
920
+ start,
921
+ (x, y) => {
922
+ const element = gridRef.current?.ownerDocument
923
+ .elementFromPoint(x, y)
924
+ ?.closest<HTMLElement>("[data-day]");
925
+ if (!element || !gridRef.current?.contains(element)) return;
926
+ const day = touchHandlers.current.dayLookup.get(element.dataset.day ?? "");
927
+ if (day && !day.isDisabled) touchHandlers.current.extendDrag(day.date);
928
+ },
929
+ () => {
930
+ dragRef.current = null;
931
+ setDrag(null);
932
+ movedRef.current = false;
933
+ },
934
+ () => {
935
+ heldTouch.current = null;
936
+ heldOrigin.current = null;
937
+ },
938
+ );
939
+ };
940
+ useEffect(
941
+ () => () => {
942
+ heldTouch.current?.();
943
+ },
944
+ [windowKey],
945
+ );
946
+ // Parent selection updates can replace callbacks and equivalent props during
947
+ // a hold. Cancel only when its origin or current destination becomes invalid.
948
+ useEffect(() => {
949
+ const origin = heldOrigin.current;
950
+ if (!origin) return;
951
+ const current = dragRef.current;
952
+ const destination = current
953
+ ? new Date(current.kind === "create" ? current.hover : current.to)
954
+ : origin.day;
955
+ const enabled = (day: Date) => {
956
+ const cell = dayLookup.get(format(day, "yyyy-MM-dd"));
957
+ return cell != null && !cell.isDisabled;
958
+ };
959
+ if (
960
+ !enabled(origin.day) ||
961
+ !enabled(destination) ||
962
+ (origin.event
963
+ ? !events?.includes(origin.event) || !canMove(origin.event)
964
+ : !onCreateEvent && !onSelectDrag)
965
+ ) {
966
+ heldTouch.current?.();
967
+ }
968
+ });
714
969
  const isDragging = drag !== null;
715
970
  // Whether any month drag is wired up at all, so the cells only take over touch
716
971
  // scrolling and pointer tracking when one of the handlers can actually fire.
@@ -721,6 +976,8 @@ export function MonthView<T = unknown>({
721
976
  const current = dragRef.current;
722
977
  setDrag(null);
723
978
  if (!current || !movedRef.current) return;
979
+ // Multi-week rows drop a drag whose window was replaced before release.
980
+ if (multiWeek && current.windowKey !== windowKeyRef.current) return;
724
981
  suppressClickRef.current = true;
725
982
  if (current.kind === "create") {
726
983
  const range = monthCreateRange(new Date(current.anchor), new Date(current.hover));
@@ -741,8 +998,51 @@ export function MonthView<T = unknown>({
741
998
  };
742
999
  window.addEventListener("pointerup", finish);
743
1000
  return () => window.removeEventListener("pointerup", finish);
744
- }, [isDragging, onCreateEvent, onDragEvent, eventOverlap, events]);
1001
+ }, [isDragging, multiWeek, onCreateEvent, onDragEvent, eventOverlap, events]);
1002
+ // Replacing the multi-week window cancels a drag and closes the overflow
1003
+ // popover, so neither acts on days that are no longer shown.
1004
+ const transientWindow = useRef(windowKey);
1005
+ useEffect(() => {
1006
+ if (transientWindow.current === windowKey) return;
1007
+ transientWindow.current = windowKey;
1008
+ if (!multiWeek) return;
1009
+ setDrag(null);
1010
+ setMoreOpenFor(null);
1011
+ }, [multiWeek, windowKey]);
1012
+ // Multi-week roving: the neighbouring focusable day, the same weekday in the
1013
+ // nearest week that has it enabled, or the first/last focusable day.
1014
+ const nextMultiWeekFocus = (key: string): Date | null | undefined => {
1015
+ if (!focusableDays || !focusableKeys || !period) return undefined;
1016
+ const index = focusableDays.findIndex((d) => format(d, "yyyy-MM-dd") === focusKey);
1017
+ if (index === -1) return null;
1018
+ if (key === "ArrowLeft") return focusableDays[index - 1] ?? null;
1019
+ if (key === "ArrowRight") return focusableDays[index + 1] ?? null;
1020
+ if (key === "Home") return focusableDays[0];
1021
+ if (key === "End") return focusableDays[focusableDays.length - 1];
1022
+ const step = key === "ArrowUp" ? -7 : 7;
1023
+ for (
1024
+ let cursor = addDays(focusableDays[index], step);
1025
+ cursor >= period.start && cursor < period.endExclusive;
1026
+ cursor = addDays(cursor, step)
1027
+ ) {
1028
+ if (focusableKeys.has(format(cursor, "yyyy-MM-dd"))) return cursor;
1029
+ }
1030
+ return null;
1031
+ };
745
1032
  const onKeyDown = (e: ReactKeyboardEvent<HTMLDivElement>) => {
1033
+ if (multiWeek) {
1034
+ if (!["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", "Home", "End"].includes(e.key)) {
1035
+ return;
1036
+ }
1037
+ e.preventDefault();
1038
+ const target = nextMultiWeekFocus(e.key);
1039
+ if (!target) return;
1040
+ setFocusedDate(target);
1041
+ const targetKey = format(target, "yyyy-MM-dd");
1042
+ gridRef.current?.querySelector<HTMLElement>(`[data-day="${targetKey}"]`)?.focus();
1043
+ return;
1044
+ }
1045
+ if (!focusedDate) return;
746
1046
  let next: Date | null = null;
747
1047
  if (e.key === "ArrowLeft") next = addDays(focusedDate, -1);
748
1048
  else if (e.key === "ArrowRight") next = addDays(focusedDate, 1);
@@ -761,7 +1061,14 @@ export function MonthView<T = unknown>({
761
1061
  return (
762
1062
  <div
763
1063
  className={className}
764
- style={{ fontFamily: theme.fontFamily, color: theme.text, ...style }}
1064
+ style={{
1065
+ fontFamily: theme.fontFamily,
1066
+ color: theme.text,
1067
+ // Multi-week rows keep their full height; when the view is given less
1068
+ // room, the grid scrolls under the title and weekday header.
1069
+ ...(multiWeek ? { display: "flex", flexDirection: "column", minHeight: 0 } : null),
1070
+ ...style,
1071
+ }}
765
1072
  >
766
1073
  {showTitle ? (
767
1074
  <div
@@ -769,13 +1076,16 @@ export function MonthView<T = unknown>({
769
1076
  themed: { fontSize: 17, fontWeight: 700, padding: "10px 14px 6px" },
770
1077
  })}
771
1078
  >
772
- {format(date, "MMMM yyyy", locale ? { locale } : undefined)}
1079
+ {title}
773
1080
  </div>
774
1081
  ) : null}
775
1082
  {showWeekdays ? (
776
1083
  <div
777
1084
  {...slot("weekdays", {
778
- base: { display: "grid", gridTemplateColumns: "repeat(7, minmax(0, 1fr))" },
1085
+ base: {
1086
+ display: "grid",
1087
+ gridTemplateColumns: `repeat(${weekdays.length}, minmax(0, 1fr))`,
1088
+ },
779
1089
  themed: { borderBottom: `1px solid ${theme.gridLine}`, padding: "6px 0" },
780
1090
  })}
781
1091
  >
@@ -799,11 +1109,15 @@ export function MonthView<T = unknown>({
799
1109
  <div
800
1110
  ref={gridRef}
801
1111
  role="grid"
802
- aria-label={format(date, "MMMM yyyy", locale ? { locale } : undefined)}
1112
+ tabIndex={multiWeek ? -1 : undefined}
1113
+ aria-label={title}
803
1114
  // Arrow-key roving applies whenever the day cells are a tab stop: the
804
1115
  // picker always, events mode only when keyboardDayNavigation is set.
805
1116
  onKeyDown={dayRoving ? onKeyDown : undefined}
806
- {...slot("grid")}
1117
+ {...slot(
1118
+ "grid",
1119
+ multiWeek ? { base: { flex: "1 1 auto", minHeight: 0, overflowY: "auto" } } : undefined,
1120
+ )}
807
1121
  >
808
1122
  {weeks.map((week, weekIndex) => {
809
1123
  // Spanning bars for this row (laid out once in `weekLayouts`). A multi-day
@@ -824,8 +1138,8 @@ export function MonthView<T = unknown>({
824
1138
  let firstVisCol = 0;
825
1139
  let lastVisCol = cols - 1;
826
1140
  if (eventsMode && !showAdjacentMonths) {
827
- const first = week.days.findIndex((d) => d.isCurrentMonth);
828
- const lastFromEnd = [...week.days].reverse().findIndex((d) => d.isCurrentMonth);
1141
+ const first = week.days.findIndex((d) => !isOutsidePeriod(d));
1142
+ const lastFromEnd = [...week.days].reverse().findIndex((d) => !isOutsidePeriod(d));
829
1143
  if (first !== -1) {
830
1144
  firstVisCol = first;
831
1145
  lastVisCol = cols - 1 - lastFromEnd;
@@ -852,11 +1166,14 @@ export function MonthView<T = unknown>({
852
1166
  key={week.id}
853
1167
  role="row"
854
1168
  {...slot("week", {
855
- base: { display: "grid", gridTemplateColumns: "repeat(7, minmax(0, 1fr))" },
1169
+ base: {
1170
+ display: "grid",
1171
+ gridTemplateColumns: `repeat(${week.days.length}, minmax(0, 1fr))`,
1172
+ },
856
1173
  })}
857
1174
  >
858
1175
  {week.days.map((day, dayCol) => {
859
- const hidden = !showAdjacentMonths && !day.isCurrentMonth;
1176
+ const hidden = !showAdjacentMonths && isOutsidePeriod(day);
860
1177
  const cellHeight = eventsMode ? eventRowHeight : theme.cellHeight;
861
1178
  if (hidden) {
862
1179
  return (
@@ -885,7 +1202,7 @@ export function MonthView<T = unknown>({
885
1202
  "data-selected": day.isSelected,
886
1203
  "data-range": day.isInRange,
887
1204
  "data-weekend": day.isWeekend,
888
- "data-outside": !day.isCurrentMonth,
1205
+ "data-outside": isOutsidePeriod(day),
889
1206
  "data-disabled": day.isDisabled,
890
1207
  "data-creating": inCreate,
891
1208
  "data-drop": isDropTarget,
@@ -943,15 +1260,17 @@ export function MonthView<T = unknown>({
943
1260
  // numbers and chip titles it passes over.
944
1261
  style={
945
1262
  dragWired
946
- ? { ...dayCellProps.style, touchAction: "none", userSelect: "none" }
1263
+ ? {
1264
+ ...dayCellProps.style,
1265
+ touchAction: multiWeek ? "pan-y" : "none",
1266
+ userSelect: "none",
1267
+ }
947
1268
  : dayCellProps.style
948
1269
  }
949
1270
  onPointerDown={
950
1271
  dragWired && !day.isDisabled
951
1272
  ? (e) => {
952
- if (e.button !== 0) return;
953
- beginPress();
954
- beginCreate(day.date);
1273
+ pressRow(e, day.date);
955
1274
  }
956
1275
  : undefined
957
1276
  }
@@ -1007,8 +1326,7 @@ export function MonthView<T = unknown>({
1007
1326
  ? (e) => {
1008
1327
  if (e.button !== 0) return;
1009
1328
  e.stopPropagation();
1010
- beginPress();
1011
- beginMove(b.seg.event, day.date);
1329
+ pressRow(e, day.date, b.seg.event);
1012
1330
  }
1013
1331
  : undefined
1014
1332
  }
@@ -1022,7 +1340,7 @@ export function MonthView<T = unknown>({
1022
1340
  aria-label={
1023
1341
  eventAccessibilityLabel
1024
1342
  ? eventAccessibilityLabel(b.seg.event, {
1025
- mode: "month",
1343
+ mode: viewMode,
1026
1344
  isAllDay: isAllDayEvent(b.seg.event),
1027
1345
  ampm: false,
1028
1346
  })
@@ -1044,12 +1362,13 @@ export function MonthView<T = unknown>({
1044
1362
  // target reads as where it is going.
1045
1363
  opacity: isDragged ? DRAGGED_CHIP_OPACITY : undefined,
1046
1364
  cursor: canMove(b.seg.event) ? "grab" : undefined,
1047
- touchAction: dragWired ? "none" : undefined,
1365
+ touchAction: dragWired ? (multiWeek ? "pan-y" : "none") : undefined,
1048
1366
  zIndex: 2,
1049
1367
  }}
1050
1368
  >
1051
1369
  {Chip ? (
1052
1370
  <Chip
1371
+ mode={viewMode}
1053
1372
  event={b.seg.event}
1054
1373
  onPress={() => onPressEvent?.(b.seg.event)}
1055
1374
  />
@@ -1097,7 +1416,7 @@ export function MonthView<T = unknown>({
1097
1416
  setMoreOpenFor((open) => (open === day.id ? null : day.id));
1098
1417
  }
1099
1418
  }}
1100
- {...slot("more", moreButtonDefault(theme, chipHeight))}
1419
+ {...slot("more", moreButtonDefault(theme, chipHeight, multiWeek))}
1101
1420
  aria-label={`${labels.moreEvents(hiddenEvents.length)}, ${dayLabel}`}
1102
1421
  aria-expanded={onPressMore ? undefined : moreOpenFor === day.id}
1103
1422
  >
@@ -1175,7 +1494,11 @@ export function MonthView<T = unknown>({
1175
1494
  }}
1176
1495
  >
1177
1496
  {Chip ? (
1178
- <Chip event={event} onPress={() => onPressEvent?.(event)} />
1497
+ <Chip
1498
+ mode={viewMode}
1499
+ event={event}
1500
+ onPress={() => onPressEvent?.(event)}
1501
+ />
1179
1502
  ) : (
1180
1503
  <span {...slot("chip", chipDefault(theme, chipHeight))}>
1181
1504
  {event.title}