When you use the NativebaseProvider at the root of your app, you can automatically use color mode in your apps.
By default, most components are dark mode compatible. To handle color mode manually in your application, use the useColorMode or useColorModeValue hooks.
useColorMode is a React hook that gives you access to the current color mode, and a function to toggle the color mode.
Calling toggleColorMode anywhere in your app tree toggles the color mode.
useColorModeValue is a React hook used to change any value or style based on the color mode. It takes 2 arguments: the value in light mode, and the value in dark mode.
function UseColorMode() {
const {
toggleColorMode
} = useColorMode();
const text = useColorModeValue("Light", "Dark");
const bg = useColorModeValue("warmGray.50", "coolGray.800");
return <Center>
<Box p="4" flex="1" bg={bg} maxW="300" w="100%" mt={10} safeArea>
<Text fontSize="lg" display="flex" mb={20}>
The active color mode is{" "}
<Text bold fontSize="18px">
{text}
</Text>
</Text>
<Button onPress={toggleColorMode} h={10}>
Toggle
</Button>
</Box>
</Center>;
}
const Example = () => {
return <NativeBaseProvider>
<UseColorMode />
</NativeBaseProvider>;
};
_light and _dark Pseudo props
All components accepts _light and _dark props which applies the passed props on dark and light mode.
function PseudoPropsUsage() {
const {
colorMode,
toggleColorMode
} = useColorMode();
return <Center flex={1}>
<Box p={4} maxW="300" _dark={{
bg: "coolGray.800"
}} _light={{
bg: "warmGray.50"
}}>
<Text fontSize="lg" display="flex" mb="20">
The active color mode is{' '}
<Text bold fontSize="lg">
{colorMode}
</Text>
</Text>
<Button onPress={toggleColorMode}>Toggle</Button>
</Box>
</Center>;
}
const Example = () => {
return <NativeBaseProvider>
<PseudoPropsUsage />
</NativeBaseProvider>;
};
You can set default color mode. By default, the color mode will be light. To support this, extend the default theme with a config
import { NativeBaseProvider, extendTheme, Text } from 'native-base';
const config = {
useSystemColorMode: false,
initialColorMode: 'dark',
};
const customTheme = extendTheme({ config });
function App() {
return (
<NativeBaseProvider theme={customTheme}>
</NativeBaseProvider>
);
}
Persisting the color mode
You can persist the color mode in you app by defining you color mode manager of type StorageManager and passing it to the NativeBaseProvider. This will retain the color mode even when the app is refreshed.
import React from 'react';
import { NativeBaseProvider, StorageManager, ColorMode } from 'native-base';
import AsyncStorage from '@react-native-async-storage/async-storage';
const colorModeManager: StorageManager = {
get: async () => {
try {
let val = await AsyncStorage.getItem('@color-mode');
return val === 'dark' ? 'dark' : 'light';
} catch (e) {
return 'light';
}
},
set: async (value: ColorMode) => {
try {
await AsyncStorage.setItem('@color-mode', value);
} catch (e) {
console.log(e);
}
},
};
export default function () {
return (
<NativeBaseProvider colorModeManager={colorModeManager}>
</NativeBaseProvider>
);
}
import React from 'react';
import { ColorMode, NativeBaseProvider, StorageManager } from 'native-base';
const colorModeManager: StorageManager = {
get: async () => {
let val = localStorage.getItem('@color-mode');
return val === 'dark' ? 'dark' : 'light';
},
set: async (value: ColorMode) => {
let strValue = value ? value.toString() : '';
localStorage.setItem('@color-mode', strValue);
},
};
export default function () {
return (
<NativeBaseProvider colorModeManager={colorModeManager}>
</NativeBaseProvider>
);
}