@super-calendar/dom 2.12.1 → 2.14.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
@@ -10,11 +10,16 @@ import {
10
10
  import {
11
11
  type ComponentType,
12
12
  type CSSProperties,
13
+ type ForwardedRef,
14
+ forwardRef,
13
15
  type KeyboardEvent as ReactKeyboardEvent,
16
+ type MouseEvent as ReactMouseEvent,
14
17
  type PointerEvent as ReactPointerEvent,
15
18
  type ReactElement,
16
19
  type ReactNode,
20
+ type RefAttributes,
17
21
  useEffect,
22
+ useImperativeHandle,
18
23
  useMemo,
19
24
  useRef,
20
25
  useState,
@@ -23,6 +28,7 @@ import {
23
28
  type BusinessHours,
24
29
  type BusinessHoursBand,
25
30
  type CalendarEvent,
31
+ type CalendarHandle,
26
32
  type CalendarMode,
27
33
  cellRangeFromDrag,
28
34
  clampMoveStartMinutes,
@@ -54,6 +60,7 @@ import {
54
60
  } from "@super-calendar/core";
55
61
  import { createSlots, dataState, type ResolvedSlot, type SlotStyleProps } from "./slots";
56
62
  import { type DomCalendarTheme, mergeDomTheme } from "./theme";
63
+ import { type EventGrab, waitForHold } from "./touch";
57
64
 
58
65
  /**
59
66
  * Styleable parts of {@link TimeGrid}. Pass a class or inline style per slot via
@@ -96,6 +103,7 @@ const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v
96
103
  const EDGE_ZONE = 32;
97
104
  const EDGE_DWELL_MS = 600;
98
105
  const EDGE_REPEAT_MS = 600;
106
+ const CELL_TOUCH_SLOP_PX = 8;
99
107
 
100
108
  // Off-screen but readable by assistive tech: gives an element an accessible name
101
109
  // without changing the visible layout.
@@ -151,7 +159,7 @@ export interface TimeGridProps<T = unknown> extends SlotStyleProps<TimeGridSlot>
151
159
  weekdayFormat?: WeekdayFormat;
152
160
  /** Initial pixels per hour (default 48). */
153
161
  hourHeight?: number;
154
- /** Initial scroll position, in minutes from midnight (default 8:00). */
162
+ /** Vertical scroll, in minutes from midnight (default 8:00). Applied on mount and whenever it changes. */
155
163
  scrollOffsetMinutes?: number;
156
164
  /** Pinch / Ctrl-⌘-scroll to zoom the grid (default true). */
157
165
  zoomable?: boolean;
@@ -172,6 +180,10 @@ export interface TimeGridProps<T = unknown> extends SlotStyleProps<TimeGridSlot>
172
180
  eventGap?: number;
173
181
  /** Snap dragged events to this many minutes (default 15). */
174
182
  dragStepMinutes?: number;
183
+ /** Hold duration before a DOM touch or pen drag, in ms (default 300). */
184
+ touchDragDelay?: number;
185
+ /** Movement tolerance for a DOM touch or pen hold, in px (default 8). */
186
+ touchDragThreshold?: number;
175
187
  /** Render event time ranges in 12-hour AM/PM (default false, 24h). */
176
188
  ampm?: boolean;
177
189
  /** Sub-divisions per hour for the grid lines, e.g. 2 for half-hour (default 1). */
@@ -412,61 +424,70 @@ function DefaultDomEvent<T>({
412
424
  * <TimeGrid mode="week" date={new Date()} events={events} />
413
425
  * ```
414
426
  */
415
- export function TimeGrid<T = unknown>({
416
- date,
417
- events = [],
418
- mode = "day",
419
- numberOfDays = 1,
420
- weekStartsOn = 0,
421
- weekdayFormat = "short",
422
- hourHeight: initialHourHeight = 48,
423
- scrollOffsetMinutes = 8 * 60,
424
- zoomable = true,
425
- minHourHeight = 24,
426
- maxHourHeight = 160,
427
- minEventHeight = DEFAULT_MIN_EVENT_HEIGHT,
428
- eventGap = DEFAULT_EVENT_GAP,
429
- dragStepMinutes = 15,
430
- ampm = false,
431
- timeslots = 1,
432
- minHour = 0,
433
- maxHour = 24,
434
- hideHours = false,
435
- showWeekNumber = false,
436
- weekNumberPrefix = "W",
437
- hiddenDays,
438
- keyboardEventNavigation = false,
439
- businessHours,
440
- renderBusinessHours,
441
- renderBackgroundEvent,
442
- showNowIndicator = true,
443
- highlightWeekends = true,
444
- eventStartEditable = true,
445
- eventDurationEditable = true,
446
- eventOverlap = true,
447
- now: nowProp,
448
- timeZone,
449
- showAllDayEventCell = true,
450
- showAllDayLabel = false,
451
- labels: labelsProp,
452
- locale,
453
- theme: themeOverrides,
454
- height = 600,
455
- renderEvent,
456
- eventAccessibilityLabel,
457
- hourComponent,
458
- onPressEvent,
459
- onPressDateHeader,
460
- onPressCell,
461
- onCreateEvent,
462
- onDragStart,
463
- onDragEvent,
464
- onChangeDate,
465
- className,
466
- style,
467
- classNames,
468
- styles,
469
- }: TimeGridProps<T>): ReactElement {
427
+ export const TimeGrid = forwardRef(TimeGridInner) as <T = unknown>(
428
+ props: TimeGridProps<T> & RefAttributes<CalendarHandle>,
429
+ ) => ReactElement;
430
+
431
+ function TimeGridInner<T = unknown>(
432
+ {
433
+ date,
434
+ events = [],
435
+ mode = "day",
436
+ numberOfDays = 1,
437
+ weekStartsOn = 0,
438
+ weekdayFormat = "short",
439
+ hourHeight: initialHourHeight = 48,
440
+ scrollOffsetMinutes = 8 * 60,
441
+ zoomable = true,
442
+ minHourHeight = 24,
443
+ maxHourHeight = 160,
444
+ minEventHeight = DEFAULT_MIN_EVENT_HEIGHT,
445
+ eventGap = DEFAULT_EVENT_GAP,
446
+ dragStepMinutes = 15,
447
+ touchDragDelay = 300,
448
+ touchDragThreshold = 8,
449
+ ampm = false,
450
+ timeslots = 1,
451
+ minHour = 0,
452
+ maxHour = 24,
453
+ hideHours = false,
454
+ showWeekNumber = false,
455
+ weekNumberPrefix = "W",
456
+ hiddenDays,
457
+ keyboardEventNavigation = false,
458
+ businessHours,
459
+ renderBusinessHours,
460
+ renderBackgroundEvent,
461
+ showNowIndicator = true,
462
+ highlightWeekends = true,
463
+ eventStartEditable = true,
464
+ eventDurationEditable = true,
465
+ eventOverlap = true,
466
+ now: nowProp,
467
+ timeZone,
468
+ showAllDayEventCell = true,
469
+ showAllDayLabel = false,
470
+ labels: labelsProp,
471
+ locale,
472
+ theme: themeOverrides,
473
+ height = 600,
474
+ renderEvent,
475
+ eventAccessibilityLabel,
476
+ hourComponent,
477
+ onPressEvent,
478
+ onPressDateHeader,
479
+ onPressCell,
480
+ onCreateEvent,
481
+ onDragStart,
482
+ onDragEvent,
483
+ onChangeDate,
484
+ className,
485
+ style,
486
+ classNames,
487
+ styles,
488
+ }: TimeGridProps<T>,
489
+ ref: ForwardedRef<CalendarHandle>,
490
+ ): ReactElement {
470
491
  const theme = useMemo(() => mergeDomTheme(themeOverrides), [themeOverrides]);
471
492
  const labels = useMemo(() => resolveCalendarLabels(labelsProp), [labelsProp]);
472
493
  const slot = createSlots<TimeGridSlot>({ classNames, styles });
@@ -505,6 +526,7 @@ export function TimeGrid<T = unknown>({
505
526
  const dragOrigin = useRef<{
506
527
  pointerX: number;
507
528
  pointerY: number;
529
+ pointerType: string;
508
530
  startHours: number;
509
531
  durationHours: number;
510
532
  dayIndex: number;
@@ -529,6 +551,9 @@ export function TimeGrid<T = unknown>({
529
551
  heightPx: number;
530
552
  } | null>(null);
531
553
  const createOrigin = useRef<{ el: HTMLElement; dayIndex: number; startPx: number } | null>(null);
554
+ // Touch never sweeps, so a tap presses the cell from its click, which the
555
+ // browser skips when the finger scrolled the grid instead.
556
+ const cellTouchRef = useRef<{ pointerId: number; x: number; y: number } | null>(null);
532
557
  const cellEnabled = !!onPressCell || !!onCreateEvent;
533
558
 
534
559
  const days = useMemo(
@@ -557,6 +582,13 @@ export function TimeGrid<T = unknown>({
557
582
  h: number;
558
583
  } | null>(null);
559
584
  const ghostRef = useRef<HTMLDivElement>(null);
585
+ // A held touch or pen drag stops touchmove from scrolling the grid.
586
+ const touchDragRef = useRef(false);
587
+ const activeGrabRef = useRef<EventGrab | null>(null);
588
+ // Cancels a touch or pen press that has not become a drag yet.
589
+ const cancelHoldRef = useRef<(() => void) | null>(null);
590
+ // A touch or pen tap ended on an event; its click presses the event.
591
+ const tappedRef = useRef<HTMLElement | null>(null);
560
592
  const docHandlersRef = useRef<{
561
593
  move: (e: PointerEvent) => void;
562
594
  up: (e: PointerEvent) => void;
@@ -602,6 +634,21 @@ export function TimeGrid<T = unknown>({
602
634
  );
603
635
  }, [scrollOffsetMinutes, windowStart]);
604
636
 
637
+ useImperativeHandle(
638
+ ref,
639
+ () => ({
640
+ scrollToTime: (minutes, { animated = true } = {}) => {
641
+ const el = scrollRef.current;
642
+ if (!el) return;
643
+ const top = Math.max(0, (minutes / 60 - windowStart) * hourHeightRef.current);
644
+ const smooth = animated && !window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
645
+ if (smooth && typeof el.scrollTo === "function") el.scrollTo({ top, behavior: "smooth" });
646
+ else el.scrollTop = top;
647
+ },
648
+ }),
649
+ [windowStart],
650
+ );
651
+
605
652
  // Zoom: Ctrl/⌘ + wheel (native listener so we can preventDefault), plus
606
653
  // two-pointer pinch. Both scale hourHeight about the current view.
607
654
  useEffect(() => {
@@ -616,6 +663,30 @@ export function TimeGrid<T = unknown>({
616
663
  return () => el.removeEventListener("wheel", onWheel);
617
664
  }, [zoomable, minHourHeight, maxHourHeight]);
618
665
 
666
+ // Browser touch-action is fixed for the gesture; cancel touchmove only during a held drag.
667
+ useEffect(() => {
668
+ const el = scrollRef.current;
669
+ if (!el) {
670
+ return;
671
+ }
672
+ const onTouchMove = (e: TouchEvent) => {
673
+ if (touchDragRef.current && e.cancelable) {
674
+ e.preventDefault();
675
+ }
676
+ };
677
+ const onContextMenu = (e: Event) => {
678
+ if (touchDragRef.current || cancelHoldRef.current) {
679
+ e.preventDefault();
680
+ }
681
+ };
682
+ el.addEventListener("touchmove", onTouchMove, { passive: false });
683
+ el.addEventListener("contextmenu", onContextMenu);
684
+ return () => {
685
+ el.removeEventListener("touchmove", onTouchMove);
686
+ el.removeEventListener("contextmenu", onContextMenu);
687
+ };
688
+ }, []);
689
+
619
690
  const pinch = useRef<Map<number, number>>(new Map());
620
691
  const pinchBase = useRef<{ dist: number; height: number } | null>(null);
621
692
  const onBodyPointerDown = (e: ReactPointerEvent) => {
@@ -690,6 +761,8 @@ export function TimeGrid<T = unknown>({
690
761
  edgeSide.current = 0;
691
762
  pagedRef.current = false;
692
763
  setPaged(false);
764
+ touchDragRef.current = false;
765
+ activeGrabRef.current = null;
693
766
  draggedRef.current = null;
694
767
  dragOrigin.current = null;
695
768
  applyDrag(null);
@@ -698,6 +771,16 @@ export function TimeGrid<T = unknown>({
698
771
  const moveDrag = (e: PointerEvent) => {
699
772
  const d = dragRef.current;
700
773
  if (!d || !dragOrigin.current) return;
774
+ if (
775
+ touchDragRef.current &&
776
+ !d.moved &&
777
+ Math.hypot(
778
+ e.clientX - dragOrigin.current.pointerX,
779
+ e.clientY - dragOrigin.current.pointerY,
780
+ ) <= touchDragThreshold
781
+ ) {
782
+ return;
783
+ }
701
784
  ptr.current = { x: e.clientX, y: e.clientY };
702
785
  // Read the live row height (ref, not the state closure) so a mid-drag zoom
703
786
  // keeps the math aligned with what's on screen.
@@ -757,7 +840,7 @@ export function TimeGrid<T = unknown>({
757
840
  }
758
841
  // Edge auto-advance: dwell near a horizontal edge to page the view. Read the
759
842
  // columns rect live so a mid-drag scroll/layout shift can't stale it.
760
- if (edgeAdvanceEnabled) {
843
+ if (edgeAdvanceEnabled && o.pointerType === "mouse") {
761
844
  const rect = columnsRef.current?.getBoundingClientRect();
762
845
  if (rect && rect.right > rect.left) {
763
846
  const left = rect.left + gutterWidth;
@@ -791,14 +874,20 @@ export function TimeGrid<T = unknown>({
791
874
  const d = dragRef.current;
792
875
  if (!d) return;
793
876
  try {
794
- (e.target as HTMLElement)?.releasePointerCapture?.(e.pointerId);
877
+ activeGrabRef.current?.box.releasePointerCapture?.(e.pointerId);
795
878
  } catch {
796
879
  // Best-effort release; ignore if the capture was never granted.
797
880
  }
798
881
  const held = draggedRef.current;
799
882
  if (!d.moved) {
883
+ const wasTouch = touchDragRef.current;
884
+ const tapBox = activeGrabRef.current?.box ?? null;
800
885
  finishDrag();
801
- held?.onPress();
886
+ if (wasTouch) {
887
+ tappedRef.current = tapBox;
888
+ } else {
889
+ held?.onPress();
890
+ }
802
891
  return;
803
892
  }
804
893
  const origin = dragOrigin.current;
@@ -849,25 +938,25 @@ export function TimeGrid<T = unknown>({
849
938
  // `continues*` flags are the same type, so passing them positionally invites a
850
939
  // silent transposition.
851
940
  const beginDrag = (
852
- e: ReactPointerEvent,
941
+ e: EventGrab,
853
942
  pe: PositionedEvent<T>,
854
943
  key: string,
855
944
  kind: "move" | "resize" | "resize-start",
856
945
  dayIndex: number,
857
946
  onPress?: () => void,
858
947
  ) => {
859
- if (!onDragEvent) return;
860
948
  const { event, startHours, durationHours, continuesBefore, continuesAfter } = pe;
861
- e.stopPropagation();
862
949
  try {
863
- (e.target as HTMLElement).setPointerCapture?.(e.pointerId);
950
+ e.box.setPointerCapture?.(e.pointerId);
864
951
  } catch {
865
952
  // Pointer capture is best-effort; some environments reject it.
866
953
  }
954
+ touchDragRef.current = e.pointerType !== "mouse";
955
+ activeGrabRef.current = e;
867
956
  // The event box sits inside its day column, so the parent's width is one day;
868
957
  // columns are equal-width, so measuring one is enough. Resizes never move
869
958
  // across days, so 0 is fine there.
870
- const boxEl = e.currentTarget as HTMLElement;
959
+ const boxEl = e.box;
871
960
  const column = kind === "move" ? boxEl.parentElement : null;
872
961
  const dayWidth = column ? column.getBoundingClientRect().width : 0;
873
962
  const boxRect = kind === "move" ? boxEl.getBoundingClientRect() : null;
@@ -875,6 +964,7 @@ export function TimeGrid<T = unknown>({
875
964
  dragOrigin.current = {
876
965
  pointerX: e.clientX,
877
966
  pointerY: e.clientY,
967
+ pointerType: e.pointerType,
878
968
  startHours,
879
969
  durationHours,
880
970
  dayIndex,
@@ -913,26 +1003,98 @@ export function TimeGrid<T = unknown>({
913
1003
  // columns unmounting on a page change (the dragged box's own node disappears).
914
1004
  // Detach any prior set first so a second beginDrag can't leak listeners.
915
1005
  detachDocListeners();
916
- const handlers = { move: moveDrag, up: endDrag, cancel: cancelDrag };
1006
+ const handlers = {
1007
+ move: (event: PointerEvent) => {
1008
+ if (event.pointerId === e.pointerId) {
1009
+ moveDrag(event);
1010
+ }
1011
+ },
1012
+ up: (event: PointerEvent) => {
1013
+ if (event.pointerId === e.pointerId) {
1014
+ endDrag(event);
1015
+ }
1016
+ },
1017
+ cancel: (event: PointerEvent) => {
1018
+ if (event.pointerId === e.pointerId) {
1019
+ cancelDrag();
1020
+ }
1021
+ },
1022
+ };
917
1023
  docHandlersRef.current = handlers;
918
1024
  document.addEventListener("pointermove", handlers.move);
919
1025
  document.addEventListener("pointerup", handlers.up);
920
1026
  document.addEventListener("pointercancel", handlers.cancel);
921
1027
  };
922
1028
 
1029
+ const pressEvent = (
1030
+ e: ReactPointerEvent<HTMLElement>,
1031
+ pe: PositionedEvent<T>,
1032
+ key: string,
1033
+ kind: "move" | "resize" | "resize-start",
1034
+ dayIndex: number,
1035
+ onPress?: () => void,
1036
+ ) => {
1037
+ if (!onDragEvent || e.button > 0) {
1038
+ return;
1039
+ }
1040
+ e.stopPropagation();
1041
+ const hadPendingHold = cancelHoldRef.current !== null;
1042
+ cancelHoldRef.current?.();
1043
+ tappedRef.current = null;
1044
+ if (hadPendingHold || activeGrabRef.current || (e.pointerType !== "mouse" && !e.isPrimary)) {
1045
+ return;
1046
+ }
1047
+ const grab: EventGrab = {
1048
+ box: e.currentTarget,
1049
+ pointerId: e.pointerId,
1050
+ pointerType: e.pointerType,
1051
+ clientX: e.clientX,
1052
+ clientY: e.clientY,
1053
+ };
1054
+ if (e.pointerType === "mouse") {
1055
+ beginDrag(grab, pe, key, kind, dayIndex, onPress);
1056
+ return;
1057
+ }
1058
+ cancelHoldRef.current = waitForHold(
1059
+ grab,
1060
+ touchDragDelay,
1061
+ touchDragThreshold,
1062
+ () => {
1063
+ cancelHoldRef.current = null;
1064
+ beginDrag(grab, pe, key, kind, dayIndex, onPress);
1065
+ },
1066
+ () => {
1067
+ cancelHoldRef.current = null;
1068
+ tappedRef.current = grab.box;
1069
+ },
1070
+ () => {
1071
+ cancelHoldRef.current = null;
1072
+ },
1073
+ );
1074
+ };
1075
+
923
1076
  // Remove any lingering document listeners / dwell timer if we unmount mid-drag.
924
1077
  useEffect(
925
1078
  () => () => {
1079
+ cancelHoldRef.current?.();
926
1080
  detachDocListeners();
927
1081
  clearEdgeTimer();
928
1082
  },
929
1083
  [],
930
1084
  );
931
1085
 
1086
+ useEffect(() => {
1087
+ cancelHoldRef.current?.();
1088
+ }, [date, events, onDragEvent]);
1089
+
932
1090
  const pxFromTop = (el: HTMLElement, clientY: number) => clientY - el.getBoundingClientRect().top;
933
1091
  const beginCreate = (e: ReactPointerEvent, dayIndex: number) => {
934
1092
  // Mouse / pen only: on touch the column must stay free to scroll the grid.
935
1093
  // Only from the column background, primary button, never on an event.
1094
+ cellTouchRef.current =
1095
+ e.pointerType === "touch" && e.target === e.currentTarget
1096
+ ? { pointerId: e.pointerId, x: e.clientX, y: e.clientY }
1097
+ : null;
936
1098
  if (!cellEnabled || e.pointerType === "touch") return;
937
1099
  if (e.target !== e.currentTarget || e.button > 0) return;
938
1100
  const el = e.currentTarget as HTMLElement;
@@ -946,6 +1108,14 @@ export function TimeGrid<T = unknown>({
946
1108
  setCreateBox({ dayIndex, topPx: startPx, heightPx: 0 });
947
1109
  };
948
1110
  const moveCreate = (e: ReactPointerEvent) => {
1111
+ const touch = cellTouchRef.current;
1112
+ if (
1113
+ touch &&
1114
+ e.pointerId === touch.pointerId &&
1115
+ Math.hypot(e.clientX - touch.x, e.clientY - touch.y) > CELL_TOUCH_SLOP_PX
1116
+ ) {
1117
+ cellTouchRef.current = null;
1118
+ }
949
1119
  const o = createOrigin.current;
950
1120
  if (!o) return;
951
1121
  const cur = pxFromTop(o.el, e.clientY);
@@ -977,9 +1147,23 @@ export function TimeGrid<T = unknown>({
977
1147
  createOrigin.current = null;
978
1148
  setCreateBox(null);
979
1149
  };
1150
+ const tapCell = (e: ReactMouseEvent<HTMLDivElement>, dayIndex: number) => {
1151
+ const touch = cellTouchRef.current;
1152
+ cellTouchRef.current = null;
1153
+ const day = days[dayIndex];
1154
+ if (!touch || !onPressCell || !day || e.target !== e.currentTarget) {
1155
+ return;
1156
+ }
1157
+ const px = pxFromTop(e.currentTarget, e.clientY);
1158
+ const at = cellRangeFromDrag(day, px, px, hourHeightRef.current, windowStart, snapMinutes);
1159
+ if (at) {
1160
+ onPressCell(at.start);
1161
+ }
1162
+ };
980
1163
  // A gesture the browser/OS cancels (scroll takeover, etc.) must not commit a
981
1164
  // create — just drop the in-progress state.
982
1165
  const cancelCreate = () => {
1166
+ cellTouchRef.current = null;
983
1167
  createOrigin.current = null;
984
1168
  setCreateBox(null);
985
1169
  };
@@ -1443,6 +1627,7 @@ export function TimeGrid<T = unknown>({
1443
1627
  onPointerMove={cellEnabled ? moveCreate : undefined}
1444
1628
  onPointerUp={cellEnabled ? endCreate : undefined}
1445
1629
  onPointerCancel={cellEnabled ? cancelCreate : undefined}
1630
+ onClick={cellEnabled ? (e) => tapCell(e, dayIndex) : undefined}
1446
1631
  {...dataState({ "data-today": getIsToday(day), "data-weekend": isWeekend(day) })}
1447
1632
  {...slot("dayColumn", {
1448
1633
  base: { flex: 1, position: "relative" },
@@ -1612,10 +1797,22 @@ export function TimeGrid<T = unknown>({
1612
1797
  // remounting on a page change.
1613
1798
  onPointerDown={
1614
1799
  canMove
1615
- ? (e) => beginDrag(e, pe, key, "move", dayIndex, onPress)
1800
+ ? (e) => pressEvent(e, pe, key, "move", dayIndex, onPress)
1616
1801
  : undefined
1617
1802
  }
1618
- onClick={canMove ? undefined : onPress}
1803
+ onClick={
1804
+ canMove
1805
+ ? (e) => {
1806
+ if (
1807
+ e.detail === 0 ||
1808
+ (tappedRef.current && e.currentTarget.contains(tappedRef.current))
1809
+ ) {
1810
+ tappedRef.current = null;
1811
+ onPress();
1812
+ }
1813
+ }
1814
+ : onPress
1815
+ }
1619
1816
  {...dataState({ "data-dragging": !!active && !hiddenForMove })}
1620
1817
  {...slot("event", {
1621
1818
  base: {
@@ -1629,7 +1826,9 @@ export function TimeGrid<T = unknown>({
1629
1826
  left: `calc(${pe.column * widthPct}% + ${eventGap}px)`,
1630
1827
  width: `calc(${widthPct}% - ${eventGap * 2}px)`,
1631
1828
  cursor: canMove ? "grab" : "pointer",
1632
- touchAction: canMove ? "none" : "auto",
1829
+ touchAction: canMove ? "pan-y" : "auto",
1830
+ userSelect: canMove ? "none" : undefined,
1831
+ WebkitTouchCallout: canMove ? "none" : undefined,
1633
1832
  zIndex: active ? 3 : 1,
1634
1833
  opacity: active ? 0.85 : 1,
1635
1834
  visibility: hiddenForMove ? "hidden" : undefined,
@@ -1652,8 +1851,7 @@ export function TimeGrid<T = unknown>({
1652
1851
  {canResize && !pe.continuesBefore ? (
1653
1852
  <div
1654
1853
  onPointerDown={(e) => {
1655
- e.stopPropagation();
1656
- beginDrag(
1854
+ pressEvent(
1657
1855
  e,
1658
1856
  pe,
1659
1857
  key,
@@ -1669,7 +1867,7 @@ export function TimeGrid<T = unknown>({
1669
1867
  top: 0,
1670
1868
  height: 8,
1671
1869
  cursor: "ns-resize",
1672
- touchAction: "none",
1870
+ touchAction: "pan-y",
1673
1871
  }}
1674
1872
  />
1675
1873
  ) : null}
@@ -1681,8 +1879,7 @@ export function TimeGrid<T = unknown>({
1681
1879
  {canResize && !pe.continuesAfter ? (
1682
1880
  <div
1683
1881
  onPointerDown={(e) => {
1684
- e.stopPropagation();
1685
- beginDrag(
1882
+ pressEvent(
1686
1883
  e,
1687
1884
  pe,
1688
1885
  key,
@@ -1698,7 +1895,7 @@ export function TimeGrid<T = unknown>({
1698
1895
  bottom: 0,
1699
1896
  height: 8,
1700
1897
  cursor: "ns-resize",
1701
- touchAction: "none",
1898
+ touchAction: "pan-y",
1702
1899
  }}
1703
1900
  />
1704
1901
  ) : null}
package/src/index.ts CHANGED
@@ -63,6 +63,7 @@ export {
63
63
  type BusinessHours,
64
64
  type BusinessHoursBand,
65
65
  type CalendarEvent,
66
+ type CalendarHandle,
66
67
  type CalendarLabels,
67
68
  defaultCalendarLabels,
68
69
  type CalendarMode,
@@ -82,6 +83,7 @@ export {
82
83
  type WeekStartsOn,
83
84
  type RecurrenceRule,
84
85
  type RecurrenceFrequency,
86
+ type ScrollToTimeOptions,
85
87
  expandRecurringEvents,
86
88
  getViewDays,
87
89
  backgroundBandsForDay,
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
+ }