Replies: 13 comments 6 replies
-
What happens when multiple parent nodes use the same id ? |
Beta Was this translation helpful? Give feedback.
-
Would it be possible to support an API like: const id = stylex.id()
...
[`${id}:hover &`]: {
// ...
} I would think there could be a couple of valid string interpolations the user could do, and I think these are possible to have typed in TypeScript because you can make expressions like That being said, I can respect that you are trying to limit the API surface area as much as possible by relying on explicit, imported function calls rather than magical strings. One other thing is I'd prefer if I don't need to create n+1 files everytime I want to do this kind of thing (+1 file for Right now when I need to implement this kind of pattern, I use the CSS variable hack as alluded to in other threads. The one benefit this has over the ID approach is that I don't need to create a There is a real need for this kind of pattern overall and relying on JavaScript and event listeners is unwieldy and CSS variables feels like a work around. |
Beta Was this translation helpful? Give feedback.
-
If two anscestor have the same id, then if either one matches the given pseudo-class, then the style would be applied. In practice, this is usually not a problem:
Most other pseudo-classes don't even make sense as something to observed IMO. The use-case for multiple IDs is mostly specific to peers or descendents (using @zaydek I considered an API with strings instead of function calls. There are a few reasons I don't prefer this.
We might loosen the requirement for a However, whether we loosen the requirement for
Officially, you need a |
Beta Was this translation helpful? Give feedback.
-
@nmn Got it, thanks. Other quick questions -- is Besides that I'm on board with this. I find |
Beta Was this translation helpful? Give feedback.
-
Too much freedom will lead to additional learning costs. And i noticed @zaydek mentioned whether when is composable. I think this is something that needs to be made clear. Repeating definitions creates extra work. Using |
Beta Was this translation helpful? Give feedback.
-
Examples are provided for the ancestor (descendant combinator), e.g. Is this just for brevity of the examples or were you thinking these selectors conflicted with the constraints somehow? |
Beta Was this translation helpful? Give feedback.
-
How about something like this? It prevents styling at a distance (you can still only target elements that you can set a classname on), offers the flexibility for additional selectors without expanding the API, avoids template string styles (apart from the import {create, props, combinator, child, descendant, nextSibling, subsequentSibling} from '@stylexjs/stylex';
import {myId} from './target.stylex';
const styles = create({
button: {
color: {
default: 'black',
':hover': 'red',
[combinator(myId, ':hover', child, '$')]: 'blue',
// .a-group:hover > .xjygtsyrt
[combinator(myId, ':hover', descendant, '$')]: 'blue',
// .a-group:hover .xjygtsyrt
[combinator(myId, ':hover', nextSibling, '$')]: 'blue',
// .a-group:hover + .xjygtsyrt
[combinator(myId, ':hover', subsequentSibling, '$')]: 'blue',
// .a-group:hover ~ .xjygtsyrt
},
}
});
<Card {...props(styles.card, ..., myId)}>
<button {...props(styles.button} />
</Card> |
Beta Was this translation helpful? Give feedback.
-
@zaydek It's not intended to be.
Thanks for this feedback. Having stricter errors is definitely on our roadmap.
@nonzzz I hope your concerns/questions have been answered above as well.
@keeganstreet This is an intentional part of the design. It all flows from the core principles:
You explicitly set an ID on elements you want to observe, and then you observe them positionally in the general way possible. Using the more general combinators is enough achieve all use-cases with fewer unique CSS rules.
We don't want the additional flexibility. We want the most constraints while fulfilling use-cases. Please present me use-cases where you need the additional complex selectors. |
Beta Was this translation helpful? Give feedback.
-
Is there some progress? Really hope to see it in some form soon. Thx. |
Beta Was this translation helpful? Give feedback.
-
@predaytor Sorry for the late reply. I was on a vacation. This work is currently on the back-burner as there isn't an obvious good API for this feature yet. We are continuing to iterate on the right design for now. |
Beta Was this translation helpful? Give feedback.
-
Moving to Discussions |
Beta Was this translation helpful? Give feedback.
-
When creating a design system I found descendant selectors to helpful in a few places. One was an InputGroup that was a div that changed the way buttons and inputs rendered. It would take rounded corners of all but the start of the left of the first item and right of the last item. The other place was :hover and :active effects on compound elements sometimes needed maintain contrast of an inner element when the background of an outer changed. I have to say it was rare that I needed to do this. The button case could be done context in React (but I prefer CSS). The hover and active should not be done JS. So it is important for me to be able to do when I need to, but I don't think the API to do it needs to be convenient as it is not something I use much at all. In a library with about 100 components we probably had three or four that needed this. I don't mind if the solution to this feels like and escape hatch. |
Beta Was this translation helpful? Give feedback.
-
My current use case is to style an uncontrolled Radix UI component. It has a data attribute on the root component, and I need to style the child elements based on its parent's data attribute ( |
Beta Was this translation helpful? Give feedback.
-
Describe the feature request
Disclaimer
This proposal is not "ready" or "approved". This proposal is being shared publicly to help consolidate all discussions about adding such a feature.
Motivation
StyleX has a few constraints in service of making things work consistently and generating styles that are performant and scalable. StyleX has disallowed descendent and sibling selectors to maintain it's core principles such as:
However, the alternative to such selectors is to use Javascript which may often not be desirable and cause other trade-offs.
Constraints
Any design we adopt for descendent selectors must not compromise on StyleX's core principles.
We must also ensure that such a feature won't significantly affect the performance or size of the generated CSS.
Core Concept
When it comes to avoiding styling at a distance,
.csuifyiu:hover > div
is unsafe, butdiv:hover > .ksghfhjsfg
is safe.Inspiration
group
andpeer
from Tailwind.Proposal
This proposal would add at least two new functions to the StyleX API surface. Although the names could change, for the sake of this explainer, assume they're
stylex.id
andstylex.when
.The core concept is that
stylex.when
can be used toe read the state of an element targeted withstylex.id
and apply styles conditionally.stylex.when
would be allowed to be used where pseudo classes are allowed today.stylex.id
would have the same constraints asstylex.defineVars
.Additionally,
stylex.id.default
should be available as a "default" id that can be used instead of creating ids manually.Concerns
CSS Bloat
Adding this capability can lead to CSS becoming bloated.
One way to minimise bloat would be disallow creating custom
id
s and enforce that the same id is used for all use-cases. This would cause the same constraints asgroup
andpeer
in Tailwind where there can only be a single "layer" of combinator styles at a time. This should be sufficient in the vast majority of use-cases and we can suggest using JS for the edge-casesAPI Bloat
This API makes StyleX harder to learn and increases the API surface area.
Naming Bikeshed
The naming of the APIs is up for debate.
Feedback
How important is this feature?
Is the ability to create multiple custom IDs for targeting in styles important?
Beta Was this translation helpful? Give feedback.
All reactions