-
-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Questions about theming #22
Comments
I have same use case.
I have spent days trying to find a solution. Theme support feels like it should be built into react native core. Any suggestions please? |
@brunolemos does it produce some errors or just not working silently? |
Calling a build() anytime during runtime, throws no error and still uses the old theme that was used by build() on app start. |
@AnuraagBasu may I ask you to make a simple full example for me to get in context? |
@vitalets This is what I'm trying to achieve in my index file. import React, {Component} from 'react';
import {
Text,
View,
TouchableOpacity,
AppRegistry
} from 'react-native';
import EStyleSheet from 'react-native-extended-stylesheet';
class TestApp extends Component {
constructor (props) {
super(props);
this.state = {
selectedTheme: 'classic'
};
EStyleSheet.build(Themes.classic);
}
render () {
return (
<View style={[Styles.bodyBackground, {flex: 1, justifyContent: 'center', alignItems: 'center'}]}>
<TouchableOpacity activeOpacity={0.8}
onPress={this._changeTheme.bind(this)}
style={{
justifyContent: 'center',
alignItems: 'center',
width: 200,
height: 50,
padding: 20,
backgroundColor: '#efe'
}}>
<Text style={{color: '#000'}}>Change Background</Text>
</TouchableOpacity>
</View>
);
}
_changeTheme () {
var changeThemeTo = 'classic';
if (this.state.selectedTheme == 'classic') {
changeThemeTo = 'winter';
}
EStyleSheet.build(Themes[changeThemeTo]);
this.setState({
selectedTheme: changeThemeTo
});
}
}
const Themes = {
classic: {
color1: '#301631'
},
winter: {
color1: '#145E8B'
}
};
const Styles = EStyleSheet.create({
bodyBackground: {
backgroundColor: '$color1'
}
});
AppRegistry.registerComponent('TestApp', () => TestApp);` I'm expecting the background color of the View to change on click of the TochableOpacity . |
@vitalets I'm still trying to figure out how to make this work. Any thoughts on this? |
@AnuraagBasu sorry for delay.. It does not work now because You can try to remove that behavior and play with it. But the main problem here that I see is how to re-render all react components after theme change? In your example with single component you just call |
I have dynamic themes working using redux. I am a JS newbie and redux is magic to me and I do not understand the internals of how it works. This is not a recommendation. It is only what I have been able to get to work for myself. Suggestions are welcome.
File: dark.js
File: light.js
I pass themeStyle around the app as a prop. File:MyScene.js
This "magic code" mapStateToProps results in dynamic theme changes triggering render of all tab scenes, menus, etc. File: ViewProfileParent.js
-Ed |
@esutton yes, if you store styles in component state - it will re-render on style change. It's good idea! To pre-build themes you should not pass theme variables to // light.js
export default {
colorPrimary: 'white'
}
// dark.js
export default {
colorPrimary: 'black'
}
// app.js
import themeLight from './light.js';
import themeDark from './dark.js';
const srcStyles = {
text: {
color: '$colorPrimary'
},
}
const themeStyles = {
light: EStyleSheet.create(Object.assign({}, themeLight, srcStyles)),
dark: EStyleSheet.create(Object.assign({}, themeDark, srcStyles)),
}
function getThemeStyleSheet(currentTheme) {
return themeStyles[currentTheme];
} I think this could be implemented in EStyleSheet.addTheme('light', variables);
EStyleSheet.addTheme('dark', variables);
const styles = EStyleSheet.create({
text: {
color: '$colorPrimary'
},
}, {
themes: ['light', 'dark']
}); |
@vitalets Thanks for guiding me in the right direction. It does work for me now. |
@AnuraagBasu ok, thanks for the feedback! |
@vitalets |
@arthur-tse7 could you show a bit more code? |
@vitalets I've abandoned dynamic theming for now, but when I get back to it, I'll reply again ! |
I have the same problem as @arthur-tse7 's. The object created by app.js
dark.js
light.js
The console returns an empty {}. Dependencies: |
Finally added working examples with static and dynamic themes: |
Is this code still valid? I need a static theme scheme |
For example, the app loaded
EStyleSheet.build(DarkTheme)
in theindex.js
. How to change to Light Theme? Callingbuild
again does not seem to work.The text was updated successfully, but these errors were encountered: