-
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
Apply scoped styles to child components #574
Conversation
👋 thanks for the PR. This change is going to be a breaking one as currently users don't expect to get a scoped class. I guess I will keep the PR open until we decide to cut a major release and evaluate if it makes sense to introduce this change |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's wait for the next major release
@bernhardc I create a minimal repo with your fork and |
@bernhardc @giuseppeg @pushkine I would like to reactivate this feature by doing opt-in using a new syntax. For example:
maybe It wouldn't break the current API, I think. What could stop this improvement? |
I merged @bernhardc commits on current it would be necessary to add the code to make it opt-in Please tell me if this is worth it or not ... |
@bySabi I think making this feature opt-in would be a good solution since the next major release doesn't seem to be in sight. Of course it also would be possible to toggle it on the level of the I also checked out your example repo. The reason why it didn't work is that the override of the |
6a5ff6d
to
52f3296
Compare
52f3296
to
8922c89
Compare
Thanks for answering after so much time has passed since this PR. Yes, And I also agree that the configuration option would be sufficient and much easier to implement. I really like
For this you can use This how is doned on: styled-jsx-repo: https://github.com/bySabi/styled-jsx-repo/blob/master/package.json#L15 I hope this PR can be merge soon. Thanks for the hard work |
We could give you the final boost and add the option, What do you think? |
If you want to do it be my guest. Otherwise I think I could implement it next week. @giuseppeg Would you be OK with this feature being opt-in via a compiler configuration toggle, therefore making this change non-breaking? |
I need this feature just right now. I will try to do my best, is a lot of work here, code, tests, docs, ... |
@bernhardc could be @giuseppeg we need a little of your time here please. |
Well, I have a working branch with the Let's see if I can add some test ... EDIT: added some test. I think it's done Lets put some Docs ... EDIT2: @bernhardc I need some help from you. I'm almost done, only the README is left. |
Hey folks! Sorry for the late reply – I have been busy at my day job :) Tim and I have some reservations on this feature because with this you'd potentially pass a prop to a component that doesn't wanted it or that can't accept it. For example if you have a declarative router component or if you have a component that accepts a function MyComponent() {
return (
<div>
<SomeOtherComponent />
<style jsx>{`
div { margin: 300px }
`}</style>
</div>
)
}
function SomeOtherComponent({ className }) {
return <div className={className}>hello</div>
} Here we are styling the The right way to style a component is to do it explicitly. To do this styled-jsx already provides a solution that is the import css from 'styled-jsx/css'
const { className, styles } = css.resolve`
div {
color: green
}
`
function MyComponent() {
return (
<div>
<SomeOtherComponent className={className} />
<style jsx>{`
div { margin: 300px }
`}</style>
<style jsx>{styles}</style>
</div>
)
}
function SomeOtherComponent({ className }) {
return <div className={className}>hello</div>
} Finally to mention what @bySabi wrote in the other PR:
This is indeed correct, we cannot scope child components from the parent. These are probably the only options you have at the moment https://github.com/zeit/styled-jsx#styling-third-parties--child-components-from-the-parent I am going to close these PR for now as I think that the downsides make implementing this feature not worth it. Thank you a lot for working on these PRs instead of just shouting at us in an issue and expecting us to implement this. I feel very sorry that you invested time and energies pursuing this option and in the end I am not merging it, again thank you for your time and contribution! Maybe in the future we can discuss feature requests in an issue. |
@giuseppeg Thanks for your reply! The issue you mentioned with styles for a I originally came across this project while searching for a solution to recreate the great developer experience of Vue single file components in React. So I had a look how Vue handles this. As it turns out, it's exactly the same: the styling for our
Since the vast majority of React components that accept a I understand your concerns, and if you're still cautious I'd suggest an explicit opt-in via the And regarding the issue that you'd pass a What do you think? |
Folks can do this with
Do you know how this is done? Do they use css like
If we support full css we cannot make this assumption.
Using deep to pass the className to the component but then don't provide any guarantee that it will be set on the elements of the child component (because that depends on their implementation) could be very confusing. Again I think that either passing it explicitly via |
I'm using this library to build a website currently and ran into this problem (thinking it was a bug). I've read through the comments and think the In a real world application you compose components a lot. It is tedious to write every single selector that needs to be passed down in a different The way it works currently makes me add a An extra attribute for passing scoped styles would work, but changing the default to apply styles to child components would make more sense in my opinion. |
@nicolasmn unfortunately like in ShadowDOM this kind of encapsulation makes it very hard to style nested components. In the web standards world they suggest to use CSS custom properties to tweak values from a parent. With
This is a very abstract statement, how would you pass scoped styles?
We don't apply styles but best case we can pass the scoped |
@giuseppeg thanks for the fast reply!
I meant an extra attribute on the
Of course it's not the actual styles that would get passed but the scoped
I understand that passing a scoped const HeaderMenuItem = ({ children }) => (
<>
<MenuItem className="HeaderMenuItem">
<span>{children}</span>
</MenuItem>
<style jsx>{`
/* This should be passed */
.HeaderMenuItem { … }
/* This should stay local */
span { … }
`}</style>
</>
)
/* MenuItem implementation for reference */
const MenuItem = ({ className, ...props }) => (
<>
<div className={[ 'MenuItem', className ].join(,)} { ...props } />
<style jsx>{`
.MenuItem { … }
`}</style>
</>
) |
@giuseppeg Isn't |
Implements #573.
I also cleaned up that one if-condition a bit.