@super-calendar/native 2.8.1 → 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.
@@ -34,6 +34,7 @@ import {
34
34
  StyleSheet,
35
35
  type StyleProp,
36
36
  Text,
37
+ type TextStyle,
37
38
  useWindowDimensions,
38
39
  View,
39
40
  type ViewStyle,
@@ -57,6 +58,8 @@ import type {
57
58
  BusinessHoursBand,
58
59
  CalendarEvent,
59
60
  CalendarMode,
61
+ EventDragHandler,
62
+ EventDragStartHandler,
60
63
  EventKeyExtractor,
61
64
  RenderEvent,
62
65
  TimeGridMode,
@@ -72,6 +75,7 @@ import {
72
75
  import {
73
76
  backgroundBandsForDay,
74
77
  cellRangeFromDrag,
78
+ clampMoveStartMinutes,
75
79
  useNow,
76
80
  closedHourBands,
77
81
  overlapsOtherEvents,
@@ -82,7 +86,12 @@ import {
82
86
  import { formatHour, layoutDayEvents, type PositionedEvent } from "@super-calendar/core";
83
87
  import type { EventAccessibilityLabeler } from "@super-calendar/core";
84
88
  import { type WeekdayFormat, weekdayFormatToken } from "@super-calendar/core";
85
- import { SlotStylesProvider, type SlotStyleProps, useSlots } from "../utils/slots";
89
+ import {
90
+ type ResolvedSlot,
91
+ SlotStylesProvider,
92
+ type SlotStyleProps,
93
+ useSlots,
94
+ } from "../utils/slots";
86
95
  import { useWebGridZoom } from "../utils/useWebGridZoom";
87
96
  import { useWebPagerKeys } from "../utils/useWebPagerKeys";
88
97
  import { withEventAccessibilityLabel } from "../utils/withEventAccessibilityLabel";
@@ -179,6 +188,78 @@ const EdgePagingContext = createContext<EdgePaging | null>(null);
179
188
 
180
189
  const noop = () => {};
181
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
+
182
263
  /**
183
264
  * The floating copy of a dragged event, rendered above the pager so a cross-week
184
265
  * drag stays visible after its source page pages away. Position, size, and
@@ -218,23 +299,10 @@ function DragGhost<T>({
218
299
  );
219
300
  }
220
301
 
221
- /**
222
- * Called when an event is dragged (moved or resized) to new start/end times.
223
- * Return `false` to reject the drop — the event snaps back to where it started
224
- * (e.g. to forbid overlaps or out-of-bounds slots). Any other return accepts it.
225
- */
226
- export type EventDragHandler<T> = (
227
- event: CalendarEvent<T>,
228
- start: Date,
229
- end: Date,
230
- ) => void | boolean;
231
- /**
232
- * Called when a move or resize gesture begins, before any change is committed:
233
- * on grab for a move (after the long-press), and when the resize drag starts.
234
- * Handy for haptic feedback (e.g. `expo-haptics`). Requires drag to be enabled
235
- * via `onDragEvent`.
236
- */
237
- 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
+
238
306
  // Hour labels are nudged up so the number sits centred on its grid line. Pad the
239
307
  // scroll content by the same amount so the top-most label is never clipped.
240
308
  const HOUR_LABEL_TOP_INSET = 12;
@@ -244,8 +312,14 @@ type AnimatedEventBoxProps<T> = {
244
312
  positioned: PositionedEvent<T>;
245
313
  cellHeight: SharedValue<number>;
246
314
  minHour: number;
315
+ maxHour: number;
247
316
  left: number;
248
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;
249
323
  // Drag-to-move across days needs the column width and this box's day index
250
324
  // within the visible range, so a horizontal drag maps to (and clamps to) days.
251
325
  dayWidth: number;
@@ -279,8 +353,11 @@ function AnimatedEventBox<T>({
279
353
  positioned,
280
354
  cellHeight,
281
355
  minHour,
356
+ maxHour,
282
357
  left,
283
358
  width,
359
+ dayLeftPx,
360
+ nextDayDirection,
284
361
  dayWidth,
285
362
  dayIndex,
286
363
  dayCount,
@@ -341,6 +418,10 @@ function AnimatedEventBox<T>({
341
418
  // Raised to DRAG_EVENT_Z while a gesture is active so the dragged event floats
342
419
  // above all the others, then back to 0 when it settles.
343
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);
344
425
  // Edge auto-advance: which side the drag has pushed past the visible columns
345
426
  // (-1 left / 0 none / +1 right).
346
427
  const edgeDir = useSharedValue(0);
@@ -360,6 +441,20 @@ function AnimatedEventBox<T>({
360
441
  // ("Cannot copy value of type `Date`").
361
442
  const startHours = positioned.startHours;
362
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
+ );
363
458
 
364
459
  // Live pixel height of the box, driven on the UI thread by the shared
365
460
  // cellHeight (plus any in-progress resize). Handed to renderEvent so custom
@@ -374,15 +469,76 @@ function AnimatedEventBox<T>({
374
469
  [durationHours],
375
470
  );
376
471
 
377
- const boxStyle = useAnimatedStyle(
378
- () => ({
379
- // A top-edge resize pushes the box down and shortens it (start moves later).
380
- top: (startHours - minHour) * cellHeight.value + moveOffset.value + resizeStartDelta.value,
381
- 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,
382
495
  transform: [{ translateX: moveOffsetX.value }],
383
496
  zIndex: dragZ.value,
384
- }),
385
- [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
+ },
386
542
  );
387
543
 
388
544
  // Clear the drag preview once the committed change re-renders this box at its
@@ -518,6 +674,7 @@ function AnimatedEventBox<T>({
518
674
  .enabled(canMove)
519
675
  .onStart((event) => {
520
676
  dragZ.value = DRAG_EVENT_Z;
677
+ moving.value = 1;
521
678
  edgeDir.value = 0;
522
679
  lifted.value = 0;
523
680
  // Where in the box the finger grabbed, and the live finger position, so
@@ -536,8 +693,16 @@ function AnimatedEventBox<T>({
536
693
  if (lifted.value && pagerLeftSV && pagerTopSV && ghostXSV && ghostYSV) {
537
694
  ghostXSV.value = event.absoluteX - grabX.value - pagerLeftSV.value;
538
695
  ghostYSV.value = event.absoluteY - grabY.value - pagerTopSV.value;
539
- } else {
540
- 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;
541
706
  moveOffsetX.value = event.translationX;
542
707
  }
543
708
  // Dragging the finger to the pager's left/right edge arms an edge dwell
@@ -559,7 +724,17 @@ function AnimatedEventBox<T>({
559
724
  // gesture ends cleanly or is cancelled as the page moves under it), so the
560
725
  // drop lands even when onEnd doesn't fire on device. Nothing to do here.
561
726
  if (lifted.value) return;
562
- 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;
563
738
  // Map the horizontal drag to whole day columns, clamped so the event
564
739
  // can't leave the visible range.
565
740
  const rawDayDelta = dayWidth > 0 ? Math.round(event.translationX / dayWidth) : 0;
@@ -594,6 +769,10 @@ function AnimatedEventBox<T>({
594
769
  moveOffsetX.value = 0;
595
770
  lifted.value = 0;
596
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;
597
776
  runOnJS(releaseEdge)();
598
777
  });
599
778
  // Native: long-press to pick up. Web: activate past a small drag in either
@@ -610,6 +789,10 @@ function AnimatedEventBox<T>({
610
789
  moveOffset,
611
790
  moveOffsetX,
612
791
  dragZ,
792
+ moving,
793
+ dragStartSV,
794
+ minHour,
795
+ maxHour,
613
796
  edgeDir,
614
797
  lifted,
615
798
  grabX,
@@ -767,7 +950,7 @@ function AnimatedEventBox<T>({
767
950
  <RenderEventComponent
768
951
  event={positioned.event}
769
952
  mode={mode}
770
- boxHeight={boxHeight}
953
+ boxHeight={visibleBoxHeight}
771
954
  continuesBefore={positioned.continuesBefore}
772
955
  continuesAfter={positioned.continuesAfter}
773
956
  accessibilityActions={accessibilityActions}
@@ -801,7 +984,28 @@ function AnimatedEventBox<T>({
801
984
  // Only wrap in the move gesture when movable; the resize grips inside `box`
802
985
  // carry their own gestures, so a resize-only event still works.
803
986
  if (!canMove) return box;
804
- 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
+ );
805
1009
  }
806
1010
 
807
1011
  /** Replace the hour-axis label. Receives the hour (0–23) and the `ampm` flag. */
@@ -842,7 +1046,7 @@ const HourRow = ({
842
1046
  <View style={{ width: hourColumnWidth }}>{hourComponent(hour, ampm)}</View>
843
1047
  ) : (
844
1048
  <Text
845
- {...slot("hourLabel", {
1049
+ {...slot<TextStyle>("hourLabel", {
846
1050
  base: [styles.hourLabel, { width: hourColumnWidth }],
847
1051
  themed: [theme.text.hourLabel, { color: theme.colors.textMuted }],
848
1052
  })}
@@ -1617,8 +1821,11 @@ function TimetablePageInner<T>({
1617
1821
  positioned={positioned}
1618
1822
  cellHeight={heightSource}
1619
1823
  minHour={minHour}
1824
+ maxHour={maxHour}
1620
1825
  left={dayLeft(dayIndex) + positioned.column * columnWidth}
1621
1826
  width={columnWidth}
1827
+ dayLeftPx={dayLeft(dayIndex)}
1828
+ nextDayDirection={isRTL ? -1 : 1}
1622
1829
  dayWidth={dayWidth}
1623
1830
  dayIndex={dayIndex}
1624
1831
  dayCount={days.length}
@@ -1960,6 +2167,7 @@ function TimeGridInner<T>({
1960
2167
  containerWidth,
1961
2168
  hourColumnWidth,
1962
2169
  minHour: clampedMinHour,
2170
+ maxHour: clampedMaxHour,
1963
2171
  snapMinutes: Math.max(1, dragStepMinutes),
1964
2172
  onDragEvent,
1965
2173
  onChangeDate,
@@ -1982,6 +2190,7 @@ function TimeGridInner<T>({
1982
2190
  containerWidth: cw,
1983
2191
  hourColumnWidth: hcw,
1984
2192
  minHour: min,
2193
+ maxHour: max,
1985
2194
  snapMinutes: snap,
1986
2195
  onDragEvent: onDrag,
1987
2196
  onChangeDate: onDate,
@@ -2002,7 +2211,9 @@ function TimeGridInner<T>({
2002
2211
  const hoursFromMin =
2003
2212
  (ghostLocalY - gridTop.value - HOUR_LABEL_TOP_INSET + scrollY.value) / cellHeight.value;
2004
2213
  const rawMinutes = (min + hoursFromMin) * MINUTES_PER_HOUR;
2005
- 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);
2006
2217
  const start = new Date(days[col]);
2007
2218
  start.setHours(0, 0, 0, 0);
2008
2219
  start.setMinutes(minutes);
@@ -2129,6 +2340,7 @@ function TimeGridInner<T>({
2129
2340
  containerWidth,
2130
2341
  hourColumnWidth,
2131
2342
  minHour: clampedMinHour,
2343
+ maxHour: clampedMaxHour,
2132
2344
  snapMinutes: Math.max(1, dragStepMinutes),
2133
2345
  onDragEvent,
2134
2346
  onChangeDate,
@@ -2557,7 +2769,7 @@ const DefaultHeader = ({
2557
2769
  <View style={[styles.weekNumberGutter, { width: hourColumnWidth }]}>
2558
2770
  {showWeekNumber && hourColumnWidth > 0 && days[0] ? (
2559
2771
  <Text
2560
- {...slot("weekNumber", {
2772
+ {...slot<TextStyle>("weekNumber", {
2561
2773
  themed: [theme.text.hourLabel, { color: theme.colors.textMuted }],
2562
2774
  })}
2563
2775
  allowFontScaling={false}
@@ -2623,7 +2835,7 @@ const DayHeader = ({
2623
2835
  const content = (
2624
2836
  <>
2625
2837
  <Text
2626
- {...slot("columnHeaderWeekday", {
2838
+ {...slot<TextStyle>("columnHeaderWeekday", {
2627
2839
  themed: [{ color: theme.colors.textMuted }, theme.text.columnHeaderWeekday],
2628
2840
  })}
2629
2841
  allowFontScaling={false}
@@ -2641,7 +2853,7 @@ const DayHeader = ({
2641
2853
  })}
2642
2854
  >
2643
2855
  <Text
2644
- {...slot("columnHeaderDateText", {
2856
+ {...slot<TextStyle>("columnHeaderDateText", {
2645
2857
  themed: [
2646
2858
  theme.text.dayNumber,
2647
2859
  // The state colour stays last so a themed dayNumber can't break the