Skip to content

Instantly share code, notes, and snippets.

@tpjnorton
Created October 23, 2020 09:22
Show Gist options
  • Save tpjnorton/a15db455677b5cf6806527467911a8ff to your computer and use it in GitHub Desktop.
Save tpjnorton/a15db455677b5cf6806527467911a8ff to your computer and use it in GitHub Desktop.
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