NativeBase provides multiple tools to use the central theme defined in the app. The first tool is
, which you can use to access the values from the current theme.
function ColorPalete() {
const {
colors
} = useTheme();
return <SimpleGrid columns={5}>
{Object.keys(colors['emerald']).map(colorKey => <Box p={5} bg={`emerald.${colorKey}`} />)}
</SimpleGrid>;
}
function Example() {
return <NativeBaseProvider>
<Center flex={1} p={3}>
<ColorPalete />
</Center>
</NativeBaseProvider>;
}
You can also get specific values from the theme with
hook.
function Tokens() {
const [contrastThreshold, lightText] = useToken('colors', ['contrastThreshold', 'lightText']);
return <Center bg="emerald.400" flexDirection="row" p={4} rounded={4}>
Contrast threshold is:{' '}
<Text color={lightText} fontWeight="bold">
{contrastThreshold}
</Text>
</Center>;
}
function Example() {
return <NativeBaseProvider>
<Center flex={1}>
<Tokens />
</Center>
</NativeBaseProvider>;
}
If you are defining the background yourself and pass a contrasting color to the text, then you can use
.
function UseContrastingTextHook() {
const bgDark = 'emerald.700';
const bgLight = 'emerald.200';
const colorContrastDark = useContrastText(bgDark);
const colorContrastLight = useContrastText(bgLight);
return <Stack space={4}>
<Button bg={bgDark} _text={{
color: colorContrastDark
}}>
NativeBase
</Button>
<Button bg={bgLight} _text={{
color: colorContrastLight
}}>
NativeBase
</Button>
</Stack>;
}
function Example() {
return <NativeBaseProvider>
<Center flex={1}>
<UseContrastingTextHook />
</Center>
</NativeBaseProvider>;
}
If you want to define some conditionals based on current color mode or change the color mode, then you can try
.
function UseColorMode() {
const {
colorMode,
toggleColorMode
} = useColorMode();
return <Center flex={1} bg={colorMode === 'dark' ? 'black' : 'white'}>
<Text fontSize="lg" display="flex">
The active color mode is <Text bold>{colorMode}</Text>
</Text>
<Button onPress={toggleColorMode}>Toggle</Button>
</Center>;
}
function Example() {
return <NativeBaseProvider>
<UseColorMode />
</NativeBaseProvider>;
}
If you do not want to add conditionals for color mode everywhere and keep the code clean, then you can use
hook. It takes two parameters, light mode value as the first and dark mode value as second.
function UseColorMode() {
const {
colorMode,
toggleColorMode
} = useColorMode();
return <Center flex={1} bg={useColorModeValue('white', 'black')}>
<Text fontSize="lg" display="flex">
The active color mode is{' '}
<Text bold>{useColorModeValue('Light', 'Dark')}</Text>
</Text>
<Button onPress={toggleColorMode}>Toggle</Button>
</Center>;
}
function Example() {
return <NativeBaseProvider>
<UseColorMode />
</NativeBaseProvider>;
}