-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4dec8f
commit 23309a5
Showing
3 changed files
with
30 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,44 @@ | ||
import {useFocusEffect} from '@react-navigation/native'; | ||
import React from 'react'; | ||
import {InteractionManager} from 'react-native'; | ||
import {interpolate, interpolateColor, useAnimatedStyle, useSharedValue, withDelay, withSequence, withTiming} from 'react-native-reanimated'; | ||
import CONST from '@src/CONST'; | ||
import useTheme from './useTheme'; | ||
|
||
type Props = { | ||
height: number; | ||
shouldHighlight: boolean; | ||
highlightDuration?: number; | ||
delay?: number; | ||
}; | ||
|
||
/** | ||
* Returns a highlight style that interpolates the colour, height and opacity giving a fading effect. | ||
*/ | ||
export default function useAnimatedHighlightStyle(shouldHighlight: boolean, highlightDuration: number = CONST.ANIMATED_TRANSITION, delay = 100) { | ||
export default function useAnimatedHighlightStyle({shouldHighlight, highlightDuration = CONST.ANIMATED_TRANSITION, delay = CONST.ANIMATION_IN_TIMING, height}: Props) { | ||
const repeatableProgress = useSharedValue(0); | ||
const nonRepeatableProgress = useSharedValue(0); | ||
const theme = useTheme(); | ||
|
||
const highlightBackgroundStyle = useAnimatedStyle(() => ({ | ||
backgroundColor: interpolateColor(repeatableProgress.value, [0, 1], ['rgba(0, 0, 0, 0)', theme.border]), | ||
flex: interpolate(nonRepeatableProgress.value, [0, 1], [0, 1]), | ||
height: interpolate(nonRepeatableProgress.value, [0, 1], [0, height]), | ||
opacity: interpolate(nonRepeatableProgress.value, [0, 1], [0, 1]), | ||
})); | ||
|
||
useFocusEffect( | ||
React.useCallback(() => { | ||
if (!shouldHighlight) { | ||
return; | ||
} | ||
React.useEffect(() => { | ||
if (!shouldHighlight) { | ||
return; | ||
} | ||
InteractionManager.runAfterInteractions(() => { | ||
nonRepeatableProgress.value = withTiming(1, {duration: highlightDuration}); | ||
InteractionManager.runAfterInteractions(() => { | ||
repeatableProgress.value = withSequence( | ||
withDelay(delay, withTiming(0)), | ||
withTiming(1, {duration: highlightDuration}), | ||
withDelay(delay, withTiming(1)), | ||
withTiming(0, {duration: highlightDuration}), | ||
); | ||
}); | ||
}, [shouldHighlight, highlightDuration, delay, repeatableProgress, nonRepeatableProgress]), | ||
); | ||
repeatableProgress.value = withSequence( | ||
withDelay(delay, withTiming(0)), | ||
withTiming(1, {duration: highlightDuration}), | ||
withDelay(delay, withTiming(1)), | ||
withTiming(0, {duration: highlightDuration}), | ||
); | ||
}); | ||
}, [shouldHighlight, highlightDuration, delay, repeatableProgress, nonRepeatableProgress]); | ||
|
||
return highlightBackgroundStyle; | ||
return shouldHighlight ? highlightBackgroundStyle : null; | ||
} |
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