@super-calendar/native 2.8.2 → 2.9.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.
@@ -0,0 +1,164 @@
1
+ import { addDays, differenceInCalendarDays, startOfDay } from "date-fns";
2
+ import { useMemo, useState } from "react";
3
+ import Animated, {
4
+ runOnJS,
5
+ type SharedValue,
6
+ useAnimatedReaction,
7
+ useAnimatedStyle,
8
+ useDerivedValue,
9
+ } from "react-native-reanimated";
10
+ import { shiftMinutes } from "@super-calendar/core";
11
+ import type { CalendarEvent, CalendarMode, RenderEvent } from "../types";
12
+ import { useCalendarTheme } from "../theme";
13
+ import { useSlots } from "../utils/slots";
14
+ import type { TimeGridSlot } from "./TimeGrid";
15
+
16
+ export type MultiDayMove<T> = {
17
+ event: CalendarEvent<T>;
18
+ eventIndex: number;
19
+ phase: "dragging" | "dropped";
20
+ dayIndex: number;
21
+ offsetX: SharedValue<number>;
22
+ offsetY: SharedValue<number>;
23
+ lifted: SharedValue<number>;
24
+ };
25
+
26
+ type PreviewProps<T> = {
27
+ move: MultiDayMove<T>;
28
+ days: Date[];
29
+ cellHeight: SharedValue<number>;
30
+ dayWidth: number;
31
+ hourColumnWidth: number;
32
+ minHour: number;
33
+ mode: CalendarMode;
34
+ renderEvent: RenderEvent<T>;
35
+ };
36
+
37
+ const noop = () => {};
38
+ const MIN_EVENT_HEIGHT = 32;
39
+
40
+ // Mount once on grab. Only numbers and shared values cross to the UI thread;
41
+ // moving the pointer does not rerender the page or rebuild its gestures.
42
+ export function MultiDayMovePreview<T>(props: PreviewProps<T>) {
43
+ const { move, days } = props;
44
+ const dayOffsets = useMemo(
45
+ () => days.map((day) => differenceInCalendarDays(day, days[move.dayIndex]) * 1440),
46
+ [days, move],
47
+ );
48
+ return days.map((day, index) => (
49
+ <PreviewDay
50
+ key={day.toISOString()}
51
+ {...props}
52
+ dayOffsets={dayOffsets}
53
+ index={index}
54
+ dayStart={startOfDay(day).getTime()}
55
+ dayEnd={addDays(startOfDay(day), 1).getTime()}
56
+ />
57
+ ));
58
+ }
59
+
60
+ function PreviewDay<T>({
61
+ move,
62
+ cellHeight,
63
+ dayWidth,
64
+ hourColumnWidth,
65
+ minHour,
66
+ mode,
67
+ renderEvent: RenderEventComponent,
68
+ dayOffsets,
69
+ index,
70
+ dayStart,
71
+ dayEnd,
72
+ }: PreviewProps<T> & {
73
+ dayOffsets: number[];
74
+ index: number;
75
+ dayStart: number;
76
+ dayEnd: number;
77
+ }) {
78
+ const theme = useCalendarTheme();
79
+ const slot = useSlots<TimeGridSlot>();
80
+ const { offsetX, offsetY, lifted, dayIndex, event } = move;
81
+ const duration = event.end.getTime() - event.start.getTime();
82
+ const startTimestamp = event.start.getTime();
83
+ const segment = useDerivedValue(() => {
84
+ const columnDelta = dayWidth > 0 ? Math.round(offsetX.value / dayWidth) : 0;
85
+ const target = Math.min(Math.max(dayIndex + columnDelta, 0), dayOffsets.length - 1);
86
+ const minuteDelta = cellHeight.value > 0 ? (offsetY.value / cellHeight.value) * 60 : 0;
87
+ const start = shiftMinutes(
88
+ new Date(startTimestamp),
89
+ dayOffsets[target] + minuteDelta,
90
+ ).getTime();
91
+ const end = start + duration;
92
+ const visible = !lifted.value && start < dayEnd && end > dayStart;
93
+ return {
94
+ top: (Math.max(start, dayStart) - dayStart) / 3_600_000,
95
+ hours: visible
96
+ ? Math.max((Math.min(end, dayEnd) - Math.max(start, dayStart)) / 3_600_000, 0.25)
97
+ : 0,
98
+ continuesBefore: start < dayStart,
99
+ continuesAfter: end > dayEnd,
100
+ };
101
+ }, [dayWidth, dayIndex, dayOffsets, startTimestamp, duration, dayStart, dayEnd]);
102
+ const boxHeight = useDerivedValue(() =>
103
+ segment.value.hours > 0
104
+ ? Math.max(segment.value.hours * cellHeight.value, MIN_EVENT_HEIGHT)
105
+ : 0,
106
+ );
107
+ const style = useAnimatedStyle(
108
+ () => ({
109
+ top: (segment.value.top - minHour) * cellHeight.value,
110
+ height: boxHeight.value,
111
+ opacity: boxHeight.value > 0 ? 1 : 0,
112
+ }),
113
+ [minHour],
114
+ );
115
+ const [continuation, setContinuation] = useState(() => ({
116
+ continuesBefore: segment.value.continuesBefore,
117
+ continuesAfter: segment.value.continuesAfter,
118
+ }));
119
+ useAnimatedReaction(
120
+ () => ({
121
+ continuesBefore: segment.value.continuesBefore,
122
+ continuesAfter: segment.value.continuesAfter,
123
+ }),
124
+ (next, previous) => {
125
+ if (
126
+ previous &&
127
+ (next.continuesBefore !== previous.continuesBefore ||
128
+ next.continuesAfter !== previous.continuesAfter)
129
+ ) {
130
+ runOnJS(setContinuation)(next);
131
+ }
132
+ },
133
+ );
134
+ const eventSlot = slot("event", { themed: theme.containers.timeGridEvent });
135
+ return (
136
+ <Animated.View
137
+ {...eventSlot}
138
+ testID="multi-day-move-preview"
139
+ pointerEvents="none"
140
+ accessibilityElementsHidden
141
+ importantForAccessibility="no-hide-descendants"
142
+ style={[
143
+ {
144
+ position: "absolute",
145
+ overflow: "hidden",
146
+ padding: 2,
147
+ left: hourColumnWidth + index * dayWidth,
148
+ width: dayWidth,
149
+ zIndex: 100,
150
+ },
151
+ eventSlot.style,
152
+ style,
153
+ ]}
154
+ >
155
+ <RenderEventComponent
156
+ event={event}
157
+ mode={mode}
158
+ boxHeight={boxHeight}
159
+ {...continuation}
160
+ onPress={noop}
161
+ />
162
+ </Animated.View>
163
+ );
164
+ }