Draft
Conversation
Member
Author
|
Just thought of something... If we want to use props in styles, why not use a similar, but a bit different API? E.g. const MyComponent: React.FC<MyComponentProps> = ({ primary, disabled, onClick }) => {
const styles = useStyles({ primary, disabled })
// or
const styles = useStyles(props)
}
// When defining styles
const useStyles = createStylesHook<MyComponent>(
['primary', 'disabled'], // Props on which styles are dependent
(
{ colors, radius, elevation } // Theme,
{ primary, disabled } // Dependent prop values
) => ({
// Style definitions
button: {
backgroundColor: primary ? colors.fill.primary.default : colors.fill.accent.default
}
// ...
})
)Pros:
Cons:
|
Member
Author
|
See Styled Components API |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improve Styling API to move styles outside of component definition.
Styles are wrapped in a generator function called
createStyles. This function is then passed touseStylesBuilderwhich uses the active theme to build the styles. This is in fact the exact same API as before, but enforces the "mentality" of not using Component's props in the styles definition.This way, style object stays the same for the lifetime of the component (as long as theme doesn't change). However, this makes style objects larger than if using props directly in style definition. Memory usage is larger but overally performance and CPU usage should be lower. If using props, we would need to invalidate style objects on each prop change, which could be performance intensive on certain components.
A great thing would be to use React Native
StyleSheetAPI, as this optimizes performance by sending each style to the native layer only once, but since theme is dynamic, I don't think this is really possible with current React Native API.