@codeleap/mobile 3.15.2 → 3.15.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/mobile",
3
- "version": "3.15.2",
3
+ "version": "3.15.4",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -1,7 +1,7 @@
1
1
  import {
2
+ AnyFunction,
2
3
  ComponentVariants,
3
4
  onUpdate,
4
- PropsOf,
5
5
  TypeGuards,
6
6
  useDefaultComponentStyle,
7
7
  useWarning,
@@ -16,7 +16,6 @@ import {
16
16
  StyleSheet,
17
17
  } from 'react-native'
18
18
  import { StylesOf } from '../../types/utility'
19
- import { ScrollProps } from '../Scroll'
20
19
  import { View } from '../View'
21
20
  import { PagerPresets, PagerComposition } from './styles'
22
21
  export * from './styles'
@@ -29,9 +28,10 @@ export type PageProps = {
29
28
  page: number
30
29
  index: number
31
30
  isPrevious: boolean
32
-
33
31
  }
34
32
 
33
+ type ScrollEvent = NativeSyntheticEvent<NativeScrollEvent>
34
+
35
35
  export type PagerProps = React.PropsWithChildren<{
36
36
  variants?: ComponentVariants<typeof PagerPresets>['variants']
37
37
  styles?: StylesOf<PagerComposition>
@@ -43,9 +43,13 @@ export type PagerProps = React.PropsWithChildren<{
43
43
  renderPageWrapper?: React.FC<PageProps>
44
44
  pageWrapperProps?: any
45
45
  width?: number
46
- onScroll: ScrollProps['onScroll']
46
+ onScroll?: (event: ScrollEvent, args: { isLeft: boolean; isRight: boolean; x: number; }) => void
47
47
  /** If TRUE render page, nextPage and prevPage only */
48
48
  windowing?: boolean
49
+ scrollRightEnabled?: boolean
50
+ scrollLeftEnabled?: boolean
51
+ scrollDirectionThrottle?: number
52
+ onSwipeLastPage?: (event: ScrollEvent) => void
49
53
  } & ScrollViewProps>
50
54
 
51
55
  const defaultProps: Partial<PagerProps> = {
@@ -54,8 +58,11 @@ const defaultProps: Partial<PagerProps> = {
54
58
  page: 0,
55
59
  returnEarly: true,
56
60
  windowing: false,
57
- scrollEnabled: true,
58
61
  keyboardShouldPersistTaps: 'handled',
62
+ scrollEnabled: true,
63
+ scrollRightEnabled: true,
64
+ scrollLeftEnabled: true,
65
+ scrollDirectionThrottle: 650
59
66
  }
60
67
 
61
68
  export const Pager = (pagerProps: PagerProps) => {
@@ -72,6 +79,11 @@ export const Pager = (pagerProps: PagerProps) => {
72
79
  windowing = false,
73
80
  setPage,
74
81
  scrollEnabled = true,
82
+ scrollLeftEnabled,
83
+ scrollRightEnabled,
84
+ onScroll,
85
+ scrollDirectionThrottle,
86
+ onSwipeLastPage,
75
87
  } = {
76
88
  ...defaultProps,
77
89
  ...pagerProps,
@@ -80,6 +92,8 @@ export const Pager = (pagerProps: PagerProps) => {
80
92
  const childArr = React.Children.toArray(children)
81
93
  const scrollRef = useRef<ScrollView>(null)
82
94
  const [positionX, setPositionX] = React.useState(0)
95
+ const [scrollPositionX, setScrollPositionX] = React.useState(0)
96
+ const [_scrollEnabled, setScrollEnabled] = React.useState(true)
83
97
 
84
98
  const variantStyles = useDefaultComponentStyle<'u:Pager', typeof PagerPresets>(
85
99
  'u:Pager',
@@ -97,7 +111,6 @@ export const Pager = (pagerProps: PagerProps) => {
97
111
 
98
112
  if (!validWidth) {
99
113
  width = windowWidth
100
-
101
114
  }
102
115
 
103
116
  useWarning(
@@ -112,22 +125,59 @@ export const Pager = (pagerProps: PagerProps) => {
112
125
 
113
126
  const WrapperComponent = renderPageWrapper || View
114
127
 
128
+ const hasScrollDirectionDisabled = !scrollLeftEnabled || !scrollRightEnabled
129
+
115
130
  const handleScrollEnd = useCallback(
116
- ({ nativeEvent }: NativeSyntheticEvent<NativeScrollEvent>) => {
117
- const x = nativeEvent.contentOffset.x
131
+ (event: ScrollEvent) => {
132
+ const x = event?.nativeEvent.contentOffset.x
118
133
  const toPage = Math.ceil(x / width)
119
134
 
120
- if (toPage !== page && toPage <= childArr.length - 1 && !!scrollEnabled) {
135
+ if (toPage !== page && toPage <= childArr.length - 1) {
121
136
  setPage(toPage)
122
137
  setPositionX(toPage * width)
138
+ } else if (toPage >= childArr.length - 1 && TypeGuards.isFunction(onSwipeLastPage)) {
139
+ onSwipeLastPage?.(event)
123
140
  }
124
141
  },
125
142
  [childArr, page, setPage],
126
143
  )
127
144
 
145
+ const handleScroll = (event: ScrollEvent) => {
146
+ const scrollX = event?.nativeEvent?.contentOffset?.x
147
+
148
+ if (!_scrollEnabled) {
149
+ setScrollPositionX(scrollX)
150
+ return null
151
+ }
152
+
153
+ const isRight = scrollX < scrollPositionX
154
+ const isLeft = scrollX > scrollPositionX
155
+
156
+ if (TypeGuards.isFunction(onScroll)) onScroll?.(event, { isLeft, isRight, x: scrollX })
157
+
158
+ if (hasScrollDirectionDisabled) {
159
+ if (isRight && !scrollRightEnabled || isLeft && !scrollLeftEnabled) {
160
+ setScrollEnabled(false)
161
+
162
+ setTimeout(() => {
163
+ scrollRef.current.scrollTo({
164
+ x: positionX,
165
+ animated: true,
166
+ })
167
+
168
+ setTimeout(() => {
169
+ setScrollEnabled(true)
170
+ }, scrollDirectionThrottle)
171
+ })
172
+ }
173
+ }
174
+
175
+ setScrollPositionX(scrollX)
176
+ }
177
+
128
178
  onUpdate(() => {
129
179
  const x = width * page
130
- if (scrollRef.current && x !== positionX) {
180
+ if (scrollRef.current && x !== positionX && !hasScrollDirectionDisabled) {
131
181
  scrollRef.current.scrollTo({
132
182
  x,
133
183
  animated: true,
@@ -142,12 +192,12 @@ export const Pager = (pagerProps: PagerProps) => {
142
192
  horizontal
143
193
  pagingEnabled
144
194
  onMomentumScrollEnd={handleScrollEnd}
145
- scrollEventThrottle={300}
195
+ scrollEventThrottle={hasScrollDirectionDisabled ? 2000 : 300}
146
196
  showsHorizontalScrollIndicator={false}
147
197
  style={[variantStyles.wrapper, style]}
148
198
  {...pagerProps}
149
- scrollEnabled={childArr.length > 1 && scrollEnabled}
150
-
199
+ onScroll={handleScroll}
200
+ scrollEnabled={childArr.length > 1 && scrollEnabled && _scrollEnabled}
151
201
  >
152
202
  {childArr.map((child: PagerProps['children'][number], index) => {
153
203