@mpxjs/webpack-plugin 2.10.14-beta.3 → 2.10.14-beta.4-1
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import React, { useContext, useEffect } from 'react';
|
|
1
|
+
import React, { useContext, useEffect, useRef } from 'react';
|
|
2
2
|
import { Keyboard, View } from 'react-native';
|
|
3
|
-
import Animated, { useSharedValue, useAnimatedStyle, withTiming, Easing } from 'react-native-reanimated';
|
|
3
|
+
import Animated, { useSharedValue, useAnimatedStyle, withTiming, Easing, cancelAnimation } from 'react-native-reanimated';
|
|
4
4
|
import { KeyboardAvoidContext } from './context';
|
|
5
5
|
import { isIOS } from './utils';
|
|
6
6
|
const KeyboardAvoidingView = ({ children, style, contentContainerStyle }) => {
|
|
@@ -9,14 +9,25 @@ const KeyboardAvoidingView = ({ children, style, contentContainerStyle }) => {
|
|
|
9
9
|
const offset = useSharedValue(0);
|
|
10
10
|
const basic = useSharedValue('auto');
|
|
11
11
|
const keyboardAvoid = useContext(KeyboardAvoidContext);
|
|
12
|
+
// fix: 某些特殊机型下隐藏键盘可能会先触发一次 keyboardWillShow,
|
|
13
|
+
// 比如机型 iPhone 11 Pro,可能会导致显隐动画冲突
|
|
14
|
+
// 因此增加状态标记 + clearTimeout + cancelAnimation 来优化
|
|
15
|
+
const isShow = useRef(false);
|
|
16
|
+
const timerRef = useRef(null);
|
|
12
17
|
const animatedStyle = useAnimatedStyle(() => ({
|
|
13
18
|
transform: [{ translateY: -offset.value }],
|
|
14
19
|
flexBasis: basic.value
|
|
15
20
|
}));
|
|
16
21
|
const resetKeyboard = () => {
|
|
22
|
+
if (!isShow.current) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
isShow.current = false;
|
|
26
|
+
timerRef.current && clearTimeout(timerRef.current);
|
|
17
27
|
if (keyboardAvoid?.current) {
|
|
18
28
|
keyboardAvoid.current = null;
|
|
19
29
|
}
|
|
30
|
+
cancelAnimation(offset);
|
|
20
31
|
offset.value = withTiming(0, { duration, easing });
|
|
21
32
|
basic.value = 'auto';
|
|
22
33
|
};
|
|
@@ -30,16 +41,20 @@ const KeyboardAvoidingView = ({ children, style, contentContainerStyle }) => {
|
|
|
30
41
|
if (isIOS) {
|
|
31
42
|
subscriptions = [
|
|
32
43
|
Keyboard.addListener('keyboardWillShow', (evt) => {
|
|
33
|
-
if (!keyboardAvoid?.current)
|
|
44
|
+
if (!keyboardAvoid?.current || isShow.current) {
|
|
34
45
|
return;
|
|
46
|
+
}
|
|
47
|
+
isShow.current = true;
|
|
48
|
+
timerRef.current && clearTimeout(timerRef.current);
|
|
35
49
|
const { endCoordinates } = evt;
|
|
36
50
|
const { ref, cursorSpacing = 0 } = keyboardAvoid.current;
|
|
37
|
-
setTimeout(() => {
|
|
51
|
+
timerRef.current = setTimeout(() => {
|
|
38
52
|
ref?.current?.measure((x, y, width, height, pageX, pageY) => {
|
|
39
53
|
const aboveOffset = offset.value + pageY + height - endCoordinates.screenY;
|
|
40
54
|
const aboveValue = -aboveOffset >= cursorSpacing ? 0 : aboveOffset + cursorSpacing;
|
|
41
55
|
const belowValue = Math.min(endCoordinates.height, aboveOffset + cursorSpacing);
|
|
42
56
|
const value = aboveOffset > 0 ? belowValue : aboveValue;
|
|
57
|
+
cancelAnimation(offset);
|
|
43
58
|
offset.value = withTiming(value, { duration, easing }, (finished) => {
|
|
44
59
|
if (finished) {
|
|
45
60
|
// Set flexBasic after animation to trigger re-layout and reset layout information
|
|
@@ -55,8 +70,10 @@ const KeyboardAvoidingView = ({ children, style, contentContainerStyle }) => {
|
|
|
55
70
|
else {
|
|
56
71
|
subscriptions = [
|
|
57
72
|
Keyboard.addListener('keyboardDidShow', (evt) => {
|
|
58
|
-
if (!keyboardAvoid?.current)
|
|
73
|
+
if (!keyboardAvoid?.current || isShow.current) {
|
|
59
74
|
return;
|
|
75
|
+
}
|
|
76
|
+
isShow.current = true;
|
|
60
77
|
const { endCoordinates } = evt;
|
|
61
78
|
const { ref, cursorSpacing = 0 } = keyboardAvoid.current;
|
|
62
79
|
ref?.current?.measure((x, y, width, height, pageX, pageY) => {
|
|
@@ -65,6 +82,7 @@ const KeyboardAvoidingView = ({ children, style, contentContainerStyle }) => {
|
|
|
65
82
|
const aboveValue = -aboveOffset >= cursorSpacing ? 0 : aboveOffset + cursorSpacing;
|
|
66
83
|
const belowValue = Math.min(belowOffset, cursorSpacing);
|
|
67
84
|
const value = aboveOffset > 0 ? belowValue : aboveValue;
|
|
85
|
+
cancelAnimation(offset);
|
|
68
86
|
offset.value = withTiming(value, { duration, easing }, (finished) => {
|
|
69
87
|
if (finished) {
|
|
70
88
|
// Set flexBasic after animation to trigger re-layout and reset layout information
|
|
@@ -78,6 +96,7 @@ const KeyboardAvoidingView = ({ children, style, contentContainerStyle }) => {
|
|
|
78
96
|
}
|
|
79
97
|
return () => {
|
|
80
98
|
subscriptions.forEach(subscription => subscription.remove());
|
|
99
|
+
timerRef.current && clearTimeout(timerRef.current);
|
|
81
100
|
};
|
|
82
101
|
}, [keyboardAvoid]);
|
|
83
102
|
return (<View style={style} onTouchEnd={onTouchEnd}>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import React, { ReactNode, useContext, useEffect } from 'react'
|
|
1
|
+
import React, { ReactNode, useContext, useEffect, useRef } from 'react'
|
|
2
2
|
import { DimensionValue, EmitterSubscription, Keyboard, View, ViewStyle, NativeSyntheticEvent, NativeTouchEvent } from 'react-native'
|
|
3
|
-
import Animated, { useSharedValue, useAnimatedStyle, withTiming, Easing } from 'react-native-reanimated'
|
|
3
|
+
import Animated, { useSharedValue, useAnimatedStyle, withTiming, Easing, cancelAnimation } from 'react-native-reanimated'
|
|
4
4
|
import { KeyboardAvoidContext } from './context'
|
|
5
5
|
import { isIOS } from './utils'
|
|
6
6
|
|
|
@@ -18,15 +18,30 @@ const KeyboardAvoidingView = ({ children, style, contentContainerStyle }: Keyboa
|
|
|
18
18
|
const basic = useSharedValue('auto')
|
|
19
19
|
const keyboardAvoid = useContext(KeyboardAvoidContext)
|
|
20
20
|
|
|
21
|
+
// fix: 某些特殊机型下隐藏键盘可能会先触发一次 keyboardWillShow,
|
|
22
|
+
// 比如机型 iPhone 11 Pro,可能会导致显隐动画冲突
|
|
23
|
+
// 因此增加状态标记 + clearTimeout + cancelAnimation 来优化
|
|
24
|
+
const isShow = useRef<boolean>(false)
|
|
25
|
+
const timerRef = useRef<NodeJS.Timeout | null>(null)
|
|
26
|
+
|
|
21
27
|
const animatedStyle = useAnimatedStyle(() => ({
|
|
22
28
|
transform: [{ translateY: -offset.value }],
|
|
23
29
|
flexBasis: basic.value as DimensionValue
|
|
24
30
|
}))
|
|
25
31
|
|
|
26
32
|
const resetKeyboard = () => {
|
|
33
|
+
if (!isShow.current) {
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
isShow.current = false
|
|
38
|
+
timerRef.current && clearTimeout(timerRef.current)
|
|
39
|
+
|
|
27
40
|
if (keyboardAvoid?.current) {
|
|
28
41
|
keyboardAvoid.current = null
|
|
29
42
|
}
|
|
43
|
+
|
|
44
|
+
cancelAnimation(offset)
|
|
30
45
|
offset.value = withTiming(0, { duration, easing })
|
|
31
46
|
basic.value = 'auto'
|
|
32
47
|
}
|
|
@@ -43,15 +58,23 @@ const KeyboardAvoidingView = ({ children, style, contentContainerStyle }: Keyboa
|
|
|
43
58
|
if (isIOS) {
|
|
44
59
|
subscriptions = [
|
|
45
60
|
Keyboard.addListener('keyboardWillShow', (evt: any) => {
|
|
46
|
-
if (!keyboardAvoid?.current)
|
|
61
|
+
if (!keyboardAvoid?.current || isShow.current) {
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
isShow.current = true
|
|
66
|
+
timerRef.current && clearTimeout(timerRef.current)
|
|
67
|
+
|
|
47
68
|
const { endCoordinates } = evt
|
|
48
69
|
const { ref, cursorSpacing = 0 } = keyboardAvoid.current
|
|
49
|
-
|
|
70
|
+
|
|
71
|
+
timerRef.current = setTimeout(() => {
|
|
50
72
|
ref?.current?.measure((x: number, y: number, width: number, height: number, pageX: number, pageY: number) => {
|
|
51
73
|
const aboveOffset = offset.value + pageY + height - endCoordinates.screenY
|
|
52
74
|
const aboveValue = -aboveOffset >= cursorSpacing ? 0 : aboveOffset + cursorSpacing
|
|
53
75
|
const belowValue = Math.min(endCoordinates.height, aboveOffset + cursorSpacing)
|
|
54
76
|
const value = aboveOffset > 0 ? belowValue : aboveValue
|
|
77
|
+
cancelAnimation(offset)
|
|
55
78
|
offset.value = withTiming(value, { duration, easing }, (finished) => {
|
|
56
79
|
if (finished) {
|
|
57
80
|
// Set flexBasic after animation to trigger re-layout and reset layout information
|
|
@@ -66,15 +89,22 @@ const KeyboardAvoidingView = ({ children, style, contentContainerStyle }: Keyboa
|
|
|
66
89
|
} else {
|
|
67
90
|
subscriptions = [
|
|
68
91
|
Keyboard.addListener('keyboardDidShow', (evt: any) => {
|
|
69
|
-
if (!keyboardAvoid?.current)
|
|
92
|
+
if (!keyboardAvoid?.current || isShow.current) {
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
isShow.current = true
|
|
97
|
+
|
|
70
98
|
const { endCoordinates } = evt
|
|
71
99
|
const { ref, cursorSpacing = 0 } = keyboardAvoid.current
|
|
100
|
+
|
|
72
101
|
ref?.current?.measure((x: number, y: number, width: number, height: number, pageX: number, pageY: number) => {
|
|
73
102
|
const aboveOffset = pageY + height - endCoordinates.screenY
|
|
74
103
|
const belowOffset = endCoordinates.height - aboveOffset
|
|
75
104
|
const aboveValue = -aboveOffset >= cursorSpacing ? 0 : aboveOffset + cursorSpacing
|
|
76
105
|
const belowValue = Math.min(belowOffset, cursorSpacing)
|
|
77
106
|
const value = aboveOffset > 0 ? belowValue : aboveValue
|
|
107
|
+
cancelAnimation(offset)
|
|
78
108
|
offset.value = withTiming(value, { duration, easing }, (finished) => {
|
|
79
109
|
if (finished) {
|
|
80
110
|
// Set flexBasic after animation to trigger re-layout and reset layout information
|
|
@@ -89,6 +119,7 @@ const KeyboardAvoidingView = ({ children, style, contentContainerStyle }: Keyboa
|
|
|
89
119
|
|
|
90
120
|
return () => {
|
|
91
121
|
subscriptions.forEach(subscription => subscription.remove())
|
|
122
|
+
timerRef.current && clearTimeout(timerRef.current)
|
|
92
123
|
}
|
|
93
124
|
}, [keyboardAvoid])
|
|
94
125
|
|