-
Notifications
You must be signed in to change notification settings - Fork 375
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
observedStyles #856
Comments
There is no such plan. Observing changes in the computed / used style is very expensive because browser engines don’t eagerly update the style. I don’t think we can add a feature like this unless there are very compelling use cases. |
Apologies for this copy and paste, but I'm on my phone and this would be hard to rewrite. This is a comment of mine from another discussion on style observers, where there was a concern about their performance:
|
That's not really a thing. It's true that some builtin elements have special style magic that responds to style changes but that's more of a missing CSS features. Anyway, what we need is a list of concrete use cases. There is no way we're gonna add a serious foot gun like this unless there are extremely compelling reasons to do so. |
Not sure what you mean by "extremely compelling reason", in what other way can we design custom elements that responds to style changes? That by itself should be reason enough. |
We’re doing this currently in some places within a RAF loop. This has taken two forms:
As far as I’m aware, the former case doesn’t trigger paint or layout. The latter may: a workaround for it is to use an existing For a few property reads like this, this is pretty harmless, but I don’t think it would scale acceptably for cases where numerous elements respond to custom properties. While the usages we have had for these strategies to date have happened to track closely to element definitions, conceptually the properties don’t ‘belong’ to the element, and indeed there are cases where more than one element honors the same custom-property-whose-realized-effect-cannot-occur-in-css-alone. So I don’t think this belongs to the custom elements API, but rather to CSS APIs. The issue is closely related to In other words I think this is a real need for authoring custom elements, especially UI widgets, that have coherent and consistent APIs that separate styling concerns from semantic concerns, but I don’t think it really belongs within the custom elements API. |
That’s not a use case. That presumes the need to define the orientation of a component in CSS. A use case would depict a concrete scenario like building a calendar widget which poses a problem, not a need for a particular solution. For example, a concrete use case for a mode of transportation goes like: I’d like to live near a coast line but want to work in downtown, which is 20km away, and I’d like my commute to be less than 30 minutes. A particular solution to this problem might be laying out more public transportation, or have the person a buy a motorized vehicle; but the use case didn’t define or allude to any particular solution.
MutationObserver observes content attribute change to a DOM element, not its computed or used style; the letter is way more expensive to compute because we’d have to match & apply all rules that apply to the given element and its ancestors for inheritance. There is no way we can do this sync. Remembering what the obvious style of an element was is also quite expensive. We very carefully avoid having that kind of infrastructure in WebKit right now. |
Agreed that whatever form a hook for this might take, it must not be sync. Realizing a property’s effect only matters come time to draw a new frame. |
Let's try to make it easier than, you don't need the old value of the style, a custom element should already know its own state (vertical, horizontal), can this be applied only to custom css properties, can a callback be fired whenever any style changes without telling me what has changed, we just need to know that something happened then we can check the computedStyle on whatever property we care about, something like that. |
@rniwa makes a good point, anyone not already familiar with how we go about adding new features please read https://whatwg.org/faq#adding-new-features. |
It may surprise you but none of that makes this feature substantially easier to implement (at least in WebKit). But again, the difficulty of implementation is secondary. The primary thing we need is a list of concrete use cases. Nothing is gonna happen until we have that. |
I can provide some examples from our popup generic (used within dropdown menu, select, etc), whose layout logic occurs partly in ES. It supports a number of CSS properties, but four in particular are needed during the ES layout calc.
CSS properties used in
|
Ugh... didn’t mean to close this. |
These properties are unique in that they’re specific to this custom element. As far as I know, there is no built in CSS property which only applies to a specific element and affects it’s behavior. But again, this is too close to a particular solution. To keep my analogy to a mode of transportation, this is like saying NYC runs subway 24 hours with specific trains & stations. That’s not really defining the underlying use cases. We need to step back and ask why anyone is using CSS properties to define these things. |
The element in question is responsible for a layout effect, so yes — ideally though I could ditch the element, which isn’t used directly but only as an implementation detail for the widgets which are meant for direct consumption. In other words, custom elements are currently providing the only hook I know of for managing custom layout effects. The elements that ‘really’ use these CSS properties are diverse, including select, typeahead, tooltip, and dropdown menu widgets. I think I did make it very clear why we’re using CSS: the values are CSS values used to realize CSS effects. They need to be able to handle CSS variables and resolve to CSS value types. An attribute cannot take |
Why anyone is using css properties to define these things is because they don't belong to html attributes, they are used for styling purposes so they should be defined in css and like bathos said some of these properties need to be converted to px from other length units. |
Another example, some pages let you change language, some languages can be rtl ot bt, |
As a concrete example where I've wanted this is changing internal DOM to change between two styles of math. For example choosing between this: And this: Both are semantically equivalent math notation and choosing between them is just a stylistic preference so it would be natural to be able to encode these into the stylesheet e.g.: math-expression {
--summation-style: superscript subscript;
}
@media (orientation: portrait) {
/* Consume vertical space instead on portrait screens */
math-expression {
--summation-style: over under;
}
} Without |
I would like to offer another use case for In the Consider this basic example of the markup an author may write today: <model-viewer src="astronaut.gltf"
alt="An astronaut"
field-of-view="60deg"></model-viewer>
</model-viewer> Here the author has configured the This is one example, but we have probably a dozen or so attributes (e.g., |
@cdata Plus CSS has the |
I have the same use case as @Jamesernator for https://github.com/justinfagnani/katex-elements Right now I vend two separate elements for the "inline" and "display" rendering styles, but this really should be a CSS property, or even better it would just change depending on whether Another use-case I've heard from AMP is to control how many images their carousel shows at a time. On narrow screens it may be 1, on wide screens it may be 2 or 3, but this should be styleable by the component user with a media query. |
@bathos indeed, and just to underscore the value of that kind of thing to us: we leverage CSS-like units for many of our "style" attributes (lengths, angles and percent), including support for complex expressions like |
Just posted a few use cases in w3c/css-houdini-drafts#1010 since I was unaware of this issue.
|
AMP team is also very interested in this, although we also have use cases that would benefit from a more generic First, AMP has special treatment of AMP runs as a library that's used on anyone's webpage. The HTML Publisher could be using any number of selectors to set For a more generic observer, AMP is also interested in |
Sometime a custom element may need something like an orientation attribute to make it
vertically or horizontally (think to a slider for example), in theory it would be better
to have that as a css style instead of an attribute but currently there is no way to
observe a change in a style (being inline or in a class) so the custom element cannot
respond to it.
Is there any plan to add something like observedStyles() similar to observedAttributes()
or any other similar method to do it?
Sorry if this not the right place to ask...
The text was updated successfully, but these errors were encountered: