useContrastText is a custom hook used to get a contrasting color (either lightText or darkText) to the color passed as a parameter.
import { useContrastText } from 'native-base';
function UseContrastingTextHook() {
const bgDark = 'gray.900';
const bgLight = 'gray.50';
const colorContrastDark = useContrastText(bgDark);
const colorContrastLight = useContrastText(bgLight);
return <>
<Button bg={bgDark} _text={{
color: colorContrastDark
}} my={6}>
NativeBase
</Button>
<Button bg={bgLight} _text={{
color: colorContrastLight
}}>
NativeBase
</Button>
</>;
}
function Example() {
return <NativeBaseProvider>
<Center flex={1}>
<UseContrastingTextHook />
</Center>
</NativeBaseProvider>;
}
By default, NativeBase provides contrasting colors based on its theme. You can also choose to get colors with better
with the help of
hook.
function UseContrastingTextHook() {
let [,, toggleAccessibleColors] = useAccessibleColors();
const {
colors
} = useTheme();
return <>
{Object.keys(colors.teal).map(key => {
const colorContrast = useContrastText(`teal.${key}`);
return <Button key={`teal.${key}`} bg={`teal.${key}`} _text={{
color: colorContrast
}} mb={1}>
NativeBase
</Button>;
})}
<Button mt={2} onPress={toggleAccessibleColors} bg={'indigo.600'}>
Toggle Accessible Colors
</Button>
</>;
}
function Example() {
return <NativeBaseProvider>
<Center flex={1}>
<UseContrastingTextHook />
</Center>
</NativeBaseProvider>;
}
Accepts and returns a color defined in the theme.