react-native-snap-sheet 1.0.5 → 1.0.7

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": "react-native-snap-sheet",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/deflexable/react-native-snap-sheet#readme",
6
6
  "bugs": {
@@ -19,7 +19,7 @@
19
19
  "test": "echo \"Error: no test specified\" && exit 1"
20
20
  },
21
21
  "dependencies": {
22
- "react-native-dodge-keyboard": "^1.0.5",
22
+ "react-native-dodge-keyboard": "^1.0.7",
23
23
  "react-native-push-back": "^1.0.0"
24
24
  }
25
25
  }
package/src/snapsheet.js CHANGED
@@ -55,7 +55,7 @@ const SnapSheet = forwardRef(function SnapSheet({
55
55
  if (isLift) {
56
56
  const realChecker = __checkIfElementIsFocused;
57
57
  __checkIfElementIsFocused = (r, refs) => {
58
- return !!r?.[CheckFocusedNode] && refs.some(v => realChecker ? realChecker?.(v) : v?.isFocused?.());
58
+ return !!r?.[CheckFocusedNode] && refs.some(v => realChecker ? realChecker?.(v, refs) : v?.isFocused?.());
59
59
  };
60
60
  if (keyboardDodgingOffset === undefined) {
61
61
  keyboardDodgingOffset = 0;
@@ -69,8 +69,6 @@ const SnapSheet = forwardRef(function SnapSheet({
69
69
 
70
70
  const [scrollEnabled, setScrollEnabled] = useState(false);
71
71
  const [dodgeOffset, setDodgeOffset] = useState(0);
72
- const [currentIndex, setCurrentIndex] = useState(initialSnapIndex);
73
- const [finishedIndex, setFinishedIndex] = useState(initialSnapIndex);
74
72
  const [prefferedAnchor, setPrefferedAnchor] = useState();
75
73
 
76
74
  snapPoints = snapPoints.map(v => v + dodgeOffset);
@@ -100,6 +98,7 @@ const SnapSheet = forwardRef(function SnapSheet({
100
98
  * @type {import("react").RefObject<{[key: string]: { ref: import('react-native').ScrollView, scrollY: 0, location: number[], anchorId: boolean }}>}
101
99
  */
102
100
  const scrollRefObj = useRef({});
101
+ const dodgeRef = useRef();
103
102
  const lastOffset = useRef(translateY._value);
104
103
  const lastSnapIndex = useRef(initialSnapIndex);
105
104
  const instantPrefferAnchor = useRef();
@@ -120,6 +119,9 @@ const SnapSheet = forwardRef(function SnapSheet({
120
119
  }
121
120
 
122
121
  const snapToIndex = useRef();
122
+ const stillSnapping = useRef();
123
+ const lastSnappingInstance = useRef(0);
124
+ const shouldRefreshDodge = useRef();
123
125
 
124
126
  snapToIndex.current = (index, force, velocity, onFinish) => {
125
127
  if (disabled && !force) return;
@@ -133,7 +135,6 @@ const SnapSheet = forwardRef(function SnapSheet({
133
135
 
134
136
  const prevY = translateY._value;
135
137
  setScrollEnabled(index === snapPoints.length - 1);
136
- setCurrentIndex(index);
137
138
 
138
139
  // console.log('snapping:', index);
139
140
  let wasFinished;
@@ -147,6 +148,8 @@ const SnapSheet = forwardRef(function SnapSheet({
147
148
  const timeout = pixel * PixelRate;
148
149
 
149
150
  const timer = setTimeout(guessFinish, Math.max(300, timeout));
151
+ const thisRef = ++lastSnappingInstance.current;
152
+ stillSnapping.current = true;
150
153
 
151
154
  // console.log('snapTimer:', { timeout, pixel }, ' newY:', newY, ' snapPoint:', initSnapPoints, ' snapTrans:', snapTranslateValues);
152
155
 
@@ -156,8 +159,16 @@ const SnapSheet = forwardRef(function SnapSheet({
156
159
  useNativeDriver: true
157
160
  }).start(() => {
158
161
  clearTimeout(timer);
159
- setFinishedIndex(index);
160
162
  guessFinish();
163
+ if (thisRef === lastSnappingInstance.current) {
164
+ requestIdleCallback(() => {
165
+ stillSnapping.current = false;
166
+ if (shouldRefreshDodge.current) {
167
+ isLifting.current = false;
168
+ dodgeRef.current.trigger();
169
+ }
170
+ }, { timeout: 700 });
171
+ }
161
172
  });
162
173
 
163
174
  lastOffset.current = newY;
@@ -305,44 +316,52 @@ const SnapSheet = forwardRef(function SnapSheet({
305
316
  }), [handleColor]);
306
317
 
307
318
  const disableDodging = keyboardDodgingBehaviour === 'off';
308
- const sameIndex = currentIndex === finishedIndex;
309
319
 
310
320
  const instanceIdIterator = useRef(0);
311
321
  const prevKE = useRef();
312
- const prevLiftOffset = useRef(0);
313
322
  const isLifting = useRef();
323
+ const lastLiftInstance = useRef(0);
314
324
 
315
- const quicklyDodgeKeyboard = (offset, keyboardEvent) => {
316
- // console.log('quicklyDodgeKeyboard offset:', offset, ' keyboardEvent:', keyboardEvent);
325
+ const quicklyDodgeKeyboard = async (event) => {
326
+ let { liftUp, keyboardEvent } = event;
327
+ // console.log('quicklyDodgeKeyboard event:', event);
317
328
 
318
329
  if (!keyboardEvent) {
319
- if (!(keyboardEvent = prevKE.current)) {
320
- prevLiftOffset.current = offset;
321
- return;
322
- }
330
+ if (!(keyboardEvent = prevKE.current)) return;
323
331
  }
324
332
  if (keyboardEvent.endCoordinates.height) {
325
333
  prevKE.current = keyboardEvent;
326
334
  } else {
327
335
  if (prevKE.current) keyboardEvent = prevKE.current;
328
336
  }
337
+ isLifting.current = true;
338
+ if (stillSnapping.current) {
339
+ if (!liftUp) {
340
+ isLifting.current = false;
341
+ setDodgeOffset(0);
342
+ return;
343
+ }
344
+ shouldRefreshDodge.current = true;
345
+ return;
346
+ }
347
+ const thisRef = ++lastLiftInstance.current;
329
348
 
330
- if (prevLiftOffset.current === offset) return;
331
- prevLiftOffset.current = offset;
349
+ const kh = keyboardEvent?.endCoordinates?.height || 0;
350
+ const realSnapPoint = initSnapPoints[lastSnapIndex.current];
332
351
 
333
- const newPosY = MODAL_HEIGHT - (initSnapPoints[lastSnapIndex.current] + offset);
334
- const newDuration = (Math.abs(newPosY - translateY._value) * keyboardEvent.duration) / keyboardEvent?.endCoordinates.height;
352
+ const newY = liftUp ? translateY._value - liftUp : (MODAL_HEIGHT - realSnapPoint);
353
+ const newDodgeOffset = (MODAL_HEIGHT - newY) - realSnapPoint;
354
+ const newDuration = kh > 0 ? (Math.min(Math.abs(liftUp), kh) / kh) * keyboardEvent.duration : 0;
335
355
 
336
- // console.log('newPosY:', newPosY, ' timing newDuration:', newDuration);
337
- isLifting.current = true;
356
+ // console.log('newPosY:', newY, ' timing newDuration:', newDuration, ' newDodgeOffset:', newDodgeOffset);
338
357
  Animated.timing(translateY, {
339
358
  duration: Math.max((newDuration || 0) - 70, 0),
340
- toValue: newPosY,
341
- useNativeDriver: false
359
+ toValue: newY,
360
+ useNativeDriver: true
342
361
  }).start(() => {
343
- if (offset === prevLiftOffset.current) {
362
+ if (thisRef === lastLiftInstance.current) {
344
363
  isLifting.current = false;
345
- setDodgeOffset(offset);
364
+ setDodgeOffset(newDodgeOffset);
346
365
  }
347
366
  });
348
367
  }
@@ -361,12 +380,11 @@ const SnapSheet = forwardRef(function SnapSheet({
361
380
  )}
362
381
  <View style={styling.flexer}>
363
382
  <DodgeKeyboard
383
+ ref={dodgeRef}
364
384
  offset={keyboardDodgingOffset}
365
- disabled={!sameIndex || disableDodging}
385
+ disabled={disableDodging}
366
386
  checkIfElementIsFocused={__checkIfElementIsFocused}
367
- onHandleDodging={({ liftUp, keyboardEvent }) => {
368
- quicklyDodgeKeyboard(liftUp, keyboardEvent);
369
- }}>
387
+ onHandleDodging={quicklyDodgeKeyboard}>
370
388
  {ReactHijacker({
371
389
  children,
372
390
  enableLocator: true,
@@ -435,6 +453,7 @@ const SnapSheet = forwardRef(function SnapSheet({
435
453
  }
436
454
  }}
437
455
  dodge_keyboard_input
456
+ dodge_keyboard_clipping
438
457
  style={styling.fakePlaceholder}
439
458
  /> : null}
440
459
  </DodgeKeyboard>
@@ -78,6 +78,7 @@ export const SnapSheetModalBase = forwardRef(function SnapSheetModalBase({
78
78
  useMemo(makeSizingPromise, []);
79
79
 
80
80
  const sizingReady = !centered || (contentHeight !== undefined && viewHeight !== undefined);
81
+ const hasClosed = futureState === 'closed' && currentState === 'closed';
81
82
 
82
83
  useEffect(() => {
83
84
  if (sizingReady) {
@@ -103,7 +104,6 @@ export const SnapSheetModalBase = forwardRef(function SnapSheetModalBase({
103
104
 
104
105
  const willClose = futureState === 'closed';
105
106
  const isOpened = futureState !== 'closed';
106
- const hasClosed = futureState === 'closed' && currentState === 'closed';
107
107
 
108
108
  const sheetRef = useRef();
109
109
  const inputRefs = useRef({});
@@ -179,7 +179,7 @@ export const SnapSheetModalBase = forwardRef(function SnapSheetModalBase({
179
179
  onClosed?.();
180
180
  } else onOpened?.();
181
181
  hasMountOpened.current = true;
182
- }, [hasClosed]);
182
+ }, [futureState, currentState]);
183
183
 
184
184
  useBackButton(() => {
185
185
  snapModal.current(0);
@@ -256,7 +256,9 @@ export const SnapSheetModalBase = forwardRef(function SnapSheetModalBase({
256
256
  ...isLift ? {
257
257
  __checkIfElementIsFocused: (r, refs) => {
258
258
  const realChecker = restProps?.__checkIfElementIsFocused;
259
- return !!r?.[CheckFocusedNode] && refs.some(v => realChecker ? realChecker?.(v) : v?.isFocused?.());
259
+ return !!r?.[CheckFocusedNode] &&
260
+ !willClose &&
261
+ refs.some(v => realChecker ? realChecker?.(v, refs) : v?.isFocused?.());
260
262
  },
261
263
  keyboardDodgingBehaviour: 'optimum'
262
264
  } : {}
@@ -298,6 +300,7 @@ export const SnapSheetModalBase = forwardRef(function SnapSheetModalBase({
298
300
  }
299
301
  }}
300
302
  dodge_keyboard_input
303
+ dodge_keyboard_clipping
301
304
  style={styling.fakePlaceholder}
302
305
  /> : null}
303
306
  </View>}