@super-calendar/dom 2.6.0 → 2.7.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/dist/index.d.mts +84 -15
- package/dist/index.d.ts +84 -15
- package/dist/index.js +571 -118
- package/dist/index.mjs +549 -120
- package/package.json +2 -2
- package/src/Agenda.tsx +5 -2
- package/src/Calendar.tsx +30 -0
- package/src/MonthList.tsx +3 -1
- package/src/MonthView.tsx +135 -3
- package/src/ResourceTimeline.tsx +269 -20
- package/src/TimeGrid.tsx +364 -80
- package/src/YearView.tsx +14 -4
- package/src/index.ts +5 -0
package/src/TimeGrid.tsx
CHANGED
|
@@ -13,10 +13,14 @@ import {
|
|
|
13
13
|
} from "react";
|
|
14
14
|
import {
|
|
15
15
|
type BusinessHours,
|
|
16
|
+
type BusinessHoursBand,
|
|
16
17
|
type CalendarEvent,
|
|
17
18
|
type CalendarMode,
|
|
18
19
|
cellRangeFromDrag,
|
|
20
|
+
pageStepDays,
|
|
21
|
+
backgroundBandsForDay,
|
|
19
22
|
closedHourBands,
|
|
23
|
+
isBackgroundEvent,
|
|
20
24
|
eventAccessibilityLabel as defaultEventAccessibilityLabel,
|
|
21
25
|
type EventAccessibilityLabeler,
|
|
22
26
|
eventChipLayout,
|
|
@@ -28,6 +32,7 @@ import {
|
|
|
28
32
|
isSameCalendarDay,
|
|
29
33
|
isWeekend,
|
|
30
34
|
layoutDayEvents,
|
|
35
|
+
useNow,
|
|
31
36
|
type TimeGridMode,
|
|
32
37
|
titleNumberOfLines,
|
|
33
38
|
type WeekdayFormat,
|
|
@@ -56,6 +61,7 @@ export type TimeGridSlot =
|
|
|
56
61
|
| "dayColumn"
|
|
57
62
|
| "gridLines"
|
|
58
63
|
| "businessHours"
|
|
64
|
+
| "backgroundEvent"
|
|
59
65
|
| "event"
|
|
60
66
|
| "eventBox"
|
|
61
67
|
| "nowIndicator"
|
|
@@ -64,6 +70,13 @@ export type TimeGridSlot =
|
|
|
64
70
|
const GUTTER_WIDTH = 56;
|
|
65
71
|
const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));
|
|
66
72
|
|
|
73
|
+
// Edge auto-advance: how close to the day-columns edge (px) counts as "at the
|
|
74
|
+
// edge", how long to dwell there before paging, and the repeat interval while
|
|
75
|
+
// the pointer stays in the zone.
|
|
76
|
+
const EDGE_ZONE = 32;
|
|
77
|
+
const EDGE_DWELL_MS = 600;
|
|
78
|
+
const EDGE_REPEAT_MS = 600;
|
|
79
|
+
|
|
67
80
|
// Off-screen but readable by assistive tech: gives an element an accessible name
|
|
68
81
|
// without changing the visible layout.
|
|
69
82
|
const VISUALLY_HIDDEN: CSSProperties = {
|
|
@@ -142,12 +155,28 @@ export interface TimeGridProps<T = unknown> extends SlotStyleProps<TimeGridSlot>
|
|
|
142
155
|
showWeekNumber?: boolean;
|
|
143
156
|
/** Prefix for the week number, e.g. "W" → "W28". Default "W". */
|
|
144
157
|
weekNumberPrefix?: string;
|
|
158
|
+
/** Weekdays (0=Sunday…6=Saturday) hidden from the grid, e.g. `[0, 6]` for weekends off. */
|
|
159
|
+
hiddenDays?: number[];
|
|
145
160
|
/** Shade the hours outside business hours; `null` shades the whole day. */
|
|
146
161
|
businessHours?: BusinessHours;
|
|
162
|
+
/**
|
|
163
|
+
* Render a closed-hours band's content yourself (a label, icon, pattern).
|
|
164
|
+
* The grid keeps positioning the band; when set, the themed tint is dropped
|
|
165
|
+
* and your output fills the band instead. Decorative only: the
|
|
166
|
+
* band stays non-interactive and hidden from assistive tech.
|
|
167
|
+
*/
|
|
168
|
+
renderBusinessHours?: (band: BusinessHoursBand) => ReactNode;
|
|
147
169
|
/** Show the current-time indicator on today's column (default true). */
|
|
148
170
|
showNowIndicator?: boolean;
|
|
171
|
+
/** Fixed "now" instant for the indicator (doesn't tick). Defaults to the device clock. */
|
|
172
|
+
now?: Date;
|
|
173
|
+
/** Shift the now indicator into this IANA zone (pair with `eventsInTimeZone`). */
|
|
174
|
+
timeZone?: string;
|
|
149
175
|
/** Show the all-day lane above the grid (default true). */
|
|
150
176
|
showAllDayEventCell?: boolean;
|
|
177
|
+
/** Tint Saturday/Sunday columns with the weekend background (default true). Set
|
|
178
|
+
* false to treat weekends like any other day. */
|
|
179
|
+
highlightWeekends?: boolean;
|
|
151
180
|
/** date-fns locale for the column headers and time labels. */
|
|
152
181
|
locale?: Locale;
|
|
153
182
|
/** Theme overrides; falls back to the default light theme. */
|
|
@@ -188,6 +217,13 @@ export interface TimeGridProps<T = unknown> extends SlotStyleProps<TimeGridSlot>
|
|
|
188
217
|
* Return `false` to reject the drop (the event snaps back).
|
|
189
218
|
*/
|
|
190
219
|
onDragEvent?: (event: CalendarEvent<T>, start: Date, end: Date) => void | boolean;
|
|
220
|
+
/**
|
|
221
|
+
* Page the grid to a new anchor date. Supplying it alongside `onDragEvent`
|
|
222
|
+
* turns on edge auto-advance: dragging an event to the left/right edge and
|
|
223
|
+
* dwelling briefly pages the view so the event can be dropped on a day in the
|
|
224
|
+
* previous/next period. Controlled, like the native renderer's `onChangeDate`.
|
|
225
|
+
*/
|
|
226
|
+
onChangeDate?: (date: Date) => void;
|
|
191
227
|
/** Class applied to the root element. */
|
|
192
228
|
className?: string;
|
|
193
229
|
/** Inline styles applied to the root element. */
|
|
@@ -302,21 +338,8 @@ function DefaultDomEvent<T>({
|
|
|
302
338
|
);
|
|
303
339
|
}
|
|
304
340
|
|
|
305
|
-
const NOW_TICK_MS = 60_000;
|
|
306
|
-
|
|
307
341
|
// A `Date` that advances every minute while `enabled`, so the now-indicator
|
|
308
342
|
// tracks the wall clock instead of freezing at the last render. Mirrors the
|
|
309
|
-
// native renderer's `useNow`.
|
|
310
|
-
function useNow(enabled: boolean): Date {
|
|
311
|
-
const [now, setNow] = useState(() => new Date());
|
|
312
|
-
useEffect(() => {
|
|
313
|
-
if (!enabled) return;
|
|
314
|
-
setNow(new Date());
|
|
315
|
-
const id = setInterval(() => setNow(new Date()), NOW_TICK_MS);
|
|
316
|
-
return () => clearInterval(id);
|
|
317
|
-
}, [enabled]);
|
|
318
|
-
return now;
|
|
319
|
-
}
|
|
320
343
|
|
|
321
344
|
/**
|
|
322
345
|
* A day / week / N-day time grid rendered with plain DOM elements. Events are
|
|
@@ -349,9 +372,14 @@ export function TimeGrid<T = unknown>({
|
|
|
349
372
|
hideHours = false,
|
|
350
373
|
showWeekNumber = false,
|
|
351
374
|
weekNumberPrefix = "W",
|
|
375
|
+
hiddenDays,
|
|
352
376
|
keyboardEventNavigation = false,
|
|
353
377
|
businessHours,
|
|
378
|
+
renderBusinessHours,
|
|
354
379
|
showNowIndicator = true,
|
|
380
|
+
highlightWeekends = true,
|
|
381
|
+
now: nowProp,
|
|
382
|
+
timeZone,
|
|
355
383
|
showAllDayEventCell = true,
|
|
356
384
|
locale,
|
|
357
385
|
theme: themeOverrides,
|
|
@@ -365,6 +393,7 @@ export function TimeGrid<T = unknown>({
|
|
|
365
393
|
onCreateEvent,
|
|
366
394
|
onDragStart,
|
|
367
395
|
onDragEvent,
|
|
396
|
+
onChangeDate,
|
|
368
397
|
className,
|
|
369
398
|
style,
|
|
370
399
|
classNames,
|
|
@@ -387,8 +416,9 @@ export function TimeGrid<T = unknown>({
|
|
|
387
416
|
[windowStart, windowHours],
|
|
388
417
|
);
|
|
389
418
|
|
|
390
|
-
// Ticks every minute so the red now-line follows the wall clock
|
|
391
|
-
|
|
419
|
+
// Ticks every minute so the red now-line follows the wall clock; a `now`
|
|
420
|
+
// override pins it, and `timeZone` shifts it to match zone-shifted events.
|
|
421
|
+
const now = useNow(showNowIndicator, { now: nowProp, timeZone });
|
|
392
422
|
|
|
393
423
|
const [hourHeight, setHourHeight] = useState(initialHourHeight);
|
|
394
424
|
useEffect(() => setHourHeight(initialHourHeight), [initialHourHeight]);
|
|
@@ -423,10 +453,45 @@ export function TimeGrid<T = unknown>({
|
|
|
423
453
|
const cellEnabled = !!onPressCell || !!onCreateEvent;
|
|
424
454
|
|
|
425
455
|
const days = useMemo(
|
|
426
|
-
() => getViewDays(mode, date, weekStartsOn, numberOfDays),
|
|
427
|
-
[mode, date, weekStartsOn, numberOfDays],
|
|
456
|
+
() => getViewDays(mode, date, weekStartsOn, numberOfDays, false, undefined, hiddenDays),
|
|
457
|
+
[mode, date, weekStartsOn, numberOfDays, hiddenDays],
|
|
428
458
|
);
|
|
429
459
|
|
|
460
|
+
// Edge auto-advance. During a move drag the pointer stream is handled at the
|
|
461
|
+
// document level (attached in beginDrag) so it survives the day columns
|
|
462
|
+
// unmounting when a page change swaps `date`. The listeners read these live
|
|
463
|
+
// refs rather than a stale render closure; `daysRef`/`pageDepsRef` update every
|
|
464
|
+
// render so the drop and the next page use the current view.
|
|
465
|
+
const edgeAdvanceEnabled = !!onChangeDate && !!onDragEvent;
|
|
466
|
+
const columnsRef = useRef<HTMLDivElement>(null);
|
|
467
|
+
const edgeTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
468
|
+
const edgeSide = useRef<-1 | 0 | 1>(0);
|
|
469
|
+
const ptr = useRef({ x: 0, y: 0 });
|
|
470
|
+
const pagedRef = useRef(false);
|
|
471
|
+
const [paged, setPaged] = useState(false);
|
|
472
|
+
const draggedRef = useRef<{
|
|
473
|
+
event: CalendarEvent<T>;
|
|
474
|
+
onPress: () => void;
|
|
475
|
+
grabX: number;
|
|
476
|
+
grabY: number;
|
|
477
|
+
w: number;
|
|
478
|
+
h: number;
|
|
479
|
+
} | null>(null);
|
|
480
|
+
const ghostRef = useRef<HTMLDivElement>(null);
|
|
481
|
+
const docHandlersRef = useRef<{
|
|
482
|
+
move: (e: PointerEvent) => void;
|
|
483
|
+
up: (e: PointerEvent) => void;
|
|
484
|
+
cancel: (e: PointerEvent) => void;
|
|
485
|
+
} | null>(null);
|
|
486
|
+
const daysRef = useRef(days);
|
|
487
|
+
daysRef.current = days;
|
|
488
|
+
const pageDepsRef = useRef({ date, mode, weekStartsOn, numberOfDays });
|
|
489
|
+
pageDepsRef.current = { date, mode, weekStartsOn, numberOfDays };
|
|
490
|
+
const onChangeDateRef = useRef(onChangeDate);
|
|
491
|
+
onChangeDateRef.current = onChangeDate;
|
|
492
|
+
const onDragEventRef = useRef(onDragEvent);
|
|
493
|
+
onDragEventRef.current = onDragEvent;
|
|
494
|
+
|
|
430
495
|
const allDayByDay = useMemo(
|
|
431
496
|
// A multi-day all-day event shows in every column it overlaps (matching the
|
|
432
497
|
// native AllDayLane), not just its start day.
|
|
@@ -434,7 +499,9 @@ export function TimeGrid<T = unknown>({
|
|
|
434
499
|
days.map((day) => {
|
|
435
500
|
const dayStart = startOfDay(day);
|
|
436
501
|
const dayEnd = addDays(dayStart, 1);
|
|
437
|
-
return events.filter(
|
|
502
|
+
return events.filter(
|
|
503
|
+
(e) => isAllDayEvent(e) && !isBackgroundEvent(e) && e.start < dayEnd && e.end > dayStart,
|
|
504
|
+
);
|
|
438
505
|
}),
|
|
439
506
|
[days, events],
|
|
440
507
|
);
|
|
@@ -492,45 +559,61 @@ export function TimeGrid<T = unknown>({
|
|
|
492
559
|
const Renderer = renderEvent;
|
|
493
560
|
const totalHeight = windowHours * hourHeight;
|
|
494
561
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
durationHours: number,
|
|
502
|
-
dayIndex: number,
|
|
503
|
-
) => {
|
|
504
|
-
if (!onDragEvent) return;
|
|
505
|
-
e.stopPropagation();
|
|
506
|
-
try {
|
|
507
|
-
(e.target as HTMLElement).setPointerCapture?.(e.pointerId);
|
|
508
|
-
} catch {
|
|
509
|
-
// Pointer capture is best-effort; some environments reject it.
|
|
562
|
+
// The day under a screen point, from the `data-date` on the column beneath the
|
|
563
|
+
// pointer (the ghost is pointer-transparent, so it doesn't shadow it). Guarded
|
|
564
|
+
// for jsdom, which has no `elementFromPoint`.
|
|
565
|
+
const dayAt = (x: number, y: number): Date | null => {
|
|
566
|
+
if (typeof document === "undefined" || typeof document.elementFromPoint !== "function") {
|
|
567
|
+
return null;
|
|
510
568
|
}
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
const column = kind === "move" ? (e.currentTarget as HTMLElement).parentElement : null;
|
|
515
|
-
const dayWidth = column ? column.getBoundingClientRect().width : 0;
|
|
516
|
-
dragOrigin.current = {
|
|
517
|
-
pointerX: e.clientX,
|
|
518
|
-
pointerY: e.clientY,
|
|
519
|
-
startHours,
|
|
520
|
-
durationHours,
|
|
521
|
-
dayIndex,
|
|
522
|
-
dayWidth,
|
|
523
|
-
};
|
|
524
|
-
applyDrag({ key, kind, startHours, durationHours, dayDelta: 0, dayOffsetPx: 0, moved: false });
|
|
525
|
-
onDragStart?.(event);
|
|
569
|
+
const el = document.elementFromPoint(x, y)?.closest?.("[data-date]");
|
|
570
|
+
const iso = el?.getAttribute("data-date");
|
|
571
|
+
return iso ? new Date(iso) : null;
|
|
526
572
|
};
|
|
527
|
-
|
|
528
|
-
|
|
573
|
+
|
|
574
|
+
const clearEdgeTimer = () => {
|
|
575
|
+
if (edgeTimer.current) {
|
|
576
|
+
clearTimeout(edgeTimer.current);
|
|
577
|
+
edgeTimer.current = null;
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
// Page one period toward the held edge, then re-arm so holding keeps advancing.
|
|
582
|
+
const fireEdge = () => {
|
|
583
|
+
const side = edgeSide.current;
|
|
584
|
+
if (side === 0) return;
|
|
585
|
+
const p = pageDepsRef.current;
|
|
586
|
+
const step = pageStepDays(p.mode, p.date, p.weekStartsOn, p.numberOfDays);
|
|
587
|
+
onChangeDateRef.current?.(addDays(p.date, side * step));
|
|
588
|
+
pagedRef.current = true;
|
|
589
|
+
setPaged(true);
|
|
590
|
+
edgeTimer.current = setTimeout(fireEdge, EDGE_REPEAT_MS);
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
const detachDocListeners = () => {
|
|
594
|
+
const h = docHandlersRef.current;
|
|
595
|
+
if (!h) return;
|
|
596
|
+
document.removeEventListener("pointermove", h.move);
|
|
597
|
+
document.removeEventListener("pointerup", h.up);
|
|
598
|
+
document.removeEventListener("pointercancel", h.cancel);
|
|
599
|
+
docHandlersRef.current = null;
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
const finishDrag = () => {
|
|
603
|
+
detachDocListeners();
|
|
604
|
+
clearEdgeTimer();
|
|
605
|
+
edgeSide.current = 0;
|
|
606
|
+
pagedRef.current = false;
|
|
607
|
+
setPaged(false);
|
|
608
|
+
draggedRef.current = null;
|
|
529
609
|
dragOrigin.current = null;
|
|
610
|
+
applyDrag(null);
|
|
530
611
|
};
|
|
531
|
-
|
|
612
|
+
|
|
613
|
+
const moveDrag = (e: PointerEvent) => {
|
|
532
614
|
const d = dragRef.current;
|
|
533
615
|
if (!d || !dragOrigin.current) return;
|
|
616
|
+
ptr.current = { x: e.clientX, y: e.clientY };
|
|
534
617
|
// Read the live row height (ref, not the state closure) so a mid-drag zoom
|
|
535
618
|
// keeps the math aligned with what's on screen.
|
|
536
619
|
const dHours = (e.clientY - dragOrigin.current.pointerY) / hourHeightRef.current;
|
|
@@ -543,13 +626,35 @@ export function TimeGrid<T = unknown>({
|
|
|
543
626
|
windowStart,
|
|
544
627
|
Math.max(windowStart, windowEnd - d.durationHours),
|
|
545
628
|
);
|
|
546
|
-
// Map the horizontal drag to whole day columns, clamped so the
|
|
547
|
-
// can't leave the visible range (mirrors the native renderer).
|
|
629
|
+
// Map the horizontal drag to whole day columns, clamped so the in-view box
|
|
630
|
+
// can't leave the visible range (mirrors the native renderer). Once paged,
|
|
631
|
+
// the drop day comes from the hit-test instead.
|
|
548
632
|
const o = dragOrigin.current;
|
|
549
633
|
const rawDayDelta = o.dayWidth > 0 ? Math.round((e.clientX - o.pointerX) / o.dayWidth) : 0;
|
|
550
|
-
const targetDay = clamp(o.dayIndex + rawDayDelta, 0,
|
|
634
|
+
const targetDay = clamp(o.dayIndex + rawDayDelta, 0, daysRef.current.length - 1);
|
|
551
635
|
const dayDelta = targetDay - o.dayIndex;
|
|
552
636
|
applyDrag({ ...d, startHours, dayDelta, dayOffsetPx: dayDelta * o.dayWidth, moved: true });
|
|
637
|
+
// Follow the pointer with the floating ghost once the origin column is gone.
|
|
638
|
+
const g = ghostRef.current;
|
|
639
|
+
const held = draggedRef.current;
|
|
640
|
+
if (g && held) {
|
|
641
|
+
g.style.transform = `translate(${e.clientX - held.grabX}px, ${e.clientY - held.grabY}px)`;
|
|
642
|
+
}
|
|
643
|
+
// Edge auto-advance: dwell near a horizontal edge to page the view. Read the
|
|
644
|
+
// columns rect live so a mid-drag scroll/layout shift can't stale it.
|
|
645
|
+
if (edgeAdvanceEnabled) {
|
|
646
|
+
const rect = columnsRef.current?.getBoundingClientRect();
|
|
647
|
+
if (rect && rect.right > rect.left) {
|
|
648
|
+
const left = rect.left + gutterWidth;
|
|
649
|
+
const side: -1 | 0 | 1 =
|
|
650
|
+
e.clientX <= left + EDGE_ZONE ? -1 : e.clientX >= rect.right - EDGE_ZONE ? 1 : 0;
|
|
651
|
+
if (side !== edgeSide.current) {
|
|
652
|
+
clearEdgeTimer();
|
|
653
|
+
edgeSide.current = side;
|
|
654
|
+
if (side !== 0) edgeTimer.current = setTimeout(fireEdge, EDGE_DWELL_MS);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
553
658
|
} else {
|
|
554
659
|
const durationHours = clamp(
|
|
555
660
|
snap(dragOrigin.current.durationHours + dHours),
|
|
@@ -559,33 +664,118 @@ export function TimeGrid<T = unknown>({
|
|
|
559
664
|
applyDrag({ ...d, durationHours, moved: true });
|
|
560
665
|
}
|
|
561
666
|
};
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
day: Date,
|
|
565
|
-
event: CalendarEvent<T>,
|
|
566
|
-
onPress: () => void,
|
|
567
|
-
) => {
|
|
667
|
+
|
|
668
|
+
const endDrag = (e: PointerEvent) => {
|
|
568
669
|
const d = dragRef.current;
|
|
569
670
|
if (!d) return;
|
|
570
671
|
try {
|
|
571
|
-
(e.target as HTMLElement)
|
|
672
|
+
(e.target as HTMLElement)?.releasePointerCapture?.(e.pointerId);
|
|
572
673
|
} catch {
|
|
573
674
|
// Best-effort release; ignore if the capture was never granted.
|
|
574
675
|
}
|
|
676
|
+
const held = draggedRef.current;
|
|
575
677
|
if (!d.moved) {
|
|
576
|
-
|
|
577
|
-
onPress();
|
|
678
|
+
finishDrag();
|
|
679
|
+
held?.onPress();
|
|
578
680
|
return;
|
|
579
681
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
682
|
+
const originIndex = dragOrigin.current?.dayIndex ?? 0;
|
|
683
|
+
let base: Date | null = null;
|
|
684
|
+
if (d.kind === "move") {
|
|
685
|
+
if (pagedRef.current) {
|
|
686
|
+
// The origin column has scrolled off with the page change, so the day
|
|
687
|
+
// under the pointer is authoritative; dropping off the grid snaps back.
|
|
688
|
+
const day = dayAt(e.clientX, e.clientY);
|
|
689
|
+
base = day ? startOfDay(day) : null;
|
|
690
|
+
} else {
|
|
691
|
+
// In view: resolve from the days array (correct under hiddenDays), as
|
|
692
|
+
// before. `dayDelta` is clamped to the visible range at drag time.
|
|
693
|
+
base = startOfDay(
|
|
694
|
+
daysRef.current[originIndex + d.dayDelta] ?? daysRef.current[originIndex],
|
|
695
|
+
);
|
|
696
|
+
}
|
|
697
|
+
} else {
|
|
698
|
+
base = startOfDay(daysRef.current[originIndex] ?? daysRef.current[0]);
|
|
699
|
+
}
|
|
700
|
+
if (base && held) {
|
|
701
|
+
const start = addMinutes(base, Math.round(d.startHours * 60));
|
|
702
|
+
const end = addMinutes(base, Math.round((d.startHours + d.durationHours) * 60));
|
|
703
|
+
onDragEventRef.current?.(held.event, start, end);
|
|
704
|
+
}
|
|
705
|
+
finishDrag();
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
const cancelDrag = () => {
|
|
709
|
+
finishDrag();
|
|
587
710
|
};
|
|
588
711
|
|
|
712
|
+
const beginDrag = (
|
|
713
|
+
e: ReactPointerEvent,
|
|
714
|
+
event: CalendarEvent<T>,
|
|
715
|
+
key: string,
|
|
716
|
+
kind: "move" | "resize",
|
|
717
|
+
startHours: number,
|
|
718
|
+
durationHours: number,
|
|
719
|
+
dayIndex: number,
|
|
720
|
+
onPress?: () => void,
|
|
721
|
+
) => {
|
|
722
|
+
if (!onDragEvent) return;
|
|
723
|
+
e.stopPropagation();
|
|
724
|
+
try {
|
|
725
|
+
(e.target as HTMLElement).setPointerCapture?.(e.pointerId);
|
|
726
|
+
} catch {
|
|
727
|
+
// Pointer capture is best-effort; some environments reject it.
|
|
728
|
+
}
|
|
729
|
+
// The event box sits inside its day column, so the parent's width is one day;
|
|
730
|
+
// columns are equal-width, so measuring one is enough. Resizes never move
|
|
731
|
+
// across days, so 0 is fine there.
|
|
732
|
+
const boxEl = e.currentTarget as HTMLElement;
|
|
733
|
+
const column = kind === "move" ? boxEl.parentElement : null;
|
|
734
|
+
const dayWidth = column ? column.getBoundingClientRect().width : 0;
|
|
735
|
+
const boxRect = kind === "move" ? boxEl.getBoundingClientRect() : null;
|
|
736
|
+
dragOrigin.current = {
|
|
737
|
+
pointerX: e.clientX,
|
|
738
|
+
pointerY: e.clientY,
|
|
739
|
+
startHours,
|
|
740
|
+
durationHours,
|
|
741
|
+
dayIndex,
|
|
742
|
+
dayWidth,
|
|
743
|
+
};
|
|
744
|
+
draggedRef.current = {
|
|
745
|
+
event,
|
|
746
|
+
onPress: onPress ?? (() => {}),
|
|
747
|
+
grabX: boxRect ? e.clientX - boxRect.left : 0,
|
|
748
|
+
grabY: boxRect ? e.clientY - boxRect.top : 0,
|
|
749
|
+
w: boxRect ? boxRect.width : 0,
|
|
750
|
+
h: boxRect ? boxRect.height : 0,
|
|
751
|
+
};
|
|
752
|
+
ptr.current = { x: e.clientX, y: e.clientY };
|
|
753
|
+
edgeSide.current = 0;
|
|
754
|
+
pagedRef.current = false;
|
|
755
|
+
setPaged(false);
|
|
756
|
+
clearEdgeTimer();
|
|
757
|
+
applyDrag({ key, kind, startHours, durationHours, dayDelta: 0, dayOffsetPx: 0, moved: false });
|
|
758
|
+
onDragStart?.(event);
|
|
759
|
+
// Transport the rest of the drag at the document level so it survives the day
|
|
760
|
+
// columns unmounting on a page change (the dragged box's own node disappears).
|
|
761
|
+
// Detach any prior set first so a second beginDrag can't leak listeners.
|
|
762
|
+
detachDocListeners();
|
|
763
|
+
const handlers = { move: moveDrag, up: endDrag, cancel: cancelDrag };
|
|
764
|
+
docHandlersRef.current = handlers;
|
|
765
|
+
document.addEventListener("pointermove", handlers.move);
|
|
766
|
+
document.addEventListener("pointerup", handlers.up);
|
|
767
|
+
document.addEventListener("pointercancel", handlers.cancel);
|
|
768
|
+
};
|
|
769
|
+
|
|
770
|
+
// Remove any lingering document listeners / dwell timer if we unmount mid-drag.
|
|
771
|
+
useEffect(
|
|
772
|
+
() => () => {
|
|
773
|
+
detachDocListeners();
|
|
774
|
+
clearEdgeTimer();
|
|
775
|
+
},
|
|
776
|
+
[],
|
|
777
|
+
);
|
|
778
|
+
|
|
589
779
|
const pxFromTop = (el: HTMLElement, clientY: number) => clientY - el.getBoundingClientRect().top;
|
|
590
780
|
const beginCreate = (e: ReactPointerEvent, dayIndex: number) => {
|
|
591
781
|
// Mouse / pen only: on touch the column must stay free to scroll the grid.
|
|
@@ -731,6 +921,20 @@ export function TimeGrid<T = unknown>({
|
|
|
731
921
|
() => days.map((day) => closedHourBands(day, businessHours, windowStart, windowEnd)),
|
|
732
922
|
[days, businessHours, windowStart, windowEnd],
|
|
733
923
|
);
|
|
924
|
+
// `display: "background"` events, sliced per day and clipped to the window.
|
|
925
|
+
const backgroundByDay = useMemo(
|
|
926
|
+
() =>
|
|
927
|
+
days.map((day) =>
|
|
928
|
+
backgroundBandsForDay(events, day)
|
|
929
|
+
.map((b) => ({
|
|
930
|
+
...b,
|
|
931
|
+
startHours: Math.max(b.startHours, windowStart),
|
|
932
|
+
endHours: Math.min(b.endHours, windowEnd),
|
|
933
|
+
}))
|
|
934
|
+
.filter((b) => b.endHours > b.startHours),
|
|
935
|
+
),
|
|
936
|
+
[days, events, windowStart, windowEnd],
|
|
937
|
+
);
|
|
734
938
|
const gridLines = useMemo(() => {
|
|
735
939
|
const hourLines = `repeating-linear-gradient(to bottom, transparent 0, transparent ${hourHeight - 1}px, ${theme.gridLine} ${hourHeight - 1}px, ${theme.gridLine} ${hourHeight}px)`;
|
|
736
940
|
if (timeslots <= 1) return hourLines;
|
|
@@ -961,7 +1165,10 @@ export function TimeGrid<T = unknown>({
|
|
|
961
1165
|
touchAction: zoomable ? "pan-y" : "auto",
|
|
962
1166
|
}}
|
|
963
1167
|
>
|
|
964
|
-
<div
|
|
1168
|
+
<div
|
|
1169
|
+
ref={columnsRef}
|
|
1170
|
+
style={{ display: "flex", height: totalHeight, position: "relative" }}
|
|
1171
|
+
>
|
|
965
1172
|
{/* Hour gutter (hidden when hideHours; the grid lines stay). */}
|
|
966
1173
|
{hideHours ? null : (
|
|
967
1174
|
<div
|
|
@@ -1004,6 +1211,8 @@ export function TimeGrid<T = unknown>({
|
|
|
1004
1211
|
return (
|
|
1005
1212
|
<div
|
|
1006
1213
|
key={day.toISOString()}
|
|
1214
|
+
// Identifies the drop day for a cross-page drag hit-test.
|
|
1215
|
+
data-date={day.toISOString()}
|
|
1007
1216
|
// Empty columns are a pointer-only create surface: drag to sweep
|
|
1008
1217
|
// out an event. They are deliberately not tab stops, so keyboard
|
|
1009
1218
|
// focus moves through events only, not every empty day.
|
|
@@ -1018,7 +1227,9 @@ export function TimeGrid<T = unknown>({
|
|
|
1018
1227
|
// tint sits behind the grid lines, business-hours shade and events.
|
|
1019
1228
|
themed: {
|
|
1020
1229
|
borderLeft: `1px solid ${theme.gridLine}`,
|
|
1021
|
-
...(isWeekend(day)
|
|
1230
|
+
...(highlightWeekends && isWeekend(day)
|
|
1231
|
+
? { background: theme.weekendBackground }
|
|
1232
|
+
: null),
|
|
1022
1233
|
},
|
|
1023
1234
|
})}
|
|
1024
1235
|
>
|
|
@@ -1037,7 +1248,31 @@ export function TimeGrid<T = unknown>({
|
|
|
1037
1248
|
pointerEvents: "none",
|
|
1038
1249
|
zIndex: 0,
|
|
1039
1250
|
},
|
|
1040
|
-
themed:
|
|
1251
|
+
themed: renderBusinessHours
|
|
1252
|
+
? undefined
|
|
1253
|
+
: { background: theme.outsideHoursBackground },
|
|
1254
|
+
})}
|
|
1255
|
+
>
|
|
1256
|
+
{renderBusinessHours?.({ date: day, start: b.start, end: b.end })}
|
|
1257
|
+
</div>
|
|
1258
|
+
))}
|
|
1259
|
+
{/* Background events: shaded, non-interactive time ranges. */}
|
|
1260
|
+
{backgroundByDay[dayIndex].map((b, bandIndex) => (
|
|
1261
|
+
<div
|
|
1262
|
+
key={`bg-${bandIndex}`}
|
|
1263
|
+
aria-hidden
|
|
1264
|
+
title={b.event.title}
|
|
1265
|
+
{...slot("backgroundEvent", {
|
|
1266
|
+
base: {
|
|
1267
|
+
position: "absolute",
|
|
1268
|
+
left: 0,
|
|
1269
|
+
right: 0,
|
|
1270
|
+
top: (b.startHours - windowStart) * hourHeight,
|
|
1271
|
+
height: (b.endHours - b.startHours) * hourHeight,
|
|
1272
|
+
pointerEvents: "none",
|
|
1273
|
+
zIndex: 0,
|
|
1274
|
+
},
|
|
1275
|
+
themed: { background: theme.backgroundEvent },
|
|
1041
1276
|
})}
|
|
1042
1277
|
/>
|
|
1043
1278
|
))}
|
|
@@ -1051,7 +1286,10 @@ export function TimeGrid<T = unknown>({
|
|
|
1051
1286
|
/>
|
|
1052
1287
|
{positioned.map((pe, idx) => {
|
|
1053
1288
|
const key = `${dayIndex}:${idx}`;
|
|
1054
|
-
|
|
1289
|
+
// Not while paged: after a page change the origin box is gone and
|
|
1290
|
+
// the ghost stands in, so a same-slot event on the new page (e.g. a
|
|
1291
|
+
// recurring one) must not also render lifted.
|
|
1292
|
+
const active = !paged && drag?.key === key ? drag : null;
|
|
1055
1293
|
const startHours = active ? active.startHours : pe.startHours;
|
|
1056
1294
|
const durationHours = active ? active.durationHours : pe.durationHours;
|
|
1057
1295
|
// Drop events that fall entirely outside the visible window; those
|
|
@@ -1075,7 +1313,9 @@ export function TimeGrid<T = unknown>({
|
|
|
1075
1313
|
ampm,
|
|
1076
1314
|
onPress,
|
|
1077
1315
|
};
|
|
1078
|
-
|
|
1316
|
+
// `draggable: false` locks a single event: it keeps its normal
|
|
1317
|
+
// look and still responds to clicks, it just can't be dragged.
|
|
1318
|
+
const draggable = !!onDragEvent && pe.event.draggable !== false;
|
|
1079
1319
|
return (
|
|
1080
1320
|
<div
|
|
1081
1321
|
key={idx}
|
|
@@ -1103,6 +1343,9 @@ export function TimeGrid<T = unknown>({
|
|
|
1103
1343
|
}
|
|
1104
1344
|
if (keyboardEventNavigation) onEventKeyDown(key, e);
|
|
1105
1345
|
}}
|
|
1346
|
+
// Pointer move/up/cancel are handled at the document level
|
|
1347
|
+
// (attached in beginDrag) so the drag survives the columns
|
|
1348
|
+
// remounting on a page change.
|
|
1106
1349
|
onPointerDown={
|
|
1107
1350
|
draggable
|
|
1108
1351
|
? (e) =>
|
|
@@ -1114,14 +1357,10 @@ export function TimeGrid<T = unknown>({
|
|
|
1114
1357
|
pe.startHours,
|
|
1115
1358
|
pe.durationHours,
|
|
1116
1359
|
dayIndex,
|
|
1360
|
+
onPress,
|
|
1117
1361
|
)
|
|
1118
1362
|
: undefined
|
|
1119
1363
|
}
|
|
1120
|
-
onPointerMove={draggable ? moveDrag : undefined}
|
|
1121
|
-
onPointerUp={
|
|
1122
|
-
draggable ? (e) => endDrag(e, day, pe.event, onPress) : undefined
|
|
1123
|
-
}
|
|
1124
|
-
onPointerCancel={draggable ? cancelDrag : undefined}
|
|
1125
1364
|
onClick={draggable ? undefined : onPress}
|
|
1126
1365
|
{...dataState({ "data-dragging": !!active })}
|
|
1127
1366
|
{...slot("event", {
|
|
@@ -1235,6 +1474,51 @@ export function TimeGrid<T = unknown>({
|
|
|
1235
1474
|
})}
|
|
1236
1475
|
</div>
|
|
1237
1476
|
</div>
|
|
1477
|
+
{/* Floating drag ghost: shown only after a page change, when the dragged
|
|
1478
|
+
event's own column has unmounted. Positioned imperatively per pointer
|
|
1479
|
+
move (see moveDrag) and pointer-transparent so the drop hit-test reads
|
|
1480
|
+
the column beneath it. */}
|
|
1481
|
+
{paged && draggedRef.current ? (
|
|
1482
|
+
<div
|
|
1483
|
+
ref={ghostRef}
|
|
1484
|
+
aria-hidden
|
|
1485
|
+
style={{
|
|
1486
|
+
position: "fixed",
|
|
1487
|
+
left: 0,
|
|
1488
|
+
top: 0,
|
|
1489
|
+
width: draggedRef.current.w,
|
|
1490
|
+
height: draggedRef.current.h,
|
|
1491
|
+
transform: `translate(${ptr.current.x - draggedRef.current.grabX}px, ${
|
|
1492
|
+
ptr.current.y - draggedRef.current.grabY
|
|
1493
|
+
}px)`,
|
|
1494
|
+
pointerEvents: "none",
|
|
1495
|
+
zIndex: 1000,
|
|
1496
|
+
opacity: 0.85,
|
|
1497
|
+
}}
|
|
1498
|
+
>
|
|
1499
|
+
{Renderer ? (
|
|
1500
|
+
<Renderer
|
|
1501
|
+
event={draggedRef.current.event}
|
|
1502
|
+
mode={mode}
|
|
1503
|
+
isAllDay={false}
|
|
1504
|
+
boxHeight={draggedRef.current.h}
|
|
1505
|
+
ampm={ampm}
|
|
1506
|
+
onPress={() => {}}
|
|
1507
|
+
/>
|
|
1508
|
+
) : (
|
|
1509
|
+
<DefaultDomEvent
|
|
1510
|
+
event={draggedRef.current.event}
|
|
1511
|
+
mode={mode}
|
|
1512
|
+
isAllDay={false}
|
|
1513
|
+
boxHeight={draggedRef.current.h}
|
|
1514
|
+
ampm={ampm}
|
|
1515
|
+
onPress={() => {}}
|
|
1516
|
+
theme={theme}
|
|
1517
|
+
boxProps={slot("eventBox")}
|
|
1518
|
+
/>
|
|
1519
|
+
)}
|
|
1520
|
+
</div>
|
|
1521
|
+
) : null}
|
|
1238
1522
|
</div>
|
|
1239
1523
|
);
|
|
1240
1524
|
}
|