@super-calendar/native 2.5.0 → 2.7.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,286 @@
1
+ import { format, startOfDay, type Locale } from "date-fns";
2
+ import { memo, type ReactElement, useMemo, useState } from "react";
3
+ import {
4
+ type LayoutChangeEvent,
5
+ Pressable,
6
+ ScrollView,
7
+ StyleSheet,
8
+ Text,
9
+ View,
10
+ } from "react-native";
11
+ import { useCalendarTheme } from "../theme";
12
+ import type { CalendarEvent, WeekStartsOn } from "../types";
13
+ import { createSlots, type SlotStyleProps } from "../utils/slots";
14
+ import {
15
+ buildMonthWeeks,
16
+ getIsToday,
17
+ filterHiddenDays,
18
+ getWeekDays,
19
+ getYearMonths,
20
+ groupEventsByDay,
21
+ isBackgroundEvent,
22
+ isSameCalendarDay,
23
+ weekdayFormatToken,
24
+ } from "@super-calendar/core";
25
+
26
+ /**
27
+ * The styleable parts of {@link YearView}. `dayText` and `eventDot` are the
28
+ * text/marker inside a day cell (React Native text colour doesn't inherit).
29
+ */
30
+ export type YearViewSlot =
31
+ | "grid"
32
+ | "month"
33
+ | "monthTitle"
34
+ | "weekdays"
35
+ | "weekday"
36
+ | "week"
37
+ | "day"
38
+ | "dayText"
39
+ | "eventDot";
40
+
41
+ /** Props for {@link YearView}, the twelve mini-month year grid. */
42
+ export type YearViewProps<T = unknown> = SlotStyleProps<YearViewSlot> & {
43
+ /** Any date inside the year to render. */
44
+ date: Date;
45
+ /** Days holding at least one event get a dot. Omit for a plain year. */
46
+ events?: CalendarEvent<T>[];
47
+ weekStartsOn: WeekStartsOn;
48
+ /** Weekdays (0=Sunday…6=Saturday) hidden from the grid, e.g. `[0, 6]` for weekends off. */
49
+ hiddenDays?: number[];
50
+ locale?: Locale;
51
+ /** Highlight this date instead of the real "today". */
52
+ activeDate?: Date;
53
+ /**
54
+ * Smallest width a mini month may take before the grid drops a column
55
+ * (default 150). The grid fits 2–4 columns from the measured width.
56
+ */
57
+ minMonthWidth?: number;
58
+ /** Tap a day cell (e.g. to drill into the day or month view). */
59
+ onPressDay?: (date: Date) => void;
60
+ /** Tap a month's title (e.g. to jump to that month's view). */
61
+ onPressMonth?: (month: Date) => void;
62
+ };
63
+
64
+ // Seed before the first layout pass so the grid renders immediately (and in
65
+ // tests, where onLayout never fires).
66
+ const FALLBACK_COLUMNS = 3;
67
+
68
+ function YearViewInner<T>({
69
+ date,
70
+ events,
71
+ weekStartsOn,
72
+ hiddenDays,
73
+ locale,
74
+ activeDate,
75
+ minMonthWidth = 150,
76
+ onPressDay,
77
+ onPressMonth,
78
+ classNames,
79
+ styles: styleOverrides,
80
+ }: YearViewProps<T>): ReactElement {
81
+ const theme = useCalendarTheme();
82
+ const slot = createSlots<YearViewSlot>({ classNames, styles: styleOverrides });
83
+
84
+ const [columns, setColumns] = useState(FALLBACK_COLUMNS);
85
+ const handleLayout = (event: LayoutChangeEvent) => {
86
+ const width = event.nativeEvent.layout.width;
87
+ if (width <= 0) return;
88
+ setColumns(Math.max(2, Math.min(4, Math.floor(width / minMonthWidth))));
89
+ };
90
+
91
+ const months = useMemo(() => getYearMonths(date), [date]);
92
+ // Weekday initials for one shared header per mini month.
93
+ const weekdayLabels = useMemo(
94
+ () =>
95
+ filterHiddenDays(getWeekDays(date, weekStartsOn), hiddenDays).map((d) =>
96
+ format(d, weekdayFormatToken("narrow"), { locale }),
97
+ ),
98
+ [date, weekStartsOn, locale, hiddenDays],
99
+ );
100
+ // Days that hold at least one event, keyed like `groupEventsByDay`.
101
+ const eventDays = useMemo(
102
+ () =>
103
+ new Set(
104
+ events && events.length > 0
105
+ ? groupEventsByDay(events.filter((e) => !isBackgroundEvent(e))).keys()
106
+ : [],
107
+ ),
108
+ [events],
109
+ );
110
+
111
+ return (
112
+ <ScrollView onLayout={handleLayout} showsVerticalScrollIndicator>
113
+ <View {...slot("grid", { base: styles.grid })}>
114
+ {months.map((month) => {
115
+ const weeks = buildMonthWeeks(month, weekStartsOn, { hiddenDays });
116
+ const title = format(month, "MMMM", { locale });
117
+ const monthLabel = format(month, "MMMM yyyy", { locale });
118
+ const titleText = (
119
+ <Text
120
+ {...slot("monthTitle", {
121
+ base: styles.monthTitle,
122
+ themed: [styles.monthTitleText, { color: theme.colors.todayBackground }],
123
+ })}
124
+ allowFontScaling={false}
125
+ >
126
+ {title}
127
+ </Text>
128
+ );
129
+ return (
130
+ <View
131
+ key={month.toISOString()}
132
+ {...slot("month", { base: [styles.month, { width: `${100 / columns}%` }] })}
133
+ >
134
+ {onPressMonth ? (
135
+ <Pressable
136
+ onPress={() => onPressMonth(month)}
137
+ accessibilityRole="button"
138
+ accessibilityLabel={monthLabel}
139
+ >
140
+ {titleText}
141
+ </Pressable>
142
+ ) : (
143
+ <View accessible accessibilityRole="header" accessibilityLabel={monthLabel}>
144
+ {titleText}
145
+ </View>
146
+ )}
147
+ {/* Decorative initials: repeating 84 single letters across the twelve
148
+ mini months would drown a screen reader, so hide the rows (the
149
+ day cells carry full date labels). */}
150
+ <View
151
+ {...slot("weekdays", { base: styles.weekdays })}
152
+ accessibilityElementsHidden
153
+ importantForAccessibility="no-hide-descendants"
154
+ >
155
+ {weekdayLabels.map((label, i) => (
156
+ <Text
157
+ key={i}
158
+ {...slot("weekday", {
159
+ base: styles.weekday,
160
+ themed: [styles.weekdayText, { color: theme.colors.textMuted }],
161
+ })}
162
+ allowFontScaling={false}
163
+ >
164
+ {label}
165
+ </Text>
166
+ ))}
167
+ </View>
168
+ {weeks.map((week) => (
169
+ <View key={week[0].toISOString()} {...slot("week", { base: styles.week })}>
170
+ {week.map((day) => {
171
+ // Adjacent-month days keep the grid shape but stay blank,
172
+ // like the mini months of other year views.
173
+ if (day.getMonth() !== month.getMonth()) {
174
+ return <View key={day.toISOString()} style={styles.day} />;
175
+ }
176
+ const isHighlighted = activeDate
177
+ ? isSameCalendarDay(day, activeDate)
178
+ : getIsToday(day);
179
+ const hasEvents = eventDays.has(startOfDay(day).toISOString());
180
+ const label = `${format(day, "EEEE, d LLLL yyyy", { locale })}${
181
+ getIsToday(day) ? ", today" : ""
182
+ }${hasEvents ? ", has events" : ""}`;
183
+ const daySlot = slot("day", {
184
+ base: styles.day,
185
+ themed: isHighlighted
186
+ ? {
187
+ backgroundColor: theme.colors.todayBackground,
188
+ borderRadius: theme.todayBadgeRadius,
189
+ }
190
+ : undefined,
191
+ });
192
+ const content = (
193
+ <>
194
+ <Text
195
+ {...slot("dayText", {
196
+ themed: [
197
+ styles.dayText,
198
+ {
199
+ color: isHighlighted ? theme.colors.todayText : theme.colors.text,
200
+ },
201
+ ],
202
+ })}
203
+ allowFontScaling={false}
204
+ >
205
+ {day.getDate()}
206
+ </Text>
207
+ {hasEvents ? (
208
+ <View
209
+ {...slot("eventDot", {
210
+ base: styles.eventDot,
211
+ themed: {
212
+ backgroundColor: isHighlighted
213
+ ? theme.colors.todayText
214
+ : theme.colors.todayBackground,
215
+ },
216
+ })}
217
+ />
218
+ ) : null}
219
+ </>
220
+ );
221
+ return onPressDay ? (
222
+ <Pressable
223
+ key={day.toISOString()}
224
+ onPress={() => onPressDay(day)}
225
+ accessibilityRole="button"
226
+ accessibilityLabel={label}
227
+ {...daySlot}
228
+ >
229
+ {content}
230
+ </Pressable>
231
+ ) : (
232
+ <View
233
+ key={day.toISOString()}
234
+ accessible
235
+ accessibilityLabel={label}
236
+ {...daySlot}
237
+ >
238
+ {content}
239
+ </View>
240
+ );
241
+ })}
242
+ </View>
243
+ ))}
244
+ </View>
245
+ );
246
+ })}
247
+ </View>
248
+ </ScrollView>
249
+ );
250
+ }
251
+
252
+ /**
253
+ * A year at a glance: the twelve months as compact mini grids, with today
254
+ * highlighted and a dot under days that hold events. Tap a day or a month
255
+ * title to drill into a denser view. It's the view `Calendar` renders for
256
+ * `mode="year"`.
257
+ *
258
+ * @example
259
+ * ```tsx
260
+ * import { YearView } from "@super-calendar/native";
261
+ *
262
+ * <YearView
263
+ * date={new Date()}
264
+ * events={events}
265
+ * weekStartsOn={1}
266
+ * onPressDay={(day) => console.log(day)}
267
+ * />
268
+ * ```
269
+ */
270
+ export const YearView = memo(YearViewInner) as typeof YearViewInner;
271
+
272
+ const styles = StyleSheet.create({
273
+ grid: { flexDirection: "row", flexWrap: "wrap" },
274
+ month: { padding: 8 },
275
+ // Structural layout / themed typography split per slot, so a slot class can
276
+ // replace the look without breaking the layout.
277
+ monthTitle: { paddingBottom: 4 },
278
+ monthTitleText: { fontSize: 13, fontWeight: "700" },
279
+ weekdays: { flexDirection: "row" },
280
+ weekday: { flex: 1, textAlign: "center" },
281
+ weekdayText: { fontSize: 9, fontWeight: "600" },
282
+ week: { flexDirection: "row" },
283
+ day: { flex: 1, aspectRatio: 1, alignItems: "center", justifyContent: "center" },
284
+ dayText: { fontSize: 11 },
285
+ eventDot: { position: "absolute", bottom: 1, width: 3, height: 3, borderRadius: 2 },
286
+ });
package/src/index.tsx CHANGED
@@ -23,6 +23,7 @@
23
23
  export { Calendar, type CalendarProps, type CalendarSlot } from "./components/Calendar";
24
24
  export { Agenda, type AgendaProps, type AgendaSlot } from "./components/Agenda";
25
25
  export { MonthView, type MonthViewProps, type MonthViewSlot } from "./components/MonthView";
26
+ export { YearView, type YearViewProps, type YearViewSlot } from "./components/YearView";
26
27
  export { type ResolvedSlot, type SlotDefault, type SlotStyleProps } from "./utils/slots";
27
28
  export { MonthPager, type MonthPagerProps } from "./components/MonthPager";
28
29
  export { MonthList, type MonthListProps, type MonthListSlot } from "./components/MonthList";
@@ -54,6 +55,7 @@ export {
54
55
  } from "./theme";
55
56
  export type {
56
57
  BusinessHours,
58
+ BusinessHoursBand,
57
59
  CalendarEvent,
58
60
  CalendarMode,
59
61
  EventAccessibilityLabelContext,
@@ -79,6 +81,8 @@ export {
79
81
  isWithinDateRange,
80
82
  nextDateRange,
81
83
  useDateRange,
84
+ useEventSource,
85
+ useNow,
82
86
  } from "@super-calendar/core";
83
87
  export {
84
88
  buildMonthGrid,
@@ -107,4 +111,10 @@ export {
107
111
  isSameCalendarDay,
108
112
  minutesIntoDay,
109
113
  } from "@super-calendar/core";
110
- export { layoutDayEvents, isAllDayEvent, type PositionedEvent } from "@super-calendar/core";
114
+ export {
115
+ layoutDayEvents,
116
+ isAllDayEvent,
117
+ isBackgroundEvent,
118
+ backgroundBandsForDay,
119
+ type PositionedEvent,
120
+ } from "@super-calendar/core";
package/src/types.ts CHANGED
@@ -12,6 +12,7 @@ import type { CalendarEvent, CalendarMode } from "@super-calendar/core";
12
12
  // importing them from "../types".
13
13
  export type {
14
14
  BusinessHours,
15
+ BusinessHoursBand,
15
16
  CalendarEvent,
16
17
  CalendarMode,
17
18
  EventAccessibilityLabelContext,