-
Notifications
You must be signed in to change notification settings - Fork 76
Make example for component as selector #222
Comments
@kentcdodds any progress here? |
Here's an example of one thing you could do: const redDivClassName = 'my-own-unique-class-name-i-made-up'
const RedDiv = glamorous.div(redDivClassName, {color: 'red'})
RedDiv.className = redDivClassName
// elsewhere...
const ContainerDiv = glamorous.div({
[`& ${RedDiv.className}`]: {
color: 'blue',
}
})
<ContainerDiv><RedDiv>Hello</RedDiv></ContainerDiv>
// Renders <div><div>Hello</div></div>
// with "color: blue" applied to <div>Hello</div> Would you like to make a real example to add to the docs? |
@kentcdodds I don't see why this |
I suppose it could, but generated class names that are non-deterministic are generally not good for server-side rendering. And honestly, this isn't really a practice that we want to encourage. |
@kentcdodds I wonder how |
I'm not sure, but you can look at my attempt: https://github.com/paypal/glamorous/pull/183/files It worked great, but it added complexity to the codebase to support a pattern we don't want to encourage anyway so I backed out of it. You should hopefully not be doing this a whole lot... |
Well, I don't know how to do styling with props in my case styled.div({
[`&:not(:first-child) ${AuthorName}`]: {
display: 'none',
},
[`&:not(:last-child) ${Avatar}`]: {
visibility: 'hidden',
}
}) |
Sorry @goodmind. Could you make an example of what you're trying to do and share it here? Can base it on this: https://help.glamorous.rocks |
@kentcdodds https://codesandbox.io/s/j2P5JVwo4 It looks clumsy with this |
I see, and I agree that it does look a little clumsy. But not enough to justify complicating the API/implementation. I don't think that people will be doing this all that frequently... What do you suggest? |
In addition, there are other ways to do what you're doing there which do not require you to reference components in your selectors (like, just don't render the avatar multiple times in any group). This would actually be more performant and probably more readable as well... |
Our team is in the process of migrating our small-but-growing app from sass to glamorous (it's been awesome so far!) and we've come across an issue where we need to style a child component based on the hover of a parent component. It sounds a lot like what you guys are talking about here. This is the snippet of the original sass-implementation: .carousel-item {
z-index: 1;
position: relative;
padding: $grid;
&:hover {
.carousel-item__title {
color: $cerulean;
cursor: pointer;
}
.carousel-item__image-container {
&:after {
opacity: 1;
}
}
}
} const Item = g.div({
zIndex: 1,
position: 'relative',
padding: grid,
boxSizing: 'border-box',
// ... how do we modify Title and ImageContainer components?
`:hover ${ ? }`: { ... }
});
const Title = g.div({ ... });
const ImageContainer = g.div({ ... }); |
@stoikerty see the example below, const footerHeaderClassName = `app-footer-header`;
const FooterHeader = glamorous.div(footerHeaderClassName, { color: 'red' });
const Footer = glamorous.div({
height: '2rem',
display: 'flex',
alignItems: 'center',
backgroundColor: 'rgba(0,0,0,0.2)',
':hover': {
backgroundColor: 'rgba(0,0,0,0.8)',
// here space is necessary
[` .${footerHeaderClassName}`]: {
color: 'white',
},
},
}); in render <Footer>
<div>
<FooterHeader>Footer</FooterHeader>
</div>
</Footer> |
Thanks @istarkov but this solution is not ideal and not scalable as it now requires me to track down and know all places where classnames are used which is arguably one of the things that brought me to For modifiers via props I can just use the prop on both components where the modifier should have effect, however for things like |
Seems like it's the only solution now, so is it scalable or ideal doesn't matter ;-) |
Maybe for now it doesn't matter but for the future it matters, I believe there can be a better way to do this and it's important for people to express the need, especially if an attempt at solving this issue has already been made. Nothing is set in stone ;) |
"Not ideal" I agree with. As indicated in the attempt PR, it's a trade-off of choosing to not complicate the implementation in favor of enabling an infrequently and generally avoidable/undesirable pattern. "not scalable" confuses me a little bit. One thing that you could do to improve that example @istarkov kindly provided would be: const footerHeaderClassName = `app-footer-header`;
const FooterHeader = glamorous.div(footerHeaderClassName, { color: 'red' });
FooterHeader.className = footerHeaderClassName
const Footer = glamorous.div({
height: '2rem',
display: 'flex',
alignItems: 'center',
backgroundColor: 'rgba(0,0,0,0.2)',
':hover': {
backgroundColor: 'rgba(0,0,0,0.8)',
// here space is necessary, also I suggest using the & to make things more explicit
[`& .${FooterHeader.className}`]: {
color: 'white',
},
},
}); |
Thanks for further improving @istarkov's example. What I mean by it not being scalable is that the hashing that glamourous is leveraging internally is not being used in this case, but it would be useful. Off the top of my head, there are 2 scenarios that I can think of why this matters. Scenario 1 is a large application with about 10 people working on it where hoverable items are used extensively, both in old and newly commited code. In this case, understanding why your new component might have the wrong style is an unexpected issue. This can easily be fixed by using a different classname, but it's time and effort wasted on debugging and understanding the issue and fixing it once does not prevent future issues. Scenario 2 is two 3rd-party components clashing. Ok, sure you're going to say this is far-fetched but hear me out. I have an app that I'm building for a company and I need to integrate 2 components that I found on NPM, they're really good and there's hardly any alternatives that I can use. There is a catch though, they both use @kentcdodds I very much made this up but these things really happen. As my team is moving towards shareable components with NPM, we're discussing of using glamorous to power that. Therefore I'm naturally inclined to thing about more complex use-cases. |
Thanks for the scenarios @stoikerty. I appreciate the shareable components use case, and I'm glad that you're considering glamorous for that. I think that it'll serve you extremely well. Here's the way I feel about it:
What do you think? |
About 3 its frequent use case at my work. We are using css modules with support of themr library, and having a daily changing requirements we just cant extend components api forever. Solutions like current allows us to make changes without making any interventions in controls api. Imo this is really needed feature. |
BTW current solution is enougth for me even it does not solve all the edge cases, it solves mine |
Ok, it's pretty clear folks want this feature. If someone wants to create a PR on the main repo to add it I'm happy to review (no promises). It needs to be well tested and as simple as possible. Please use my original PR as a reference because there were a few decisions I made in that PR that I think are important. Thanks for the discussion everyone! |
Sounds good @kentcdodds. I would love to contribute, will take me a while to parse the old PR and figure out contributing etc. Wouldn't be surprised if someone gets it done before me but I'm up for it, love creating simple API's :) |
Ok, I'm more positive that folks want this so: paypal/glamorous#316 |
Problem Description:
Sometimes it's useful
Suggested Solution:
Document how you'd do that. See examples in paypal/glamorous#136 and paypal/glamorous#183 (also use cases in paypal/glamorous#202)
The text was updated successfully, but these errors were encountered: