Created
February 19, 2022 20:14
-
-
Save Stringsaeed/8ebfd6ddc0205d28df292bfa3a4f17db to your computer and use it in GitHub Desktop.
react native animated status bar hook
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
import {StatusBar} from 'react-native'; | |
import {useCallback} from 'use-memo-one'; | |
import {useFocusEffect} from '@react-navigation/native'; | |
import Animated, {runOnJS, useDerivedValue} from 'react-native-reanimated'; | |
const updateStatusBarStyle = ( | |
shared: Animated.SharedValue<number>, | |
maxNo: number, | |
) => { | |
'worklet'; | |
if (shared.value > maxNo) { | |
runOnJS(StatusBar.setBarStyle)('dark-content'); | |
} else { | |
runOnJS(StatusBar.setBarStyle)('light-content'); | |
} | |
}; | |
const useAnimatedStatusBar = ( | |
shared: Animated.SharedValue<number>, | |
maxNo: number, | |
) => { | |
useFocusEffect( | |
useCallback(() => { | |
updateStatusBarStyle(shared, maxNo); | |
}, [maxNo, shared]), | |
); | |
useDerivedValue(() => { | |
updateStatusBarStyle(shared, maxNo); | |
}, [shared]); | |
}; | |
export default useAnimatedStatusBar; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment