-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
138 lines (126 loc) · 3.76 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import "react-native-gesture-handler";
import React from "react";
import { ModalOptions, ModalProvider, ModalStackConfig, createModalStack } from "react-native-modalfy";
import { ImageBackground, Dimensions, StyleSheet, StatusBar, Easing, Text, Animated, Platform } from "react-native";
import DemoModal from "./components/DemoModal";
import IntroModal from "./components/IntroModal";
import IntroButton from "./components/IntroButton";
interface Modal<N, C> {
origin: "Hooks" | "Class" | "Plain JS";
color: C | "darkgreen";
// 👆 Comment this one and uncomment that one 👇 to remove all the TypeScript errors
// color: 'lightsalmon' | 'deepskyblue' | 'deeppink' | 'darkgreen'
// Note: the TS errors were left voluntarily to showcase the type inference & autocomplete possibilities
name: N;
}
export interface ModalStackParamsList {
ModalA: Modal<"ModalA", "lightsalmon">;
ModalB: Modal<"ModalB", "deepskyblue">;
ModalC: Modal<"ModalC", "deeppink">;
IntroModal: undefined;
}
export type ModalName = Exclude<keyof ModalStackParamsList, "IntroModal">;
const { height, width } = Dimensions.get("screen");
const config: ModalStackConfig = {
IntroModal: {
modal: IntroModal,
position: "bottom",
backdropOpacity: 0.4,
animateInConfig: {
easing: Easing.inOut(Easing.exp),
duration: 300,
},
animateOutConfig: {
easing: Easing.inOut(Easing.exp),
duration: 500,
},
transitionOptions: (animatedValue) => ({
transform: [
{
translateY: animatedValue.interpolate({
inputRange: [0, 1, 2],
outputRange: [height, 0, height],
}),
},
],
}),
},
ModalA: DemoModal,
ModalB: DemoModal,
ModalC: DemoModal,
};
const animate = (animatedValue: Animated.Value, toValue: number, callback?: () => void) => {
Animated.spring(animatedValue, {
toValue,
damping: 10,
mass: 0.35,
stiffness: 100,
overshootClamping: true,
restSpeedThreshold: 0.001,
restDisplacementThreshold: 0.001,
useNativeDriver: true,
}).start(({ finished }) => {
if (finished) callback?.();
});
};
const defaultOptions: ModalOptions = {
backdropOpacity: 0.4,
animationIn: animate,
animationOut: animate,
transitionOptions: (animatedValue) => ({
opacity: animatedValue.interpolate({
inputRange: [0, 1, 2],
outputRange: [0, 1, 0.9],
}),
transform: [
{ perspective: 2000 },
{
translateX: animatedValue.interpolate({
inputRange: [0, 1, 2],
outputRange: [-width / 1.5, 0, width / 1.5],
extrapolate: "clamp",
}),
},
{
rotateY: animatedValue.interpolate({
inputRange: [0, 1, 2],
outputRange: ["90deg", "0deg", "-90deg"],
extrapolate: "clamp",
}),
},
{
scale: animatedValue.interpolate({
inputRange: [0, 1, 2],
outputRange: [1.2, 1, 0.9],
extrapolate: "clamp",
}),
},
],
}),
};
const stack = createModalStack<ModalStackParamsList>(config, defaultOptions);
const App = () => (
<ModalProvider stack={stack}>
<ImageBackground style={styles.container} source={require("./background.jpg")}>
<StatusBar translucent barStyle="light-content" backgroundColor="transparent" />
<Text style={styles.title}>🥞 Modalfy</Text>
<IntroButton />
</ImageBackground>
</ModalProvider>
);
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
// Showcases how Modalfy prevents scrolling on Web when the stack is opened.
...Platform.select({ web: { height: "150vh" } }),
},
title: {
fontSize: 54,
color: "white",
marginBottom: 50,
fontWeight: "bold",
},
});
export default App;