Created
October 23, 2020 09:22
-
-
Save tpjnorton/a15db455677b5cf6806527467911a8ff to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export const swapLightForDark = (appearance: string) => { | |
switch (appearance) { | |
case 'light': | |
return 'dark'; | |
case 'dark': | |
return 'light'; | |
default: | |
throw new Error('Unknown appearance'); | |
} | |
}; | |
export const useAppearanceAwareStatusBar = () => { | |
const appearance = useColorScheme(); | |
const preference = useSelector(getAppearancePreference); | |
useFocusEffect( | |
useCallback(() => { | |
switch (preference) { | |
case 'automatic': | |
StatusBar.setBarStyle( | |
appearance ? (`${swapLightForDark(appearance)}-content` as StatusBarStyle) : 'dark-content', | |
true, | |
); | |
break; | |
case 'light': | |
StatusBar.setBarStyle('dark-content', true); | |
break; | |
case 'dark': | |
StatusBar.setBarStyle('light-content', true); | |
break; | |
} | |
}, [appearance, preference]), | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment