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
Discuss alternate way to apply props to css attribute using bindProp #532
Comments
I did think about something like this—you could make a proxy that records property access. It came down to an issue a single issue, which I think yours will still suffer from. Just for example, we'll say you don't pass down any styling props. Now say you want to allow computed properties (which seems to be a common use case). This might look like this, const FineSoFar = styled.div`
color: ${bindProp('active', active => active ? 'red' : 'blue')};
` But what if your conditional now references another prop, const Broken = styled.div`
color: ${bindProp('active', active => active ? bindProp('primary') : bindProp('secondary')};
` Until I.e. if you do |
@jacobp100 You make an excellent point. How about this instead const Button = styled.div`
${bindProps("active", "primary", "secondary")}
color: ${(props) => props.active ? props.primary : props.secondary};
` One declaration at the top. It would simply populate a list upon interpolation and substitute its value as an empty string or be skipped. It requires no custom css addons. You could even take this further and make declaring style props more cohesive. const Button = styled.div`
${stylePropTypes({
active: React.PropTypes.bool,
primary: React.PropTypes.string,
secondary: React.PropTypes.string})}
${defaultStyleProps({
active: false,
primary: "blue",
secondary: "gray"})}
color: ${(props) => props.active ? props.primary : props.secondary};
` Declaring a class as a string template essentially. You could drop the "stylePropTypes" call and simply detect if the object is describing the types of props or setting the props by doing instance checks on the values inside. const Button = styled.div`
${{
active: React.PropTypes.bool,
primary: React.PropTypes.string,
secondary: React.PropTypes.string}}
${{
active: false,
primary: "blue",
secondary: "gray"}}
color: ${(props) => props.active ? props.primary : props.secondary};
` Merge the two objects keys together and you'll have a list of properties strictly for style. |
I'll copy this over into the other thread! |
Cool thank you. |
This solves the issue of unwanted style properties being passed to components. It could also slightly improve performance by reducing function calls during property updates. Just an idea I thought I would share.
The text was updated successfully, but these errors were encountered: