-
Notifications
You must be signed in to change notification settings - Fork 50
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
Support themes #7
Comments
Glad it's of use :). The library behaves like a singleton, yes. If I reuse styles I tend to factor them out into own react-components. Is that what you meant? |
Thanks for the reply @fab1an ! Well, sort of. What I was visualizing is having separate palettes (one for each theme), then 'somehow' use each style to display the ui. |
So you are talking about colors only? |
Right. With platform.select you can account for differences between android/ios. Your library handles typographic/spacing scaling. I currently have two stylesheets in my app, the user can select between them. Each stylesheet has different palettes of colors, as well as ios/android specific code. Just trying to figure out how to use this library in this context. |
I can imagine a couple of ways of approaching this:
|
Right, that makes sense ! I'll play around with this a bit. Thanks @fab1an ! |
@fab1an, I tried a couple of things. For my use case, I had to change the library a bit, most notably because the singleton wasn't allowing me to have dynamic styling. What I do in my components is ...
const { theme } = this.props.theme
<View style={theme.background}>
</View> Somewhere else in the code, I make 'this.props.theme' point to another stylesheet when the user changes themes. The changes I made were: -- /* placeholder */
-- styles: Object.create(null),
++ /* placeholder */
++ // styles: Object.create(null), -- // _.assign(NativeTachyons.styles, StyleSheet.create(styleSheet));
++ let styles = Object.create(null);
++ _.assign(styles, StyleSheet.create(styleSheet));
++ return styles; -- export const styles = NativeTachyons.styles;
++ // export const styles = NativeTachyons.styles; This is breaking api, so I didn't create a PR I also make lighten/darken optional -- /* light and dark alternatives */
-- result[`light-${key}`] = Color(val).lighten(options.colors.lighten).hexString();
-- result[`dark-${key}`] = Color(val).darken(options.colors.darken).hexString();
++ /* light and dark alternatives */
++ if (options.colors.lighten !== 0) {
++ result[`light-${key}`] = Color(val).lighten(options.colors.lighten).hexString();
++ }
++ if (options.colors.darken !== 0) {
++ result[`dark-${key}`] = Color(val).darken(options.colors.darken).hexString();
++ } |
If you want to you can also just modify the Or you could specify colors like render() {
return (
<View cls={`bg-colorButton-${themeName}`}>
</View>
)
} Or assign it to the styles object: const themes = {
theme1: {
backgroundColor: "ff66ff",
…
},
theme2: {
},
}
render() {
return (
<View styles={[s.flx_i, s.h2, themes[themeName]]}>
</View>
)
} Or, in the last example just reference the default-styles again from your theme. The possibilities are endless ;) |
Thanks ! I'll be testing your suggestions. |
I gave it some thought and since I don't want to lose sync with your library, I'll implement something similar to your 2nd suggestion. I'll have a theme object composed of NativeTachyons.styles + a dynamic color object, which is generated with the same logic you used for the palette (it generates the color, background and border syntax) const theme = {
styles: NativeTachyons.styles,
colors: palette['colors1']
} Then I can switch palettes whenever needed |
I love this library, since this is a problem I'm having in my app, the MAGIC sizes :)
How would you approach having app-wide themes ?
I think it could be done by having the library return the Stylesheet.create(..), but currently it sets the styles property and this makes it behave like a singleton (as far as I can understand).
Any thoughts on this ?
The text was updated successfully, but these errors were encountered: