I want to set a default theme in Native Base, and have seen a lot of links to official customisation docs and the nativebase customiser. But they're all dead.
Native Base already provides a default theme with a bunch of colours. When you launch an app, by default it seems to have the cyan colour palette. All I want to do is switch from cyan to one of their other colour swatches, so that by default all the colours come from, say, amber.
I know there's the option to set default colours with extendTheme -- is that the only option? Extend the theme and manually add in all the hex codes I want? Or can I just set the theme to amber somewhere and all the colours will be applied?
I had the same issue i wanted to be able to create a primary theme colors and switch the theme without typing out all the colors so far this solution is working for me to select colors from the existing default theme.
const {colors} = extendTheme(colors);
const currentColor = colors.green;
const theme = extendTheme({
colors: {
// Add new color
primary: currentColor,
},
config: {
// Changing initialColorMode to 'dark
useSystemColorMode: false,
initialColorMode: 'dark',
},
});
export default function App() {
return (
<NativeBaseProvider theme={theme}>
<NavigationContainer>
<App />
</NavigationContainer>
</NativeBaseProvider>
);
}
Related
I want to change the colorScheme of a NativeBase Input component.
Other components, like button, slider or checkbox, have a prop "colorScheme" which, if set, changes all the associated colors of that component. Input does not have that prop.
These approaches would technically work:
Setting colors for border, background etc. manually
-> But it would be a lot or work and pretty ugly
Changing the primary color in the theme
-> But maybe I want to have two different color schemes for Input on the same page, so it would not make sense to change the theme.
So, what is the best way to easily change all the associated colors of an Input component, without setting all colors manually or changing the overall theme?
I've never used NativeBase ( but now I'll take a look, thanks for this discovery... )
I highly recommend you to have a read at the Customizing Components doc which provide a way to do what you're looking for...
You can create a "variant" which is a predefined theme for a specific component. You have to define it in your customized theme ( If you don't use a <NativeBaseProvider theme={theme} /> yet check this )
export default function () {
const theme = extendTheme({
components: {
Input: {
variants: {
otherColorInput: ({ colorScheme }) => {
return {
bg: /*otherColorToSet*/ ,
};
},
},
},
},
});
return (
<NativeBaseProvider theme={theme}>{/* components */}</NativeBaseProvider>
);
};
Then you can call you new Input like this :
<Input variant="otherColorInput">
Is there a way to access colours outside of components? It appears that any access to the styles is through hooks. I am using react-native-snackbar that accepts colours but runs through my middle ware so doesn't have any access to a component.
You can import theme.
import theme from './theme';
const { colors } = theme;
The theme object is created as -
export const theme = createTheme({ ... })
I need to add a custom font to my app. And all the text, including the button title, have to be displayed in the custom font.
How can I achieve this?
I have added custom font in my app. But it needs to provide font family style to every text and can't change the font family of the button title.
These answers seem like they didn't really read your question.. So here's how to use a font globally without the need of specifying it each and every time:
Create a component like so:
import * as React from 'react';
import { Text } from 'react-native';
export default (props) => {
const defaultStyle = { fontFamily: 'Muli', color: '#14181c' };
const incomingStyle = Array.isArray(props.style) ? props.style : [props.style];
return <Text {...props} style={[defaultStyle, ...incomingStyle]} />;
};
Now, everywhere you have
import { Text } from 'react-native',
replace with
import { Text } from 'src/global-components' :)
Usual way of doing this is:
download font families you want
add all the font files you want to an “assets/fonts” folder in the root of your react native project next
add the location of font file in your package.json "assets": ["./assets/fonts/"]
Run react-native link
And you can use it by name convention of your font files.
I'm new to React Native (0.59.3) and I'm trying to set default font for all Text components in my app. I've read https://stackoverflow.com/a/47925418/811405 and How to disable font scaling in React Native for IOS app?.
In my App.js I've put:
import { Text } from 'react-native';
Text.defaultProps.style = {
fontFamily: 'AmericanTypewriter' //just for testing
}
But I get Cannot set property 'style' of undefined.
When I add the line Text.defaultProps = Text.defaultProps || {}; before that, I don't get an error, but nothing happens (all Text components still use the default font). I've tried with different fonts (both built-in iOS fonts and my custom fonts that I've linked and verified), using their PostScript names, though nothing happens.
What am I doing wrong?
Add this before changing fontFamily style
import { Text } from 'react-native';
Text.defaultProps = Text.defaultProps || {};
If you want to have your own custom Text in your app, you usually create a custom Text component ... and either use it directly in your screens ... or use it internally in your other custom components ... this way you're going to have consistent look and feel throughout your app
Example
You'd use AppText in your entire app ... and forget about Text
const AppText = ({ text }) => (
<Text style={{ fontFamily: 'AmericanTypewriter', ...restOfYourStyles }}>
{text}
</Text>
);
If user has selected light-theme, and switches to dark-theme, then all scenes will immediately render to using the dark-theme.
I am using react-native-router-flux if this helps.
Thanks in advance,
Different approaches are possible. One of them is to use React context. Generally it should be used with caution but theming is one of the official examples where it is suitable.
Theming is a good example of when you might want an entire subtree to have access to some piece of information
So the example might look like
class App extends Component {
getChildContext() {
return {theme: { primaryColor: "purple" }};
}
...
}
App.childContextTypes = {
theme: React.PropTypes.object
};
where you set the context for rest of your application and then you use it in your components
class Button extends Component {
render() {
return <TouchableHighlight underlayColor={this.context.theme.primaryColor}>...
}
}
Button.contextTypes = {
theme: React.PropTypes.object
};
If you want to switch theme, you can set context based on your state/props that can be based on user selection.
Currently we are dealing with same questions and therefore we have starting prototyping library called react-native-themeable.
The idea is to use context to store theme and (re)implement RN components so they can replace original components but supports theming.
Example of theme switching you can find in https://github.com/instea/react-native-themeable/blob/master/examples/src/SwitchTheme.js
You can start by creating one JS file containing two objects that have the colors for each theme. Then just use one as a default and if the user picks another theme save it in the database or using local storage and pull the relevant object.
export const theme1 = {
blue: '#fddd',
red: '#ddddd',
buttonColor: '#fff'
}
export const theme2 = {
blue: '#fddd',
red: '#ddddd'
buttonColor: '#fff'
}
Then just import the relevant file:
import * as themes from 'Themes'
const currentTheme = this.props.currentTheme ? this.props.currentTheme : 'theme1'
const styles = StyleSheet.create({
button: {
color: themes[currentTheme].buttonColor
},
})
Wouldn’t it be great if you could import a colors object directly, and modify the object every time you change the theme and have those new colors propagate to the entire app? Then your components wouldn’t need to have any special theme logic. I was able to achieve this using a combination of AsyncStorage, forced app restart, and async module loading.