@onekeyfe/react-native-pager-view 6.7.0-3 → 6.7.0-31

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.
Files changed (52) hide show
  1. package/README.md +21 -0
  2. package/android/build.gradle +1 -0
  3. package/android/src/androidTest/AndroidManifest.xml +7 -0
  4. package/android/src/androidTest/java/com/reactnativepagerview/CollapsiblePagerTabReservationTest.kt +476 -0
  5. package/android/src/main/java/com/reactnativepagerview/CollapsiblePagerHost.kt +215 -39
  6. package/android/src/main/java/com/reactnativepagerview/CollapsiblePagerNativeHeaders.kt +518 -21
  7. package/android/src/main/java/com/reactnativepagerview/CollapsiblePagerViewManager.kt +40 -0
  8. package/android/src/main/java/com/reactnativepagerview/NativeScrollerScrollAwayInsets.kt +19 -0
  9. package/android/src/main/java/com/reactnativepagerview/NativeScrollerView.kt +50 -6
  10. package/android/src/main/java/com/reactnativepagerview/NestedScrollableHost.kt +8 -2
  11. package/android/src/main/java/com/reactnativepagerview/event/NativeHeaderPressEvent.kt +8 -0
  12. package/android/src/test/java/com/reactnativepagerview/NativeScrollerScrollAwayInsetsTest.kt +40 -0
  13. package/docs/SPEC.md +606 -10
  14. package/ios/RNCCollapsiblePagerViewComponentView.mm +1903 -91
  15. package/ios/RNCNativeScrollerComponentView.h +2 -0
  16. package/ios/RNCNativeScrollerComponentView.mm +42 -0
  17. package/ios/tests/RNCCollapsiblePagerFirstFrameTests.mm +562 -1
  18. package/ios/tests/RNCCollapsiblePagerHitTestTests.mm +1116 -0
  19. package/ios/tests/RNCCollapsiblePagerPressCancellationTests.mm +74 -9
  20. package/ios/tests/RNCCollapsiblePagerRefreshControlTests.mm +50 -0
  21. package/lib/module/CollapsiblePagerView.js +70 -11
  22. package/lib/module/CollapsiblePagerView.web.js +532 -8
  23. package/lib/module/CollapsiblePagerViewNativeComponent.ts +57 -42
  24. package/lib/module/NativeScroller.web.js +3 -0
  25. package/lib/module/NativeScrollerNativeComponent.ts +42 -46
  26. package/lib/module/PagerViewNativeComponent.ts +20 -25
  27. package/lib/module/WebCollapsiblePagerPage.js +7 -2
  28. package/lib/module/index.native.js +9 -0
  29. package/lib/typescript/src/CollapsiblePagerView.d.ts +42 -4
  30. package/lib/typescript/src/CollapsiblePagerView.web.d.ts +48 -2
  31. package/lib/typescript/src/CollapsiblePagerViewNativeComponent.d.ts +54 -38
  32. package/lib/typescript/src/NativeScroller.d.ts +5 -3
  33. package/lib/typescript/src/NativeScroller.web.d.ts +1 -1
  34. package/lib/typescript/src/NativeScrollerNativeComponent.d.ts +47 -48
  35. package/lib/typescript/src/PagerView.d.ts +1 -1
  36. package/lib/typescript/src/PagerView.web.d.ts +1 -1
  37. package/lib/typescript/src/PagerViewNativeComponent.d.ts +20 -21
  38. package/lib/typescript/src/WebCollapsiblePagerPage.d.ts +1 -1
  39. package/lib/typescript/src/index.native.d.ts +13 -0
  40. package/lib/typescript/src/usePagerView.d.ts +2 -2
  41. package/lib/typescript/src/utils.d.ts +2 -2
  42. package/package.json +22 -7
  43. package/react-native-pager-view.podspec +1 -0
  44. package/src/CollapsiblePagerView.tsx +179 -12
  45. package/src/CollapsiblePagerView.web.tsx +486 -7
  46. package/src/CollapsiblePagerViewNativeComponent.ts +57 -42
  47. package/src/NativeScroller.tsx +14 -4
  48. package/src/NativeScroller.web.tsx +15 -1
  49. package/src/NativeScrollerNativeComponent.ts +42 -46
  50. package/src/PagerViewNativeComponent.ts +20 -25
  51. package/src/WebCollapsiblePagerPage.tsx +7 -2
  52. package/src/index.native.tsx +30 -0
@@ -1,6 +1,6 @@
1
1
  import type { ScrollEdgeEffect } from "./ScrollEdgeEffect";
2
2
  import React from "react";
3
- import { I18nManager, Platform, StyleSheet, View } from "react-native";
3
+ import { I18nManager, Platform, StyleSheet, View, processColor } from "react-native";
4
4
  import type * as ReactNative from "react-native";
5
5
 
6
6
  import CollapsiblePagerViewNativeComponent, {
@@ -32,11 +32,15 @@ export type CollapsiblePagerNativeTabBarItem = Readonly<{
32
32
  }>;
33
33
 
34
34
  export type CollapsiblePagerNativeTabBarStyle = Readonly<{
35
+ variant?: "underline" | "segmented" | "text";
35
36
  height?: number;
37
+ trailingReservedWidth?: number;
36
38
  contentPaddingHorizontal?: number;
39
+ contentPaddingVertical?: number;
37
40
  itemSpacing?: number;
38
41
  fontSize?: number;
39
42
  fontFamily?: string;
43
+ activeFontFamily?: string;
40
44
  backgroundColor?: ReactNative.ColorValue;
41
45
  activeTextColor?: ReactNative.ColorValue;
42
46
  inactiveTextColor?: ReactNative.ColorValue;
@@ -45,7 +49,30 @@ export type CollapsiblePagerNativeTabBarStyle = Readonly<{
45
49
  indicatorBottom?: number;
46
50
  }>;
47
51
 
52
+ export type CollapsiblePagerToolbarItem = Readonly<{
53
+ iconName: string;
54
+ selected?: boolean;
55
+ accessibilityLabel?: string;
56
+ testID?: string;
57
+ iconColor?: string;
58
+ selectedBackgroundColor?: string;
59
+ hoverBackgroundColor?: string;
60
+ pressedBackgroundColor?: string;
61
+ size?: number;
62
+ iconSize?: number;
63
+ contentWidth?: number;
64
+ trailingInset?: number;
65
+ }>;
66
+
67
+ export type CollapsiblePagerToolbarPress = Readonly<{
68
+ key: string;
69
+ anchorRect: Readonly<{ x: number; y: number; width: number; height: number }>;
70
+ }>;
71
+
48
72
  export type CollapsiblePagerNativeTabBarConfig = Readonly<{
73
+ controlled?: boolean;
74
+ selectedKey?: string;
75
+ toolbar?: Readonly<Record<string, CollapsiblePagerToolbarItem>>;
49
76
  items: readonly CollapsiblePagerNativeTabBarItem[];
50
77
  style?: CollapsiblePagerNativeTabBarStyle;
51
78
  }>;
@@ -87,14 +114,20 @@ export interface CollapsiblePagerViewProps
87
114
  | "children"
88
115
  | "headerHeight"
89
116
  | "stickyHeaderHeight"
117
+ | "stickyHeaderHeights"
90
118
  | "topScrollEdgeEffect"
91
119
  | "topOverlayHeight"
92
120
  | "externalTopOverlayTag"
93
121
  | "pageKeys"
94
122
  | "retainedPages"
123
+ | "nativeTabBarConfig"
124
+ | "onNativeToolbarPress"
95
125
  | "nativeTabBarItems"
96
126
  | "nativeTabBarHeight"
127
+ | "nativeTabBarVariant"
128
+ | "nativeTabBarTrailingReservedWidth"
97
129
  | "nativeTabBarContentPaddingHorizontal"
130
+ | "nativeTabBarContentPaddingVertical"
98
131
  | "nativeTabBarItemSpacing"
99
132
  | "nativeTabBarFontSize"
100
133
  | "nativeTabBarFontFamily"
@@ -118,8 +151,12 @@ export interface CollapsiblePagerViewProps
118
151
  headerHeight: number;
119
152
  /** Measured sticky bar height. */
120
153
  stickyHeaderHeight: number;
154
+ /** iOS only. Sticky heights in child order, applied before each page is displayed. */
155
+ stickyHeaderHeights?: readonly number[];
121
156
  /** Fixed content above the collapsible header on iOS 26 and newer. */
122
157
  topOverlay?: React.ReactNode;
158
+ /** Known overlay height to reserve until its first layout measurement. */
159
+ topOverlayHeightHint?: number;
123
160
  /** Existing fixed header shared with a parent pager on iOS 26 and newer. */
124
161
  externalTopOverlayTag?: number;
125
162
  /** Selects UIKit's top scroll-edge effect on iOS 26 and newer. */
@@ -140,6 +177,7 @@ export interface CollapsiblePagerViewProps
140
177
  layoutDirection?: "ltr" | "rtl" | "locale";
141
178
  /** Optional native tab bar for iOS and Android. */
142
179
  nativeTabBar?: CollapsiblePagerNativeTabBarConfig;
180
+ onNativeToolbarPress?: (event: ReactNative.NativeSyntheticEvent<CollapsiblePagerToolbarPress>) => void;
143
181
  onNativeTabPress?: (event: CollapsiblePagerViewOnNativeTabPressEvent) => void;
144
182
  /**
145
183
  * Defaults to animating adjacent native tab presses and jumping directly to
@@ -158,7 +196,21 @@ export interface CollapsiblePagerViewProps
158
196
  type State = {
159
197
  selectedPage: number;
160
198
  transientRetainedPages: readonly number[];
161
- topOverlayHeight: number;
199
+ topOverlayHeight: number | null;
200
+ };
201
+
202
+ const nativeToolbar = (toolbar: CollapsiblePagerNativeTabBarConfig["toolbar"]) => {
203
+ if (!toolbar) return undefined;
204
+ return Object.fromEntries(Object.entries(toolbar).map(([key, item]) => {
205
+ const color = processColor(item.iconColor ?? "#000000");
206
+ const argb = typeof color === "number" ? color >>> 0 : 0xff000000;
207
+ return [key, { ...item,
208
+ iconColor: `rgba(${(argb >>> 16) & 255},${(argb >>> 8) & 255},${argb & 255},${Math.round(((argb >>> 24) / 255) * 1000000) / 1000000})`,
209
+ selectedBackgroundColor: processColor(item.selectedBackgroundColor ?? "transparent"),
210
+ hoverBackgroundColor: processColor(item.hoverBackgroundColor ?? "transparent"),
211
+ pressedBackgroundColor: processColor(item.pressedBackgroundColor ?? "transparent"),
212
+ }];
213
+ }));
162
214
  };
163
215
 
164
216
  const pageIndex = (value: number) => {
@@ -186,7 +238,7 @@ export class CollapsiblePagerView extends React.PureComponent<
186
238
  State
187
239
  > {
188
240
  state: State = {
189
- topOverlayHeight: 0,
241
+ topOverlayHeight: null,
190
242
  selectedPage: clampedPageIndex(
191
243
  this.props.initialPage ?? 0,
192
244
  React.Children.toArray(this.props.children).length
@@ -219,7 +271,22 @@ export class CollapsiblePagerView extends React.PureComponent<
219
271
  this.notifyMountedPagesChanged();
220
272
  }
221
273
 
222
- componentDidUpdate() {
274
+ componentDidUpdate(previousProps: CollapsiblePagerViewProps) {
275
+ const previousOverlayEnabled =
276
+ previousProps.topOverlay != null &&
277
+ (previousProps.topScrollEdgeEffect === "hard" ||
278
+ previousProps.topScrollEdgeEffect === "soft");
279
+ const overlayEnabled =
280
+ this.props.topOverlay != null &&
281
+ (this.props.topScrollEdgeEffect === "hard" ||
282
+ this.props.topScrollEdgeEffect === "soft");
283
+ if (
284
+ previousOverlayEnabled &&
285
+ !overlayEnabled &&
286
+ this.state.topOverlayHeight !== null
287
+ ) {
288
+ this.setState({ topOverlayHeight: null });
289
+ }
223
290
  const pageCount = this.pages().length;
224
291
  const selectedPage = clampedPageIndex(this.state.selectedPage, pageCount);
225
292
  if (selectedPage !== this.state.selectedPage) {
@@ -237,7 +304,7 @@ export class CollapsiblePagerView extends React.PureComponent<
237
304
  position >= 0 &&
238
305
  position < this.pages().length
239
306
  ) {
240
- if (this.nativeTabBarEnabled()) {
307
+ if (this.nativeTabBarEnabled() && !this.props.nativeTabBar?.controlled) {
241
308
  this.dispatchNativeTabPageCommand(position, true);
242
309
  } else {
243
310
  CollapsiblePagerViewNativeCommands.setPage(this.nativeRef, position);
@@ -253,7 +320,7 @@ export class CollapsiblePagerView extends React.PureComponent<
253
320
  position >= 0 &&
254
321
  position < this.pages().length
255
322
  ) {
256
- if (this.nativeTabBarEnabled()) {
323
+ if (this.nativeTabBarEnabled() && !this.props.nativeTabBar?.controlled) {
257
324
  this.dispatchNativeTabPageCommand(position, false);
258
325
  } else {
259
326
  CollapsiblePagerViewNativeCommands.setPageWithoutAnimation(
@@ -273,6 +340,22 @@ export class CollapsiblePagerView extends React.PureComponent<
273
340
  );
274
341
  };
275
342
 
343
+ public scrollHeaderBy = (delta: number) => {
344
+ if (this.nativeRef && Number.isFinite(delta))
345
+ CollapsiblePagerViewNativeCommands.scrollHeaderBy(
346
+ this.nativeRef,
347
+ delta
348
+ );
349
+ };
350
+
351
+ public scrollHeaderToOffset = (offset: number) => {
352
+ if (this.nativeRef && Number.isFinite(offset))
353
+ CollapsiblePagerViewNativeCommands.scrollHeaderToOffset(
354
+ this.nativeRef,
355
+ offset
356
+ );
357
+ };
358
+
276
359
  public scrollToStickyTop = (options?: Readonly<{ animated?: boolean }>) => {
277
360
  if (this.nativeRef)
278
361
  CollapsiblePagerViewNativeCommands.scrollToTop(
@@ -302,7 +385,11 @@ export class CollapsiblePagerView extends React.PureComponent<
302
385
  );
303
386
  }
304
387
 
305
- private dispatchNativeTabPageCommand(position: number, animated: boolean) {
388
+ private dispatchNativeTabPageCommand(
389
+ position: number,
390
+ animated: boolean,
391
+ expectedKey?: string
392
+ ) {
306
393
  const commandId = ++this.nativeTabCommandId;
307
394
  this.nativeTabCommandQueuedDuringScroll =
308
395
  this.nativePagerScrollState !== "idle";
@@ -315,7 +402,13 @@ export class CollapsiblePagerView extends React.PureComponent<
315
402
  }),
316
403
  () => {
317
404
  requestAnimationFrame(() => {
318
- if (commandId !== this.nativeTabCommandId || !this.nativeRef) {
405
+ if (
406
+ commandId !== this.nativeTabCommandId ||
407
+ !this.nativeRef ||
408
+ (expectedKey !== undefined &&
409
+ (!this.nativeTabBarEnabled() ||
410
+ this.props.nativeTabBar?.items[position]?.key !== expectedKey))
411
+ ) {
319
412
  return;
320
413
  }
321
414
  this.dispatchedNativeTabTarget = position;
@@ -441,7 +534,14 @@ export class CollapsiblePagerView extends React.PureComponent<
441
534
  ) => {
442
535
  const position = pageIndex(event.nativeEvent.position);
443
536
  if (
444
- position !== null &&
537
+ !this.nativeTabBarEnabled() ||
538
+ position === null ||
539
+ this.props.nativeTabBar?.items[position]?.key !== event.nativeEvent.key
540
+ ) {
541
+ return;
542
+ }
543
+ if (
544
+ !this.props.nativeTabBar?.controlled &&
445
545
  position >= 0 &&
446
546
  position < this.pages().length &&
447
547
  (position !== this.state.selectedPage ||
@@ -453,12 +553,20 @@ export class CollapsiblePagerView extends React.PureComponent<
453
553
  this.dispatchNativeTabPageCommand(
454
554
  position,
455
555
  this.props.nativeTabPressAnimationEnabled ??
456
- Math.abs(position - currentPage) <= 1
556
+ Math.abs(position - currentPage) <= 1,
557
+ event.nativeEvent.key
457
558
  );
458
559
  }
459
560
  this.props.onNativeTabPress?.(event);
460
561
  };
461
562
 
563
+ private onNativeToolbarPress = (
564
+ event: ReactNative.NativeSyntheticEvent<CollapsiblePagerToolbarPress>
565
+ ) => {
566
+ if (!this.props.nativeTabBar?.toolbar?.[event.nativeEvent.key]?.iconName) return;
567
+ this.props.onNativeToolbarPress?.(event);
568
+ };
569
+
462
570
  private onNativeSubHeaderPress = (
463
571
  event: ReactNative.NativeSyntheticEvent<OnNativeTabPressEventData>
464
572
  ) => {
@@ -472,7 +580,9 @@ export class CollapsiblePagerView extends React.PureComponent<
472
580
  stickyHeader,
473
581
  headerHeight,
474
582
  stickyHeaderHeight,
583
+ stickyHeaderHeights,
475
584
  topOverlay,
585
+ topOverlayHeightHint,
476
586
  externalTopOverlayTag,
477
587
  topScrollEdgeEffect,
478
588
  topOverlayColorScheme,
@@ -498,6 +608,11 @@ export class CollapsiblePagerView extends React.PureComponent<
498
608
  supportsTopScrollEdge &&
499
609
  (topScrollEdgeEffect === "hard" || topScrollEdgeEffect === "soft");
500
610
  const hasTopOverlay = scrollEdgeEnabled && topOverlay != null;
611
+ const initialTopOverlayHeight =
612
+ typeof topOverlayHeightHint === "number" &&
613
+ Number.isFinite(topOverlayHeightHint)
614
+ ? Math.max(0, Math.ceil(topOverlayHeightHint))
615
+ : 0;
501
616
  const pageKeys = pages.map(pageKey);
502
617
  const retained = new Set(this.mountedPages(pages.length));
503
618
  const deducedLayoutDirection =
@@ -510,6 +625,22 @@ export class CollapsiblePagerView extends React.PureComponent<
510
625
  (Platform.OS === "ios" || Platform.OS === "android") &&
511
626
  !!nativeTabBar?.items.length;
512
627
  const nativeTabBarStyle = nativeTabBar?.style;
628
+ const requestedContentPaddingVertical =
629
+ nativeTabBarStyle?.contentPaddingVertical;
630
+ const nativeTabBarContentPaddingVertical =
631
+ typeof requestedContentPaddingVertical === "number" &&
632
+ Number.isFinite(requestedContentPaddingVertical) &&
633
+ requestedContentPaddingVertical >= 0
634
+ ? requestedContentPaddingVertical
635
+ : 0;
636
+ const requestedTrailingReservedWidth =
637
+ nativeTabBarStyle?.trailingReservedWidth;
638
+ const nativeTabBarTrailingReservedWidth =
639
+ typeof requestedTrailingReservedWidth === "number" &&
640
+ Number.isFinite(requestedTrailingReservedWidth) &&
641
+ requestedTrailingReservedWidth >= 0
642
+ ? requestedTrailingReservedWidth
643
+ : 0;
513
644
  const nativeSubHeaderEnabled =
514
645
  (Platform.OS === "ios" || Platform.OS === "android") &&
515
646
  !!nativeSubHeader?.items.length;
@@ -538,11 +669,24 @@ export class CollapsiblePagerView extends React.PureComponent<
538
669
  offscreenPageLimit={Math.max(1, offscreenPageLimit ?? 1)}
539
670
  headerHeight={Math.max(0, Math.round(headerHeight))}
540
671
  stickyHeaderHeight={Math.max(0, Math.round(stickyHeaderHeight))}
672
+ stickyHeaderHeights={
673
+ Platform.OS === "ios" && stickyHeaderHeights
674
+ ? JSON.stringify(
675
+ stickyHeaderHeights.slice(0, pages.length).map((height) =>
676
+ Number.isFinite(height) && height >= 0 ? Math.round(height) : null
677
+ )
678
+ )
679
+ : undefined
680
+ }
541
681
  topScrollEdgeEffect={scrollEdgeEnabled ? topScrollEdgeEffect : "none"}
542
682
  topOverlayColorScheme={
543
683
  scrollEdgeEnabled ? topOverlayColorScheme : undefined
544
684
  }
545
- topOverlayHeight={hasTopOverlay ? this.state.topOverlayHeight : 0}
685
+ topOverlayHeight={
686
+ hasTopOverlay
687
+ ? this.state.topOverlayHeight ?? initialTopOverlayHeight
688
+ : 0
689
+ }
546
690
  externalTopOverlayTag={
547
691
  supportsTopScrollEdge && !hasTopOverlay
548
692
  ? externalTopOverlayTag ?? 0
@@ -553,22 +697,45 @@ export class CollapsiblePagerView extends React.PureComponent<
553
697
  retainedPages={JSON.stringify([...retained])}
554
698
  onPageSelected={this.onPageSelected}
555
699
  onPageScrollStateChanged={this.onPageScrollStateChanged}
700
+ nativeTabBarConfig={nativeTabBarEnabled ? JSON.stringify({
701
+ controlled: nativeTabBar.controlled,
702
+ selectedKey: nativeTabBar.selectedKey,
703
+ pageKeys: (() => {
704
+ const keys: string[] = [];
705
+ React.Children.forEach(this.props.children, (child) => {
706
+ if (child !== null && child !== undefined && typeof child !== "boolean") keys.push(pageKey(child, keys.length));
707
+ });
708
+ return keys;
709
+ })(),
710
+ toolbar: nativeToolbar(nativeTabBar.toolbar),
711
+ activeFontFamily: nativeTabBar.style?.activeFontFamily,
712
+ }) : undefined}
713
+ onNativeToolbarPress={this.onNativeToolbarPress}
556
714
  nativeTabBarItems={
557
715
  nativeTabBarEnabled ? JSON.stringify(nativeTabBar.items) : undefined
558
716
  }
559
717
  nativeTabBarHeight={
560
718
  nativeTabBarEnabled ? nativeTabBarStyle?.height ?? 44 : undefined
561
719
  }
720
+ nativeTabBarVariant={
721
+ nativeTabBarEnabled ? nativeTabBarStyle?.variant ?? "underline" : undefined
722
+ }
723
+ nativeTabBarTrailingReservedWidth={
724
+ nativeTabBarEnabled ? nativeTabBarTrailingReservedWidth : 0
725
+ }
562
726
  nativeTabBarContentPaddingHorizontal={
563
727
  nativeTabBarEnabled
564
728
  ? nativeTabBarStyle?.contentPaddingHorizontal ?? 20
565
729
  : undefined
566
730
  }
731
+ nativeTabBarContentPaddingVertical={
732
+ nativeTabBarEnabled ? nativeTabBarContentPaddingVertical : undefined
733
+ }
567
734
  nativeTabBarItemSpacing={
568
735
  nativeTabBarEnabled ? nativeTabBarStyle?.itemSpacing ?? 8 : undefined
569
736
  }
570
737
  nativeTabBarFontSize={
571
- nativeTabBarEnabled ? nativeTabBarStyle?.fontSize ?? 16 : undefined
738
+ nativeTabBarEnabled ? nativeTabBarStyle?.fontSize ?? (nativeTabBarStyle?.variant === "text" ? 18 : 16) : undefined
572
739
  }
573
740
  nativeTabBarFontFamily={
574
741
  nativeTabBarEnabled ? nativeTabBarStyle?.fontFamily : undefined