@super-calendar/native 2.6.0 → 2.8.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.
@@ -1,8 +1,12 @@
1
1
  import { format, type Locale, isSameMonth, startOfDay } from "date-fns";
2
2
  import { memo, type ReactElement, useMemo, useState } from "react";
3
3
  import {
4
+ type DimensionValue,
4
5
  type LayoutChangeEvent,
6
+ Modal,
5
7
  Platform,
8
+ Pressable,
9
+ ScrollView,
6
10
  StyleSheet,
7
11
  type StyleProp,
8
12
  Text,
@@ -30,12 +34,19 @@ import { dayBadgeKind, rangeBandKind } from "@super-calendar/core";
30
34
  import {
31
35
  buildMonthWeeks,
32
36
  getIsToday,
37
+ filterHiddenDays,
33
38
  getWeekDays,
34
39
  isSameCalendarDay,
35
40
  isWeekend,
36
41
  } from "@super-calendar/core";
37
42
  import { monthEventCapacity, monthVisibleCount } from "@super-calendar/core";
38
- import { compareDayEvents, groupEventsByDay, isAllDayEvent } from "@super-calendar/core";
43
+ import { layoutMonthWeek, type MonthWeekEvents } from "@super-calendar/core";
44
+ import {
45
+ compareDayEvents,
46
+ groupEventsByDay,
47
+ isAllDayEvent,
48
+ isBackgroundEvent,
49
+ } from "@super-calendar/core";
39
50
 
40
51
  // Day-cell metrics, mirrored from the styles below, used to estimate how many
41
52
  // event chips fit when auto-fitting `maxVisibleEventCount`.
@@ -45,12 +56,41 @@ const DATE_BADGE_HEIGHT = 24;
45
56
  const BAND_CENTER_Y = DAY_CELL_PADDING_TOP + DATE_BADGE_HEIGHT / 2;
46
57
  const CELL_ROW_GAP = 2;
47
58
  const CHIP_PADDING_V = 2;
59
+ // Where the first event row sits below the date badge (padding + badge + gap).
60
+ const BADGE_AREA = DAY_CELL_PADDING_TOP + DATE_BADGE_HEIGHT + CELL_ROW_GAP;
61
+ // Stable empty layout for the events-free picker (avoids a fresh object per render).
62
+ const EMPTY_LAYOUT = { segments: [], laneCount: 0 } as const;
63
+ // Horizontal inset of a chip within its cell (mirrors `styles.monthEvent`).
64
+ const CHIP_INSET_H = 4;
48
65
  // Pre-measure fallback so the first paint isn't empty or overflowing.
49
66
  const FALLBACK_VISIBLE_COUNT = 3;
50
67
 
51
68
  const numericStyle = (value: number | string | undefined, fallback: number) =>
52
69
  typeof value === "number" ? value : fallback;
53
70
 
71
+ // The distinct events touching a week row, in the per-day index's sorted order.
72
+ // A multi-day event lives in each covered day's bucket, so dedupe by reference.
73
+ function collectRowEvents<T>(
74
+ week: Date[],
75
+ eventsByDay: ReadonlyMap<string, CalendarEvent<T>[]>,
76
+ ): CalendarEvent<T>[] {
77
+ const seen = new Set<CalendarEvent<T>>();
78
+ const out: CalendarEvent<T>[] = [];
79
+ for (const day of week) {
80
+ const list = eventsByDay.get(startOfDay(day).toISOString());
81
+ if (!list) continue;
82
+ for (const event of list) {
83
+ if (!seen.has(event)) {
84
+ seen.add(event);
85
+ out.push(event);
86
+ }
87
+ }
88
+ }
89
+ return out;
90
+ }
91
+
92
+ const pct = (n: number): DimensionValue => `${n}%` as DimensionValue;
93
+
54
94
  /**
55
95
  * The styleable parts of {@link MonthView}. Mirrors the dom renderer's slot
56
96
  * names where the structure matches; `dayBadgeText` is native-only (React
@@ -67,20 +107,26 @@ export type MonthViewSlot =
67
107
  | "dayBadge"
68
108
  | "dayBadgeText"
69
109
  | "rangeBand"
70
- | "more";
110
+ | "more"
111
+ | "morePopover";
71
112
 
72
113
  /** Props for {@link MonthView}, the single-month grid. */
73
114
  export type MonthViewProps<T> = SlotStyleProps<MonthViewSlot> & {
74
115
  date: Date;
75
116
  events: CalendarEvent<T>[];
76
117
  /**
77
- * Max event chips per day cell. Omit to auto-fit as many as the cell height
78
- * allows (the default); set a number for a fixed cap. Extra events collapse
79
- * into a "+N more" label. Auto-fit assumes the built-in chip size — pass an
118
+ * Max event lanes (stacked rows) shown per week before the rest of a day's
119
+ * events collapse into a "+N more" label. Because multi-day events draw as one
120
+ * bar spanning a lane across the week, this caps lanes per row, not chips per
121
+ * day, so a long bar can push a lightly-booked day's own events into "+N more".
122
+ * Omit to auto-fit as many lanes as the cell height allows (the default); set a
123
+ * number for a fixed cap. Auto-fit assumes the built-in chip size — pass an
80
124
  * explicit value when using a custom `renderEvent`.
81
125
  */
82
126
  maxVisibleEventCount?: number;
83
127
  weekStartsOn: WeekStartsOn;
128
+ /** Weekdays (0=Sunday…6=Saturday) hidden from the grid, e.g. `[0, 6]` for weekends off. */
129
+ hiddenDays?: number[];
84
130
  /** Weekday header label width: `narrow` ("M"), `short` ("Mon", default), or `long` ("Monday"). */
85
131
  weekdayFormat?: WeekdayFormat;
86
132
  locale?: Locale;
@@ -148,6 +194,7 @@ function MonthViewInner<T>({
148
194
  events,
149
195
  maxVisibleEventCount,
150
196
  weekStartsOn,
197
+ hiddenDays,
151
198
  weekdayFormat = "short",
152
199
  locale,
153
200
  sortedMonthView = true,
@@ -203,18 +250,34 @@ function MonthViewInner<T>({
203
250
  // Picker: which day is being pressed, so the tap dims only its badge (the circle),
204
251
  // not the whole cell background. Stays null in the events calendar.
205
252
  const [pressedKey, setPressedKey] = useState<string | null>(null);
253
+ // Built-in "+N more" popover: opens when the consumer doesn't handle
254
+ // `onPressMore` themselves; lists the day's events in a modal card.
255
+ const [moreOpenFor, setMoreOpenFor] = useState<{
256
+ day: Date;
257
+ events: CalendarEvent<T>[];
258
+ } | null>(null);
206
259
 
207
260
  const weeks = useMemo(
208
- () => buildMonthWeeks(date, weekStartsOn, { showSixWeeks, isRTL }),
209
- [date, weekStartsOn, isRTL, showSixWeeks],
261
+ () => buildMonthWeeks(date, weekStartsOn, { showSixWeeks, isRTL, hiddenDays }),
262
+ [date, weekStartsOn, isRTL, showSixWeeks, hiddenDays],
210
263
  );
211
264
 
212
265
  // Weekday labels for the header row (any week works; reuse this month). Reversed
213
266
  // in RTL so they line up with the mirrored day columns.
214
267
  const weekdayLabels = useMemo(() => {
215
- const days = getWeekDays(date, weekStartsOn);
268
+ const days = filterHiddenDays(getWeekDays(date, weekStartsOn), hiddenDays);
216
269
  return isRTL ? days.reverse() : days;
217
- }, [date, weekStartsOn, isRTL]);
270
+ }, [date, weekStartsOn, isRTL, hiddenDays]);
271
+
272
+ // The built-in chip's height and per-row stride, from the theme's title type.
273
+ // Shared by the capacity estimate and the spanning-bar overlay so the bars line
274
+ // up exactly with the rows the cells reserve for them.
275
+ const chipMetrics = useMemo(() => {
276
+ const fontSize = numericStyle(theme.text.eventTitle.fontSize, 12);
277
+ const lineHeight = numericStyle(theme.text.eventTitle.lineHeight, Math.ceil(fontSize * 1.3));
278
+ const chipHeight = lineHeight + CHIP_PADDING_V * 2;
279
+ return { chipHeight, chipRowHeight: chipHeight + CELL_ROW_GAP };
280
+ }, [theme]);
218
281
 
219
282
  // How many chips fit per cell: a fixed cap when `maxVisibleEventCount` is set,
220
283
  // else derived from the measured cell height and the (default) chip metrics.
@@ -226,21 +289,19 @@ function MonthViewInner<T>({
226
289
  return { full: FALLBACK_VISIBLE_COUNT, withMore: FALLBACK_VISIBLE_COUNT };
227
290
  }
228
291
  const rowHeight = gridHeight / weeks.length;
229
- const fontSize = numericStyle(theme.text.eventTitle.fontSize, 12);
230
- const lineHeight = numericStyle(theme.text.eventTitle.lineHeight, Math.ceil(fontSize * 1.3));
231
- const chipRowHeight = lineHeight + CHIP_PADDING_V * 2 + CELL_ROW_GAP;
232
292
  const moreFontSize = numericStyle(theme.text.more.fontSize, 11);
233
293
  const moreRowHeight = Math.ceil(moreFontSize * 1.3) + CELL_ROW_GAP;
234
294
  const available = rowHeight - DAY_CELL_PADDING_TOP - DATE_BADGE_HEIGHT;
235
- return monthEventCapacity(available, chipRowHeight, moreRowHeight);
236
- }, [maxVisibleEventCount, gridHeight, weeks.length, theme]);
295
+ return monthEventCapacity(available, chipMetrics.chipRowHeight, moreRowHeight);
296
+ }, [maxVisibleEventCount, gridHeight, weeks.length, theme, chipMetrics]);
237
297
 
238
298
  // Group events by calendar day once per `events` change (shared with the dom
239
299
  // renderer via core's `groupEventsByDay`), rather than scanning the whole list
240
300
  // inside every one of the (up to) 42 day cells on each render. Multi-day events
241
301
  // are indexed under every day they span.
242
302
  const eventsByDay = useMemo(() => {
243
- const map = groupEventsByDay(events);
303
+ // Background events shade the time grid; the month grid ignores them.
304
+ const map = groupEventsByDay(events.filter((event) => !isBackgroundEvent(event)));
244
305
  if (sortedMonthView) {
245
306
  // All-day events head the day, then timed events by start (shared with dom).
246
307
  for (const list of map.values()) list.sort(compareDayEvents);
@@ -252,7 +313,21 @@ function MonthViewInner<T>({
252
313
  // picker reads cleaner without it (matching the dom renderer).
253
314
  const showGrid = events.length > 0;
254
315
 
255
- const renderDay = (day: Date) => {
316
+ // Lay out each week row's spanning bars once per data change, not per render.
317
+ const weekLayouts = useMemo(
318
+ () =>
319
+ showGrid
320
+ ? weeks.map((week) => layoutMonthWeek(week, collectRowEvents(week, eventsByDay)))
321
+ : [],
322
+ [showGrid, weeks, eventsByDay],
323
+ );
324
+
325
+ const renderDay = (
326
+ day: Date,
327
+ dayCol: number,
328
+ rowLayout: MonthWeekEvents<T>,
329
+ visibleLanes: number,
330
+ ) => {
256
331
  const isCurrentMonth = isSameMonth(day, date);
257
332
 
258
333
  // Blank out adjacent-month days when they're hidden, keeping the grid shape.
@@ -288,8 +363,12 @@ function MonthViewInner<T>({
288
363
  { selectedDates, selectedRange },
289
364
  { minDate, maxDate, isDateDisabled },
290
365
  );
291
- const visibleCount = monthVisibleCount(dayEvents.length, capacity);
292
- const hiddenCount = dayEvents.length - visibleCount;
366
+ // Events past the visible lanes (this day's segments the row can't show)
367
+ // collapse into "+N more"; the popover still lists the whole day.
368
+ const hiddenEvents = rowLayout.segments
369
+ .filter((s) => s.startCol <= dayCol && s.endCol >= dayCol && s.lane >= visibleLanes)
370
+ .map((s) => s.event);
371
+ const hiddenCount = hiddenEvents.length;
293
372
 
294
373
  // The range shows as a band behind the days; endpoints and discrete selected
295
374
  // days get a filled badge on top. Today's badge wins when it coincides. The
@@ -456,23 +535,14 @@ function MonthViewInner<T>({
456
535
  </Text>
457
536
  </View>
458
537
  )}
459
- {dayEvents.slice(0, visibleCount).map((event, index) => (
538
+ {/* Reserve one row per visible lane so the overlay bars have space and
539
+ "+more" sits below them; the bars themselves render in the row overlay
540
+ (they span cells). */}
541
+ {Array.from({ length: visibleLanes }, (_, i) => (
460
542
  <View
461
- key={keyExtractor(event, index)}
462
- style={[styles.monthEvent, theme.containers.monthEvent]}
463
- >
464
- <RenderEventComponent
465
- event={event}
466
- mode="month"
467
- isAllDay={isAllDayEvent(event)}
468
- onPress={disableMonthEventCellPress ? () => {} : () => onPressEvent(event)}
469
- onLongPress={
470
- disableMonthEventCellPress || !onLongPressEvent
471
- ? undefined
472
- : () => onLongPressEvent(event)
473
- }
474
- />
475
- </View>
543
+ key={`lane-${i}`}
544
+ style={{ height: chipMetrics.chipHeight, pointerEvents: "none" }}
545
+ />
476
546
  ))}
477
547
  {hiddenCount > 0 ? (
478
548
  <Text
@@ -480,7 +550,11 @@ function MonthViewInner<T>({
480
550
  base: styles.moreLabel,
481
551
  themed: [theme.text.more, { color: theme.colors.textMuted }],
482
552
  })}
483
- onPress={onPressMore ? () => onPressMore(dayEvents, day) : undefined}
553
+ onPress={
554
+ onPressMore
555
+ ? () => onPressMore(dayEvents, day)
556
+ : () => setMoreOpenFor({ day, events: dayEvents })
557
+ }
484
558
  accessibilityRole="button"
485
559
  accessibilityLabel={`Show ${hiddenCount} more events`}
486
560
  allowFontScaling={false}
@@ -531,15 +605,118 @@ function MonthViewInner<T>({
531
605
  ))}
532
606
  </View>
533
607
  ) : null}
534
- <View {...slot("grid", { base: styles.container })} onLayout={handleLayout}>
535
- {weeks.map((week) => (
536
- <View
537
- {...slot("week", { base: styles.weekRow, themed: theme.containers.weekRow })}
538
- key={week[0].toISOString()}
608
+ {moreOpenFor ? (
609
+ <Modal transparent animationType="fade" visible onRequestClose={() => setMoreOpenFor(null)}>
610
+ <Pressable
611
+ style={styles.moreBackdrop}
612
+ accessibilityLabel="Close"
613
+ onPress={() => setMoreOpenFor(null)}
539
614
  >
540
- {week.map((day) => renderDay(day))}
541
- </View>
542
- ))}
615
+ <Pressable
616
+ // Swallow taps on the card so only the backdrop dismisses.
617
+ onPress={() => {}}
618
+ {...slot("morePopover", {
619
+ base: styles.moreCard,
620
+ themed: {
621
+ backgroundColor: theme.colors.surface,
622
+ borderColor: theme.colors.gridLine,
623
+ },
624
+ })}
625
+ >
626
+ <Text
627
+ accessibilityRole="header"
628
+ style={[styles.moreCardTitle, { color: theme.colors.text }]}
629
+ allowFontScaling={false}
630
+ >
631
+ {format(moreOpenFor.day, "EEEE, d LLLL yyyy", { locale })}
632
+ </Text>
633
+ <ScrollView>
634
+ {moreOpenFor.events.map((event, index) => (
635
+ <View key={keyExtractor(event, index)} style={styles.moreCardRow}>
636
+ <RenderEventComponent
637
+ event={event}
638
+ mode="month"
639
+ isAllDay={isAllDayEvent(event)}
640
+ onPress={() => {
641
+ setMoreOpenFor(null);
642
+ onPressEvent(event);
643
+ }}
644
+ />
645
+ </View>
646
+ ))}
647
+ </ScrollView>
648
+ </Pressable>
649
+ </Pressable>
650
+ </Modal>
651
+ ) : null}
652
+ <View {...slot("grid", { base: styles.container })} onLayout={handleLayout}>
653
+ {weeks.map((week, weekIndex) => {
654
+ // Spanning bars for this row (laid out once in `weekLayouts`). A multi-day
655
+ // event is one bar across its columns, stacked into lanes; the bars render
656
+ // in an overlay so they can span cells, and the cells reserve the lane rows
657
+ // so "+more" sits below them. `visibleLanes` mirrors the per-day cap.
658
+ const rowLayout = weekLayouts[weekIndex] ?? EMPTY_LAYOUT;
659
+ const visibleLanes = monthVisibleCount(rowLayout.laneCount, capacity);
660
+ const cols = week.length;
661
+ // With adjacent months hidden, clamp bars to the current-month columns so
662
+ // none draw over the blank leading/trailing cells.
663
+ let firstVisCol = 0;
664
+ let lastVisCol = cols - 1;
665
+ if (showGrid && !showAdjacentMonths) {
666
+ const first = week.findIndex((d) => isSameMonth(d, date));
667
+ if (first !== -1) {
668
+ firstVisCol = first;
669
+ lastVisCol = cols - 1 - [...week].reverse().findIndex((d) => isSameMonth(d, date));
670
+ }
671
+ }
672
+ return (
673
+ <View
674
+ {...slot("week", { base: styles.weekRow, themed: theme.containers.weekRow })}
675
+ key={week[0].toISOString()}
676
+ >
677
+ {week.map((day, dayCol) => renderDay(day, dayCol, rowLayout, visibleLanes))}
678
+ {showGrid ? (
679
+ <View style={[StyleSheet.absoluteFill, { pointerEvents: "box-none" }]}>
680
+ {rowLayout.segments
681
+ .filter((seg) => seg.lane < visibleLanes)
682
+ .map((seg) => {
683
+ const startCol = Math.max(seg.startCol, firstVisCol);
684
+ const endCol = Math.min(seg.endCol, lastVisCol);
685
+ if (startCol > endCol) return null;
686
+ return (
687
+ <View
688
+ key={`bar-${seg.event.start.toISOString()}:${seg.event.title}:${seg.lane}`}
689
+ style={{
690
+ position: "absolute",
691
+ left: pct((startCol / cols) * 100),
692
+ width: pct(((endCol - startCol + 1) / cols) * 100),
693
+ top: BADGE_AREA + seg.lane * chipMetrics.chipRowHeight,
694
+ height: chipMetrics.chipHeight,
695
+ paddingHorizontal: CHIP_INSET_H,
696
+ pointerEvents: "box-none",
697
+ }}
698
+ >
699
+ <RenderEventComponent
700
+ event={seg.event}
701
+ mode="month"
702
+ isAllDay={isAllDayEvent(seg.event)}
703
+ onPress={
704
+ disableMonthEventCellPress ? () => {} : () => onPressEvent(seg.event)
705
+ }
706
+ onLongPress={
707
+ disableMonthEventCellPress || !onLongPressEvent
708
+ ? undefined
709
+ : () => onLongPressEvent(seg.event)
710
+ }
711
+ />
712
+ </View>
713
+ );
714
+ })}
715
+ </View>
716
+ ) : null}
717
+ </View>
718
+ );
719
+ })}
543
720
  </View>
544
721
  </View>
545
722
  );
@@ -620,4 +797,23 @@ const styles = StyleSheet.create({
620
797
  marginTop: 2,
621
798
  marginHorizontal: 4,
622
799
  },
800
+ // The built-in "+N more" popover card.
801
+ moreBackdrop: {
802
+ flex: 1,
803
+ backgroundColor: "rgba(0, 0, 0, 0.3)",
804
+ alignItems: "center",
805
+ justifyContent: "center",
806
+ padding: 24,
807
+ },
808
+ moreCard: {
809
+ width: "100%",
810
+ maxWidth: 360,
811
+ maxHeight: "60%",
812
+ borderRadius: 12,
813
+ borderWidth: StyleSheet.hairlineWidth,
814
+ padding: 12,
815
+ gap: 6,
816
+ },
817
+ moreCardTitle: { fontSize: 14, fontWeight: "600", marginBottom: 4 },
818
+ moreCardRow: { marginBottom: 4 },
623
819
  });