-
Notifications
You must be signed in to change notification settings - Fork 376
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
Interface/class-based selector #719
Comments
I'm not sure adding a class is either a very high barrier or actually even wonky in terms of 'current state of things'. For example, there is no way to select |
Well, until we had webcomponents it wasn't an issue: whether or not one element was a subclass of another was implementation-dependent and not exposed to web developers. Now it is: thanks to webcomponents we can create custom tags that can inherit from each other. Even the heading element is bounded: there are only so many of them and typing out As for adding a class or attribute not being that big of a barrier, the problem is that (I thought) part of the idea of webcomponents was that we want people to create extensible elements so that others can use them, allowing a good ol cambrian explosion of front-end innovation. If every component author under the sun needs to remember to add a class in the constructor and expose that as part of the API contract so that subclasses can inherit their styles a lot of them won't. An alternative solution is to apply styles directly to the element in the constructor but that has its own set of problems. The other option is to use shadow DOM and style |
@bkardell A tangential but still-related question: By “much discussion/proposals around what to do [about CSS selectors for I’m planning to eventually propose a The original post presumes that all major UA vendors will ever even reach consensus on implementing customized built-in elements (cf. #509, #648). Assuming that consensus does occur in favor of keeping them, then a CSS pseudo-class like |
@js-choi you're probably just not looking hard enough :) I know for sure it is on the wiki and I'm nearly certain that at some point(s) in time it was even in drafts (you'd have to look through them but, for example, there were things in 'css 3' initial drafts in 1998/99 that we still don't have - this is one reason jQuery supported more selectors than browsers)... and I recall @wycats and I both pushed to close that gap. Yes, one way that was discussed is ultimately reflected in @tabatkins' draft you linked above. I don't really think that there is significant division on custom built-ins - you can extend your own components and there is a whitelist in the spec of 'simple' non-interactive elements. In any case, in response to both this and @jasmith79's comments, my feelings are currently still expressed in my original reply: If you want to chase this, it seems like this is the wrong place. If this is a need, I feel like the same magic should apply to native elements that extend. Seems bad if custom elements invents/has its own special magic for that to me. Just my 2 cents. |
@bkardell Ah, the wiki, of course. Thanks, this helps a lot! Yes, this issue probably would belong more in w3c/csswg-drafts than here in w3c/webcomponents. And whatever approach should be as applicable to built-in |
whatwg/html#83 contains a proposal for a I'm not entirely sure how interface-based selectors would work though. I could imagine using whatwg/webidl#97, but that won't be as useful for custom elements, though I suppose we can make it work somehow. (Note that we cannot directly poke at JavaScript, it'll have to be some value(s) that cannot ever change that selectors can match against.) |
FWIW, I somewhat agree with @bkardell that it needs to be generic, but I think there are specific concerns here that only apply to custom elements. (To some extent they apply to normal elements too, e.g., if you do |
I guess there are two approaches to consider:
(1) is probably a non-starter because we can't define when to evaluate JS states. And observing changes to JS states is extremely expensive. |
Right, (1) seems pretty bad, I think we can reject that out of hand. For the specific use-case in this thread, the subclassing isn't mutable JS state at all, right? It's defined when you call |
Nah, subclassing can change at any time, using |
(At least for the case of the OP. For the case of customized built-in elements, there's an immutable relationship, but that's a bit separate.) |
Subclassing is totally mutable. In fact, you can have a |
Yeah I'm not a browser implementor but that certainly sounds expensive for a rather esoteric feature. Possibly having it tied to the inheritance hierarchy at the time |
I think if we defined [[Brand]] in IDL for platform objects that would presumably hold a set of brands. EventTarget -> Node -> Element -> HTMLElement. (Note that we need to do this anyway, see what I referenced earlier.) We could define a selector that operates on [[Brand]]. For custom elements we could maybe store something about the hierarchy when the HTMLElement constructor is invoked. Probably not in [[Brand]] but in [[CustomBrand]] or some such. We could then define the selector to operate on both. |
certainly, but that does not apply to the inherited lifecycle callbacks, which for many folks will be unexpected. if you are using this is just a side note, not that I agree with (1) in any shape or form. on the other hand, (2) seems realistic. |
In Tokyo we concluded there's not enough interest to pursue this further. |
#738 may cover many of the same use cases in a way that is reasonable to implement: you can define your base element to give itself (and any elements inheriting from it) a custom pseudo-class. |
Consider the following:
In the parlance of object inheritance, instances of the second class have an 'is-a' relationship with the first: a MyMoreSpecificElem is a MyElem.
This relationship is captured in JavaScript:
But there is no seamless way to capture this relationship using CSS selectors. All of the current CSS relational selectors (e.g.
thing1 > thing2
,thing1 ~ thing2
) deal with document position, not inheritance.Since the whole point of inheritance is re-use, that's kind of a bummer: if some UI library offers up a card-element and I want to make a my-fancy-card-element those styles should carry over.
Certainly one can add a CSS class or attribute (the latter being more in keeping with the best practices guidelines) in the constructor and select on that (call to
super
will take care of subclass instances), but that seems like a hacky workaround for a lack of something that should be built-in.The text was updated successfully, but these errors were encountered: