@super-calendar/dom 2.8.2 → 2.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/TimeGrid.tsx CHANGED
@@ -1,4 +1,12 @@
1
- import { addDays, addMinutes, format, getISOWeek, type Locale, startOfDay } from "date-fns";
1
+ import {
2
+ addDays,
3
+ addMinutes,
4
+ differenceInCalendarDays,
5
+ format,
6
+ getISOWeek,
7
+ type Locale,
8
+ startOfDay,
9
+ } from "date-fns";
2
10
  import {
3
11
  type ComponentType,
4
12
  type CSSProperties,
@@ -17,6 +25,7 @@ import {
17
25
  type CalendarEvent,
18
26
  type CalendarMode,
19
27
  cellRangeFromDrag,
28
+ clampMoveStartMinutes,
20
29
  pageStepDays,
21
30
  backgroundBandsForDay,
22
31
  closedHourBands,
@@ -33,6 +42,7 @@ import {
33
42
  isWeekend,
34
43
  layoutDayEvents,
35
44
  overlapsOtherEvents,
45
+ type PositionedEvent,
36
46
  useNow,
37
47
  type TimeGridMode,
38
48
  titleNumberOfLines,
@@ -69,6 +79,7 @@ export type TimeGridSlot =
69
79
  | "createGhost";
70
80
 
71
81
  const GUTTER_WIDTH = 56;
82
+ const HOURS_PER_DAY = 24;
72
83
  const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));
73
84
 
74
85
  // Edge auto-advance: how close to the day-columns edge (px) counts as "at the
@@ -249,6 +260,13 @@ type DragState = {
249
260
  dayDelta: number;
250
261
  /** Pixel equivalent of dayDelta, applied as a transform on the dragged box. */
251
262
  dayOffsetPx: number;
263
+ /** Whether the dragged segment carries on from the previous day / into the next. */
264
+ continuesBefore: boolean;
265
+ continuesAfter: boolean;
266
+ /** Hours of a move that run past the end of the day, previewed in the next column. */
267
+ spillHours: number;
268
+ /** Column that previews the spill, or -1 when there is none to show. */
269
+ spillDayIndex: number;
252
270
  moved: boolean;
253
271
  };
254
272
 
@@ -416,7 +434,10 @@ export function TimeGrid<T = unknown>({
416
434
  const slot = createSlots<TimeGridSlot>({ classNames, styles });
417
435
  const scrollRef = useRef<HTMLDivElement>(null);
418
436
  const dfns = locale ? { locale } : undefined;
419
- const snapHours = dragStepMinutes / 60;
437
+ // Clamped like the native renderer: a zero or negative step would divide by zero
438
+ // in the snap below and put NaN through every drag commit.
439
+ const snapMinutes = Math.max(1, dragStepMinutes);
440
+ const snapHours = snapMinutes / 60;
420
441
 
421
442
  // The visible hour window [windowStart, windowEnd). Clamped the same way as the
422
443
  // native renderer so out-of-range props can't invert or overflow the day.
@@ -449,6 +470,12 @@ export function TimeGrid<T = unknown>({
449
470
  durationHours: number;
450
471
  dayIndex: number;
451
472
  dayWidth: number;
473
+ // The dragged *segment*'s bounds when the gesture began. A multi-day event is
474
+ // laid out as one segment per day, so the commit shifts the real event by how
475
+ // far its segment moved rather than rebuilding the event from the segment
476
+ // (which would truncate everything outside the dragged day).
477
+ segmentStart: Date;
478
+ segmentEnd: Date;
452
479
  } | null>(null);
453
480
  const applyDrag = (next: DragState | null) => {
454
481
  dragRef.current = next;
@@ -638,13 +665,15 @@ export function TimeGrid<T = unknown>({
638
665
  const dHours = (e.clientY - dragOrigin.current.pointerY) / hourHeightRef.current;
639
666
  const snap = (v: number) => Math.round(v / snapHours) * snapHours;
640
667
  if (d.kind === "move") {
641
- // Guard the upper bound: an event taller than the window would otherwise
642
- // invert the clamp (`windowEnd - duration < windowStart`) and teleport.
643
- const startHours = clamp(
644
- snap(dragOrigin.current.startHours + dHours),
645
- windowStart,
646
- Math.max(windowStart, windowEnd - d.durationHours),
647
- );
668
+ // Only the start is held inside the window; the end may run past the end of
669
+ // the day, continuing on the next one (see `clampMoveStartMinutes`). A
670
+ // segment that continues *before* is skipped: its 00:00 top edge is where the
671
+ // layout clipped the event, not where the event starts, so holding it there
672
+ // would make the tail of a midnight-spanning event impossible to drag earlier.
673
+ const raw = snap(dragOrigin.current.startHours + dHours);
674
+ const startHours = d.continuesBefore
675
+ ? raw
676
+ : clampMoveStartMinutes(raw * 60, windowStart, windowEnd, snapMinutes) / 60;
648
677
  // Map the horizontal drag to whole day columns, clamped so the in-view box
649
678
  // can't leave the visible range (mirrors the native renderer). Once paged,
650
679
  // the drop day comes from the hit-test instead.
@@ -652,7 +681,35 @@ export function TimeGrid<T = unknown>({
652
681
  const rawDayDelta = o.dayWidth > 0 ? Math.round((e.clientX - o.pointerX) / o.dayWidth) : 0;
653
682
  const targetDay = clamp(o.dayIndex + rawDayDelta, 0, daysRef.current.length - 1);
654
683
  const dayDelta = targetDay - o.dayIndex;
655
- applyDrag({ ...d, startHours, dayDelta, dayOffsetPx: dayDelta * o.dayWidth, moved: true });
684
+ // Preview the part that runs past *midnight* at the top of the next column.
685
+ // Measured against the end of the day, not `windowEnd`: a narrowed window
686
+ // (say 8–18) leaves plenty of events hanging below the last visible hour
687
+ // without them reaching the next day at all.
688
+ const spillHours = Math.min(
689
+ Math.max(startHours + d.durationHours - HOURS_PER_DAY, 0),
690
+ windowHours,
691
+ );
692
+ const nextDay = daysRef.current[targetDay + 1];
693
+ const spillDayIndex =
694
+ spillHours > 0 &&
695
+ nextDay &&
696
+ // Only when that column really is the next calendar day (`hiddenDays` can
697
+ // break the run), otherwise the spill lands off-view and isn't shown.
698
+ differenceInCalendarDays(nextDay, daysRef.current[targetDay]) === 1 &&
699
+ // A segment that already continues after owns a real next-day box; a
700
+ // preview on top of it would just double up.
701
+ !d.continuesAfter
702
+ ? targetDay + 1
703
+ : -1;
704
+ applyDrag({
705
+ ...d,
706
+ startHours,
707
+ dayDelta,
708
+ dayOffsetPx: dayDelta * o.dayWidth,
709
+ spillHours,
710
+ spillDayIndex,
711
+ moved: true,
712
+ });
656
713
  // Follow the pointer with the floating ghost once the origin column is gone.
657
714
  const g = ghostRef.current;
658
715
  const held = draggedRef.current;
@@ -705,7 +762,8 @@ export function TimeGrid<T = unknown>({
705
762
  held?.onPress();
706
763
  return;
707
764
  }
708
- const originIndex = dragOrigin.current?.dayIndex ?? 0;
765
+ const origin = dragOrigin.current;
766
+ const originIndex = origin?.dayIndex ?? 0;
709
767
  let base: Date | null = null;
710
768
  if (d.kind === "move") {
711
769
  if (pagedRef.current) {
@@ -723,10 +781,23 @@ export function TimeGrid<T = unknown>({
723
781
  } else {
724
782
  base = startOfDay(daysRef.current[originIndex] ?? daysRef.current[0]);
725
783
  }
726
- if (base && held) {
727
- const start = addMinutes(base, Math.round(d.startHours * 60));
728
- const end = addMinutes(base, Math.round((d.startHours + d.durationHours) * 60));
729
- onDragEventRef.current?.(held.event, start, end);
784
+ if (base && held && origin) {
785
+ // Shift the real event by how far its segment moved, so a move that now
786
+ // spans midnight (and every later drag of the resulting multi-day event)
787
+ // keeps the parts outside the dragged day instead of clipping to it.
788
+ const segmentStart = addMinutes(base, Math.round(d.startHours * 60));
789
+ const segmentEnd = addMinutes(base, Math.round((d.startHours + d.durationHours) * 60));
790
+ const startShift =
791
+ d.kind === "resize" ? 0 : segmentStart.getTime() - origin.segmentStart.getTime();
792
+ const endShift =
793
+ d.kind === "move"
794
+ ? startShift
795
+ : d.kind === "resize-start"
796
+ ? 0
797
+ : segmentEnd.getTime() - origin.segmentEnd.getTime();
798
+ const start = new Date(held.event.start.getTime() + startShift);
799
+ const end = new Date(held.event.end.getTime() + endShift);
800
+ if (end > start) onDragEventRef.current?.(held.event, start, end);
730
801
  }
731
802
  finishDrag();
732
803
  };
@@ -735,17 +806,19 @@ export function TimeGrid<T = unknown>({
735
806
  finishDrag();
736
807
  };
737
808
 
809
+ // Takes the laid-out segment whole rather than five of its fields: the two
810
+ // `continues*` flags are the same type, so passing them positionally invites a
811
+ // silent transposition.
738
812
  const beginDrag = (
739
813
  e: ReactPointerEvent,
740
- event: CalendarEvent<T>,
814
+ pe: PositionedEvent<T>,
741
815
  key: string,
742
816
  kind: "move" | "resize" | "resize-start",
743
- startHours: number,
744
- durationHours: number,
745
817
  dayIndex: number,
746
818
  onPress?: () => void,
747
819
  ) => {
748
820
  if (!onDragEvent) return;
821
+ const { event, startHours, durationHours, continuesBefore, continuesAfter } = pe;
749
822
  e.stopPropagation();
750
823
  try {
751
824
  (e.target as HTMLElement).setPointerCapture?.(e.pointerId);
@@ -759,6 +832,7 @@ export function TimeGrid<T = unknown>({
759
832
  const column = kind === "move" ? boxEl.parentElement : null;
760
833
  const dayWidth = column ? column.getBoundingClientRect().width : 0;
761
834
  const boxRect = kind === "move" ? boxEl.getBoundingClientRect() : null;
835
+ const originBase = startOfDay(days[dayIndex] ?? days[0]);
762
836
  dragOrigin.current = {
763
837
  pointerX: e.clientX,
764
838
  pointerY: e.clientY,
@@ -766,6 +840,8 @@ export function TimeGrid<T = unknown>({
766
840
  durationHours,
767
841
  dayIndex,
768
842
  dayWidth,
843
+ segmentStart: addMinutes(originBase, Math.round(startHours * 60)),
844
+ segmentEnd: addMinutes(originBase, Math.round((startHours + durationHours) * 60)),
769
845
  };
770
846
  draggedRef.current = {
771
847
  event,
@@ -780,7 +856,19 @@ export function TimeGrid<T = unknown>({
780
856
  pagedRef.current = false;
781
857
  setPaged(false);
782
858
  clearEdgeTimer();
783
- applyDrag({ key, kind, startHours, durationHours, dayDelta: 0, dayOffsetPx: 0, moved: false });
859
+ applyDrag({
860
+ key,
861
+ kind,
862
+ startHours,
863
+ durationHours,
864
+ continuesBefore,
865
+ continuesAfter,
866
+ dayDelta: 0,
867
+ dayOffsetPx: 0,
868
+ spillHours: 0,
869
+ spillDayIndex: -1,
870
+ moved: false,
871
+ });
784
872
  onDragStart?.(event);
785
873
  // Transport the rest of the drag at the document level so it survives the day
786
874
  // columns unmounting on a page change (the dragged box's own node disappears).
@@ -841,10 +929,10 @@ export function TimeGrid<T = unknown>({
841
929
  const moved = Math.abs(endPx - o.startPx) > 4;
842
930
  const h = hourHeightRef.current;
843
931
  if (moved && onCreateEvent) {
844
- const range = cellRangeFromDrag(day, o.startPx, endPx, h, windowStart, dragStepMinutes);
932
+ const range = cellRangeFromDrag(day, o.startPx, endPx, h, windowStart, snapMinutes);
845
933
  if (range) onCreateEvent(range.start, range.end);
846
934
  } else if (onPressCell) {
847
- const at = cellRangeFromDrag(day, o.startPx, o.startPx, h, windowStart, dragStepMinutes);
935
+ const at = cellRangeFromDrag(day, o.startPx, o.startPx, h, windowStart, snapMinutes);
848
936
  if (at) onPressCell(at.start);
849
937
  }
850
938
  createOrigin.current = null;
@@ -865,6 +953,59 @@ export function TimeGrid<T = unknown>({
865
953
  [days, events],
866
954
  );
867
955
 
956
+ // Keep the original boxes mounted for pointer capture, but replace every
957
+ // segment of a moving multi-day event with the same shifted range.
958
+ const moveOrigin = dragOrigin.current;
959
+ const heldEvent = draggedRef.current?.event;
960
+ const movingEvent =
961
+ heldEvent &&
962
+ (events.includes(heldEvent)
963
+ ? heldEvent
964
+ : (events.find(
965
+ (event) =>
966
+ event.start.getTime() === heldEvent.start.getTime() &&
967
+ event.end.getTime() === heldEvent.end.getTime() &&
968
+ event.title === heldEvent.title,
969
+ ) ?? heldEvent));
970
+ const multiDayMove =
971
+ !paged &&
972
+ drag?.kind === "move" &&
973
+ (drag.continuesBefore || drag.continuesAfter) &&
974
+ moveOrigin &&
975
+ movingEvent
976
+ ? (() => {
977
+ const target = days[moveOrigin.dayIndex + drag.dayDelta];
978
+ const segmentStart = addMinutes(startOfDay(target), Math.round(drag.startHours * 60));
979
+ const shift = segmentStart.getTime() - moveOrigin.segmentStart.getTime();
980
+ return {
981
+ ...movingEvent,
982
+ start: new Date(movingEvent.start.getTime() + shift),
983
+ end: new Date(movingEvent.end.getTime() + shift),
984
+ allDay: false,
985
+ };
986
+ })()
987
+ : null;
988
+
989
+ // The tail of an in-progress move that runs past midnight, shown on `dayIndex`
990
+ // so the drop is visible on both days at once. `drag` and `draggedRef` are set
991
+ // together in `beginDrag` and cleared together in `finishDrag`, so the guard on
992
+ // the state keeps the ref read consistent.
993
+ const spillPreviewFor = (dayIndex: number): DomRenderEventArgs<T> | null => {
994
+ if (paged || !drag || drag.kind !== "move" || drag.spillDayIndex !== dayIndex) return null;
995
+ const dragged = draggedRef.current?.event;
996
+ if (!dragged) return null;
997
+ return {
998
+ event: dragged,
999
+ mode,
1000
+ isAllDay: false,
1001
+ boxHeight: Math.max(drag.spillHours * hourHeight, 14),
1002
+ continuesBefore: true,
1003
+ continuesAfter: false,
1004
+ ampm,
1005
+ onPress: () => {},
1006
+ };
1007
+ };
1008
+
868
1009
  // Arrow-key navigation across events (`keyboardEventNavigation`). This is purely
869
1010
  // additive: every event stays a tab stop (so screen-reader users keep full
870
1011
  // access), and the arrow keys are a convenience for sighted keyboard users. It's
@@ -1234,6 +1375,13 @@ export function TimeGrid<T = unknown>({
1234
1375
  const nowTop = (nowHours - windowStart) * hourHeight;
1235
1376
  const bands = bandsByDay[dayIndex];
1236
1377
  const ghost = createBox?.dayIndex === dayIndex ? createBox : null;
1378
+ // The tail of a move that runs past midnight, previewed at the top of
1379
+ // this column. The dragged event is looked up from its `drag.key`
1380
+ // ("<originColumn>:<index>"), so no extra state has to track it.
1381
+ const spill = multiDayMove ? null : spillPreviewFor(dayIndex);
1382
+ const moveSegment = multiDayMove
1383
+ ? layoutDayEvents<T>([multiDayMove], day)[0]
1384
+ : undefined;
1237
1385
  return (
1238
1386
  <div
1239
1387
  key={day.toISOString()}
@@ -1315,7 +1463,11 @@ export function TimeGrid<T = unknown>({
1315
1463
  // Not while paged: after a page change the origin box is gone and
1316
1464
  // the ghost stands in, so a same-slot event on the new page (e.g. a
1317
1465
  // recurring one) must not also render lifted.
1318
- const active = !paged && drag?.key === key ? drag : null;
1466
+ const active =
1467
+ !paged && drag?.key === key && (!multiDayMove || pe.event === movingEvent)
1468
+ ? drag
1469
+ : null;
1470
+ const hiddenForMove = !!multiDayMove && pe.event === movingEvent;
1319
1471
  const startHours = active ? active.startHours : pe.startHours;
1320
1472
  const durationHours = active ? active.durationHours : pe.durationHours;
1321
1473
  // Drop events that fall entirely outside the visible window; those
@@ -1326,7 +1478,14 @@ export function TimeGrid<T = unknown>({
1326
1478
  )
1327
1479
  return null;
1328
1480
  const top = (startHours - windowStart) * hourHeight;
1329
- const boxHeight = Math.max(durationHours * hourHeight, 14);
1481
+ // A drag that runs past midnight is cut off at the day boundary
1482
+ // (a move previews the remainder in the next column). Applied to
1483
+ // resting boxes too, where `layoutDayEvents` has already clipped
1484
+ // them to the day, so the height doesn't jump on commit.
1485
+ const boxHeight = Math.max(
1486
+ Math.min(durationHours, HOURS_PER_DAY - startHours) * hourHeight,
1487
+ 14,
1488
+ );
1330
1489
  const widthPct = 100 / pe.columns;
1331
1490
  const onPress = () => onPressEvent?.(pe.event);
1332
1491
  const args: DomRenderEventArgs<T> = {
@@ -1379,21 +1538,11 @@ export function TimeGrid<T = unknown>({
1379
1538
  // remounting on a page change.
1380
1539
  onPointerDown={
1381
1540
  canMove
1382
- ? (e) =>
1383
- beginDrag(
1384
- e,
1385
- pe.event,
1386
- key,
1387
- "move",
1388
- pe.startHours,
1389
- pe.durationHours,
1390
- dayIndex,
1391
- onPress,
1392
- )
1541
+ ? (e) => beginDrag(e, pe, key, "move", dayIndex, onPress)
1393
1542
  : undefined
1394
1543
  }
1395
1544
  onClick={canMove ? undefined : onPress}
1396
- {...dataState({ "data-dragging": !!active })}
1545
+ {...dataState({ "data-dragging": !!active && !hiddenForMove })}
1397
1546
  {...slot("event", {
1398
1547
  base: {
1399
1548
  position: "absolute",
@@ -1409,6 +1558,7 @@ export function TimeGrid<T = unknown>({
1409
1558
  touchAction: canMove ? "none" : "auto",
1410
1559
  zIndex: active ? 3 : 1,
1411
1560
  opacity: active ? 0.85 : 1,
1561
+ visibility: hiddenForMove ? "hidden" : undefined,
1412
1562
  // Snap the dragged box over the target day column so the
1413
1563
  // drop location is visible before release.
1414
1564
  ...(active && active.dayOffsetPx !== 0
@@ -1426,15 +1576,7 @@ export function TimeGrid<T = unknown>({
1426
1576
  <div
1427
1577
  onPointerDown={(e) => {
1428
1578
  e.stopPropagation();
1429
- beginDrag(
1430
- e,
1431
- pe.event,
1432
- key,
1433
- "resize-start",
1434
- pe.startHours,
1435
- pe.durationHours,
1436
- dayIndex,
1437
- );
1579
+ beginDrag(e, pe, key, "resize-start", dayIndex);
1438
1580
  }}
1439
1581
  style={{
1440
1582
  position: "absolute",
@@ -1447,19 +1589,16 @@ export function TimeGrid<T = unknown>({
1447
1589
  }}
1448
1590
  />
1449
1591
  ) : null}
1450
- {canResize ? (
1592
+ {/* Only the segment that owns the real end may be resized
1593
+ from the bottom (matching the native renderer). On a
1594
+ continued segment the bottom edge is the day boundary,
1595
+ not the event's end, so dragging it would move an end
1596
+ the user can't see. */}
1597
+ {canResize && !pe.continuesAfter ? (
1451
1598
  <div
1452
1599
  onPointerDown={(e) => {
1453
1600
  e.stopPropagation();
1454
- beginDrag(
1455
- e,
1456
- pe.event,
1457
- key,
1458
- "resize",
1459
- pe.startHours,
1460
- pe.durationHours,
1461
- dayIndex,
1462
- );
1601
+ beginDrag(e, pe, key, "resize", dayIndex);
1463
1602
  }}
1464
1603
  style={{
1465
1604
  position: "absolute",
@@ -1475,6 +1614,70 @@ export function TimeGrid<T = unknown>({
1475
1614
  </div>
1476
1615
  );
1477
1616
  })}
1617
+ {moveSegment ? (
1618
+ <div
1619
+ aria-hidden
1620
+ {...dataState({ "data-dragging": true })}
1621
+ {...slot("event", {
1622
+ base: {
1623
+ position: "absolute",
1624
+ top: (moveSegment.startHours - windowStart) * hourHeight,
1625
+ left: 1,
1626
+ right: 1,
1627
+ height: Math.max(moveSegment.durationHours * hourHeight, 14),
1628
+ pointerEvents: "none",
1629
+ zIndex: 3,
1630
+ opacity: 0.85,
1631
+ },
1632
+ })}
1633
+ >
1634
+ {(() => {
1635
+ const args: DomRenderEventArgs<T> = {
1636
+ event: moveSegment.event,
1637
+ mode,
1638
+ isAllDay: false,
1639
+ boxHeight: Math.max(moveSegment.durationHours * hourHeight, 14),
1640
+ continuesBefore: moveSegment.continuesBefore,
1641
+ continuesAfter: moveSegment.continuesAfter,
1642
+ ampm,
1643
+ onPress: () => {},
1644
+ };
1645
+ return Renderer ? (
1646
+ <Renderer {...args} />
1647
+ ) : (
1648
+ <DefaultDomEvent {...args} theme={theme} boxProps={slot("eventBox")} />
1649
+ );
1650
+ })()}
1651
+ </div>
1652
+ ) : null}
1653
+ {spill ? (
1654
+ <div
1655
+ aria-hidden
1656
+ {...dataState({ "data-dragging": true })}
1657
+ {...slot("event", {
1658
+ base: {
1659
+ position: "absolute",
1660
+ // Anchored at midnight, which sits `windowStart` hours above
1661
+ // the top of the grid. With a window that starts after 00:00
1662
+ // the preview is scrolled out of view, exactly like the
1663
+ // continuation the drop will commit.
1664
+ top: -windowStart * hourHeight,
1665
+ left: 1,
1666
+ right: 1,
1667
+ height: spill.boxHeight,
1668
+ pointerEvents: "none",
1669
+ zIndex: 3,
1670
+ opacity: 0.85,
1671
+ },
1672
+ })}
1673
+ >
1674
+ {Renderer ? (
1675
+ <Renderer {...spill} />
1676
+ ) : (
1677
+ <DefaultDomEvent {...spill} theme={theme} boxProps={slot("eventBox")} />
1678
+ )}
1679
+ </div>
1680
+ ) : null}
1478
1681
  {showNow ? (
1479
1682
  <div
1480
1683
  {...slot("nowIndicator", {