-
Notifications
You must be signed in to change notification settings - Fork 262
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
Scoped styles for child components #573
Comments
Quoting myself:
|
🎉 Any idea when this is getting released ? or is there a way to use this with nextjs without editing package-lock.json ( as it is a nested dependency ) ?
Could make it opt-in ? |
@giuseppeg Can you consider hiding this functionality behind a feature flag? That way one can use this package like one would normally use css. The issue addressed here is compounded by not having anything but element selectors apply when Further compounded if you have this case;
CSS stands for "Cascading Style Sheets" and should be defaulting to that behaviour. @bernhardc How do I use your pacakge instead of zeit's? |
I think my point can be summarised as:
|
I'm with you @haf but ...
With the @bernhardc PR, and my PR, <MyComponent className="jsx-inyected_className">
<AChildComponent className="jsx-inyected_className">
...
const AChildComponent = ({ className }) => (
<div className={className}>
<p>Testing child <span className="other-color">Component</span></p>
</div>
) But this is not really useful cause even with this PR you will never reach You can test by your self cloning my initial proposal repo that include this PR: https://github.com/bySabi/styled-jsx-repo |
@bySabi I see, I thought I'd be able to reach
Almost everything inside this menu should inherit my CSS from the parent, and since there's such an abundance of things inside children, I'd have to create A lot of the styles also need to know whether the menu is expanded, which is done by having a selector matching the parent's I don't fully understand why I should need React component encapsulation to the extent you're suggesting. As long as I use specificity as it's designed, its effect should be that if I qualify a child component's selector more from the parent, it applies, otherwise it doesn't. And then since I'm using styled components, at least I don't have to think about e.g. the styles of an I mean; the alternative is just to go back to "vanilla SCSS". Even just having a unique number generated at my point of choosing, and having that (class name) number prefixed before every selector that gets loaded by my overly competent menu would be enough for me. Don't other people do apps/web sites like me? 🤷♂️ |
No, you can't unless you use
A well designed tool cannot break the encapsulation of the component because it cannot be assumed how it will be rendered. A React component is not an HTML snippet but a javascript code.
This is still valid but applies when the application has been rendered in the browser.
I don't know for sure how this is implemented with the styled components but at the end of the day it can only be implemented by injecting inline styles or with generated unique classNames. React is basically Javascript that inserts Nodes elements in the DOM using the browser API but does not know anything about CSS, except inline styles. So it doesn't handle concepts like cascade, specificity or inheritance. This is a browser thing. At the end of the day you will have to use a strategy to stylize the components in insolation with already existing JS tools (styled, styled-jsx, ... ) or with utility classes CSS approach like TailwindCSS or use global CSS files using vanilla CSS or preferably with In my personal opinion it is better to use global CSS together with a methodology or architecture that helps you avoid the common problems inherent to CSS. In my case I use C3CSS which in turn is based on Enduring CSS and BEM With a good layout system like: https://every-layout.dev/ |
From what I can gather this is a lib that traverses the AST at compile-time and matches selectors to elements/discovers styles. Since the AST contains logic that cannot be evaluated at compile-time, classes cannot be assigned directly in all cases. So all selectors are hoised into a stylesheet-registry and then applied at render-time, logically every time render happens. Rendered react components are a tree, so that tree can be traversed in order to cascade selectors. |
But if you skip the limits of the component where is the limit of the parser? |
Well, I don't think there should be a depth limit; it should work like in the browser's cascades. Hopefully you don't have react component trees thousands of items deep. |
Well you already have the ingredients, need and dissatisfaction, for the next big thing on CSS/React tooling |
I always thought It'd be to cool to define "from this component and downwards, inherit." I tried to use css.resolve, too much hassle. I decided to use |
Has there been any motion on this? I'm considering using styled-jsx for my next project, but this is a real dealbreaker. A child component shouldn't handle its own positioning. That's definitely for the parent to handle. What's the justification for disallowing className support on children? There's no danger of mis-application as far as I can see...? |
I'm not currently using the library, so I stopped pushing this issue forward. |
@giuseppeg this feature is important from a DX standpoint. Too verbose to create a new jsx-??? with |
Agreed, I would love to have this feature in Even if it's a "breaking change", why are we not doing one ? Maybe it's time to release a new major version. |
Any new ways about handling this problem in 2021? |
Just noticed this issue while giving this library a chance over CSS Modules and SC. That's really something that we need. |
There're few discussion here that enabling this feature by default is bit unideal, unless we can have other elegent solution to solve these problems mentioned there: |
+1 for this feature, I reverted to css module for those cases. |
I really love working with this library, the only thing that bugs me is that currently there is no simple way to pass scoped styles to child components:
This issue had already been discussed several times (#273, #197, #121). So I thought the current behavior (with the
:global()
workaround) was due a technical limitation. However, after digging into the code a bit, I discovered that it's actually quite easy to apply the jsx-* scoped class name to child React components as well.I already created a PR for this. My change adds the jsx-* class to all child React components where a custom className is passed. In the example above, that apply to the "Submit" button,
but not the "Cancel" button. This leads to the following code being generated:
As you can see, the button with our custom class "login-button" gets the jsx-* class, the other one doesn't. Works like a charm, the scoped style for .login-button is applied correctly. It also doesn't mess with the existing classes of our child components ("button button--secondary" in this example).
So are there any reasons I'm currently missing why this bevahior isn't possible or advisable? I'd really like this feature to be included.
The text was updated successfully, but these errors were encountered: