@super-calendar/dom 2.13.0 → 2.14.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,6 +1,5 @@
1
1
  import {
2
2
  addDays,
3
- addMinutes,
4
3
  differenceInCalendarDays,
5
4
  format,
6
5
  getISOWeek,
@@ -13,6 +12,7 @@ import {
13
12
  type ForwardedRef,
14
13
  forwardRef,
15
14
  type KeyboardEvent as ReactKeyboardEvent,
15
+ type MouseEvent as ReactMouseEvent,
16
16
  type PointerEvent as ReactPointerEvent,
17
17
  type ReactElement,
18
18
  type ReactNode,
@@ -31,6 +31,9 @@ import {
31
31
  type CalendarMode,
32
32
  cellRangeFromDrag,
33
33
  clampMoveStartMinutes,
34
+ resolveDraggedBounds,
35
+ movedClockSpan,
36
+ shiftMinutes,
34
37
  pageStepDays,
35
38
  backgroundBandsForDay,
36
39
  closedHourBands,
@@ -59,6 +62,7 @@ import {
59
62
  } from "@super-calendar/core";
60
63
  import { createSlots, dataState, type ResolvedSlot, type SlotStyleProps } from "./slots";
61
64
  import { type DomCalendarTheme, mergeDomTheme } from "./theme";
65
+ import { type EventGrab, waitForHold } from "./touch";
62
66
 
63
67
  /**
64
68
  * Styleable parts of {@link TimeGrid}. Pass a class or inline style per slot via
@@ -101,6 +105,7 @@ const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v
101
105
  const EDGE_ZONE = 32;
102
106
  const EDGE_DWELL_MS = 600;
103
107
  const EDGE_REPEAT_MS = 600;
108
+ const CELL_TOUCH_SLOP_PX = 8;
104
109
 
105
110
  // Off-screen but readable by assistive tech: gives an element an accessible name
106
111
  // without changing the visible layout.
@@ -177,6 +182,10 @@ export interface TimeGridProps<T = unknown> extends SlotStyleProps<TimeGridSlot>
177
182
  eventGap?: number;
178
183
  /** Snap dragged events to this many minutes (default 15). */
179
184
  dragStepMinutes?: number;
185
+ /** Hold duration before a DOM touch or pen drag, in ms (default 300). */
186
+ touchDragDelay?: number;
187
+ /** Movement tolerance for a DOM touch or pen hold, in px (default 8). */
188
+ touchDragThreshold?: number;
180
189
  /** Render event time ranges in 12-hour AM/PM (default false, 24h). */
181
190
  ampm?: boolean;
182
191
  /** Sub-divisions per hour for the grid lines, e.g. 2 for half-hour (default 1). */
@@ -292,6 +301,10 @@ type DragState = {
292
301
  // top edge (changes the start, end fixed).
293
302
  kind: "move" | "resize" | "resize-start";
294
303
  startHours: number;
304
+ // The row a move asked for. `startHours` is where it lands, an hour later when
305
+ // that row is in the hour a spring-forward change skips; the commit re-resolves
306
+ // the request against the day it drops on.
307
+ requestedStartHours: number;
295
308
  durationHours: number;
296
309
  /** Whole day columns the box has been dragged across, clamped to the view. */
297
310
  dayDelta: number;
@@ -313,8 +326,25 @@ type DragState = {
313
326
  // both. The time reserves two lines (it can wrap on a narrow column).
314
327
  const DOM_TITLE_LINE_HEIGHT = 16;
315
328
  const DOM_TIME_LINE_HEIGHT = 30;
329
+
316
330
  const DOM_BOX_PADDING_V = 2;
317
331
 
332
+ // Where a dragged segment's start lands: shifted on the clock by as many minutes
333
+ // as its row moved, plus whole days to `base`, from the instant it started at.
334
+ // Shifting the real instant rather than rebuilding it from rows keeps a start in
335
+ // the hour a fall-back change repeats on its own occurrence.
336
+ function movedSegmentStart(
337
+ origin: { startHours: number; segmentStart: Date; base: Date },
338
+ base: Date,
339
+ startHours: number,
340
+ ): Date {
341
+ const dayMinutes = differenceInCalendarDays(base, origin.base) * HOURS_PER_DAY * 60;
342
+ return shiftMinutes(
343
+ origin.segmentStart,
344
+ dayMinutes + Math.round((startHours - origin.startHours) * 60),
345
+ );
346
+ }
347
+
318
348
  function DefaultDomEvent<T>({
319
349
  event,
320
350
  mode,
@@ -437,6 +467,8 @@ function TimeGridInner<T = unknown>(
437
467
  minEventHeight = DEFAULT_MIN_EVENT_HEIGHT,
438
468
  eventGap = DEFAULT_EVENT_GAP,
439
469
  dragStepMinutes = 15,
470
+ touchDragDelay = 300,
471
+ touchDragThreshold = 8,
440
472
  ampm = false,
441
473
  timeslots = 1,
442
474
  minHour = 0,
@@ -517,6 +549,7 @@ function TimeGridInner<T = unknown>(
517
549
  const dragOrigin = useRef<{
518
550
  pointerX: number;
519
551
  pointerY: number;
552
+ pointerType: string;
520
553
  startHours: number;
521
554
  durationHours: number;
522
555
  dayIndex: number;
@@ -527,6 +560,8 @@ function TimeGridInner<T = unknown>(
527
560
  // (which would truncate everything outside the dragged day).
528
561
  segmentStart: Date;
529
562
  segmentEnd: Date;
563
+ // Midnight of the day the segment was grabbed on.
564
+ base: Date;
530
565
  } | null>(null);
531
566
  const applyDrag = (next: DragState | null) => {
532
567
  dragRef.current = next;
@@ -541,6 +576,9 @@ function TimeGridInner<T = unknown>(
541
576
  heightPx: number;
542
577
  } | null>(null);
543
578
  const createOrigin = useRef<{ el: HTMLElement; dayIndex: number; startPx: number } | null>(null);
579
+ // Touch never sweeps, so a tap presses the cell from its click, which the
580
+ // browser skips when the finger scrolled the grid instead.
581
+ const cellTouchRef = useRef<{ pointerId: number; x: number; y: number } | null>(null);
544
582
  const cellEnabled = !!onPressCell || !!onCreateEvent;
545
583
 
546
584
  const days = useMemo(
@@ -569,6 +607,13 @@ function TimeGridInner<T = unknown>(
569
607
  h: number;
570
608
  } | null>(null);
571
609
  const ghostRef = useRef<HTMLDivElement>(null);
610
+ // A held touch or pen drag stops touchmove from scrolling the grid.
611
+ const touchDragRef = useRef(false);
612
+ const activeGrabRef = useRef<EventGrab | null>(null);
613
+ // Cancels a touch or pen press that has not become a drag yet.
614
+ const cancelHoldRef = useRef<(() => void) | null>(null);
615
+ // A touch or pen tap ended on an event; its click presses the event.
616
+ const tappedRef = useRef<HTMLElement | null>(null);
572
617
  const docHandlersRef = useRef<{
573
618
  move: (e: PointerEvent) => void;
574
619
  up: (e: PointerEvent) => void;
@@ -643,6 +688,30 @@ function TimeGridInner<T = unknown>(
643
688
  return () => el.removeEventListener("wheel", onWheel);
644
689
  }, [zoomable, minHourHeight, maxHourHeight]);
645
690
 
691
+ // Browser touch-action is fixed for the gesture; cancel touchmove only during a held drag.
692
+ useEffect(() => {
693
+ const el = scrollRef.current;
694
+ if (!el) {
695
+ return;
696
+ }
697
+ const onTouchMove = (e: TouchEvent) => {
698
+ if (touchDragRef.current && e.cancelable) {
699
+ e.preventDefault();
700
+ }
701
+ };
702
+ const onContextMenu = (e: Event) => {
703
+ if (touchDragRef.current || cancelHoldRef.current) {
704
+ e.preventDefault();
705
+ }
706
+ };
707
+ el.addEventListener("touchmove", onTouchMove, { passive: false });
708
+ el.addEventListener("contextmenu", onContextMenu);
709
+ return () => {
710
+ el.removeEventListener("touchmove", onTouchMove);
711
+ el.removeEventListener("contextmenu", onContextMenu);
712
+ };
713
+ }, []);
714
+
646
715
  const pinch = useRef<Map<number, number>>(new Map());
647
716
  const pinchBase = useRef<{ dist: number; height: number } | null>(null);
648
717
  const onBodyPointerDown = (e: ReactPointerEvent) => {
@@ -717,6 +786,8 @@ function TimeGridInner<T = unknown>(
717
786
  edgeSide.current = 0;
718
787
  pagedRef.current = false;
719
788
  setPaged(false);
789
+ touchDragRef.current = false;
790
+ activeGrabRef.current = null;
720
791
  draggedRef.current = null;
721
792
  dragOrigin.current = null;
722
793
  applyDrag(null);
@@ -725,6 +796,16 @@ function TimeGridInner<T = unknown>(
725
796
  const moveDrag = (e: PointerEvent) => {
726
797
  const d = dragRef.current;
727
798
  if (!d || !dragOrigin.current) return;
799
+ if (
800
+ touchDragRef.current &&
801
+ !d.moved &&
802
+ Math.hypot(
803
+ e.clientX - dragOrigin.current.pointerX,
804
+ e.clientY - dragOrigin.current.pointerY,
805
+ ) <= touchDragThreshold
806
+ ) {
807
+ return;
808
+ }
728
809
  ptr.current = { x: e.clientX, y: e.clientY };
729
810
  // Read the live row height (ref, not the state closure) so a mid-drag zoom
730
811
  // keeps the math aligned with what's on screen.
@@ -737,7 +818,7 @@ function TimeGridInner<T = unknown>(
737
818
  // layout clipped the event, not where the event starts, so holding it there
738
819
  // would make the tail of a midnight-spanning event impossible to drag earlier.
739
820
  const raw = snap(dragOrigin.current.startHours + dHours);
740
- const startHours = d.continuesBefore
821
+ const requestedHours = d.continuesBefore
741
822
  ? raw
742
823
  : clampMoveStartMinutes(raw * 60, windowStart, windowEnd, snapMinutes) / 60;
743
824
  // Map the horizontal drag to whole day columns, clamped so the in-view box
@@ -747,12 +828,27 @@ function TimeGridInner<T = unknown>(
747
828
  const rawDayDelta = o.dayWidth > 0 ? Math.round((e.clientX - o.pointerX) / o.dayWidth) : 0;
748
829
  const targetDay = clamp(o.dayIndex + rawDayDelta, 0, daysRef.current.length - 1);
749
830
  const dayDelta = targetDay - o.dayIndex;
831
+ // Draw the box where the drop commits it: a start in the hour a
832
+ // spring-forward change skips moves on past it, and the kept elapsed
833
+ // duration covers a different number of rows once a DST change falls
834
+ // inside it or stops falling inside it.
835
+ const dayMinutes =
836
+ differenceInCalendarDays(startOfDay(daysRef.current[targetDay] ?? o.base), o.base) *
837
+ HOURS_PER_DAY *
838
+ 60;
839
+ const moved = movedClockSpan(
840
+ o.segmentStart,
841
+ o.segmentEnd,
842
+ dayMinutes + Math.round((requestedHours - o.startHours) * 60),
843
+ );
844
+ const startHours = o.startHours + moved.startHours - dayMinutes / 60;
845
+ const durationHours = moved.hours;
750
846
  // Preview the part that runs past *midnight* at the top of the next column.
751
847
  // Measured against the end of the day, not `windowEnd`: a narrowed window
752
848
  // (say 8–18) leaves plenty of events hanging below the last visible hour
753
849
  // without them reaching the next day at all.
754
850
  const spillHours = Math.min(
755
- Math.max(startHours + d.durationHours - HOURS_PER_DAY, 0),
851
+ Math.max(startHours + durationHours - HOURS_PER_DAY, 0),
756
852
  windowHours,
757
853
  );
758
854
  const nextDay = daysRef.current[targetDay + 1];
@@ -770,6 +866,8 @@ function TimeGridInner<T = unknown>(
770
866
  applyDrag({
771
867
  ...d,
772
868
  startHours,
869
+ requestedStartHours: requestedHours,
870
+ durationHours,
773
871
  dayDelta,
774
872
  dayOffsetPx: dayDelta * o.dayWidth,
775
873
  spillHours,
@@ -784,7 +882,7 @@ function TimeGridInner<T = unknown>(
784
882
  }
785
883
  // Edge auto-advance: dwell near a horizontal edge to page the view. Read the
786
884
  // columns rect live so a mid-drag scroll/layout shift can't stale it.
787
- if (edgeAdvanceEnabled) {
885
+ if (edgeAdvanceEnabled && o.pointerType === "mouse") {
788
886
  const rect = columnsRef.current?.getBoundingClientRect();
789
887
  if (rect && rect.right > rect.left) {
790
888
  const left = rect.left + gutterWidth;
@@ -818,14 +916,20 @@ function TimeGridInner<T = unknown>(
818
916
  const d = dragRef.current;
819
917
  if (!d) return;
820
918
  try {
821
- (e.target as HTMLElement)?.releasePointerCapture?.(e.pointerId);
919
+ activeGrabRef.current?.box.releasePointerCapture?.(e.pointerId);
822
920
  } catch {
823
921
  // Best-effort release; ignore if the capture was never granted.
824
922
  }
825
923
  const held = draggedRef.current;
826
924
  if (!d.moved) {
925
+ const wasTouch = touchDragRef.current;
926
+ const tapBox = activeGrabRef.current?.box ?? null;
827
927
  finishDrag();
828
- held?.onPress();
928
+ if (wasTouch) {
929
+ tappedRef.current = tapBox;
930
+ } else {
931
+ held?.onPress();
932
+ }
829
933
  return;
830
934
  }
831
935
  const origin = dragOrigin.current;
@@ -847,20 +951,26 @@ function TimeGridInner<T = unknown>(
847
951
  } else {
848
952
  base = startOfDay(daysRef.current[originIndex] ?? daysRef.current[0]);
849
953
  }
850
- if (base && held && origin) {
954
+ if (held && origin && d.kind === "resize") {
955
+ // Resolve the end like the native renderer, from the edge the layout drew,
956
+ // so it lands at the row the edge was dragged to on either side of a DST
957
+ // change.
958
+ const endDelta = Math.round(
959
+ (d.startHours + d.durationHours - origin.startHours - origin.durationHours) * 60,
960
+ );
961
+ const next = resolveDraggedBounds(held.event.start, held.event.end, 0, endDelta, snapMinutes);
962
+ if (next) onDragEventRef.current?.(held.event, next.start, next.end);
963
+ } else if (base && held && origin) {
851
964
  // Shift the real event by how far its segment moved, so a move that now
852
965
  // spans midnight (and every later drag of the resulting multi-day event)
853
966
  // keeps the parts outside the dragged day instead of clipping to it.
854
- const segmentStart = addMinutes(base, Math.round(d.startHours * 60));
855
- const segmentEnd = addMinutes(base, Math.round((d.startHours + d.durationHours) * 60));
856
- const startShift =
857
- d.kind === "resize" ? 0 : segmentStart.getTime() - origin.segmentStart.getTime();
858
- const endShift =
859
- d.kind === "move"
860
- ? startShift
861
- : d.kind === "resize-start"
862
- ? 0
863
- : segmentEnd.getTime() - origin.segmentEnd.getTime();
967
+ const segmentStart = movedSegmentStart(
968
+ origin,
969
+ base,
970
+ d.kind === "move" ? d.requestedStartHours : d.startHours,
971
+ );
972
+ const startShift = segmentStart.getTime() - origin.segmentStart.getTime();
973
+ const endShift = d.kind === "move" ? startShift : 0;
864
974
  const start = new Date(held.event.start.getTime() + startShift);
865
975
  const end = new Date(held.event.end.getTime() + endShift);
866
976
  if (end > start) onDragEventRef.current?.(held.event, start, end);
@@ -876,38 +986,44 @@ function TimeGridInner<T = unknown>(
876
986
  // `continues*` flags are the same type, so passing them positionally invites a
877
987
  // silent transposition.
878
988
  const beginDrag = (
879
- e: ReactPointerEvent,
989
+ e: EventGrab,
880
990
  pe: PositionedEvent<T>,
881
991
  key: string,
882
992
  kind: "move" | "resize" | "resize-start",
883
993
  dayIndex: number,
884
994
  onPress?: () => void,
885
995
  ) => {
886
- if (!onDragEvent) return;
887
996
  const { event, startHours, durationHours, continuesBefore, continuesAfter } = pe;
888
- e.stopPropagation();
889
997
  try {
890
- (e.target as HTMLElement).setPointerCapture?.(e.pointerId);
998
+ e.box.setPointerCapture?.(e.pointerId);
891
999
  } catch {
892
1000
  // Pointer capture is best-effort; some environments reject it.
893
1001
  }
1002
+ touchDragRef.current = e.pointerType !== "mouse";
1003
+ activeGrabRef.current = e;
894
1004
  // The event box sits inside its day column, so the parent's width is one day;
895
1005
  // columns are equal-width, so measuring one is enough. Resizes never move
896
1006
  // across days, so 0 is fine there.
897
- const boxEl = e.currentTarget as HTMLElement;
1007
+ const boxEl = e.box;
898
1008
  const column = kind === "move" ? boxEl.parentElement : null;
899
1009
  const dayWidth = column ? column.getBoundingClientRect().width : 0;
900
1010
  const boxRect = kind === "move" ? boxEl.getBoundingClientRect() : null;
901
1011
  const originBase = startOfDay(days[dayIndex] ?? days[0]);
1012
+ const originNextDay = addDays(originBase, 1);
1013
+ const segmentStart = event.start > originBase ? event.start : originBase;
902
1014
  dragOrigin.current = {
903
1015
  pointerX: e.clientX,
904
1016
  pointerY: e.clientY,
1017
+ pointerType: e.pointerType,
905
1018
  startHours,
906
1019
  durationHours,
907
1020
  dayIndex,
908
1021
  dayWidth,
909
- segmentStart: addMinutes(originBase, Math.round(startHours * 60)),
910
- segmentEnd: addMinutes(originBase, Math.round((startHours + durationHours) * 60)),
1022
+ // The segment's real edges rather than ones rebuilt from its rows: in the
1023
+ // hour a fall-back change repeats, one row maps to two instants.
1024
+ segmentStart,
1025
+ segmentEnd: event.end < originNextDay ? event.end : originNextDay,
1026
+ base: originBase,
911
1027
  };
912
1028
  draggedRef.current = {
913
1029
  event,
@@ -926,6 +1042,7 @@ function TimeGridInner<T = unknown>(
926
1042
  key,
927
1043
  kind,
928
1044
  startHours,
1045
+ requestedStartHours: startHours,
929
1046
  durationHours,
930
1047
  continuesBefore,
931
1048
  continuesAfter,
@@ -940,26 +1057,98 @@ function TimeGridInner<T = unknown>(
940
1057
  // columns unmounting on a page change (the dragged box's own node disappears).
941
1058
  // Detach any prior set first so a second beginDrag can't leak listeners.
942
1059
  detachDocListeners();
943
- const handlers = { move: moveDrag, up: endDrag, cancel: cancelDrag };
1060
+ const handlers = {
1061
+ move: (event: PointerEvent) => {
1062
+ if (event.pointerId === e.pointerId) {
1063
+ moveDrag(event);
1064
+ }
1065
+ },
1066
+ up: (event: PointerEvent) => {
1067
+ if (event.pointerId === e.pointerId) {
1068
+ endDrag(event);
1069
+ }
1070
+ },
1071
+ cancel: (event: PointerEvent) => {
1072
+ if (event.pointerId === e.pointerId) {
1073
+ cancelDrag();
1074
+ }
1075
+ },
1076
+ };
944
1077
  docHandlersRef.current = handlers;
945
1078
  document.addEventListener("pointermove", handlers.move);
946
1079
  document.addEventListener("pointerup", handlers.up);
947
1080
  document.addEventListener("pointercancel", handlers.cancel);
948
1081
  };
949
1082
 
1083
+ const pressEvent = (
1084
+ e: ReactPointerEvent<HTMLElement>,
1085
+ pe: PositionedEvent<T>,
1086
+ key: string,
1087
+ kind: "move" | "resize" | "resize-start",
1088
+ dayIndex: number,
1089
+ onPress?: () => void,
1090
+ ) => {
1091
+ if (!onDragEvent || e.button > 0) {
1092
+ return;
1093
+ }
1094
+ e.stopPropagation();
1095
+ const hadPendingHold = cancelHoldRef.current !== null;
1096
+ cancelHoldRef.current?.();
1097
+ tappedRef.current = null;
1098
+ if (hadPendingHold || activeGrabRef.current || (e.pointerType !== "mouse" && !e.isPrimary)) {
1099
+ return;
1100
+ }
1101
+ const grab: EventGrab = {
1102
+ box: e.currentTarget,
1103
+ pointerId: e.pointerId,
1104
+ pointerType: e.pointerType,
1105
+ clientX: e.clientX,
1106
+ clientY: e.clientY,
1107
+ };
1108
+ if (e.pointerType === "mouse") {
1109
+ beginDrag(grab, pe, key, kind, dayIndex, onPress);
1110
+ return;
1111
+ }
1112
+ cancelHoldRef.current = waitForHold(
1113
+ grab,
1114
+ touchDragDelay,
1115
+ touchDragThreshold,
1116
+ () => {
1117
+ cancelHoldRef.current = null;
1118
+ beginDrag(grab, pe, key, kind, dayIndex, onPress);
1119
+ },
1120
+ () => {
1121
+ cancelHoldRef.current = null;
1122
+ tappedRef.current = grab.box;
1123
+ },
1124
+ () => {
1125
+ cancelHoldRef.current = null;
1126
+ },
1127
+ );
1128
+ };
1129
+
950
1130
  // Remove any lingering document listeners / dwell timer if we unmount mid-drag.
951
1131
  useEffect(
952
1132
  () => () => {
1133
+ cancelHoldRef.current?.();
953
1134
  detachDocListeners();
954
1135
  clearEdgeTimer();
955
1136
  },
956
1137
  [],
957
1138
  );
958
1139
 
1140
+ useEffect(() => {
1141
+ cancelHoldRef.current?.();
1142
+ }, [date, events, onDragEvent]);
1143
+
959
1144
  const pxFromTop = (el: HTMLElement, clientY: number) => clientY - el.getBoundingClientRect().top;
960
1145
  const beginCreate = (e: ReactPointerEvent, dayIndex: number) => {
961
1146
  // Mouse / pen only: on touch the column must stay free to scroll the grid.
962
1147
  // Only from the column background, primary button, never on an event.
1148
+ cellTouchRef.current =
1149
+ e.pointerType === "touch" && e.target === e.currentTarget
1150
+ ? { pointerId: e.pointerId, x: e.clientX, y: e.clientY }
1151
+ : null;
963
1152
  if (!cellEnabled || e.pointerType === "touch") return;
964
1153
  if (e.target !== e.currentTarget || e.button > 0) return;
965
1154
  const el = e.currentTarget as HTMLElement;
@@ -973,6 +1162,14 @@ function TimeGridInner<T = unknown>(
973
1162
  setCreateBox({ dayIndex, topPx: startPx, heightPx: 0 });
974
1163
  };
975
1164
  const moveCreate = (e: ReactPointerEvent) => {
1165
+ const touch = cellTouchRef.current;
1166
+ if (
1167
+ touch &&
1168
+ e.pointerId === touch.pointerId &&
1169
+ Math.hypot(e.clientX - touch.x, e.clientY - touch.y) > CELL_TOUCH_SLOP_PX
1170
+ ) {
1171
+ cellTouchRef.current = null;
1172
+ }
976
1173
  const o = createOrigin.current;
977
1174
  if (!o) return;
978
1175
  const cur = pxFromTop(o.el, e.clientY);
@@ -1004,9 +1201,23 @@ function TimeGridInner<T = unknown>(
1004
1201
  createOrigin.current = null;
1005
1202
  setCreateBox(null);
1006
1203
  };
1204
+ const tapCell = (e: ReactMouseEvent<HTMLDivElement>, dayIndex: number) => {
1205
+ const touch = cellTouchRef.current;
1206
+ cellTouchRef.current = null;
1207
+ const day = days[dayIndex];
1208
+ if (!touch || !onPressCell || !day || e.target !== e.currentTarget) {
1209
+ return;
1210
+ }
1211
+ const px = pxFromTop(e.currentTarget, e.clientY);
1212
+ const at = cellRangeFromDrag(day, px, px, hourHeightRef.current, windowStart, snapMinutes);
1213
+ if (at) {
1214
+ onPressCell(at.start);
1215
+ }
1216
+ };
1007
1217
  // A gesture the browser/OS cancels (scroll takeover, etc.) must not commit a
1008
1218
  // create — just drop the in-progress state.
1009
1219
  const cancelCreate = () => {
1220
+ cellTouchRef.current = null;
1010
1221
  createOrigin.current = null;
1011
1222
  setCreateBox(null);
1012
1223
  };
@@ -1041,7 +1252,11 @@ function TimeGridInner<T = unknown>(
1041
1252
  movingEvent
1042
1253
  ? (() => {
1043
1254
  const target = days[moveOrigin.dayIndex + drag.dayDelta];
1044
- const segmentStart = addMinutes(startOfDay(target), Math.round(drag.startHours * 60));
1255
+ const segmentStart = movedSegmentStart(
1256
+ moveOrigin,
1257
+ startOfDay(target),
1258
+ drag.requestedStartHours,
1259
+ );
1045
1260
  const shift = segmentStart.getTime() - moveOrigin.segmentStart.getTime();
1046
1261
  return {
1047
1262
  ...movingEvent,
@@ -1470,6 +1685,7 @@ function TimeGridInner<T = unknown>(
1470
1685
  onPointerMove={cellEnabled ? moveCreate : undefined}
1471
1686
  onPointerUp={cellEnabled ? endCreate : undefined}
1472
1687
  onPointerCancel={cellEnabled ? cancelCreate : undefined}
1688
+ onClick={cellEnabled ? (e) => tapCell(e, dayIndex) : undefined}
1473
1689
  {...dataState({ "data-today": getIsToday(day), "data-weekend": isWeekend(day) })}
1474
1690
  {...slot("dayColumn", {
1475
1691
  base: { flex: 1, position: "relative" },
@@ -1639,10 +1855,22 @@ function TimeGridInner<T = unknown>(
1639
1855
  // remounting on a page change.
1640
1856
  onPointerDown={
1641
1857
  canMove
1642
- ? (e) => beginDrag(e, pe, key, "move", dayIndex, onPress)
1858
+ ? (e) => pressEvent(e, pe, key, "move", dayIndex, onPress)
1643
1859
  : undefined
1644
1860
  }
1645
- onClick={canMove ? undefined : onPress}
1861
+ onClick={
1862
+ canMove
1863
+ ? (e) => {
1864
+ if (
1865
+ e.detail === 0 ||
1866
+ (tappedRef.current && e.currentTarget.contains(tappedRef.current))
1867
+ ) {
1868
+ tappedRef.current = null;
1869
+ onPress();
1870
+ }
1871
+ }
1872
+ : onPress
1873
+ }
1646
1874
  {...dataState({ "data-dragging": !!active && !hiddenForMove })}
1647
1875
  {...slot("event", {
1648
1876
  base: {
@@ -1656,7 +1884,9 @@ function TimeGridInner<T = unknown>(
1656
1884
  left: `calc(${pe.column * widthPct}% + ${eventGap}px)`,
1657
1885
  width: `calc(${widthPct}% - ${eventGap * 2}px)`,
1658
1886
  cursor: canMove ? "grab" : "pointer",
1659
- touchAction: canMove ? "none" : "auto",
1887
+ touchAction: canMove ? "pan-y" : "auto",
1888
+ userSelect: canMove ? "none" : undefined,
1889
+ WebkitTouchCallout: canMove ? "none" : undefined,
1660
1890
  zIndex: active ? 3 : 1,
1661
1891
  opacity: active ? 0.85 : 1,
1662
1892
  visibility: hiddenForMove ? "hidden" : undefined,
@@ -1679,8 +1909,7 @@ function TimeGridInner<T = unknown>(
1679
1909
  {canResize && !pe.continuesBefore ? (
1680
1910
  <div
1681
1911
  onPointerDown={(e) => {
1682
- e.stopPropagation();
1683
- beginDrag(
1912
+ pressEvent(
1684
1913
  e,
1685
1914
  pe,
1686
1915
  key,
@@ -1696,7 +1925,7 @@ function TimeGridInner<T = unknown>(
1696
1925
  top: 0,
1697
1926
  height: 8,
1698
1927
  cursor: "ns-resize",
1699
- touchAction: "none",
1928
+ touchAction: "pan-y",
1700
1929
  }}
1701
1930
  />
1702
1931
  ) : null}
@@ -1708,8 +1937,7 @@ function TimeGridInner<T = unknown>(
1708
1937
  {canResize && !pe.continuesAfter ? (
1709
1938
  <div
1710
1939
  onPointerDown={(e) => {
1711
- e.stopPropagation();
1712
- beginDrag(
1940
+ pressEvent(
1713
1941
  e,
1714
1942
  pe,
1715
1943
  key,
@@ -1725,7 +1953,7 @@ function TimeGridInner<T = unknown>(
1725
1953
  bottom: 0,
1726
1954
  height: 8,
1727
1955
  cursor: "ns-resize",
1728
- touchAction: "none",
1956
+ touchAction: "pan-y",
1729
1957
  }}
1730
1958
  />
1731
1959
  ) : null}
package/src/touch.ts ADDED
@@ -0,0 +1,61 @@
1
+ export type EventGrab = {
2
+ readonly box: HTMLElement;
3
+ readonly pointerId: number;
4
+ readonly pointerType: string;
5
+ readonly clientX: number;
6
+ readonly clientY: number;
7
+ };
8
+
9
+ export function waitForHold(
10
+ grab: EventGrab,
11
+ delay: number,
12
+ threshold: number,
13
+ onHold: () => void,
14
+ onTap: () => void,
15
+ onCancel: () => void,
16
+ ): () => void {
17
+ const detach = () => {
18
+ clearTimeout(timer);
19
+ document.removeEventListener("pointermove", move);
20
+ document.removeEventListener("pointerup", up);
21
+ document.removeEventListener("pointercancel", cancelPointer);
22
+ document.removeEventListener("pointerdown", anotherPointer);
23
+ };
24
+ const cancel = () => {
25
+ detach();
26
+ onCancel();
27
+ };
28
+ const move = (event: PointerEvent) => {
29
+ if (
30
+ event.pointerId === grab.pointerId &&
31
+ Math.hypot(event.clientX - grab.clientX, event.clientY - grab.clientY) > threshold
32
+ ) {
33
+ cancel();
34
+ }
35
+ };
36
+ const up = (event: PointerEvent) => {
37
+ if (event.pointerId === grab.pointerId) {
38
+ detach();
39
+ onTap();
40
+ }
41
+ };
42
+ const cancelPointer = (event: PointerEvent) => {
43
+ if (event.pointerId === grab.pointerId) {
44
+ cancel();
45
+ }
46
+ };
47
+ const anotherPointer = (event: PointerEvent) => {
48
+ if (event.pointerId !== grab.pointerId) {
49
+ cancel();
50
+ }
51
+ };
52
+ const timer = setTimeout(() => {
53
+ detach();
54
+ onHold();
55
+ }, delay);
56
+ document.addEventListener("pointermove", move);
57
+ document.addEventListener("pointerup", up);
58
+ document.addEventListener("pointercancel", cancelPointer);
59
+ document.addEventListener("pointerdown", anotherPointer);
60
+ return cancel;
61
+ }