-
-
Notifications
You must be signed in to change notification settings - Fork 437
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
.attrs docs needs clarification #56
Comments
Can you show an example? How would that enable composition? |
@mxstbr actually... I'll retract myself. The compositions are already possible. I might be biased since I thought it worked one way and I sort of "got used to" it working that way. I was basically using // with imaginary props functionality
styled.props({
calculatedColor: ({ c1, c2, m }) => colorMath(c1, c2, m)
})`
background-color: ${props => props.calculatedColor};
box-shadow: 0 0 8rem ${props => props.calculatedColor};
`
// without it
const getCalculatedColor = memoize(
(props) => props.c1,
(props) => props.c2,
(props) => props.m
(c1, c2, m) => colorMath(c1, c2, m)
);
styled`
background-color: ${getCalculatedColor};
box-shadow: 0 0 8px ${getCalculatedColor};
` But... the one using props would probably benefit from memoization anyway so maybe it's better the way it is now. Please disregard my PS as I only think the docs needs some work to make |
@georgesboris they're definitely two different approaches to a very similar problem. So, just leave it here and don't edit it out :) It's nice to have some "btw mentions" in some issues. For the
We should then maybe add a note that says: "Since we don't filter these props, they can trigger warnings when they're added onto DOM element targets." This though is a general problem we're facing, not a documentation problem. And I'd like to add here to clarify that we'll probably solve it by waiting for React to (hopefully / eventually) implement prop namespaces :) |
@kitten if I use defaultProps vs attrs, which one would trump if both have the same key/value? Why would you use attrs over defaultProps? also, how do you use a prop for styling such as |
@lifeiscontent : I would also like to know the answer. I made a question: #266 |
I came into the same confusion after trying out Consider this example: const Div = styled.div.attrs({
color: 'red'
})`
color: ${props => props.color};
`;
Div.defaultProps = {
color: 'green'
};
...
<Div color="blue" /> So what happens here is (step by step):
So knowing the above I think we can deduce few hints:
So replacing // replaced defaultProps with attrs based on example above
const Div = styled.div.attrs(props => ({
color: props.color || 'green'
}))`
color: ${props => props.color};
`; doesn't make any sense. If my description is wrong please correct me @kitten |
That is correct @sarneeh. I'd love for you to rewrite that doc a bit explaining this more clearly! 🙏 Want to submit a PR? |
While indeed the props from I've decided to create a separate issue describing my concerns: styled-components/styled-components#2372 |
@mxstbr I'm sorry but I can't handle this right now as I'm completely out of time 😢 |
I'm regrouping some info from 2258. I still see a lot of confusion around |
https://www.styled-components.com/docs/basics#attaching-additional-props
The
attrs
function passes all attributes directly to the dom node.so it should only be used with "safe" values that won't trigger html incompatibilities.
In the docs the word "props" is being used.
And it implies that it should be used for organizing the data logic before the css expression.
The example actually uses them for something similar to "defaultProps".
If you use it like the docs tells you to you will suddenly generate tons of react warnings.
The docs should be clearer about this function intended behavior to prevent these confusions.
PS:
The behavior that the documentation implies is actually really helpful and the impossibility to use it like this creates the need for workarounds like creating wrapper-components. Would creating a
.props
functionality that behaves exactly likeattrs
but without the dom printing be something to consider? By creating wrapper components you're suddenly breaking the styled-components "flow" preventing further "extensions". With aprops
functionality tons of compositions would be possible.The text was updated successfully, but these errors were encountered: