@r0b0t3d/react-native-collapsible 1.0.1 → 1.2.0-alpha.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.
Files changed (124) hide show
  1. package/lib/commonjs/components/CollapsibleContainer.js +4 -2
  2. package/lib/commonjs/components/CollapsibleContainer.js.map +1 -1
  3. package/lib/commonjs/components/CollapsibleView.js +4 -3
  4. package/lib/commonjs/components/CollapsibleView.js.map +1 -1
  5. package/lib/commonjs/components/{AnimatedTopView.js → header/AnimatedTopView.js} +0 -0
  6. package/lib/commonjs/components/{AnimatedTopView.js.map → header/AnimatedTopView.js.map} +0 -0
  7. package/lib/commonjs/components/{CollapsibleHeaderContainer.js → header/CollapsibleHeaderContainer.js} +9 -8
  8. package/lib/commonjs/components/header/CollapsibleHeaderContainer.js.map +1 -0
  9. package/lib/commonjs/components/{StickyView.js → header/StickyView.js} +18 -31
  10. package/lib/commonjs/components/header/StickyView.js.map +1 -0
  11. package/lib/commonjs/components/pullToRefresh/PullToRefreshContainer.js +75 -0
  12. package/lib/commonjs/components/pullToRefresh/PullToRefreshContainer.js.map +1 -0
  13. package/lib/commonjs/components/pullToRefresh/PullToRefreshProvider.js +35 -0
  14. package/lib/commonjs/components/pullToRefresh/PullToRefreshProvider.js.map +1 -0
  15. package/lib/commonjs/components/pullToRefresh/RefreshControl.js +73 -0
  16. package/lib/commonjs/components/pullToRefresh/RefreshControl.js.map +1 -0
  17. package/lib/commonjs/components/pullToRefresh/usePullToRefreshContext.js +24 -0
  18. package/lib/commonjs/components/pullToRefresh/usePullToRefreshContext.js.map +1 -0
  19. package/lib/commonjs/components/pullToRefresh/utils.js +59 -0
  20. package/lib/commonjs/components/pullToRefresh/utils.js.map +1 -0
  21. package/lib/commonjs/components/{CollapsibleFlatList.js → scrollable/CollapsibleFlatList.js} +36 -31
  22. package/lib/commonjs/components/scrollable/CollapsibleFlatList.js.map +1 -0
  23. package/lib/commonjs/components/{CollapsibleScrollView.js → scrollable/CollapsibleScrollView.js} +6 -6
  24. package/lib/commonjs/components/scrollable/CollapsibleScrollView.js.map +1 -0
  25. package/lib/commonjs/{hooks → components/scrollable}/useAnimatedScroll.js +7 -7
  26. package/lib/commonjs/components/scrollable/useAnimatedScroll.js.map +1 -0
  27. package/lib/commonjs/hooks/useInternalCollapsibleContext.js +1 -1
  28. package/lib/commonjs/hooks/useInternalCollapsibleContext.js.map +1 -1
  29. package/lib/commonjs/index.js +21 -12
  30. package/lib/commonjs/index.js.map +1 -1
  31. package/lib/commonjs/{types.js → types.d.js} +1 -1
  32. package/lib/commonjs/{types.js.map → types.d.js.map} +0 -0
  33. package/lib/commonjs/utils/debounce.js +20 -0
  34. package/lib/commonjs/utils/debounce.js.map +1 -0
  35. package/lib/commonjs/{hooks/withCollapsibleContext.js → withCollapsibleContext.js} +95 -64
  36. package/lib/commonjs/withCollapsibleContext.js.map +1 -0
  37. package/lib/module/components/CollapsibleContainer.js +1 -1
  38. package/lib/module/components/CollapsibleContainer.js.map +1 -1
  39. package/lib/module/components/CollapsibleView.js +4 -3
  40. package/lib/module/components/CollapsibleView.js.map +1 -1
  41. package/lib/module/components/{AnimatedTopView.js → header/AnimatedTopView.js} +0 -0
  42. package/lib/module/components/{AnimatedTopView.js.map → header/AnimatedTopView.js.map} +0 -0
  43. package/lib/module/components/{CollapsibleHeaderContainer.js → header/CollapsibleHeaderContainer.js} +7 -6
  44. package/lib/module/components/header/CollapsibleHeaderContainer.js.map +1 -0
  45. package/lib/module/components/{StickyView.js → header/StickyView.js} +17 -30
  46. package/lib/module/components/header/StickyView.js.map +1 -0
  47. package/lib/module/components/pullToRefresh/PullToRefreshContainer.js +56 -0
  48. package/lib/module/components/pullToRefresh/PullToRefreshContainer.js.map +1 -0
  49. package/lib/module/components/pullToRefresh/PullToRefreshProvider.js +21 -0
  50. package/lib/module/components/pullToRefresh/PullToRefreshProvider.js.map +1 -0
  51. package/lib/module/components/pullToRefresh/RefreshControl.js +55 -0
  52. package/lib/module/components/pullToRefresh/RefreshControl.js.map +1 -0
  53. package/lib/module/components/pullToRefresh/usePullToRefreshContext.js +13 -0
  54. package/lib/module/components/pullToRefresh/usePullToRefreshContext.js.map +1 -0
  55. package/lib/module/components/pullToRefresh/utils.js +42 -0
  56. package/lib/module/components/pullToRefresh/utils.js.map +1 -0
  57. package/lib/module/components/{CollapsibleFlatList.js → scrollable/CollapsibleFlatList.js} +36 -32
  58. package/lib/module/components/scrollable/CollapsibleFlatList.js.map +1 -0
  59. package/lib/module/components/{CollapsibleScrollView.js → scrollable/CollapsibleScrollView.js} +5 -5
  60. package/lib/module/components/scrollable/CollapsibleScrollView.js.map +1 -0
  61. package/lib/module/{hooks → components/scrollable}/useAnimatedScroll.js +6 -6
  62. package/lib/module/components/scrollable/useAnimatedScroll.js.map +1 -0
  63. package/lib/module/hooks/useInternalCollapsibleContext.js +1 -1
  64. package/lib/module/hooks/useInternalCollapsibleContext.js.map +1 -1
  65. package/lib/module/index.js +6 -5
  66. package/lib/module/index.js.map +1 -1
  67. package/lib/module/types.d.js +2 -0
  68. package/lib/module/{types.js.map → types.d.js.map} +0 -0
  69. package/lib/module/utils/debounce.js +13 -0
  70. package/lib/module/utils/debounce.js.map +1 -0
  71. package/lib/module/withCollapsibleContext.js +163 -0
  72. package/lib/module/withCollapsibleContext.js.map +1 -0
  73. package/lib/typescript/components/CollapsibleView.d.ts +1 -1
  74. package/lib/typescript/components/{AnimatedTopView.d.ts → header/AnimatedTopView.d.ts} +0 -0
  75. package/lib/typescript/components/{CollapsibleHeaderContainer.d.ts → header/CollapsibleHeaderContainer.d.ts} +0 -0
  76. package/lib/typescript/components/{StickyView.d.ts → header/StickyView.d.ts} +0 -0
  77. package/lib/typescript/components/pullToRefresh/PullToRefreshContainer.d.ts +8 -0
  78. package/lib/typescript/components/pullToRefresh/PullToRefreshProvider.d.ts +6 -0
  79. package/lib/typescript/components/pullToRefresh/RefreshControl.d.ts +9 -0
  80. package/lib/typescript/components/pullToRefresh/usePullToRefreshContext.d.ts +4 -0
  81. package/lib/typescript/components/pullToRefresh/utils.d.ts +20 -0
  82. package/lib/typescript/components/{CollapsibleFlatList.d.ts → scrollable/CollapsibleFlatList.d.ts} +1 -1
  83. package/lib/typescript/components/{CollapsibleScrollView.d.ts → scrollable/CollapsibleScrollView.d.ts} +1 -1
  84. package/lib/typescript/{hooks → components/scrollable}/useAnimatedScroll.d.ts +0 -0
  85. package/lib/typescript/hooks/useInternalCollapsibleContext.d.ts +1 -1
  86. package/lib/typescript/index.d.ts +6 -5
  87. package/lib/typescript/utils/debounce.d.ts +1 -0
  88. package/lib/typescript/{hooks/withCollapsibleContext.d.ts → withCollapsibleContext.d.ts} +0 -0
  89. package/package.json +4 -2
  90. package/src/components/CollapsibleContainer.tsx +1 -1
  91. package/src/components/CollapsibleView.tsx +4 -3
  92. package/src/components/{AnimatedTopView.tsx → header/AnimatedTopView.tsx} +0 -0
  93. package/src/components/{CollapsibleHeaderContainer.tsx → header/CollapsibleHeaderContainer.tsx} +6 -4
  94. package/src/components/{StickyView.tsx → header/StickyView.tsx} +15 -22
  95. package/src/components/pullToRefresh/PullToRefreshContainer.tsx +65 -0
  96. package/src/components/pullToRefresh/PullToRefreshProvider.tsx +27 -0
  97. package/src/components/pullToRefresh/RefreshControl.tsx +80 -0
  98. package/src/components/pullToRefresh/usePullToRefreshContext.ts +13 -0
  99. package/src/components/pullToRefresh/utils.ts +49 -0
  100. package/src/components/scrollable/CollapsibleFlatList.tsx +135 -0
  101. package/src/components/{CollapsibleScrollView.tsx → scrollable/CollapsibleScrollView.tsx} +6 -6
  102. package/src/{hooks → components/scrollable}/useAnimatedScroll.ts +8 -8
  103. package/src/hooks/useInternalCollapsibleContext.ts +1 -1
  104. package/src/index.tsx +6 -5
  105. package/src/{types.ts → types.d.ts} +14 -3
  106. package/src/utils/debounce.ts +10 -0
  107. package/src/withCollapsibleContext.tsx +201 -0
  108. package/lib/commonjs/components/CollapsibleFlatList.js.map +0 -1
  109. package/lib/commonjs/components/CollapsibleHeaderContainer.js.map +0 -1
  110. package/lib/commonjs/components/CollapsibleScrollView.js.map +0 -1
  111. package/lib/commonjs/components/StickyView.js.map +0 -1
  112. package/lib/commonjs/hooks/useAnimatedScroll.js.map +0 -1
  113. package/lib/commonjs/hooks/withCollapsibleContext.js.map +0 -1
  114. package/lib/module/components/CollapsibleFlatList.js.map +0 -1
  115. package/lib/module/components/CollapsibleHeaderContainer.js.map +0 -1
  116. package/lib/module/components/CollapsibleScrollView.js.map +0 -1
  117. package/lib/module/components/StickyView.js.map +0 -1
  118. package/lib/module/hooks/useAnimatedScroll.js.map +0 -1
  119. package/lib/module/hooks/withCollapsibleContext.js +0 -136
  120. package/lib/module/hooks/withCollapsibleContext.js.map +0 -1
  121. package/lib/module/types.js +0 -2
  122. package/lib/typescript/types.d.ts +0 -33
  123. package/src/components/CollapsibleFlatList.tsx +0 -119
  124. package/src/hooks/withCollapsibleContext.tsx +0 -164
@@ -1,119 +0,0 @@
1
- import React, {
2
- useCallback,
3
- useEffect,
4
- useMemo,
5
- useRef,
6
- useState,
7
- } from 'react';
8
- import { FlatListProps, FlatList, View, StyleSheet } from 'react-native';
9
- import Animated, { runOnJS, useDerivedValue } from 'react-native-reanimated';
10
- import AnimatedTopView from './AnimatedTopView';
11
- import useAnimatedScroll from '../hooks/useAnimatedScroll';
12
- import useCollapsibleContext from '../hooks/useCollapsibleContext';
13
- import type { CollapsibleProps } from '../types';
14
- import { useInternalCollapsibleContext } from '../hooks/useInternalCollapsibleContext';
15
-
16
- const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
17
-
18
- type Props<Data> = Omit<FlatListProps<Data>, 'scrollEnabled'> &
19
- CollapsibleProps;
20
-
21
- export default function CollapsibleFlatList<Data>({
22
- headerSnappable = true,
23
- ...props
24
- }: Props<Data>) {
25
- const scrollView = useRef<FlatList>(null);
26
- const { headerHeight } = useCollapsibleContext();
27
- const { contentMinHeight } = useInternalCollapsibleContext();
28
- const mounted = useRef(true);
29
- const contentHeight = useRef(0);
30
-
31
- useEffect(() => {
32
- return () => {
33
- mounted.current = false;
34
- };
35
- }, []);
36
-
37
- const scrollTo = useCallback((yValue: number, animated = true) => {
38
- scrollView.current?.scrollToOffset({
39
- offset: yValue,
40
- animated,
41
- });
42
- }, []);
43
-
44
- const handleInternalContentHeight = useCallback((value: number) => {
45
- if (mounted.current) {
46
- setInternalContentMinHeight(value);
47
- }
48
- }, []);
49
-
50
- const { scrollHandler } = useAnimatedScroll({
51
- headerSnappable,
52
- scrollTo,
53
- });
54
-
55
- const [internalContentMinHeight, setInternalContentMinHeight] = useState(
56
- contentMinHeight.value
57
- );
58
-
59
- useDerivedValue(() => {
60
- if (
61
- contentHeight.current < contentMinHeight.value &&
62
- contentMinHeight.value !== internalContentMinHeight
63
- ) {
64
- runOnJS(handleInternalContentHeight)(contentMinHeight.value);
65
- }
66
- }, [internalContentMinHeight, contentHeight.current]);
67
-
68
- const contentContainerStyle = useMemo(
69
- () => [
70
- styles.contentContainer,
71
- { minHeight: internalContentMinHeight },
72
- props.contentContainerStyle,
73
- ],
74
- [props.contentContainerStyle, internalContentMinHeight]
75
- );
76
-
77
- const handleContentSizeChange = useCallback((_, height) => {
78
- contentHeight.current = height;
79
- }, []);
80
-
81
- const renderListHeader = () => (
82
- <View>
83
- <AnimatedTopView height={headerHeight} />
84
- {props.ListHeaderComponent}
85
- </View>
86
- );
87
-
88
- return (
89
- // @ts-ignore
90
- <AnimatedFlatList
91
- ref={scrollView}
92
- bounces={false}
93
- keyboardDismissMode="on-drag"
94
- keyboardShouldPersistTaps="handled"
95
- scrollEventThrottle={16}
96
- {...props}
97
- style={[styles.container, props.style]}
98
- contentContainerStyle={contentContainerStyle}
99
- onScroll={scrollHandler}
100
- ListHeaderComponent={renderListHeader()}
101
- onContentSizeChange={handleContentSizeChange}
102
- />
103
- );
104
- }
105
-
106
- const styles = StyleSheet.create({
107
- container: {
108
- ...StyleSheet.absoluteFillObject,
109
- },
110
- contentContainer: {
111
- flexGrow: 1,
112
- },
113
- topView: {
114
- position: 'absolute',
115
- top: 0,
116
- left: 0,
117
- right: 0,
118
- },
119
- });
@@ -1,164 +0,0 @@
1
- /* eslint-disable react-hooks/exhaustive-deps */
2
- import React, { FC, useCallback, useMemo, useRef } from 'react';
3
- import type { CollapsibleHandles, LayoutParams } from '../types';
4
- import { CollapsibleContext } from './useCollapsibleContext';
5
- import { InternalCollapsibleContext } from './useInternalCollapsibleContext';
6
- import { useSharedValue, withTiming } from 'react-native-reanimated';
7
- import type { View } from 'react-native';
8
-
9
- export default function withCollapsibleContext<T>(Component: FC<T>) {
10
- return (props: T) => {
11
- const collapsibleHandlers = useRef<CollapsibleHandles>();
12
- const headerHeight = useSharedValue(0);
13
- const scrollY = useSharedValue(0);
14
- const headerCollapsed = useSharedValue(false);
15
- const contentMinHeight = useSharedValue(0);
16
- const stickyViewTops = useSharedValue<Record<string, number>>({});
17
- const stickyHeaderHeight = useRef(0);
18
- const containerHeight = useRef(0);
19
- const firstStickyViewY = useRef(1000000);
20
- const fixedHeaderHeight = useRef(0);
21
- const headerContainersHeight = useRef<Record<string, number>>({});
22
- const stickyViewPositions = useRef<Record<string, LayoutParams>>({});
23
- const containerRef = useRef<View>(null);
24
-
25
- const setCollapsibleHandlers = useCallback((handlers) => {
26
- collapsibleHandlers.current = handlers;
27
- }, []);
28
-
29
- const refreshHeaderCollapsed = useCallback(() => {
30
- const maxY = fixedHeaderHeight.current - firstStickyViewY.current;
31
- const isCollapsed = scrollY.value >= maxY;
32
- headerCollapsed.value = isCollapsed;
33
- }, []);
34
-
35
- const refreshMinContentHeight = useCallback(() => {
36
- contentMinHeight.value =
37
- containerHeight.current +
38
- fixedHeaderHeight.current -
39
- stickyHeaderHeight.current;
40
- }, []);
41
-
42
- const populateData = useCallback(() => {
43
- const viewPositions = stickyViewPositions.current;
44
- const headerHeight = fixedHeaderHeight.current;
45
-
46
- const sortedKeys = Object.keys(viewPositions).sort(
47
- (a, b) => viewPositions[a].top - viewPositions[b].top
48
- );
49
- let totalTop = 0;
50
- const values: any = {};
51
- for (let i = 0; i < sortedKeys.length; i++) {
52
- values[sortedKeys[i]] = totalTop;
53
- // Try minus 1 make it filled when scrolling up.
54
- // Otherwise, we can see a small space between the persits views
55
- totalTop += viewPositions[sortedKeys[i]].height - 1;
56
- }
57
- stickyViewTops.value = values;
58
- firstStickyViewY.current = viewPositions[sortedKeys[0]]?.top || 0;
59
- refreshHeaderCollapsed();
60
- const stickyHeader = sortedKeys.reduce((acc, key) => {
61
- const data = viewPositions[key];
62
- const isInsideHeader = data.top < headerHeight;
63
- if (isInsideHeader) {
64
- return acc + data.height;
65
- }
66
- return acc;
67
- }, 0);
68
- stickyHeaderHeight.current = stickyHeader;
69
- refreshMinContentHeight();
70
- }, [stickyViewPositions.current, fixedHeaderHeight.current]);
71
-
72
- const handleStickyViewLayout = useCallback(
73
- (viewKey: string, layout?: LayoutParams) => {
74
- if (!layout) {
75
- delete stickyViewPositions.current[viewKey];
76
- } else {
77
- stickyViewPositions.current = {
78
- ...stickyViewPositions.current,
79
- [viewKey]: layout,
80
- };
81
- }
82
- populateData();
83
- },
84
- [populateData, fixedHeaderHeight]
85
- );
86
-
87
- const handleHeaderContainerLayout = useCallback(
88
- (viewKey: string, height?: number) => {
89
- if (!height) {
90
- delete headerContainersHeight.current[viewKey];
91
- } else {
92
- headerContainersHeight.current[viewKey] = height;
93
- }
94
- const totalHeight = Object.keys(headerContainersHeight.current).reduce(
95
- (acc, key) => headerContainersHeight.current[key] + acc,
96
- 0
97
- );
98
- headerHeight.value = withTiming(totalHeight, {
99
- duration: fixedHeaderHeight.current === 0 ? 0 : 200,
100
- });
101
- fixedHeaderHeight.current = totalHeight;
102
-
103
- populateData();
104
- refreshHeaderCollapsed();
105
- refreshMinContentHeight();
106
- },
107
- [populateData, refreshHeaderCollapsed, refreshMinContentHeight]
108
- );
109
-
110
- const handleContainerHeight = useCallback(
111
- (height: number) => {
112
- containerHeight.current = height;
113
- refreshMinContentHeight();
114
- },
115
- [refreshMinContentHeight]
116
- );
117
-
118
- const context = useMemo(() => {
119
- return {
120
- collapse: () => collapsibleHandlers.current?.collapse(),
121
- expand: () => collapsibleHandlers.current?.expand(),
122
- scrollTo: (offset: number, animate?: boolean) =>
123
- collapsibleHandlers.current?.scrollTo(offset, animate),
124
- headerHeight,
125
- scrollY,
126
- headerCollapsed,
127
- };
128
- }, [scrollY, headerHeight, headerCollapsed]);
129
-
130
- const internalContext = useMemo(
131
- () => ({
132
- containerRef,
133
- handleStickyViewLayout,
134
- handleHeaderContainerLayout,
135
- setCollapsibleHandlers,
136
- handleContainerHeight,
137
- firstStickyViewY,
138
- stickyViewTops,
139
- fixedHeaderHeight,
140
- stickyHeaderHeight,
141
- contentMinHeight,
142
- }),
143
- [
144
- setCollapsibleHandlers,
145
- handleStickyViewLayout,
146
- handleHeaderContainerLayout,
147
- handleContainerHeight,
148
- firstStickyViewY,
149
- stickyViewTops,
150
- fixedHeaderHeight,
151
- stickyHeaderHeight,
152
- contentMinHeight,
153
- ]
154
- );
155
-
156
- return (
157
- <CollapsibleContext.Provider value={context}>
158
- <InternalCollapsibleContext.Provider value={internalContext}>
159
- <Component {...props} />
160
- </InternalCollapsibleContext.Provider>
161
- </CollapsibleContext.Provider>
162
- );
163
- };
164
- }