@super-calendar/dom 2.8.2 → 2.9.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/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,19 @@ 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 === "resize-start" ? 0 : segmentEnd.getTime() - origin.segmentEnd.getTime();
794
+ const start = new Date(held.event.start.getTime() + startShift);
795
+ const end = new Date(held.event.end.getTime() + endShift);
796
+ if (end > start) onDragEventRef.current?.(held.event, start, end);
730
797
  }
731
798
  finishDrag();
732
799
  };
@@ -735,17 +802,19 @@ export function TimeGrid<T = unknown>({
735
802
  finishDrag();
736
803
  };
737
804
 
805
+ // Takes the laid-out segment whole rather than five of its fields: the two
806
+ // `continues*` flags are the same type, so passing them positionally invites a
807
+ // silent transposition.
738
808
  const beginDrag = (
739
809
  e: ReactPointerEvent,
740
- event: CalendarEvent<T>,
810
+ pe: PositionedEvent<T>,
741
811
  key: string,
742
812
  kind: "move" | "resize" | "resize-start",
743
- startHours: number,
744
- durationHours: number,
745
813
  dayIndex: number,
746
814
  onPress?: () => void,
747
815
  ) => {
748
816
  if (!onDragEvent) return;
817
+ const { event, startHours, durationHours, continuesBefore, continuesAfter } = pe;
749
818
  e.stopPropagation();
750
819
  try {
751
820
  (e.target as HTMLElement).setPointerCapture?.(e.pointerId);
@@ -759,6 +828,7 @@ export function TimeGrid<T = unknown>({
759
828
  const column = kind === "move" ? boxEl.parentElement : null;
760
829
  const dayWidth = column ? column.getBoundingClientRect().width : 0;
761
830
  const boxRect = kind === "move" ? boxEl.getBoundingClientRect() : null;
831
+ const originBase = startOfDay(days[dayIndex] ?? days[0]);
762
832
  dragOrigin.current = {
763
833
  pointerX: e.clientX,
764
834
  pointerY: e.clientY,
@@ -766,6 +836,8 @@ export function TimeGrid<T = unknown>({
766
836
  durationHours,
767
837
  dayIndex,
768
838
  dayWidth,
839
+ segmentStart: addMinutes(originBase, Math.round(startHours * 60)),
840
+ segmentEnd: addMinutes(originBase, Math.round((startHours + durationHours) * 60)),
769
841
  };
770
842
  draggedRef.current = {
771
843
  event,
@@ -780,7 +852,19 @@ export function TimeGrid<T = unknown>({
780
852
  pagedRef.current = false;
781
853
  setPaged(false);
782
854
  clearEdgeTimer();
783
- applyDrag({ key, kind, startHours, durationHours, dayDelta: 0, dayOffsetPx: 0, moved: false });
855
+ applyDrag({
856
+ key,
857
+ kind,
858
+ startHours,
859
+ durationHours,
860
+ continuesBefore,
861
+ continuesAfter,
862
+ dayDelta: 0,
863
+ dayOffsetPx: 0,
864
+ spillHours: 0,
865
+ spillDayIndex: -1,
866
+ moved: false,
867
+ });
784
868
  onDragStart?.(event);
785
869
  // Transport the rest of the drag at the document level so it survives the day
786
870
  // columns unmounting on a page change (the dragged box's own node disappears).
@@ -841,10 +925,10 @@ export function TimeGrid<T = unknown>({
841
925
  const moved = Math.abs(endPx - o.startPx) > 4;
842
926
  const h = hourHeightRef.current;
843
927
  if (moved && onCreateEvent) {
844
- const range = cellRangeFromDrag(day, o.startPx, endPx, h, windowStart, dragStepMinutes);
928
+ const range = cellRangeFromDrag(day, o.startPx, endPx, h, windowStart, snapMinutes);
845
929
  if (range) onCreateEvent(range.start, range.end);
846
930
  } else if (onPressCell) {
847
- const at = cellRangeFromDrag(day, o.startPx, o.startPx, h, windowStart, dragStepMinutes);
931
+ const at = cellRangeFromDrag(day, o.startPx, o.startPx, h, windowStart, snapMinutes);
848
932
  if (at) onPressCell(at.start);
849
933
  }
850
934
  createOrigin.current = null;
@@ -865,6 +949,26 @@ export function TimeGrid<T = unknown>({
865
949
  [days, events],
866
950
  );
867
951
 
952
+ // The tail of an in-progress move that runs past midnight, shown on `dayIndex`
953
+ // so the drop is visible on both days at once. `drag` and `draggedRef` are set
954
+ // together in `beginDrag` and cleared together in `finishDrag`, so the guard on
955
+ // the state keeps the ref read consistent.
956
+ const spillPreviewFor = (dayIndex: number): DomRenderEventArgs<T> | null => {
957
+ if (paged || !drag || drag.kind !== "move" || drag.spillDayIndex !== dayIndex) return null;
958
+ const dragged = draggedRef.current?.event;
959
+ if (!dragged) return null;
960
+ return {
961
+ event: dragged,
962
+ mode,
963
+ isAllDay: false,
964
+ boxHeight: Math.max(drag.spillHours * hourHeight, 14),
965
+ continuesBefore: true,
966
+ continuesAfter: false,
967
+ ampm,
968
+ onPress: () => {},
969
+ };
970
+ };
971
+
868
972
  // Arrow-key navigation across events (`keyboardEventNavigation`). This is purely
869
973
  // additive: every event stays a tab stop (so screen-reader users keep full
870
974
  // access), and the arrow keys are a convenience for sighted keyboard users. It's
@@ -1234,6 +1338,10 @@ export function TimeGrid<T = unknown>({
1234
1338
  const nowTop = (nowHours - windowStart) * hourHeight;
1235
1339
  const bands = bandsByDay[dayIndex];
1236
1340
  const ghost = createBox?.dayIndex === dayIndex ? createBox : null;
1341
+ // The tail of a move that runs past midnight, previewed at the top of
1342
+ // this column. The dragged event is looked up from its `drag.key`
1343
+ // ("<originColumn>:<index>"), so no extra state has to track it.
1344
+ const spill = spillPreviewFor(dayIndex);
1237
1345
  return (
1238
1346
  <div
1239
1347
  key={day.toISOString()}
@@ -1326,7 +1434,14 @@ export function TimeGrid<T = unknown>({
1326
1434
  )
1327
1435
  return null;
1328
1436
  const top = (startHours - windowStart) * hourHeight;
1329
- const boxHeight = Math.max(durationHours * hourHeight, 14);
1437
+ // A drag that runs past midnight is cut off at the day boundary
1438
+ // (a move previews the remainder in the next column). Applied to
1439
+ // resting boxes too, where `layoutDayEvents` has already clipped
1440
+ // them to the day, so the height doesn't jump on commit.
1441
+ const boxHeight = Math.max(
1442
+ Math.min(durationHours, HOURS_PER_DAY - startHours) * hourHeight,
1443
+ 14,
1444
+ );
1330
1445
  const widthPct = 100 / pe.columns;
1331
1446
  const onPress = () => onPressEvent?.(pe.event);
1332
1447
  const args: DomRenderEventArgs<T> = {
@@ -1379,17 +1494,7 @@ export function TimeGrid<T = unknown>({
1379
1494
  // remounting on a page change.
1380
1495
  onPointerDown={
1381
1496
  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
- )
1497
+ ? (e) => beginDrag(e, pe, key, "move", dayIndex, onPress)
1393
1498
  : undefined
1394
1499
  }
1395
1500
  onClick={canMove ? undefined : onPress}
@@ -1426,15 +1531,7 @@ export function TimeGrid<T = unknown>({
1426
1531
  <div
1427
1532
  onPointerDown={(e) => {
1428
1533
  e.stopPropagation();
1429
- beginDrag(
1430
- e,
1431
- pe.event,
1432
- key,
1433
- "resize-start",
1434
- pe.startHours,
1435
- pe.durationHours,
1436
- dayIndex,
1437
- );
1534
+ beginDrag(e, pe, key, "resize-start", dayIndex);
1438
1535
  }}
1439
1536
  style={{
1440
1537
  position: "absolute",
@@ -1447,19 +1544,16 @@ export function TimeGrid<T = unknown>({
1447
1544
  }}
1448
1545
  />
1449
1546
  ) : null}
1450
- {canResize ? (
1547
+ {/* Only the segment that owns the real end may be resized
1548
+ from the bottom (matching the native renderer). On a
1549
+ continued segment the bottom edge is the day boundary,
1550
+ not the event's end, so dragging it would move an end
1551
+ the user can't see. */}
1552
+ {canResize && !pe.continuesAfter ? (
1451
1553
  <div
1452
1554
  onPointerDown={(e) => {
1453
1555
  e.stopPropagation();
1454
- beginDrag(
1455
- e,
1456
- pe.event,
1457
- key,
1458
- "resize",
1459
- pe.startHours,
1460
- pe.durationHours,
1461
- dayIndex,
1462
- );
1556
+ beginDrag(e, pe, key, "resize", dayIndex);
1463
1557
  }}
1464
1558
  style={{
1465
1559
  position: "absolute",
@@ -1475,6 +1569,34 @@ export function TimeGrid<T = unknown>({
1475
1569
  </div>
1476
1570
  );
1477
1571
  })}
1572
+ {spill ? (
1573
+ <div
1574
+ aria-hidden
1575
+ {...dataState({ "data-dragging": true })}
1576
+ {...slot("event", {
1577
+ base: {
1578
+ position: "absolute",
1579
+ // Anchored at midnight, which sits `windowStart` hours above
1580
+ // the top of the grid. With a window that starts after 00:00
1581
+ // the preview is scrolled out of view, exactly like the
1582
+ // continuation the drop will commit.
1583
+ top: -windowStart * hourHeight,
1584
+ left: 1,
1585
+ right: 1,
1586
+ height: spill.boxHeight,
1587
+ pointerEvents: "none",
1588
+ zIndex: 3,
1589
+ opacity: 0.85,
1590
+ },
1591
+ })}
1592
+ >
1593
+ {Renderer ? (
1594
+ <Renderer {...spill} />
1595
+ ) : (
1596
+ <DefaultDomEvent {...spill} theme={theme} boxProps={slot("eventBox")} />
1597
+ )}
1598
+ </div>
1599
+ ) : null}
1478
1600
  {showNow ? (
1479
1601
  <div
1480
1602
  {...slot("nowIndicator", {