@super-calendar/native 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.
@@ -58,6 +58,8 @@ import type {
58
58
  BusinessHoursBand,
59
59
  CalendarEvent,
60
60
  CalendarMode,
61
+ EventDragHandler,
62
+ EventDragStartHandler,
61
63
  EventKeyExtractor,
62
64
  RenderEvent,
63
65
  TimeGridMode,
@@ -73,6 +75,7 @@ import {
73
75
  import {
74
76
  backgroundBandsForDay,
75
77
  cellRangeFromDrag,
78
+ clampMoveStartMinutes,
76
79
  useNow,
77
80
  closedHourBands,
78
81
  overlapsOtherEvents,
@@ -83,7 +86,12 @@ import {
83
86
  import { formatHour, layoutDayEvents, type PositionedEvent } from "@super-calendar/core";
84
87
  import type { EventAccessibilityLabeler } from "@super-calendar/core";
85
88
  import { type WeekdayFormat, weekdayFormatToken } from "@super-calendar/core";
86
- import { SlotStylesProvider, type SlotStyleProps, useSlots } from "../utils/slots";
89
+ import {
90
+ type ResolvedSlot,
91
+ SlotStylesProvider,
92
+ type SlotStyleProps,
93
+ useSlots,
94
+ } from "../utils/slots";
87
95
  import { useWebGridZoom } from "../utils/useWebGridZoom";
88
96
  import { useWebPagerKeys } from "../utils/useWebPagerKeys";
89
97
  import { withEventAccessibilityLabel } from "../utils/withEventAccessibilityLabel";
@@ -180,6 +188,78 @@ const EdgePagingContext = createContext<EdgePaging | null>(null);
180
188
 
181
189
  const noop = () => {};
182
190
 
191
+ /**
192
+ * The tail of a move that runs past midnight, drawn at the top of the next day's
193
+ * column so the drop reads on both days before the finger lifts. Mounted only
194
+ * while a drag is actually spilling (see `spillHeight`), so an idle grid pays
195
+ * nothing for it; its geometry is driven on the UI thread from there.
196
+ */
197
+ function MoveSpillPreview<T>({
198
+ spillHeight,
199
+ moveOffsetX,
200
+ cellHeight,
201
+ dayLeftPx,
202
+ dayWidth,
203
+ nextDayDirection,
204
+ minHour,
205
+ event,
206
+ mode,
207
+ renderEvent,
208
+ slotProps,
209
+ }: {
210
+ spillHeight: SharedValue<number>;
211
+ moveOffsetX: SharedValue<number>;
212
+ cellHeight: SharedValue<number>;
213
+ dayLeftPx: number;
214
+ dayWidth: number;
215
+ nextDayDirection: number;
216
+ minHour: number;
217
+ event: CalendarEvent<T>;
218
+ mode: CalendarMode;
219
+ renderEvent: RenderEvent<T>;
220
+ slotProps: ResolvedSlot<ViewStyle>;
221
+ }): ReactElement {
222
+ const RenderEventComponent = renderEvent;
223
+ const style = useAnimatedStyle(
224
+ () => ({
225
+ height: spillHeight.value,
226
+ opacity: spillHeight.value > 0 ? 1 : 0,
227
+ zIndex: spillHeight.value > 0 ? DRAG_EVENT_Z : 0,
228
+ transform: [
229
+ { translateX: moveOffsetX.value + nextDayDirection * dayWidth },
230
+ // The box's `top` is the `minHour` gridline, but the tail starts at
231
+ // midnight. With a window that starts after 00:00 this pushes the preview
232
+ // out of view, exactly like the continuation the drop will commit.
233
+ { translateY: -minHour * cellHeight.value },
234
+ ],
235
+ }),
236
+ [dayWidth, nextDayDirection, minHour],
237
+ );
238
+ return (
239
+ <Animated.View
240
+ {...slotProps}
241
+ accessibilityElementsHidden
242
+ importantForAccessibility="no-hide-descendants"
243
+ style={[
244
+ styles.eventBox,
245
+ styles.nonInteractive,
246
+ { left: dayLeftPx, width: dayWidth, top: 0 },
247
+ slotProps.style,
248
+ style,
249
+ ]}
250
+ >
251
+ <RenderEventComponent
252
+ event={event}
253
+ mode={mode}
254
+ boxHeight={spillHeight}
255
+ continuesBefore
256
+ continuesAfter={false}
257
+ onPress={noop}
258
+ />
259
+ </Animated.View>
260
+ );
261
+ }
262
+
183
263
  /**
184
264
  * The floating copy of a dragged event, rendered above the pager so a cross-week
185
265
  * drag stays visible after its source page pages away. Position, size, and
@@ -219,23 +299,10 @@ function DragGhost<T>({
219
299
  );
220
300
  }
221
301
 
222
- /**
223
- * Called when an event is dragged (moved or resized) to new start/end times.
224
- * Return `false` to reject the drop — the event snaps back to where it started
225
- * (e.g. to forbid overlaps or out-of-bounds slots). Any other return accepts it.
226
- */
227
- export type EventDragHandler<T> = (
228
- event: CalendarEvent<T>,
229
- start: Date,
230
- end: Date,
231
- ) => void | boolean;
232
- /**
233
- * Called when a move or resize gesture begins, before any change is committed:
234
- * on grab for a move (after the long-press), and when the resize drag starts.
235
- * Handy for haptic feedback (e.g. `expo-haptics`). Requires drag to be enabled
236
- * via `onDragEvent`.
237
- */
238
- export type EventDragStartHandler<T> = (event: CalendarEvent<T>) => void;
302
+ // The drag handler types live in `../types` so the Reanimated-free month view can
303
+ // share them; re-exported here because both are part of this module's public API.
304
+ export type { EventDragHandler, EventDragStartHandler } from "../types";
305
+
239
306
  // Hour labels are nudged up so the number sits centred on its grid line. Pad the
240
307
  // scroll content by the same amount so the top-most label is never clipped.
241
308
  const HOUR_LABEL_TOP_INSET = 12;
@@ -245,8 +312,14 @@ type AnimatedEventBoxProps<T> = {
245
312
  positioned: PositionedEvent<T>;
246
313
  cellHeight: SharedValue<number>;
247
314
  minHour: number;
315
+ maxHour: number;
248
316
  left: number;
249
317
  width: number;
318
+ // Left edge of the whole day column (the box's own `left` is inset by its
319
+ // overlap column), so the "runs past midnight" preview can span the next column.
320
+ dayLeftPx: number;
321
+ // Which way the next calendar day lies, in columns: +1 normally, -1 under RTL.
322
+ nextDayDirection: number;
250
323
  // Drag-to-move across days needs the column width and this box's day index
251
324
  // within the visible range, so a horizontal drag maps to (and clamps to) days.
252
325
  dayWidth: number;
@@ -280,8 +353,11 @@ function AnimatedEventBox<T>({
280
353
  positioned,
281
354
  cellHeight,
282
355
  minHour,
356
+ maxHour,
283
357
  left,
284
358
  width,
359
+ dayLeftPx,
360
+ nextDayDirection,
285
361
  dayWidth,
286
362
  dayIndex,
287
363
  dayCount,
@@ -342,6 +418,10 @@ function AnimatedEventBox<T>({
342
418
  // Raised to DRAG_EVENT_Z while a gesture is active so the dragged event floats
343
419
  // above all the others, then back to 0 when it settles.
344
420
  const dragZ = useSharedValue(0);
421
+ // 1 while a move gesture is running. Only gates the spill preview: the box's own
422
+ // clip is geometric, so it can't disagree with the box during the frames between
423
+ // release and the committed re-render.
424
+ const moving = useSharedValue(0);
345
425
  // Edge auto-advance: which side the drag has pushed past the visible columns
346
426
  // (-1 left / 0 none / +1 right).
347
427
  const edgeDir = useSharedValue(0);
@@ -361,6 +441,20 @@ function AnimatedEventBox<T>({
361
441
  // ("Cannot copy value of type `Date`").
362
442
  const startHours = positioned.startHours;
363
443
  const durationHours = positioned.durationHours;
444
+ const ownsStart = !positioned.continuesBefore;
445
+ // Where the move gesture clamps from, mirrored into a shared value so it isn't a
446
+ // gesture dependency. A gesture memoized on a value the dragged event carries
447
+ // would be rebuilt under the finger, and so torn down mid-drag, the moment a
448
+ // consumer updates `events` (the same reason `commitDrag` reads the event
449
+ // through `latest`). `ownsStart` is false on a segment the layout clipped from
450
+ // an earlier day, whose 00:00 top edge isn't where the event starts.
451
+ const dragStartSV = useDerivedValue(
452
+ () => ({
453
+ minutes: startHours * MINUTES_PER_HOUR,
454
+ ownsStart,
455
+ }),
456
+ [startHours, ownsStart],
457
+ );
364
458
 
365
459
  // Live pixel height of the box, driven on the UI thread by the shared
366
460
  // cellHeight (plus any in-progress resize). Handed to renderEvent so custom
@@ -375,15 +469,76 @@ function AnimatedEventBox<T>({
375
469
  [durationHours],
376
470
  );
377
471
 
378
- const boxStyle = useAnimatedStyle(
379
- () => ({
380
- // A top-edge resize pushes the box down and shortens it (start moves later).
381
- top: (startHours - minHour) * cellHeight.value + moveOffset.value + resizeStartDelta.value,
382
- height: boxHeight.value,
472
+ // The source segment may stop at midnight while the full event continues in
473
+ // the next column. Keep the height passed to custom renderers identical to the
474
+ // wrapper they fill, while `boxHeight` retains the complete duration for drag
475
+ // math and the floating cross-page ghost.
476
+ const visibleBoxHeight = useDerivedValue(() => {
477
+ const top =
478
+ (startHours - minHour) * cellHeight.value + moveOffset.value + resizeStartDelta.value;
479
+ const dayEnd = (HOURS_PER_DAY - minHour) * cellHeight.value;
480
+ return Math.max(Math.min(boxHeight.value, dayEnd - top), MIN_EVENT_HEIGHT);
481
+ }, [startHours, minHour]);
482
+
483
+ const boxStyle = useAnimatedStyle(() => {
484
+ // A top-edge resize pushes the box down and shortens it (start moves later).
485
+ const top =
486
+ (startHours - minHour) * cellHeight.value + moveOffset.value + resizeStartDelta.value;
487
+ // Cut the box off at midnight; whatever runs past it shows in the next column
488
+ // instead (see MoveSpillPreview), the way a committed event spanning midnight
489
+ // already renders. Purely geometric, so a resting box (already clipped to the
490
+ // day by `layoutDayEvents`) is untouched and the height doesn't jump when a
491
+ // drag commits.
492
+ return {
493
+ top,
494
+ height: visibleBoxHeight.value,
383
495
  transform: [{ translateX: moveOffsetX.value }],
384
496
  zIndex: dragZ.value,
385
- }),
386
- [startHours, durationHours, minHour],
497
+ };
498
+ }, [startHours, minHour]);
499
+
500
+ // Pixel height of the part of a move that runs past midnight, or 0 when nothing
501
+ // spills. Measured against the end of the day, not `maxHour`: a narrowed window
502
+ // leaves plenty of events hanging below the last visible hour without them
503
+ // reaching the next day. Bails on the first line for a box that isn't being
504
+ // moved, which is every box but one.
505
+ const spillHeight = useDerivedValue(() => {
506
+ if (!moving.value || lifted.value || cellHeight.value <= 0) return 0;
507
+ const liveStartHours = startHours + moveOffset.value / cellHeight.value;
508
+ const overflowHours = liveStartHours + durationHours - HOURS_PER_DAY;
509
+ if (overflowHours <= 0) return 0;
510
+ const columnDelta = dayWidth > 0 ? Math.round(moveOffsetX.value / dayWidth) : 0;
511
+ const target = Math.min(Math.max(dayIndex + columnDelta, 0), dayCount - 1);
512
+ const next = target + nextDayDirection;
513
+ // Only when that column really is the next calendar day (`hiddenDays` can
514
+ // break the run), since the tail would otherwise land off-view.
515
+ if (next < 0 || next >= dayCount || dayOrdinals[next] - dayOrdinals[target] !== 1) return 0;
516
+ return Math.min(overflowHours, maxHour - minHour) * cellHeight.value;
517
+ }, [
518
+ startHours,
519
+ durationHours,
520
+ minHour,
521
+ maxHour,
522
+ dayWidth,
523
+ dayIndex,
524
+ dayCount,
525
+ dayOrdinals,
526
+ nextDayDirection,
527
+ ]);
528
+
529
+ // Mount the spill preview only while a drag is actually spilling, off the UI
530
+ // thread. It's one extra view plus the consumer's event renderer, so mounting it
531
+ // on every box would inflate the grid's node count for something that is almost
532
+ // never showing. Mounting it at the grab would also re-render at the most
533
+ // jank-sensitive moment of the gesture.
534
+ const [previewMounted, setPreviewMounted] = useState(false);
535
+ useAnimatedReaction(
536
+ () => spillHeight.value > 0,
537
+ (spilling, previous) => {
538
+ // `previous` is null on the reaction's first run, which would otherwise cost
539
+ // every box on every page a thread hop to set the state it already has.
540
+ if (previous !== null && spilling !== previous) runOnJS(setPreviewMounted)(spilling);
541
+ },
387
542
  );
388
543
 
389
544
  // Clear the drag preview once the committed change re-renders this box at its
@@ -519,6 +674,7 @@ function AnimatedEventBox<T>({
519
674
  .enabled(canMove)
520
675
  .onStart((event) => {
521
676
  dragZ.value = DRAG_EVENT_Z;
677
+ moving.value = 1;
522
678
  edgeDir.value = 0;
523
679
  lifted.value = 0;
524
680
  // Where in the box the finger grabbed, and the live finger position, so
@@ -537,8 +693,16 @@ function AnimatedEventBox<T>({
537
693
  if (lifted.value && pagerLeftSV && pagerTopSV && ghostXSV && ghostYSV) {
538
694
  ghostXSV.value = event.absoluteX - grabX.value - pagerLeftSV.value;
539
695
  ghostYSV.value = event.absoluteY - grabY.value - pagerTopSV.value;
540
- } else {
541
- moveOffset.value = event.translationY;
696
+ } else if (cellHeight.value > 0) {
697
+ // Hold the start inside the day; the end is free to run past it (the
698
+ // spill preview shows where it lands). Same rule the commit applies, so
699
+ // the box never previews a position it can't be dropped in.
700
+ const { minutes: startMinutes, ownsStart } = dragStartSV.value;
701
+ const dragged = startMinutes + (event.translationY / cellHeight.value) * MINUTES_PER_HOUR;
702
+ const held = ownsStart
703
+ ? clampMoveStartMinutes(dragged, minHour, maxHour, snapMinutes)
704
+ : dragged;
705
+ moveOffset.value = ((held - startMinutes) / MINUTES_PER_HOUR) * cellHeight.value;
542
706
  moveOffsetX.value = event.translationX;
543
707
  }
544
708
  // Dragging the finger to the pager's left/right edge arms an edge dwell
@@ -560,7 +724,17 @@ function AnimatedEventBox<T>({
560
724
  // gesture ends cleanly or is cancelled as the page moves under it), so the
561
725
  // drop lands even when onEnd doesn't fire on device. Nothing to do here.
562
726
  if (lifted.value) return;
563
- const minuteDelta = snapDeltaMinutes(event.translationY, cellHeight.value, snapMinutes);
727
+ const { minutes: startMinutes, ownsStart } = dragStartSV.value;
728
+ // Snap the vertical drag, then hold the start inside the day: a move keeps
729
+ // its duration, so the end may run past midnight and continue on the next
730
+ // day, but the event still starts in the column it was dropped on. A
731
+ // segment clipped from an earlier day is exempt: it doesn't own the start,
732
+ // so holding it would stop the whole event from being dragged earlier.
733
+ const snapped = snapDeltaMinutes(event.translationY, cellHeight.value, snapMinutes);
734
+ const minuteDelta = ownsStart
735
+ ? clampMoveStartMinutes(startMinutes + snapped, minHour, maxHour, snapMinutes) -
736
+ startMinutes
737
+ : snapped;
564
738
  // Map the horizontal drag to whole day columns, clamped so the event
565
739
  // can't leave the visible range.
566
740
  const rawDayDelta = dayWidth > 0 ? Math.round(event.translationX / dayWidth) : 0;
@@ -595,6 +769,10 @@ function AnimatedEventBox<T>({
595
769
  moveOffsetX.value = 0;
596
770
  lifted.value = 0;
597
771
  }
772
+ // The preview unmounts from the `spillHeight` reaction once the commit
773
+ // lands and clears `moveOffset`; this is the safety net for a cancel,
774
+ // where `onEnd` never runs.
775
+ moving.value = 0;
598
776
  runOnJS(releaseEdge)();
599
777
  });
600
778
  // Native: long-press to pick up. Web: activate past a small drag in either
@@ -611,6 +789,10 @@ function AnimatedEventBox<T>({
611
789
  moveOffset,
612
790
  moveOffsetX,
613
791
  dragZ,
792
+ moving,
793
+ dragStartSV,
794
+ minHour,
795
+ maxHour,
614
796
  edgeDir,
615
797
  lifted,
616
798
  grabX,
@@ -768,7 +950,7 @@ function AnimatedEventBox<T>({
768
950
  <RenderEventComponent
769
951
  event={positioned.event}
770
952
  mode={mode}
771
- boxHeight={boxHeight}
953
+ boxHeight={visibleBoxHeight}
772
954
  continuesBefore={positioned.continuesBefore}
773
955
  continuesAfter={positioned.continuesAfter}
774
956
  accessibilityActions={accessibilityActions}
@@ -802,7 +984,28 @@ function AnimatedEventBox<T>({
802
984
  // Only wrap in the move gesture when movable; the resize grips inside `box`
803
985
  // carry their own gestures, so a resize-only event still works.
804
986
  if (!canMove) return box;
805
- return <GestureDetector gesture={moveGesture}>{box}</GestureDetector>;
987
+ return (
988
+ <>
989
+ <GestureDetector gesture={moveGesture}>{box}</GestureDetector>
990
+ {/* Skipped for a segment that already continues after: it owns a real
991
+ next-day box, so a preview on top of it would just double up. */}
992
+ {previewMounted && !positioned.continuesAfter ? (
993
+ <MoveSpillPreview
994
+ spillHeight={spillHeight}
995
+ moveOffsetX={moveOffsetX}
996
+ cellHeight={cellHeight}
997
+ dayLeftPx={dayLeftPx}
998
+ dayWidth={dayWidth}
999
+ nextDayDirection={nextDayDirection}
1000
+ minHour={minHour}
1001
+ event={positioned.event}
1002
+ mode={mode}
1003
+ renderEvent={renderEvent}
1004
+ slotProps={slot("event", { themed: theme.containers.timeGridEvent })}
1005
+ />
1006
+ ) : null}
1007
+ </>
1008
+ );
806
1009
  }
807
1010
 
808
1011
  /** Replace the hour-axis label. Receives the hour (0–23) and the `ampm` flag. */
@@ -1618,8 +1821,11 @@ function TimetablePageInner<T>({
1618
1821
  positioned={positioned}
1619
1822
  cellHeight={heightSource}
1620
1823
  minHour={minHour}
1824
+ maxHour={maxHour}
1621
1825
  left={dayLeft(dayIndex) + positioned.column * columnWidth}
1622
1826
  width={columnWidth}
1827
+ dayLeftPx={dayLeft(dayIndex)}
1828
+ nextDayDirection={isRTL ? -1 : 1}
1623
1829
  dayWidth={dayWidth}
1624
1830
  dayIndex={dayIndex}
1625
1831
  dayCount={days.length}
@@ -1961,6 +2167,7 @@ function TimeGridInner<T>({
1961
2167
  containerWidth,
1962
2168
  hourColumnWidth,
1963
2169
  minHour: clampedMinHour,
2170
+ maxHour: clampedMaxHour,
1964
2171
  snapMinutes: Math.max(1, dragStepMinutes),
1965
2172
  onDragEvent,
1966
2173
  onChangeDate,
@@ -1983,6 +2190,7 @@ function TimeGridInner<T>({
1983
2190
  containerWidth: cw,
1984
2191
  hourColumnWidth: hcw,
1985
2192
  minHour: min,
2193
+ maxHour: max,
1986
2194
  snapMinutes: snap,
1987
2195
  onDragEvent: onDrag,
1988
2196
  onChangeDate: onDate,
@@ -2003,7 +2211,9 @@ function TimeGridInner<T>({
2003
2211
  const hoursFromMin =
2004
2212
  (ghostLocalY - gridTop.value - HOUR_LABEL_TOP_INSET + scrollY.value) / cellHeight.value;
2005
2213
  const rawMinutes = (min + hoursFromMin) * MINUTES_PER_HOUR;
2006
- const minutes = Math.round(rawMinutes / snap) * snap;
2214
+ // Same rule as an in-page move: the start stays in the day it landed on,
2215
+ // while the duration is free to carry the end past midnight.
2216
+ const minutes = clampMoveStartMinutes(Math.round(rawMinutes / snap) * snap, min, max, snap);
2007
2217
  const start = new Date(days[col]);
2008
2218
  start.setHours(0, 0, 0, 0);
2009
2219
  start.setMinutes(minutes);
@@ -2130,6 +2340,7 @@ function TimeGridInner<T>({
2130
2340
  containerWidth,
2131
2341
  hourColumnWidth,
2132
2342
  minHour: clampedMinHour,
2343
+ maxHour: clampedMaxHour,
2133
2344
  snapMinutes: Math.max(1, dragStepMinutes),
2134
2345
  onDragEvent,
2135
2346
  onChangeDate,