@codeleap/mobile 3.15.2 → 3.15.3

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