-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #775 from andrew-bierman/haptic-feedback-implement…
…ation
- Loading branch information
Showing
7 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
selectionAsync, | ||
notificationAsync, | ||
NotificationFeedbackType, | ||
impactAsync, | ||
ImpactFeedbackStyle, | ||
} from 'expo-haptics'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { Platform } from 'react-native'; | ||
|
||
export const useHaptic = () => { | ||
const createFeedbackHandler = useCallback((action: any, type: string) => { | ||
if (Platform.OS === 'web') { | ||
return () => {}; | ||
} | ||
return () => action(type); | ||
}, []); | ||
|
||
return useMemo( | ||
() => ({ | ||
selection: Platform.OS === 'web' ? undefined : selectionAsync, | ||
success: createFeedbackHandler( | ||
notificationAsync, | ||
NotificationFeedbackType.Success, | ||
), | ||
error: createFeedbackHandler( | ||
notificationAsync, | ||
NotificationFeedbackType.Error, | ||
), | ||
warning: createFeedbackHandler( | ||
notificationAsync, | ||
NotificationFeedbackType.Warning, | ||
), | ||
light: createFeedbackHandler(impactAsync, ImpactFeedbackStyle.Light), | ||
medium: createFeedbackHandler(impactAsync, ImpactFeedbackStyle.Medium), | ||
heavy: createFeedbackHandler(impactAsync, ImpactFeedbackStyle.Heavy), | ||
}), | ||
[], | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,43 @@ | ||
import { Button, styled } from 'tamagui'; | ||
import { Button, ButtonProps, styled } from 'tamagui'; | ||
import { useHaptic } from 'app/hooks/common'; | ||
import { useCallback } from 'react'; | ||
|
||
const RButton = styled(Button, { | ||
interface HapticRButtonProps extends ButtonProps { | ||
hapticStrength?: 'light' | 'medium' | 'heavy'; | ||
} | ||
|
||
const StyledButton = styled(Button, { | ||
backgroundColor: '#0C66A1', // temp fix, we need to set up proper tamagui theme | ||
color: 'white', | ||
}); | ||
|
||
const RButton: React.FC<HapticRButtonProps> = ({ | ||
hapticStrength, | ||
onPress, | ||
...props | ||
}) => { | ||
const haptic = useHaptic(); | ||
|
||
const triggerHapticFeedback = useCallback(() => { | ||
switch (hapticStrength) { | ||
case 'light': | ||
haptic.light(); | ||
break; | ||
case 'medium': | ||
haptic.medium(); | ||
break; | ||
case 'heavy': | ||
haptic.heavy(); | ||
break; | ||
} | ||
}, [hapticStrength]); | ||
|
||
const handlePress = (e) => { | ||
onPress?.(e); | ||
triggerHapticFeedback(); | ||
}; | ||
|
||
return <StyledButton {...props} onPress={handlePress} />; | ||
}; | ||
|
||
export default RButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters