@super-calendar/native 2.18.0 → 2.20.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.
@@ -0,0 +1,201 @@
1
+ import { memo, useCallback, useLayoutEffect, useRef, type ComponentRef } from "react";
2
+ import {
3
+ type LayoutChangeEvent,
4
+ type LayoutRectangle,
5
+ Platform,
6
+ StyleSheet,
7
+ TouchableOpacity,
8
+ View,
9
+ type StyleProp,
10
+ type ViewStyle,
11
+ } from "react-native";
12
+ import type { ResolvedSlot } from "../utils/slots";
13
+
14
+ export interface MonthCellGeometry {
15
+ minimumHeight: number;
16
+ top: number;
17
+ bottom: number;
18
+ gap: number;
19
+ overlayTop: number;
20
+ weekMargin: number;
21
+ weekMarginTop: number;
22
+ }
23
+
24
+ type ProbeView = ComponentRef<typeof View>;
25
+ type Part = "frame" | "row" | "cell" | "date" | "lane" | "overlay";
26
+ const MARKER_HEIGHT = 1;
27
+ const hiddenProbeProps = {
28
+ pointerEvents: "none",
29
+ accessible: false,
30
+ accessibilityElementsHidden: true,
31
+ importantForAccessibility: "no-hide-descendants",
32
+ ...(Platform.OS === "web" ? { "aria-hidden": true, inert: "inert" } : {}),
33
+ } as const;
34
+
35
+ // Zero-height weeks leave only the grid's padding, borders and gaps. The outer
36
+ // box also includes its margins. Neither measurement depends on the drawn rows.
37
+ export const MonthGridSpacingProbe = memo(function MonthGridSpacingProbe({
38
+ grid,
39
+ weeks,
40
+ onInnerHeight,
41
+ onOuterHeight,
42
+ }: {
43
+ grid: ResolvedSlot<ViewStyle>;
44
+ weeks: number;
45
+ onInnerHeight: (height: number) => void;
46
+ onOuterHeight: (height: number) => void;
47
+ }) {
48
+ return (
49
+ <View
50
+ {...hiddenProbeProps}
51
+ style={styles.frame}
52
+ onLayout={(e) => onOuterHeight(e.nativeEvent.layout.height)}
53
+ >
54
+ <View
55
+ {...grid}
56
+ style={[grid.style, styles.gridSpacing]}
57
+ onLayout={(e) => onInnerHeight(e.nativeEvent.layout.height)}
58
+ >
59
+ {Array.from({ length: weeks }, (_, index) => (
60
+ <View key={index} style={styles.emptyWeek} />
61
+ ))}
62
+ </View>
63
+ </View>
64
+ );
65
+ });
66
+
67
+ // The two markers take the place of the date and the first event. Their natural
68
+ // positions include spacing applied by a class, including on the enclosing week.
69
+ // The probe has no fixed height, so measuring it cannot grow the visible rows.
70
+ export const MonthCellProbe = memo(function MonthCellProbe({
71
+ id,
72
+ week,
73
+ day,
74
+ cellStyle,
75
+ dayWidth,
76
+ weekWidth,
77
+ onMeasure,
78
+ }: {
79
+ id: string;
80
+ week: ResolvedSlot<ViewStyle>;
81
+ day: ResolvedSlot<ViewStyle>;
82
+ cellStyle: StyleProp<ViewStyle>;
83
+ dayWidth: number;
84
+ weekWidth: number;
85
+ onMeasure: (id: string, geometry: MonthCellGeometry) => void;
86
+ }) {
87
+ const layouts = useRef<Partial<Record<Part, LayoutRectangle>>>({});
88
+ const nodes = useRef<Partial<Record<Part, ProbeView | null>>>({});
89
+ const report = useCallback(
90
+ (
91
+ height: number,
92
+ row: Pick<LayoutRectangle, "y" | "height">,
93
+ top: number,
94
+ laneTop: number,
95
+ laneHeight: number,
96
+ overlayTop: number,
97
+ ) => {
98
+ if (height > 0)
99
+ onMeasure(id, {
100
+ minimumHeight: height,
101
+ top,
102
+ gap: Math.max(0, laneTop - top - MARKER_HEIGHT),
103
+ bottom: height - laneTop - laneHeight,
104
+ overlayTop,
105
+ weekMargin: height - row.height,
106
+ weekMarginTop: row.y,
107
+ });
108
+ },
109
+ [id, onMeasure],
110
+ );
111
+
112
+ // React Native Web's onLayout observes resizing. Changing padding can move both
113
+ // markers without resizing them. Read their layout offsets before ancestor transforms.
114
+ const readWeb = useCallback(() => {
115
+ if (Platform.OS !== "web" || typeof HTMLElement === "undefined") return false;
116
+ const frame = nodes.current.frame;
117
+ if (!(frame instanceof HTMLElement)) return false;
118
+ const layout = (part: Part) => {
119
+ const node = nodes.current[part];
120
+ if (!(node instanceof HTMLElement)) return;
121
+ let y = 0;
122
+ let current: HTMLElement = node;
123
+ while (current !== frame) {
124
+ const parent = current.offsetParent;
125
+ if (!(parent instanceof HTMLElement)) return;
126
+ y += current.offsetTop + parent.clientTop;
127
+ current = parent;
128
+ }
129
+ return { y, height: node.offsetHeight };
130
+ };
131
+ const row = layout("row"),
132
+ date = layout("date"),
133
+ lane = layout("lane"),
134
+ overlay = layout("overlay");
135
+ if (!row || !date || !lane || !overlay) return false;
136
+ report(frame.offsetHeight, row, date.y, lane.y, lane.height, overlay.y);
137
+ return true;
138
+ }, [report]);
139
+
140
+ useLayoutEffect(() => {
141
+ readWeb();
142
+ }, [readWeb, week, day, cellStyle, dayWidth, weekWidth]);
143
+
144
+ function observe(part: Part) {
145
+ return {
146
+ ...(Platform.OS === "web" && {
147
+ ref: (node: ProbeView | null) => {
148
+ nodes.current[part] = node;
149
+ },
150
+ }),
151
+ onLayout: (event: LayoutChangeEvent) => {
152
+ if (readWeb()) return;
153
+ layouts.current[part] = event.nativeEvent.layout;
154
+ const { frame, row, cell, date, lane, overlay } = layouts.current;
155
+ if (!frame || !row || !cell || !date || !lane || !overlay) return;
156
+ const cellTop = row.y + cell.y;
157
+ report(
158
+ frame.height,
159
+ row,
160
+ cellTop + date.y,
161
+ cellTop + lane.y,
162
+ lane.height,
163
+ row.y + overlay.y,
164
+ );
165
+ },
166
+ };
167
+ }
168
+ return (
169
+ <View {...observe("frame")} style={styles.frame} collapsable={false} {...hiddenProbeProps}>
170
+ <View
171
+ {...observe("row")}
172
+ {...week}
173
+ style={[week.style, styles.intrinsic, { width: weekWidth }]}
174
+ collapsable={false}
175
+ >
176
+ <TouchableOpacity
177
+ {...day}
178
+ style={[day.style, cellStyle, styles.measuredDay, { width: dayWidth }]}
179
+ disabled
180
+ onLayout={observe("cell").onLayout}
181
+ >
182
+ <View {...observe("date")} style={styles.marker} collapsable={false} />
183
+ <View {...observe("lane")} style={styles.laneMarker} collapsable={false} />
184
+ </TouchableOpacity>
185
+ <View {...observe("overlay")} style={StyleSheet.absoluteFill} collapsable={false} />
186
+ </View>
187
+ </View>
188
+ );
189
+ });
190
+
191
+ const styles = StyleSheet.create({
192
+ frame: { position: "absolute", top: 0, left: 0, right: 0, opacity: 0 },
193
+ gridSpacing: { height: "auto", minHeight: 0, maxHeight: "auto", flex: 0 },
194
+ emptyWeek: { height: 0 },
195
+ intrinsic: { height: "auto", minHeight: 0 },
196
+ // The visible cell supplies the width, including changes from gaps and hidden days.
197
+ measuredDay: { justifyContent: "flex-start", alignSelf: "stretch", flex: 0, flexBasis: "auto" },
198
+ marker: { height: MARKER_HEIGHT },
199
+ // Fill space reserved by the day height so it does not count as bottom padding.
200
+ laneMarker: { height: MARKER_HEIGHT, flexGrow: 1 },
201
+ });
@@ -44,9 +44,11 @@ import {
44
44
  type EventAccessibilityLabeler,
45
45
  type WeekdayFormat,
46
46
  isDateSelectable,
47
+ type CalendarLabels,
48
+ buildMonthWeeks,
49
+ getWeekDays,
50
+ weekdayFormatToken,
47
51
  } from "@super-calendar/core";
48
- import type { CalendarLabels } from "@super-calendar/core";
49
- import { buildMonthWeeks, getWeekDays, weekdayFormatToken } from "@super-calendar/core";
50
52
  import { createSlots, type SlotStyleProps } from "../utils/slots";
51
53
  import { DefaultMonthEvent } from "./DefaultMonthEvent";
52
54
  import { MonthView, type MonthViewSlot } from "./MonthView";
@@ -42,6 +42,7 @@ import type {
42
42
  EventDragHandler,
43
43
  EventDragStartHandler,
44
44
  EventKeyExtractor,
45
+ MonthDateInfo,
45
46
  RenderEvent,
46
47
  WeekStartsOn,
47
48
  } from "../types";
@@ -55,8 +56,8 @@ import {
55
56
  filterHiddenDays,
56
57
  getWeekDays,
57
58
  weekdayFormatToken,
59
+ type CalendarLabels,
58
60
  } from "@super-calendar/core";
59
- import type { CalendarLabels } from "@super-calendar/core";
60
61
  import { createSlots, type SlotStyleProps } from "../utils/slots";
61
62
  import { useWebPagerKeys } from "../utils/useWebPagerKeys";
62
63
  import { DateRows, type DateRowsSource, type MonthViewSlot } from "./MonthView";
@@ -143,8 +144,8 @@ export type MonthPagerProps<T> = {
143
144
  activeDate?: Date;
144
145
  /** Replace the weekday-label header above the month grid. Receives the week's days. */
145
146
  renderHeaderForMonthView?: (weekDays: Date[]) => React.ReactNode;
146
- /** Replace the default date badge in each day cell. Receives the day. */
147
- renderCustomDateForMonth?: (date: Date) => React.ReactNode;
147
+ /** Replace the default date badge in each day cell. Receives the day and its state. */
148
+ renderCustomDateForMonth?: (date: Date, info: MonthDateInfo) => React.ReactNode;
148
149
  } & SlotStyleProps<MonthViewSlot>;
149
150
 
150
151
  /**