Skip to content

Commit b568eed

Browse files
committed
feat: add animated example
1 parent b2c3c9e commit b568eed

1 file changed

Lines changed: 87 additions & 19 deletions

File tree

  • example/src/screens/Examples/KeyboardEffects

example/src/screens/Examples/KeyboardEffects/index.tsx

Lines changed: 87 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
import React, { useState } from "react";
2-
import { StyleSheet, TextInput, TouchableOpacity, View } from "react-native";
1+
import React, { useCallback, useMemo, useState } from "react";
2+
import {
3+
Image,
4+
StyleSheet,
5+
Text,
6+
TextInput,
7+
TouchableOpacity,
8+
View,
9+
} from "react-native";
310
import { KeyboardEffects } from "react-native-keyboard-controller";
411
import { SafeAreaView } from "react-native-safe-area-context";
512

13+
const GIF_SOURCE = {
14+
uri: "https://media2.giphy.com/media/v1.Y2lkPTc5MGI3NjExbG1hdjU0bDBqZ3dha3NoNXF0YTY5ajNhNTdmMmV5azZsMHhlc21pMyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/dAWZiSMbMvObDWP3aA/giphy.gif",
15+
};
16+
617
const COLORS = [
718
"#FF6B6B",
819
"#4ECDC4",
@@ -12,34 +23,75 @@ const COLORS = [
1223
"#DDA0DD",
1324
];
1425

26+
type ColorButtonProps = {
27+
color: string;
28+
index: number;
29+
selectedIndex: number;
30+
onSelect: (index: number) => void;
31+
};
32+
33+
const ColorButton = ({
34+
color,
35+
index,
36+
selectedIndex,
37+
onSelect,
38+
}: ColorButtonProps) => {
39+
const onPress = useCallback(() => onSelect(index), [index, onSelect]);
40+
const buttonStyle = useMemo(
41+
() => [
42+
styles.colorButton,
43+
{ backgroundColor: color },
44+
index === selectedIndex && styles.selectedColor,
45+
],
46+
[color, index, selectedIndex],
47+
);
48+
49+
return <TouchableOpacity style={buttonStyle} onPress={onPress} />;
50+
};
51+
1552
const KeyboardEffectsExample = () => {
1653
const [colorIndex, setColorIndex] = useState(0);
54+
const [showGif, setShowGif] = useState(false);
55+
56+
const toggleGif = useCallback(() => setShowGif((v) => !v), []);
57+
58+
const colorEffectStyle = useMemo(
59+
() => [styles.fill, { backgroundColor: COLORS[colorIndex] }],
60+
[colorIndex],
61+
);
1762

1863
return (
1964
<>
2065
<SafeAreaView style={styles.container}>
21-
<View style={styles.colorPicker}>
22-
{COLORS.map((color, index) => (
23-
<TouchableOpacity
24-
key={color}
25-
style={[
26-
styles.colorButton,
27-
{ backgroundColor: color },
28-
index === colorIndex && styles.selectedColor,
29-
]}
30-
onPress={() => setColorIndex(index)}
31-
/>
32-
))}
66+
<View style={styles.controls}>
67+
<View style={styles.colorPicker}>
68+
{COLORS.map((color, index) => (
69+
<ColorButton
70+
key={color}
71+
color={color}
72+
index={index}
73+
selectedIndex={colorIndex}
74+
onSelect={setColorIndex}
75+
/>
76+
))}
77+
</View>
78+
<TouchableOpacity style={styles.toggleButton} onPress={toggleGif}>
79+
<Text style={styles.toggleButtonText}>
80+
{showGif ? "GIF" : "Color"}
81+
</Text>
82+
</TouchableOpacity>
3383
</View>
3484
<TextInput
3585
placeholder="Tap to open keyboard..."
3686
style={styles.textInput}
3787
/>
3888
</SafeAreaView>
3989
<KeyboardEffects>
40-
<View
41-
style={[styles.effect, { backgroundColor: COLORS[colorIndex] }]}
42-
/>
90+
{showGif ? (
91+
<Image source={GIF_SOURCE} style={styles.fill} />
92+
) : (
93+
<View style={colorEffectStyle} />
94+
)}
4395
</KeyboardEffects>
4496
</>
4597
);
@@ -50,12 +102,17 @@ const styles = StyleSheet.create({
50102
flex: 1,
51103
justifyContent: "space-between",
52104
},
53-
colorPicker: {
105+
controls: {
54106
flexDirection: "row",
107+
alignItems: "center",
55108
justifyContent: "center",
56109
gap: 12,
57110
padding: 16,
58111
},
112+
colorPicker: {
113+
flexDirection: "row",
114+
gap: 12,
115+
},
59116
colorButton: {
60117
width: 40,
61118
height: 40,
@@ -65,6 +122,17 @@ const styles = StyleSheet.create({
65122
borderWidth: 3,
66123
borderColor: "#333",
67124
},
125+
toggleButton: {
126+
paddingHorizontal: 16,
127+
paddingVertical: 8,
128+
borderRadius: 20,
129+
borderWidth: 2,
130+
borderColor: "#ccc",
131+
},
132+
toggleButtonText: {
133+
fontWeight: "600",
134+
color: "#333",
135+
},
68136
textInput: {
69137
height: 50,
70138
paddingHorizontal: 16,
@@ -74,7 +142,7 @@ const styles = StyleSheet.create({
74142
borderColor: "#ccc",
75143
borderRadius: 8,
76144
},
77-
effect: {
145+
fill: {
78146
flex: 1,
79147
},
80148
});

0 commit comments

Comments
 (0)