@super-calendar/native 2.12.1 → 2.14.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.
@@ -18,12 +18,16 @@ import {
18
18
  import {
19
19
  createContext,
20
20
  type Dispatch,
21
+ type ForwardedRef,
22
+ forwardRef,
21
23
  memo,
22
24
  type ReactElement,
25
+ type RefAttributes,
23
26
  type SetStateAction,
24
27
  useCallback,
25
28
  useContext,
26
29
  useEffect,
30
+ useImperativeHandle,
27
31
  useMemo,
28
32
  useRef,
29
33
  useState,
@@ -90,7 +94,11 @@ import {
90
94
  snapDeltaMinutes,
91
95
  } from "@super-calendar/core";
92
96
  import { formatHour, layoutDayEvents, type PositionedEvent } from "@super-calendar/core";
93
- import type { CalendarLabels, EventAccessibilityLabeler } from "@super-calendar/core";
97
+ import type {
98
+ CalendarHandle,
99
+ CalendarLabels,
100
+ EventAccessibilityLabeler,
101
+ } from "@super-calendar/core";
94
102
  import { resolveCalendarLabels } from "@super-calendar/core";
95
103
  import { type WeekdayFormat, weekdayFormatToken } from "@super-calendar/core";
96
104
  import {
@@ -2346,73 +2354,76 @@ export type TimeGridProps<T> = SlotStyleProps<TimeGridSlot> & {
2346
2354
  renderHeader?: (days: Date[]) => React.ReactNode;
2347
2355
  };
2348
2356
 
2349
- function TimeGridInner<T>({
2350
- mode,
2351
- numberOfDays = 1,
2352
- weekEndsOn,
2353
- date,
2354
- events,
2355
- cellHeight,
2356
- hourHeight = DEFAULT_HOUR_HEIGHT,
2357
- weekStartsOn,
2358
- weekdayFormat = "short",
2359
- labels: labelsProp,
2360
- renderEvent,
2361
- eventAccessibilityLabel,
2362
- keyExtractor,
2363
- scrollOffsetMinutes = 0,
2364
- hourColumnWidth: hourColumnWidthProp = DEFAULT_HOUR_COLUMN_WIDTH,
2365
- hideHours = false,
2366
- timeslots = 1,
2367
- showAllDayEventCell = true,
2368
- showAllDayLabel = false,
2369
- refreshControl,
2370
- highlightWeekends = true,
2371
- calendarCellStyle,
2372
- businessHours,
2373
- renderBusinessHours,
2374
- renderBackgroundEvent,
2375
- showWeekNumber = false,
2376
- headerComponent,
2377
- minHour = 0,
2378
- maxHour = HOURS_PER_DAY,
2379
- ampm = false,
2380
- isRTL = false,
2381
- minHourHeight = DEFAULT_MIN_HOUR_HEIGHT,
2382
- maxHourHeight = DEFAULT_MAX_HOUR_HEIGHT,
2383
- minEventHeight = DEFAULT_MIN_EVENT_HEIGHT,
2384
- eventGap = DEFAULT_EVENT_GAP,
2385
- showNowIndicator = true,
2386
- locale,
2387
- freeSwipe = false,
2388
- swipeEnabled = true,
2389
- showVerticalScrollIndicator = true,
2390
- verticalScrollEnabled = true,
2391
- weekNumberPrefix = "W",
2392
- hiddenDays,
2393
- now,
2394
- timeZone,
2395
- hourComponent,
2396
- activeDate,
2397
- resetPageOnPressCell = false,
2398
- dragStepMinutes = DEFAULT_DRAG_STEP_MINUTES,
2399
- showDragHandle = true,
2400
- eventStartEditable = true,
2401
- eventDurationEditable = true,
2402
- eventOverlap = true,
2403
- onPressEvent,
2404
- onLongPressEvent,
2405
- onDragEvent: onDragEventProp,
2406
- onDragStart,
2407
- onPressCell,
2408
- onLongPressCell,
2409
- onCreateEvent,
2410
- onPressDateHeader,
2411
- onChangeDate,
2412
- renderHeader,
2413
- classNames,
2414
- styles: styleOverrides,
2415
- }: TimeGridProps<T>): ReactElement {
2357
+ function TimeGridInner<T>(
2358
+ {
2359
+ mode,
2360
+ numberOfDays = 1,
2361
+ weekEndsOn,
2362
+ date,
2363
+ events,
2364
+ cellHeight,
2365
+ hourHeight = DEFAULT_HOUR_HEIGHT,
2366
+ weekStartsOn,
2367
+ weekdayFormat = "short",
2368
+ labels: labelsProp,
2369
+ renderEvent,
2370
+ eventAccessibilityLabel,
2371
+ keyExtractor,
2372
+ scrollOffsetMinutes = 0,
2373
+ hourColumnWidth: hourColumnWidthProp = DEFAULT_HOUR_COLUMN_WIDTH,
2374
+ hideHours = false,
2375
+ timeslots = 1,
2376
+ showAllDayEventCell = true,
2377
+ showAllDayLabel = false,
2378
+ refreshControl,
2379
+ highlightWeekends = true,
2380
+ calendarCellStyle,
2381
+ businessHours,
2382
+ renderBusinessHours,
2383
+ renderBackgroundEvent,
2384
+ showWeekNumber = false,
2385
+ headerComponent,
2386
+ minHour = 0,
2387
+ maxHour = HOURS_PER_DAY,
2388
+ ampm = false,
2389
+ isRTL = false,
2390
+ minHourHeight = DEFAULT_MIN_HOUR_HEIGHT,
2391
+ maxHourHeight = DEFAULT_MAX_HOUR_HEIGHT,
2392
+ minEventHeight = DEFAULT_MIN_EVENT_HEIGHT,
2393
+ eventGap = DEFAULT_EVENT_GAP,
2394
+ showNowIndicator = true,
2395
+ locale,
2396
+ freeSwipe = false,
2397
+ swipeEnabled = true,
2398
+ showVerticalScrollIndicator = true,
2399
+ verticalScrollEnabled = true,
2400
+ weekNumberPrefix = "W",
2401
+ hiddenDays,
2402
+ now,
2403
+ timeZone,
2404
+ hourComponent,
2405
+ activeDate,
2406
+ resetPageOnPressCell = false,
2407
+ dragStepMinutes = DEFAULT_DRAG_STEP_MINUTES,
2408
+ showDragHandle = true,
2409
+ eventStartEditable = true,
2410
+ eventDurationEditable = true,
2411
+ eventOverlap = true,
2412
+ onPressEvent,
2413
+ onLongPressEvent,
2414
+ onDragEvent: onDragEventProp,
2415
+ onDragStart,
2416
+ onPressCell,
2417
+ onLongPressCell,
2418
+ onCreateEvent,
2419
+ onPressDateHeader,
2420
+ onChangeDate,
2421
+ renderHeader,
2422
+ classNames,
2423
+ styles: styleOverrides,
2424
+ }: TimeGridProps<T>,
2425
+ ref: ForwardedRef<CalendarHandle>,
2426
+ ): ReactElement {
2416
2427
  // Guard against an inverted/out-of-range window so the grid never collapses.
2417
2428
  const clampedMinHour = Math.max(0, Math.min(minHour, HOURS_PER_DAY - 1));
2418
2429
  const clampedMaxHour = Math.max(clampedMinHour + 1, Math.min(maxHour, HOURS_PER_DAY));
@@ -2495,6 +2506,9 @@ function TimeGridInner<T>({
2495
2506
  scrollY.value = event.contentOffset.y;
2496
2507
  },
2497
2508
  });
2509
+ // The scroll view's `contentOffset` holds the mount offset; a later
2510
+ // `scrollOffsetMinutes` is scrolled to by the effect further down.
2511
+ const [mountOffsetY] = useState(seedDefaultY);
2498
2512
  // react-native-web ignores `contentOffset`; apply the initial offset once the
2499
2513
  // viewport and the content have laid out instead.
2500
2514
  const webSeededRef = useRef(!isWeb);
@@ -2903,6 +2917,26 @@ function TimeGridInner<T>({
2903
2917
  return () => cancelAnimationFrame(frame);
2904
2918
  }, [seedWebScroll]);
2905
2919
 
2920
+ // Scroll so `minutes` sits at the top of the hours, at the live zoom.
2921
+ const scrollToMinutes = useCallback(
2922
+ (minutes: number, animated: boolean) => {
2923
+ webSeededRef.current = true;
2924
+ const y = Math.max(0, minutes / MINUTES_PER_HOUR - clampedMinHour) * cellHeight.value;
2925
+ scrollRef.current?.scrollTo({ y, animated });
2926
+ },
2927
+ [clampedMinHour, cellHeight, scrollRef],
2928
+ );
2929
+
2930
+ // Scroll to a changed `scrollOffsetMinutes` (or hour window). The mount offset
2931
+ // comes from `contentOffset` and the web seed above.
2932
+ const appliedOffsetRef = useRef({ minutes: scrollOffsetMinutes, minHour: clampedMinHour });
2933
+ useEffect(() => {
2934
+ const applied = appliedOffsetRef.current;
2935
+ if (applied.minutes === scrollOffsetMinutes && applied.minHour === clampedMinHour) return;
2936
+ appliedOffsetRef.current = { minutes: scrollOffsetMinutes, minHour: clampedMinHour };
2937
+ scrollToMinutes(scrollOffsetMinutes, false);
2938
+ }, [scrollOffsetMinutes, clampedMinHour, scrollToMinutes]);
2939
+
2906
2940
  // Web: LegendList's horizontal scroll container is `overflow-x: auto`, so a
2907
2941
  // trackpad swipe or horizontal wheel would scroll between pages. Paging should be
2908
2942
  // arrow-keys/toolbar only, so disable user horizontal scrolling on it (programmatic
@@ -2933,6 +2967,15 @@ function TimeGridInner<T>({
2933
2967
  // jumps when it's on.
2934
2968
  const reduceMotion = useReducedMotion();
2935
2969
 
2970
+ useImperativeHandle(
2971
+ ref,
2972
+ () => ({
2973
+ scrollToTime: (minutes, { animated = true } = {}) =>
2974
+ scrollToMinutes(minutes, animated && !reduceMotion),
2975
+ }),
2976
+ [scrollToMinutes, reduceMotion],
2977
+ );
2978
+
2936
2979
  // Page by `delta` (edge drags, web arrow keys): move the list at once and
2937
2980
  // report the date, rather than waiting for the reported date to come back
2938
2981
  // through props. The realign effect then finds the list already there.
@@ -3217,7 +3260,7 @@ function TimeGridInner<T>({
3217
3260
  onScroll={scrollHandler}
3218
3261
  scrollEventThrottle={16}
3219
3262
  refreshControl={verticalScrollEnabled ? refreshControl : undefined}
3220
- contentOffset={{ x: 0, y: seedDefaultY }}
3263
+ contentOffset={{ x: 0, y: mountOffsetY }}
3221
3264
  >
3222
3265
  <Animated.View testID="time-grid-hours" style={[styles.gridRow, gridHeightStyle]}>
3223
3266
  {hourColumnWidth > 0 ? (
@@ -3358,7 +3401,9 @@ function TimeGridInner<T>({
3358
3401
  * />
3359
3402
  * ```
3360
3403
  */
3361
- export const TimeGrid = memo(TimeGridInner) as typeof TimeGridInner;
3404
+ export const TimeGrid = memo(forwardRef(TimeGridInner)) as <T>(
3405
+ props: TimeGridProps<T> & RefAttributes<CalendarHandle>,
3406
+ ) => ReactElement;
3362
3407
 
3363
3408
  type DayHeaderRowProps = {
3364
3409
  days: Date[];
package/src/index.tsx CHANGED
@@ -57,6 +57,7 @@ export type {
57
57
  BusinessHours,
58
58
  BusinessHoursBand,
59
59
  CalendarEvent,
60
+ CalendarHandle,
60
61
  CalendarMode,
61
62
  EventAccessibilityLabelContext,
62
63
  EventAccessibilityLabeler,
@@ -66,6 +67,7 @@ export type {
66
67
  RecurrenceRule,
67
68
  RenderEvent,
68
69
  RenderEventArgs,
70
+ ScrollToTimeOptions,
69
71
  TimeGridMode,
70
72
  WeekdayFormat,
71
73
  WeekStartsOn,
package/src/types.ts CHANGED
@@ -14,6 +14,7 @@ export type {
14
14
  BusinessHours,
15
15
  BusinessHoursBand,
16
16
  CalendarEvent,
17
+ CalendarHandle,
17
18
  CalendarMode,
18
19
  EventAccessibilityLabelContext,
19
20
  EventAccessibilityLabeler,
@@ -21,6 +22,7 @@ export type {
21
22
  ICalendarEvent,
22
23
  RecurrenceFrequency,
23
24
  RecurrenceRule,
25
+ ScrollToTimeOptions,
24
26
  TimeGridMode,
25
27
  WeekdayFormat,
26
28
  WeekStartsOn,