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';
const Example = () => {
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>
</>;
};
By default, NativeBase provides contrasting colors based on its theme. You can also choose to get colors with better
with the help of
hook.
const ButtonTemplate = ({
shade
}) => {
const colorContrast = useContrastText(`yellow.${shade}`);
return <Button colorScheme="yellow" key={`yellow.${shade}`} bg={`yellow.${shade}`} _text={{
color: colorContrast
}} mb={1}>
Save Changes
</Button>;
};
const Example = () => {
let [,, toggleAccessibleColors] = useAccessibleColors();
const {
colors
} = useTheme();
return <>
{Object.keys(colors.yellow).map((key, index) => {
if (index > 2 && index < 9) return <ButtonTemplate shade={key} />;
})}
<Button mt="10" onPress={toggleAccessibleColors} colorScheme="primary">
Toggle Accessible Colors
</Button>
</>;
};
Accepts and returns a color defined in the theme.