@super-calendar/native 2.12.0 → 2.12.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.
package/dist/index.js CHANGED
@@ -845,6 +845,8 @@ const HOURS_PER_DAY = 24;
845
845
  const MINUTES_PER_DAY = 1440;
846
846
  const PAGE_WINDOW = 180;
847
847
  const PAGE_VIEWABILITY = { itemVisiblePercentThreshold: 90 };
848
+ const PAGER_IDLE_MS = 150;
849
+ const PAGER_HELD_DRAG_MS = 1e3;
848
850
  /** Default height in pixels of one hour row on the time grid. */
849
851
  const DEFAULT_HOUR_HEIGHT = 48;
850
852
  const DEFAULT_MIN_HOUR_HEIGHT = 32;
@@ -2384,16 +2386,24 @@ function TimeGridInner({ mode, numberOfDays = 1, weekEndsOn, date, events, cellH
2384
2386
  pagerOffset
2385
2387
  ]);
2386
2388
  require_MonthList.useWebPagerKeys(swipeEnabled, goToPage);
2387
- const handlePagerSettled = (0, react.useCallback)((event) => {
2388
- if (columnsWidth <= 0) return;
2389
- if (pendingScrollIndexRef.current != null) return;
2390
- const raw = Math.round(event.nativeEvent.contentOffset.x / columnsWidth);
2389
+ const commitNearestPage = (0, react.useCallback)((offsetX) => {
2390
+ const raw = Math.round(offsetX / columnsWidth);
2391
2391
  const index = Math.min(Math.max(raw, 0), pageDates.length - 1);
2392
2392
  if (index !== viewedIndexRef.current) {
2393
2393
  viewedIndexRef.current = index;
2394
2394
  const target = pageDates[index];
2395
2395
  if (target) onChangeDate(target);
2396
2396
  }
2397
+ return index;
2398
+ }, [
2399
+ columnsWidth,
2400
+ pageDates,
2401
+ onChangeDate
2402
+ ]);
2403
+ const handlePagerSettled = (0, react.useCallback)((event) => {
2404
+ if (columnsWidth <= 0) return;
2405
+ if (pendingScrollIndexRef.current != null) return;
2406
+ const index = commitNearestPage(event.nativeEvent.contentOffset.x);
2397
2407
  pagerOffset.value = index * columnsWidth;
2398
2408
  requestAnimationFrame(() => {
2399
2409
  listRef.current?.scrollToIndex({
@@ -2403,10 +2413,62 @@ function TimeGridInner({ mode, numberOfDays = 1, weekEndsOn, date, events, cellH
2403
2413
  });
2404
2414
  }, [
2405
2415
  columnsWidth,
2406
- pageDates,
2407
- onChangeDate,
2416
+ commitNearestPage,
2408
2417
  pagerOffset
2409
2418
  ]);
2419
+ const pagerDraggingRef = (0, react.useRef)(false);
2420
+ const lastPagerScrollRef = (0, react.useRef)({
2421
+ offsetX: 0,
2422
+ at: 0
2423
+ });
2424
+ const pagerIdleTimerRef = (0, react.useRef)(null);
2425
+ (0, react.useEffect)(() => () => {
2426
+ if (pagerIdleTimerRef.current) clearTimeout(pagerIdleTimerRef.current);
2427
+ }, []);
2428
+ const realignRestingPager = (0, react.useCallback)((offsetX) => {
2429
+ pagerDraggingRef.current = false;
2430
+ if (columnsWidth <= 0) return;
2431
+ const pages = offsetX / columnsWidth;
2432
+ if (Math.abs(pages - Math.round(pages)) * columnsWidth * react_native.PixelRatio.get() < .5) return;
2433
+ const index = pendingScrollIndexRef.current ?? commitNearestPage(offsetX);
2434
+ pagerOffset.value = index * columnsWidth;
2435
+ listRef.current?.scrollToIndex({
2436
+ index,
2437
+ animated: !reduceMotion
2438
+ });
2439
+ }, [
2440
+ columnsWidth,
2441
+ commitNearestPage,
2442
+ pagerOffset,
2443
+ reduceMotion
2444
+ ]);
2445
+ const realignRestingPagerRef = (0, react.useRef)(realignRestingPager);
2446
+ realignRestingPagerRef.current = realignRestingPager;
2447
+ const armPagerIdleTimer = (0, react.useCallback)((delay) => {
2448
+ pagerIdleTimerRef.current = setTimeout(() => {
2449
+ const wait = pagerDraggingRef.current ? PAGER_HELD_DRAG_MS : PAGER_IDLE_MS;
2450
+ const remaining = lastPagerScrollRef.current.at + wait - Date.now();
2451
+ if (remaining > 0) {
2452
+ armPagerIdleTimer(remaining);
2453
+ return;
2454
+ }
2455
+ pagerIdleTimerRef.current = null;
2456
+ realignRestingPagerRef.current(lastPagerScrollRef.current.offsetX);
2457
+ }, delay);
2458
+ }, []);
2459
+ const handlePagerScroll = (0, react.useCallback)((event) => {
2460
+ lastPagerScrollRef.current = {
2461
+ offsetX: event.nativeEvent.contentOffset.x,
2462
+ at: Date.now()
2463
+ };
2464
+ if (!pagerIdleTimerRef.current) armPagerIdleTimer(PAGER_IDLE_MS);
2465
+ }, [armPagerIdleTimer]);
2466
+ const handlePagerDragStart = (0, react.useCallback)(() => {
2467
+ pagerDraggingRef.current = true;
2468
+ }, []);
2469
+ const handlePagerDragEnd = (0, react.useCallback)(() => {
2470
+ pagerDraggingRef.current = false;
2471
+ }, []);
2410
2472
  const handlePressCell = (0, react.useMemo)(() => {
2411
2473
  if (!onPressCell) return void 0;
2412
2474
  if (!resetPageOnPressCell) return onPressCell;
@@ -2626,7 +2688,12 @@ function TimeGridInner({ mode, numberOfDays = 1, weekEndsOn, date, events, cellH
2626
2688
  decelerationRate: freeSwipe ? "normal" : "fast",
2627
2689
  disableIntervalMomentum: !freeSwipe,
2628
2690
  scrollEventThrottle: 16,
2629
- onMomentumScrollEnd: handlePagerSettled
2691
+ onMomentumScrollEnd: handlePagerSettled,
2692
+ ...react_native.Platform.OS === "android" ? {
2693
+ onScroll: handlePagerScroll,
2694
+ onScrollBeginDrag: handlePagerDragStart,
2695
+ onScrollEndDrag: handlePagerDragEnd
2696
+ } : null
2630
2697
  },
2631
2698
  initialScrollIndex: activeIndex,
2632
2699
  showsHorizontalScrollIndicator: false,
package/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@ import { addDays, addMonths, addWeeks, differenceInCalendarDays, endOfDay, endOf
3
3
  import { createContext, memo, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
4
4
  import Animated, { runOnJS, useAnimatedReaction, useAnimatedRef, useAnimatedScrollHandler, useAnimatedStyle, useDerivedValue, useReducedMotion, useSharedValue } from "react-native-reanimated";
5
5
  import { backgroundBandsForDay, backgroundBandsForDay as backgroundBandsForDay$1, bandRounding, buildMonthGrid, buildMonthWeeks, buildMonthWeeks as buildMonthWeeks$1, cellRangeFromDrag, clampMoveStartMinutes, closedHourBands, dayAccessibilityLabel, dayBadgeKind, daySelectionState, daySelectionState as daySelectionState$1, defaultCalendarLabels, eventAccessibilityLabel, eventChipLayout, eventTimeLabel, eventsInTimeZone, eventsInTimeZone as eventsInTimeZone$1, expandRecurringEvents, expandRecurringEvents as expandRecurringEvents$1, filterHiddenDays, formatHour, getIsToday, getIsToday as getIsToday$1, getViewDays, getViewDays as getViewDays$1, getWeekDays, getWeekDays as getWeekDays$1, getYearMonths, groupEventsByDay, isAllDayEvent, isAllDayEvent as isAllDayEvent$1, isBackgroundEvent, isBackgroundEvent as isBackgroundEvent$1, isDateSelectable, isDateSelectable as isDateSelectable$1, isRangeEndpoint, isSameCalendarDay, isSameCalendarDay as isSameCalendarDay$1, isWeekend, isWeekend as isWeekend$1, isWithinDateRange, layoutDayEvents, layoutDayEvents as layoutDayEvents$1, layoutMonthWeek, minutesIntoDay, monthCreateRange, nextDateRange, overlapsOtherEvents, pageStepDays, parseICalendar, rangeBandKind, resolveCalendarLabels, resolveDraggedBounds, shiftMinutes, snapDeltaMinutes, titleEllipsizeMode, titleNumberOfLines, toICalendar, toZonedTime, useDateRange, useEventSource, useMonthGrid, useNow, useNow as useNow$1, viewDayCount, weekdayFormatToken, weekdayFormatToken as weekdayFormatToken$1 } from "@super-calendar/core";
6
- import { Platform, Pressable, ScrollView, StyleSheet, Text, TouchableOpacity, View, useWindowDimensions } from "react-native";
6
+ import { PixelRatio, Platform, Pressable, ScrollView, StyleSheet, Text, TouchableOpacity, View, useWindowDimensions } from "react-native";
7
7
  import { Gesture, GestureDetector } from "react-native-gesture-handler";
8
8
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
9
9
  import { LegendList } from "@legendapp/list/react-native";
@@ -821,6 +821,8 @@ const HOURS_PER_DAY = 24;
821
821
  const MINUTES_PER_DAY = 1440;
822
822
  const PAGE_WINDOW = 180;
823
823
  const PAGE_VIEWABILITY = { itemVisiblePercentThreshold: 90 };
824
+ const PAGER_IDLE_MS = 150;
825
+ const PAGER_HELD_DRAG_MS = 1e3;
824
826
  /** Default height in pixels of one hour row on the time grid. */
825
827
  const DEFAULT_HOUR_HEIGHT = 48;
826
828
  const DEFAULT_MIN_HOUR_HEIGHT = 32;
@@ -2360,16 +2362,24 @@ function TimeGridInner({ mode, numberOfDays = 1, weekEndsOn, date, events, cellH
2360
2362
  pagerOffset
2361
2363
  ]);
2362
2364
  useWebPagerKeys(swipeEnabled, goToPage);
2363
- const handlePagerSettled = useCallback((event) => {
2364
- if (columnsWidth <= 0) return;
2365
- if (pendingScrollIndexRef.current != null) return;
2366
- const raw = Math.round(event.nativeEvent.contentOffset.x / columnsWidth);
2365
+ const commitNearestPage = useCallback((offsetX) => {
2366
+ const raw = Math.round(offsetX / columnsWidth);
2367
2367
  const index = Math.min(Math.max(raw, 0), pageDates.length - 1);
2368
2368
  if (index !== viewedIndexRef.current) {
2369
2369
  viewedIndexRef.current = index;
2370
2370
  const target = pageDates[index];
2371
2371
  if (target) onChangeDate(target);
2372
2372
  }
2373
+ return index;
2374
+ }, [
2375
+ columnsWidth,
2376
+ pageDates,
2377
+ onChangeDate
2378
+ ]);
2379
+ const handlePagerSettled = useCallback((event) => {
2380
+ if (columnsWidth <= 0) return;
2381
+ if (pendingScrollIndexRef.current != null) return;
2382
+ const index = commitNearestPage(event.nativeEvent.contentOffset.x);
2373
2383
  pagerOffset.value = index * columnsWidth;
2374
2384
  requestAnimationFrame(() => {
2375
2385
  listRef.current?.scrollToIndex({
@@ -2379,10 +2389,62 @@ function TimeGridInner({ mode, numberOfDays = 1, weekEndsOn, date, events, cellH
2379
2389
  });
2380
2390
  }, [
2381
2391
  columnsWidth,
2382
- pageDates,
2383
- onChangeDate,
2392
+ commitNearestPage,
2384
2393
  pagerOffset
2385
2394
  ]);
2395
+ const pagerDraggingRef = useRef(false);
2396
+ const lastPagerScrollRef = useRef({
2397
+ offsetX: 0,
2398
+ at: 0
2399
+ });
2400
+ const pagerIdleTimerRef = useRef(null);
2401
+ useEffect(() => () => {
2402
+ if (pagerIdleTimerRef.current) clearTimeout(pagerIdleTimerRef.current);
2403
+ }, []);
2404
+ const realignRestingPager = useCallback((offsetX) => {
2405
+ pagerDraggingRef.current = false;
2406
+ if (columnsWidth <= 0) return;
2407
+ const pages = offsetX / columnsWidth;
2408
+ if (Math.abs(pages - Math.round(pages)) * columnsWidth * PixelRatio.get() < .5) return;
2409
+ const index = pendingScrollIndexRef.current ?? commitNearestPage(offsetX);
2410
+ pagerOffset.value = index * columnsWidth;
2411
+ listRef.current?.scrollToIndex({
2412
+ index,
2413
+ animated: !reduceMotion
2414
+ });
2415
+ }, [
2416
+ columnsWidth,
2417
+ commitNearestPage,
2418
+ pagerOffset,
2419
+ reduceMotion
2420
+ ]);
2421
+ const realignRestingPagerRef = useRef(realignRestingPager);
2422
+ realignRestingPagerRef.current = realignRestingPager;
2423
+ const armPagerIdleTimer = useCallback((delay) => {
2424
+ pagerIdleTimerRef.current = setTimeout(() => {
2425
+ const wait = pagerDraggingRef.current ? PAGER_HELD_DRAG_MS : PAGER_IDLE_MS;
2426
+ const remaining = lastPagerScrollRef.current.at + wait - Date.now();
2427
+ if (remaining > 0) {
2428
+ armPagerIdleTimer(remaining);
2429
+ return;
2430
+ }
2431
+ pagerIdleTimerRef.current = null;
2432
+ realignRestingPagerRef.current(lastPagerScrollRef.current.offsetX);
2433
+ }, delay);
2434
+ }, []);
2435
+ const handlePagerScroll = useCallback((event) => {
2436
+ lastPagerScrollRef.current = {
2437
+ offsetX: event.nativeEvent.contentOffset.x,
2438
+ at: Date.now()
2439
+ };
2440
+ if (!pagerIdleTimerRef.current) armPagerIdleTimer(PAGER_IDLE_MS);
2441
+ }, [armPagerIdleTimer]);
2442
+ const handlePagerDragStart = useCallback(() => {
2443
+ pagerDraggingRef.current = true;
2444
+ }, []);
2445
+ const handlePagerDragEnd = useCallback(() => {
2446
+ pagerDraggingRef.current = false;
2447
+ }, []);
2386
2448
  const handlePressCell = useMemo(() => {
2387
2449
  if (!onPressCell) return void 0;
2388
2450
  if (!resetPageOnPressCell) return onPressCell;
@@ -2602,7 +2664,12 @@ function TimeGridInner({ mode, numberOfDays = 1, weekEndsOn, date, events, cellH
2602
2664
  decelerationRate: freeSwipe ? "normal" : "fast",
2603
2665
  disableIntervalMomentum: !freeSwipe,
2604
2666
  scrollEventThrottle: 16,
2605
- onMomentumScrollEnd: handlePagerSettled
2667
+ onMomentumScrollEnd: handlePagerSettled,
2668
+ ...Platform.OS === "android" ? {
2669
+ onScroll: handlePagerScroll,
2670
+ onScrollBeginDrag: handlePagerDragStart,
2671
+ onScrollEndDrag: handlePagerDragEnd
2672
+ } : null
2606
2673
  },
2607
2674
  initialScrollIndex: activeIndex,
2608
2675
  showsHorizontalScrollIndicator: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@super-calendar/native",
3
- "version": "2.12.0",
3
+ "version": "2.12.1",
4
4
  "description": "Gesture-driven, virtualized month / week / day calendar and date picker for React Native (and web via react-native-web).",
5
5
  "keywords": [
6
6
  "agenda",
@@ -64,7 +64,7 @@
64
64
  "access": "public"
65
65
  },
66
66
  "dependencies": {
67
- "@super-calendar/core": "2.12.0"
67
+ "@super-calendar/core": "2.12.1"
68
68
  },
69
69
  "peerDependencies": {
70
70
  "@legendapp/list": ">=3",
@@ -34,6 +34,7 @@ import {
34
34
  type LayoutChangeEvent,
35
35
  type NativeScrollEvent,
36
36
  type NativeSyntheticEvent,
37
+ PixelRatio,
37
38
  Platform,
38
39
  Pressable,
39
40
  StyleSheet,
@@ -127,6 +128,13 @@ const PAGE_WINDOW = 180;
127
128
  // A page must be ~fully on screen before it becomes the committed date.
128
129
  const PAGE_VIEWABILITY = { itemVisiblePercentThreshold: 90 };
129
130
 
131
+ // How long the Android pager must go without a scroll event before an off-page
132
+ // rest is snapped back onto a page.
133
+ const PAGER_IDLE_MS = 150;
134
+ // The same wait after a drag that never reported its end, which may still be
135
+ // held by a finger.
136
+ const PAGER_HELD_DRAG_MS = 1000;
137
+
130
138
  // Matches the dom renderer's default so both grids start at the same density.
131
139
  /** Default height in pixels of one hour row on the time grid. */
132
140
  export const DEFAULT_HOUR_HEIGHT = 48;
@@ -2944,6 +2952,23 @@ function TimeGridInner<T>({
2944
2952
  );
2945
2953
  useWebPagerKeys(swipeEnabled, goToPage);
2946
2954
 
2955
+ // Commit the page nearest `offsetX` and return its index. Viewability only
2956
+ // reports a page that is nearly all on screen, so a pager that rests between
2957
+ // pages would otherwise move the grid without moving the date behind the header.
2958
+ const commitNearestPage = useCallback(
2959
+ (offsetX: number) => {
2960
+ const raw = Math.round(offsetX / columnsWidth);
2961
+ const index = Math.min(Math.max(raw, 0), pageDates.length - 1);
2962
+ if (index !== viewedIndexRef.current) {
2963
+ viewedIndexRef.current = index;
2964
+ const target = pageDates[index];
2965
+ if (target) onChangeDate(target);
2966
+ }
2967
+ return index;
2968
+ },
2969
+ [columnsWidth, pageDates, onChangeDate],
2970
+ );
2971
+
2947
2972
  // A fast fling can outrun the list's on-demand mounting and leave the pager
2948
2973
  // blank until something nudges it. When momentum ends, re-anchor the list on
2949
2974
  // the page the offset landed on: `scrollToIndex` forces it to recompute its
@@ -2957,25 +2982,73 @@ function TimeGridInner<T>({
2957
2982
  // (goToPage, realign) sets `pendingScrollIndexRef` and repaints itself, so
2958
2983
  // skip those to avoid a redundant re-anchor during held edge paging.
2959
2984
  if (pendingScrollIndexRef.current != null) return;
2960
- const raw = Math.round(event.nativeEvent.contentOffset.x / columnsWidth);
2961
- const index = Math.min(Math.max(raw, 0), pageDates.length - 1);
2962
- // Commit the page the pager snaps to. Viewability only reports a page that
2963
- // is nearly all on screen, so a fling that rests between pages would
2964
- // otherwise move the grid without moving the date behind the header.
2965
- if (index !== viewedIndexRef.current) {
2966
- viewedIndexRef.current = index;
2967
- const target = pageDates[index];
2968
- if (target) onChangeDate(target);
2969
- }
2985
+ const index = commitNearestPage(event.nativeEvent.contentOffset.x);
2970
2986
  // eslint-disable-next-line react-hooks/immutability -- Reanimated shared value: assigning .value is the intended mutation API
2971
2987
  pagerOffset.value = index * columnsWidth;
2972
2988
  requestAnimationFrame(() => {
2973
2989
  void listRef.current?.scrollToIndex({ index, animated: false });
2974
2990
  });
2975
2991
  },
2976
- [columnsWidth, pageDates, onChangeDate, pagerOffset],
2992
+ [columnsWidth, commitNearestPage, pagerOffset],
2977
2993
  );
2978
2994
 
2995
+ // Android: once the pager has gone a moment without a scroll event, snap an
2996
+ // offset left between pages back onto the nearest page (or onto the target of
2997
+ // an interrupted programmatic scroll). A drag that began and has not reported
2998
+ // its end waits longer, in case a finger is still holding it.
2999
+ const pagerDraggingRef = useRef(false);
3000
+ const lastPagerScrollRef = useRef({ offsetX: 0, at: 0 });
3001
+ const pagerIdleTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
3002
+ useEffect(
3003
+ () => () => {
3004
+ if (pagerIdleTimerRef.current) clearTimeout(pagerIdleTimerRef.current);
3005
+ },
3006
+ [],
3007
+ );
3008
+ const realignRestingPager = useCallback(
3009
+ (offsetX: number) => {
3010
+ pagerDraggingRef.current = false;
3011
+ if (columnsWidth <= 0) return;
3012
+ const pages = offsetX / columnsWidth;
3013
+ const offPagePx = Math.abs(pages - Math.round(pages)) * columnsWidth * PixelRatio.get();
3014
+ if (offPagePx < 0.5) return;
3015
+ const index = pendingScrollIndexRef.current ?? commitNearestPage(offsetX);
3016
+ // eslint-disable-next-line react-hooks/immutability -- Reanimated shared value: assigning .value is the intended mutation API
3017
+ pagerOffset.value = index * columnsWidth;
3018
+ void listRef.current?.scrollToIndex({ index, animated: !reduceMotion });
3019
+ },
3020
+ [columnsWidth, commitNearestPage, pagerOffset, reduceMotion],
3021
+ );
3022
+ const realignRestingPagerRef = useRef(realignRestingPager);
3023
+ realignRestingPagerRef.current = realignRestingPager;
3024
+ // One timer per burst of scroll events: when it fires early it re-arms for
3025
+ // the rest of the wait measured from the latest event.
3026
+ const armPagerIdleTimer = useCallback((delay: number) => {
3027
+ pagerIdleTimerRef.current = setTimeout(() => {
3028
+ const wait = pagerDraggingRef.current ? PAGER_HELD_DRAG_MS : PAGER_IDLE_MS;
3029
+ const remaining = lastPagerScrollRef.current.at + wait - Date.now();
3030
+ if (remaining > 0) {
3031
+ armPagerIdleTimer(remaining);
3032
+ return;
3033
+ }
3034
+ pagerIdleTimerRef.current = null;
3035
+ realignRestingPagerRef.current(lastPagerScrollRef.current.offsetX);
3036
+ }, delay);
3037
+ }, []);
3038
+ const handlePagerScroll = useCallback(
3039
+ (event: NativeSyntheticEvent<NativeScrollEvent>) => {
3040
+ lastPagerScrollRef.current = { offsetX: event.nativeEvent.contentOffset.x, at: Date.now() };
3041
+ if (!pagerIdleTimerRef.current) armPagerIdleTimer(PAGER_IDLE_MS);
3042
+ },
3043
+ [armPagerIdleTimer],
3044
+ );
3045
+ const handlePagerDragStart = useCallback(() => {
3046
+ pagerDraggingRef.current = true;
3047
+ }, []);
3048
+ const handlePagerDragEnd = useCallback(() => {
3049
+ pagerDraggingRef.current = false;
3050
+ }, []);
3051
+
2979
3052
  // Optionally snap the pager back to the active page after an empty-cell press
2980
3053
  // (so tapping a far-swiped page returns to the committed date).
2981
3054
  const handlePressCell = useMemo(() => {
@@ -3226,6 +3299,13 @@ function TimeGridInner<T>({
3226
3299
  disableIntervalMomentum: !freeSwipe,
3227
3300
  scrollEventThrottle: 16,
3228
3301
  onMomentumScrollEnd: handlePagerSettled,
3302
+ ...(Platform.OS === "android"
3303
+ ? {
3304
+ onScroll: handlePagerScroll,
3305
+ onScrollBeginDrag: handlePagerDragStart,
3306
+ onScrollEndDrag: handlePagerDragEnd,
3307
+ }
3308
+ : null),
3229
3309
  })}
3230
3310
  initialScrollIndex={activeIndex}
3231
3311
  showsHorizontalScrollIndicator={false}