@super-calendar/dom 2.15.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/README.md +92 -0
- package/dist/index.d.mts +94 -31
- package/dist/index.d.ts +94 -31
- package/dist/index.js +1675 -526
- package/dist/index.mjs +1672 -530
- package/package.json +2 -2
- package/src/Calendar.tsx +91 -13
- package/src/DateRangePicker.tsx +60 -6
- package/src/EventActions.tsx +470 -0
- package/src/InertDiv.ts +7 -0
- package/src/MonthList.tsx +64 -38
- package/src/MonthView.tsx +398 -63
- package/src/MultiWeekView.tsx +54 -0
- package/src/ResourceTimeline.tsx +438 -98
- package/src/TimeGrid.tsx +305 -35
- package/src/index.ts +8 -0
- package/src/touch.ts +70 -0
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
|
|
36
|
-
type
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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(
|
|
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:
|
|
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
|
-
}:
|
|
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
|
-
|
|
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 =
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
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,26 +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
|
-
|
|
646
|
-
|
|
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
|
+
}
|
|
786
|
+
useEffect(() => {
|
|
787
|
+
// A parent can focus a day before the initial passive effect runs.
|
|
788
|
+
if (windowKey === focusedWindow.current) return;
|
|
789
|
+
focusedWindow.current = windowKey;
|
|
790
|
+
setFocusedDate(initialFocusRef.current);
|
|
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]);
|
|
647
807
|
|
|
648
808
|
const [hoveredKey, setHoveredKey] = useState<string | null>(null);
|
|
649
|
-
const
|
|
650
|
-
|
|
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]);
|
|
651
831
|
|
|
652
832
|
// Dragging (events mode): press a day and drag across others to sketch an
|
|
653
833
|
// all-day span, or press an event chip and drop it on another day. Both are
|
|
@@ -683,12 +863,12 @@ export function MonthView<T = unknown>({
|
|
|
683
863
|
const beginCreate = (day: Date) => {
|
|
684
864
|
if (!onCreateEvent && !onSelectDrag) return;
|
|
685
865
|
const t = startOfDay(day).getTime();
|
|
686
|
-
setDrag({ kind: "create", anchor: t, hover: t });
|
|
866
|
+
setDrag({ kind: "create", anchor: t, hover: t, windowKey });
|
|
687
867
|
};
|
|
688
868
|
const beginMove = (event: CalendarEvent<T>, day: Date) => {
|
|
689
869
|
if (!canMove(event)) return;
|
|
690
870
|
const t = startOfDay(day).getTime();
|
|
691
|
-
setDrag({ kind: "move", event, from: t, to: t });
|
|
871
|
+
setDrag({ kind: "move", event, from: t, to: t, windowKey });
|
|
692
872
|
onDragStart?.(event);
|
|
693
873
|
};
|
|
694
874
|
const extendDrag = (day: Date) => {
|
|
@@ -705,6 +885,87 @@ export function MonthView<T = unknown>({
|
|
|
705
885
|
onSelectDrag?.(new Date(lo), new Date(hi));
|
|
706
886
|
}
|
|
707
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
|
+
});
|
|
708
969
|
const isDragging = drag !== null;
|
|
709
970
|
// Whether any month drag is wired up at all, so the cells only take over touch
|
|
710
971
|
// scrolling and pointer tracking when one of the handlers can actually fire.
|
|
@@ -715,6 +976,8 @@ export function MonthView<T = unknown>({
|
|
|
715
976
|
const current = dragRef.current;
|
|
716
977
|
setDrag(null);
|
|
717
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;
|
|
718
981
|
suppressClickRef.current = true;
|
|
719
982
|
if (current.kind === "create") {
|
|
720
983
|
const range = monthCreateRange(new Date(current.anchor), new Date(current.hover));
|
|
@@ -735,8 +998,51 @@ export function MonthView<T = unknown>({
|
|
|
735
998
|
};
|
|
736
999
|
window.addEventListener("pointerup", finish);
|
|
737
1000
|
return () => window.removeEventListener("pointerup", finish);
|
|
738
|
-
}, [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
|
+
};
|
|
739
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;
|
|
740
1046
|
let next: Date | null = null;
|
|
741
1047
|
if (e.key === "ArrowLeft") next = addDays(focusedDate, -1);
|
|
742
1048
|
else if (e.key === "ArrowRight") next = addDays(focusedDate, 1);
|
|
@@ -755,7 +1061,14 @@ export function MonthView<T = unknown>({
|
|
|
755
1061
|
return (
|
|
756
1062
|
<div
|
|
757
1063
|
className={className}
|
|
758
|
-
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
|
+
}}
|
|
759
1072
|
>
|
|
760
1073
|
{showTitle ? (
|
|
761
1074
|
<div
|
|
@@ -763,13 +1076,16 @@ export function MonthView<T = unknown>({
|
|
|
763
1076
|
themed: { fontSize: 17, fontWeight: 700, padding: "10px 14px 6px" },
|
|
764
1077
|
})}
|
|
765
1078
|
>
|
|
766
|
-
{
|
|
1079
|
+
{title}
|
|
767
1080
|
</div>
|
|
768
1081
|
) : null}
|
|
769
1082
|
{showWeekdays ? (
|
|
770
1083
|
<div
|
|
771
1084
|
{...slot("weekdays", {
|
|
772
|
-
base: {
|
|
1085
|
+
base: {
|
|
1086
|
+
display: "grid",
|
|
1087
|
+
gridTemplateColumns: `repeat(${weekdays.length}, minmax(0, 1fr))`,
|
|
1088
|
+
},
|
|
773
1089
|
themed: { borderBottom: `1px solid ${theme.gridLine}`, padding: "6px 0" },
|
|
774
1090
|
})}
|
|
775
1091
|
>
|
|
@@ -793,11 +1109,15 @@ export function MonthView<T = unknown>({
|
|
|
793
1109
|
<div
|
|
794
1110
|
ref={gridRef}
|
|
795
1111
|
role="grid"
|
|
796
|
-
|
|
1112
|
+
tabIndex={multiWeek ? -1 : undefined}
|
|
1113
|
+
aria-label={title}
|
|
797
1114
|
// Arrow-key roving applies whenever the day cells are a tab stop: the
|
|
798
1115
|
// picker always, events mode only when keyboardDayNavigation is set.
|
|
799
1116
|
onKeyDown={dayRoving ? onKeyDown : undefined}
|
|
800
|
-
{...slot(
|
|
1117
|
+
{...slot(
|
|
1118
|
+
"grid",
|
|
1119
|
+
multiWeek ? { base: { flex: "1 1 auto", minHeight: 0, overflowY: "auto" } } : undefined,
|
|
1120
|
+
)}
|
|
801
1121
|
>
|
|
802
1122
|
{weeks.map((week, weekIndex) => {
|
|
803
1123
|
// Spanning bars for this row (laid out once in `weekLayouts`). A multi-day
|
|
@@ -818,8 +1138,8 @@ export function MonthView<T = unknown>({
|
|
|
818
1138
|
let firstVisCol = 0;
|
|
819
1139
|
let lastVisCol = cols - 1;
|
|
820
1140
|
if (eventsMode && !showAdjacentMonths) {
|
|
821
|
-
const first = week.days.findIndex((d) => d
|
|
822
|
-
const lastFromEnd = [...week.days].reverse().findIndex((d) => d
|
|
1141
|
+
const first = week.days.findIndex((d) => !isOutsidePeriod(d));
|
|
1142
|
+
const lastFromEnd = [...week.days].reverse().findIndex((d) => !isOutsidePeriod(d));
|
|
823
1143
|
if (first !== -1) {
|
|
824
1144
|
firstVisCol = first;
|
|
825
1145
|
lastVisCol = cols - 1 - lastFromEnd;
|
|
@@ -846,11 +1166,14 @@ export function MonthView<T = unknown>({
|
|
|
846
1166
|
key={week.id}
|
|
847
1167
|
role="row"
|
|
848
1168
|
{...slot("week", {
|
|
849
|
-
base: {
|
|
1169
|
+
base: {
|
|
1170
|
+
display: "grid",
|
|
1171
|
+
gridTemplateColumns: `repeat(${week.days.length}, minmax(0, 1fr))`,
|
|
1172
|
+
},
|
|
850
1173
|
})}
|
|
851
1174
|
>
|
|
852
1175
|
{week.days.map((day, dayCol) => {
|
|
853
|
-
const hidden = !showAdjacentMonths &&
|
|
1176
|
+
const hidden = !showAdjacentMonths && isOutsidePeriod(day);
|
|
854
1177
|
const cellHeight = eventsMode ? eventRowHeight : theme.cellHeight;
|
|
855
1178
|
if (hidden) {
|
|
856
1179
|
return (
|
|
@@ -879,7 +1202,7 @@ export function MonthView<T = unknown>({
|
|
|
879
1202
|
"data-selected": day.isSelected,
|
|
880
1203
|
"data-range": day.isInRange,
|
|
881
1204
|
"data-weekend": day.isWeekend,
|
|
882
|
-
"data-outside":
|
|
1205
|
+
"data-outside": isOutsidePeriod(day),
|
|
883
1206
|
"data-disabled": day.isDisabled,
|
|
884
1207
|
"data-creating": inCreate,
|
|
885
1208
|
"data-drop": isDropTarget,
|
|
@@ -917,6 +1240,9 @@ export function MonthView<T = unknown>({
|
|
|
917
1240
|
key={day.id}
|
|
918
1241
|
role="gridcell"
|
|
919
1242
|
data-day={day.id}
|
|
1243
|
+
onFocus={(event) => {
|
|
1244
|
+
if (event.target === event.currentTarget) setFocusedDate(day.date);
|
|
1245
|
+
}}
|
|
920
1246
|
{...dayData}
|
|
921
1247
|
tabIndex={keyboardDayNavigation ? (day.id === focusKey ? 0 : -1) : undefined}
|
|
922
1248
|
aria-disabled={day.isDisabled || undefined}
|
|
@@ -934,15 +1260,17 @@ export function MonthView<T = unknown>({
|
|
|
934
1260
|
// numbers and chip titles it passes over.
|
|
935
1261
|
style={
|
|
936
1262
|
dragWired
|
|
937
|
-
? {
|
|
1263
|
+
? {
|
|
1264
|
+
...dayCellProps.style,
|
|
1265
|
+
touchAction: multiWeek ? "pan-y" : "none",
|
|
1266
|
+
userSelect: "none",
|
|
1267
|
+
}
|
|
938
1268
|
: dayCellProps.style
|
|
939
1269
|
}
|
|
940
1270
|
onPointerDown={
|
|
941
1271
|
dragWired && !day.isDisabled
|
|
942
1272
|
? (e) => {
|
|
943
|
-
|
|
944
|
-
beginPress();
|
|
945
|
-
beginCreate(day.date);
|
|
1273
|
+
pressRow(e, day.date);
|
|
946
1274
|
}
|
|
947
1275
|
: undefined
|
|
948
1276
|
}
|
|
@@ -998,8 +1326,7 @@ export function MonthView<T = unknown>({
|
|
|
998
1326
|
? (e) => {
|
|
999
1327
|
if (e.button !== 0) return;
|
|
1000
1328
|
e.stopPropagation();
|
|
1001
|
-
|
|
1002
|
-
beginMove(b.seg.event, day.date);
|
|
1329
|
+
pressRow(e, day.date, b.seg.event);
|
|
1003
1330
|
}
|
|
1004
1331
|
: undefined
|
|
1005
1332
|
}
|
|
@@ -1013,7 +1340,7 @@ export function MonthView<T = unknown>({
|
|
|
1013
1340
|
aria-label={
|
|
1014
1341
|
eventAccessibilityLabel
|
|
1015
1342
|
? eventAccessibilityLabel(b.seg.event, {
|
|
1016
|
-
mode:
|
|
1343
|
+
mode: viewMode,
|
|
1017
1344
|
isAllDay: isAllDayEvent(b.seg.event),
|
|
1018
1345
|
ampm: false,
|
|
1019
1346
|
})
|
|
@@ -1035,12 +1362,13 @@ export function MonthView<T = unknown>({
|
|
|
1035
1362
|
// target reads as where it is going.
|
|
1036
1363
|
opacity: isDragged ? DRAGGED_CHIP_OPACITY : undefined,
|
|
1037
1364
|
cursor: canMove(b.seg.event) ? "grab" : undefined,
|
|
1038
|
-
touchAction: dragWired ? "none" : undefined,
|
|
1365
|
+
touchAction: dragWired ? (multiWeek ? "pan-y" : "none") : undefined,
|
|
1039
1366
|
zIndex: 2,
|
|
1040
1367
|
}}
|
|
1041
1368
|
>
|
|
1042
1369
|
{Chip ? (
|
|
1043
1370
|
<Chip
|
|
1371
|
+
mode={viewMode}
|
|
1044
1372
|
event={b.seg.event}
|
|
1045
1373
|
onPress={() => onPressEvent?.(b.seg.event)}
|
|
1046
1374
|
/>
|
|
@@ -1088,7 +1416,7 @@ export function MonthView<T = unknown>({
|
|
|
1088
1416
|
setMoreOpenFor((open) => (open === day.id ? null : day.id));
|
|
1089
1417
|
}
|
|
1090
1418
|
}}
|
|
1091
|
-
{...slot("more", moreButtonDefault(theme, chipHeight))}
|
|
1419
|
+
{...slot("more", moreButtonDefault(theme, chipHeight, multiWeek))}
|
|
1092
1420
|
aria-label={`${labels.moreEvents(hiddenEvents.length)}, ${dayLabel}`}
|
|
1093
1421
|
aria-expanded={onPressMore ? undefined : moreOpenFor === day.id}
|
|
1094
1422
|
>
|
|
@@ -1166,7 +1494,11 @@ export function MonthView<T = unknown>({
|
|
|
1166
1494
|
}}
|
|
1167
1495
|
>
|
|
1168
1496
|
{Chip ? (
|
|
1169
|
-
<Chip
|
|
1497
|
+
<Chip
|
|
1498
|
+
mode={viewMode}
|
|
1499
|
+
event={event}
|
|
1500
|
+
onPress={() => onPressEvent?.(event)}
|
|
1501
|
+
/>
|
|
1170
1502
|
) : (
|
|
1171
1503
|
<span {...slot("chip", chipDefault(theme, chipHeight))}>
|
|
1172
1504
|
{event.title}
|
|
@@ -1187,11 +1519,14 @@ export function MonthView<T = unknown>({
|
|
|
1187
1519
|
type="button"
|
|
1188
1520
|
role="gridcell"
|
|
1189
1521
|
data-day={day.id}
|
|
1522
|
+
onFocus={(event) => {
|
|
1523
|
+
if (event.target === event.currentTarget) setFocusedDate(day.date);
|
|
1524
|
+
}}
|
|
1190
1525
|
{...dayData}
|
|
1191
1526
|
tabIndex={day.id === focusKey ? 0 : -1}
|
|
1192
1527
|
aria-disabled={day.isDisabled || undefined}
|
|
1193
1528
|
aria-label={label}
|
|
1194
|
-
aria-
|
|
1529
|
+
aria-selected={day.isSelected || day.isInRange}
|
|
1195
1530
|
{...slot("day", dayCellDefault(day, theme, highlightWeekends))}
|
|
1196
1531
|
onClick={
|
|
1197
1532
|
day.isDisabled
|