Skip to content

Commit 0d1fe01

Browse files
RayKay91RayKay91
RayKay91
authored and
RayKay91
committed
getting closer...
1 parent c084e32 commit 0d1fe01

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

babel.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
module.exports = {
22
presets: ['module:metro-react-native-babel-preset'],
3-
plugins: ['react-native-reanimated/plugin'],
43
};

example/babel.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module.exports = function (api) {
1717
},
1818
},
1919
],
20-
'react-native-reanimated/plugin',
2120
],
2221
};
2322
};

example/src/App.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@ function B() {
2222
return (
2323
<Button
2424
title={'launch modal'}
25-
onPress={() => t.show({ title: 'nmdnmdndnd' })}
25+
onPress={() =>
26+
t.show({
27+
title: 'nmdnmdndnd',
28+
subText: 'lol',
29+
toastType: 2,
30+
onWillShow: () => console.log('willShow'),
31+
onDidShow: () => console.log('didShow'),
32+
onWillHide: () => console.log('willHide'),
33+
onDidHide: () => console.log('didHide'),
34+
})
35+
}
2636
/>
2737
);
2838
}

src/Toast.tsx

+27-9
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,28 @@ export function Toast({ delay = consts.DEFAULT_DELAY, ...props }: Props) {
7676
timer.current = setTimeout(() => props.setIsVisible(false), delay);
7777
}
7878
const isBelowFinalPosition = gestureState.dy > 0; // use 0 as event gives relative position to where view was
79+
const newPosition = FINAL_POSITION + gestureState.dy;
7980
if (isBelowFinalPosition) {
8081
// add resistance when moving down
81-
const newPosition =
82-
FINAL_POSITION + gestureState.dy ** DRAG_RESISTANCE;
82+
y.current.setValue(newPosition ** DRAG_RESISTANCE);
83+
} else {
84+
// is going up
85+
// make fling gesture)
8386
y.current.setValue(newPosition);
8487
}
8588
},
8689
onStartShouldSetPanResponder: () => true,
8790
onPanResponderRelease: (_, gestureState) => {
8891
if (gestureState.dy > 0) {
92+
// going down
8993
Animated.spring(y.current, SHOW_ANIM_CONFIG).start();
94+
} else {
95+
// going up
96+
console.log({ vY: gestureState.vy });
97+
Animated.decay(y.current, {
98+
velocity: gestureState.vy * 0.3,
99+
useNativeDriver: true,
100+
}).start();
90101
}
91102
},
92103
})
@@ -114,21 +125,28 @@ export function Toast({ delay = consts.DEFAULT_DELAY, ...props }: Props) {
114125
});
115126
}, [props]);
116127

117-
// useEffect necessary otherwise causes too many pending callbacks issue.
128+
const isFirstMount = useRef(true);
129+
130+
// useEffect necessary otherwise causes bad setState issue
118131
useEffect(() => {
132+
// create a copy because current value will have changed (react-warning)
133+
const yCopy = y.current;
134+
yCopy.addListener(({ value }) => {
135+
// call hide function if above screen without animation.
136+
if (value < TOP_OF_SCREEN && !isFirstMount) hide();
137+
});
119138
if (props.isVisible) {
120139
show();
121140
if (props.autoDismiss !== false) {
122141
timer.current = setTimeout(() => props.setIsVisible(false), delay);
123142
}
124143
}
125-
144+
// need a way for it to not call hide when it is initially mounted.
126145
if (!props.isVisible) {
127146
if (timer.current) clearTimeout(timer.current);
128-
// check this works - may need a listener instead of number conversion
129-
if (Number(y.current) !== INITIAL_POSITION) hide();
147+
// @todo fix this.
148+
if (yCopy !== INITIAL_POSITION) hide();
130149
}
131-
132150
// eslint-disable-next-line react-hooks/exhaustive-deps
133151
}, [props.isVisible]);
134152

@@ -192,7 +210,7 @@ const getToastTypeColor = (toastType: ToastType | undefined) => {
192210
}
193211
};
194212

195-
const BORDER_RADIUS = 12;
213+
const BORDER_RADIUS = 8;
196214

197215
const styles = StyleSheet.create({
198216
container: {
@@ -210,7 +228,7 @@ const styles = StyleSheet.create({
210228
borderColor: '#BBB',
211229
},
212230
accentColumn: {
213-
width: 10,
231+
width: 8,
214232
backgroundColor: 'white',
215233
borderTopLeftRadius: BORDER_RADIUS,
216234
borderBottomLeftRadius: BORDER_RADIUS,

0 commit comments

Comments
 (0)