Theme customisation is at the heart of NativeBase. Using NativeBase's extendTheme function, we can customise components.
Components can be customised by overriding baseStyle/defaultProps or adding a new variant.
Let's customise a Button component to include rounded borders and red colorScheme.
import React from 'react';
import { NativeBaseProvider, extendTheme } from 'native-base';
export default function () {
const theme = extendTheme({
components: {
Button: {
baseStyle: {
rounded: 'md',
},
defaultProps: {
colorScheme: 'red',
},
},
Heading: {
baseStyle: ({ colorMode }) => {
return {
color: colorMode === 'dark' ? 'red.300' : 'blue.300',
fontWeight: 'normal',
};
},
},
},
});
return (
<NativeBaseProvider theme={theme}>{}</NativeBaseProvider>
);
}
As shown above, we can customize components by passing the components object with the key being the name of the component. Whereas you set defaultProps or baseStyle to customize the components.
Difference between baseStyle and defaultProps
Base Style
As the name suggests, it's used to define the base style of a component.
Base style can be a function or a plain object. Use function if you want to get access to defaultProps, current colorMode (light/dark) and theme object.
Default Props
Default props can be used to initialize props of a component.
For e.g. You have a Button component and it has 2 variants. i.e. outline, solid.
<Button variant="ghost"/>
<Button variant="outline"/>
<Button />
When variant in defaultProps is solid the above button will use solid variant.
You can specify the base style of the component and use it across project.
function Example() {
const theme = extendTheme({
components: {
Text: {
baseStyle: {
color: 'emerald.400'
},
defaultProps: {
size: 'lg'
},
sizes: {
xl: {
fontSize: '64px'
},
lg: {
fontSize: '32px'
},
md: {
fontSize: '16px'
},
sm: {
fontSize: '12px'
}
}
}
}
});
return <NativeBaseProvider theme={theme}>
<Center flex={1}>
<Text>NativeBase</Text>
</Center>
</NativeBaseProvider>;
}
You can also add the variants to the components and use it across project.
function Example() {
const theme = extendTheme({
components: {
Button: {
variants: {
rounded: ({
colorScheme
}: any) => {
return {
bg: `${colorScheme}.500`,
rounded: 'full'
};
}
}
}
}
});
return <NativeBaseProvider theme={theme}>
<Center flex={1}>
<VStack space={2}>
<Button colorScheme="emerald">Default Button</Button>
<Button colorScheme="emerald" variant="rounded">
Rounded Button
</Button>
</VStack>
</Center>
</NativeBaseProvider>;
}
NativeBase ships with default styles for each components.