Skip to content

Commit 47ba829

Browse files
committed
feat(example): adding example for the usage
1 parent 85586c4 commit 47ba829

4 files changed

Lines changed: 247 additions & 5 deletions

File tree

example/src/App.tsx

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,79 @@
1-
import {View, StyleSheet} from 'react-native';
1+
import React from 'react';
2+
import {
3+
Text,
4+
Platform,
5+
ScrollView,
6+
StyleSheet,
7+
SafeAreaView,
8+
type TextStyle,
9+
useColorScheme,
10+
} from 'react-native';
11+
import Haptics from '@mhpdev/react-native-haptics';
12+
import HapticButton from './components/HapticButton';
13+
import {AndroidHaptics, Impacts, Notifications} from './core/config';
214

315
export default function App() {
16+
const scheme = useColorScheme();
17+
18+
const titleStyle = React.useMemo<TextStyle>(() => {
19+
return {
20+
fontSize: 20,
21+
fontWeight: 'bold',
22+
textAlign: 'center',
23+
marginVertical: 10,
24+
color: scheme === 'dark' ? 'white' : 'black',
25+
};
26+
}, [scheme]);
27+
428
return (
5-
<View style={styles.container}>
6-
<></>
7-
</View>
29+
<SafeAreaView style={styles.container}>
30+
<Text style={titleStyle}>React Native Haptics Examples</Text>
31+
<ScrollView
32+
showsVerticalScrollIndicator={false}
33+
contentContainerStyle={styles.content}>
34+
<Text style={titleStyle}>Haptics Impacts</Text>
35+
{Impacts.map(impact => (
36+
<HapticButton
37+
key={impact.name}
38+
title={impact.name}
39+
onPress={() => Haptics.impact(impact.style)}
40+
/>
41+
))}
42+
<Text style={titleStyle}>Haptics Notfications</Text>
43+
{Notifications.map(notification => (
44+
<HapticButton
45+
key={notification.name}
46+
title={notification.name}
47+
onPress={() => Haptics.notification(notification.type)}
48+
/>
49+
))}
50+
<Text style={titleStyle}>Haptics Selection</Text>
51+
<HapticButton title="Selection" onPress={Haptics.selection} />
52+
{Platform.OS !== 'android' ? null : (
53+
<React.Fragment>
54+
<Text style={titleStyle}>Android Haptics</Text>
55+
{AndroidHaptics.map(haptic => (
56+
<HapticButton
57+
key={haptic.name}
58+
title={haptic.name}
59+
onPress={() => Haptics.androidHaptics(haptic.type)}
60+
/>
61+
))}
62+
</React.Fragment>
63+
)}
64+
</ScrollView>
65+
</SafeAreaView>
866
);
967
}
1068

1169
const styles = StyleSheet.create({
1270
container: {
1371
flex: 1,
72+
paddingVertical: 30,
73+
},
74+
content: {
75+
marginTop: 10,
76+
paddingBottom: 40,
1477
alignItems: 'center',
15-
justifyContent: 'center',
1678
},
1779
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react';
2+
import {
3+
Text,
4+
useColorScheme,
5+
TouchableOpacity,
6+
type ViewStyle,
7+
type TextStyle,
8+
type TouchableOpacityProps,
9+
} from 'react-native';
10+
import {getRandomColor} from '../core/config';
11+
12+
interface HapticButtonProps extends TouchableOpacityProps {
13+
title: string;
14+
}
15+
16+
const HapticButton: React.FC<HapticButtonProps> = ({style, title, ...rest}) => {
17+
const scheme = useColorScheme();
18+
19+
const textStyle = React.useMemo<TextStyle>(() => {
20+
return {
21+
fontSize: 18,
22+
fontWeight: '600',
23+
textAlign: 'center',
24+
color: scheme === 'dark' ? 'white' : 'black',
25+
};
26+
}, [scheme]);
27+
28+
const buttonStyle = React.useMemo<ViewStyle>(() => {
29+
return {
30+
padding: 10,
31+
borderRadius: 7.5,
32+
marginVertical: 10,
33+
backgroundColor: getRandomColor(),
34+
};
35+
}, []);
36+
37+
return (
38+
<TouchableOpacity style={[buttonStyle, style]} {...rest}>
39+
<Text style={textStyle}>{title}</Text>
40+
</TouchableOpacity>
41+
);
42+
};
43+
44+
export default HapticButton;

example/src/core/config.ts

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import type {
2+
AndroidHapticsFeedback,
3+
ImpactFeedback,
4+
NotificationFeedback,
5+
} from '@mhpdev/react-native-haptics';
6+
import type {HapticsDataWithStyle, HapticsDataWithType} from './types';
7+
8+
export const getRandomColor = () => {
9+
return `rgba(${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)}, 0.5)`;
10+
};
11+
12+
export const Impacts: HapticsDataWithStyle<ImpactFeedback>[] = [
13+
{
14+
name: 'Light',
15+
style: 'light',
16+
},
17+
{
18+
name: 'Medium',
19+
style: 'medium',
20+
},
21+
{
22+
name: 'Heavy',
23+
style: 'heavy',
24+
},
25+
{
26+
name: 'Soft',
27+
style: 'soft',
28+
},
29+
{
30+
name: 'Rigid',
31+
style: 'rigid',
32+
},
33+
];
34+
35+
export const Notifications: HapticsDataWithType<NotificationFeedback>[] = [
36+
{
37+
name: 'Error',
38+
type: 'error',
39+
},
40+
{
41+
name: 'Success',
42+
type: 'success',
43+
},
44+
{
45+
name: 'Warning',
46+
type: 'warning',
47+
},
48+
];
49+
50+
export const AndroidHaptics: HapticsDataWithType<AndroidHapticsFeedback>[] = [
51+
{
52+
name: 'Long Press',
53+
type: 'long-press',
54+
},
55+
{
56+
name: 'Keyboard Tap',
57+
type: 'keyboard-tap',
58+
},
59+
{
60+
name: 'Virtual Key',
61+
type: 'virtual-key',
62+
},
63+
{
64+
name: 'Clock Tick',
65+
type: 'clock-tick',
66+
},
67+
{
68+
name: 'Confirm',
69+
type: 'confirm',
70+
},
71+
{
72+
name: 'Reject',
73+
type: 'reject',
74+
},
75+
{
76+
name: 'Toggle On',
77+
type: 'toggle-on',
78+
},
79+
{
80+
name: 'Toggle Off',
81+
type: 'toggle-off',
82+
},
83+
{
84+
name: 'Drag Start',
85+
type: 'drag-start',
86+
},
87+
{
88+
name: 'Gesture Start',
89+
type: 'gesture-start',
90+
},
91+
{
92+
name: 'Gesture End',
93+
type: 'gesture-end',
94+
},
95+
{
96+
name: 'Context Click',
97+
type: 'context-click',
98+
},
99+
{
100+
name: 'Keyboard Press',
101+
type: 'keyboard-press',
102+
},
103+
{
104+
name: 'Keyboard Release',
105+
type: 'keyboard-release',
106+
},
107+
{
108+
name: 'Virtual Key Release',
109+
type: 'virtual-key-release',
110+
},
111+
{
112+
name: 'Text Handle Move',
113+
type: 'text-handle-move',
114+
},
115+
{
116+
name: 'Segment Tick',
117+
type: 'segment-tick',
118+
},
119+
{
120+
name: 'Segment Frequent Tick',
121+
type: 'segment-frequent-tick',
122+
},
123+
{
124+
name: 'No Haptics',
125+
type: 'no-haptics',
126+
},
127+
];

example/src/core/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type HapticsDataWithType<T> = {
2+
name: string;
3+
type: T;
4+
};
5+
6+
export type HapticsDataWithStyle<T> = {
7+
name: string;
8+
style: T;
9+
};

0 commit comments

Comments
 (0)