-
Notifications
You must be signed in to change notification settings - Fork 18
Feature suggestions, usage examples #34
Comments
idea watch scrollWidth, scrollHeight. use case scroll to the bottom when content is added. To achieve same effect with existing ResizeObserver, you must enclose existing div inside an additional div. cons unsure if we can implement this efficiently. |
FF would like to observe location: https://bugzilla.mozilla.org/show_bug.cgi?id=1109868#c2 |
@atotic thanks for working on this! Is there any way to observe multiple elements at once, or is the preferred method to forEach on the list of elements and call observe with them one by one? My use case is to observe each
Would be great if
If overloading is a concern, maybe there could be an PS: FWIW the documentation at https://developers.google.com/web/updates/2016/10/resizeobserver#api makes it seem like
|
Making observe() accept lists would make API more pleasant to use. DOM APIs are usually minimalist. Larger API surface means more interoperability problems. This also makes them a lot less fun to use than jQuery. We will not modify observe() to accept lists for the following reasons:
|
Well, I've been musing about this idea for a while. I wish there was a way to configure what type of the box-model is observed. In most cases I've seen so far, developers are neglecting and don't leverage the content-box dimensions provided in a ResizeObserEntry for many of them need the border-box model and sometimes margin or padding boxes. Some of them even hope to receive notifications when one of the former boxes changes, not to mention that these are different events. And unfortunately there is no reliable way to handle the last case, as it requires to either modify both styles and markup or to change the box-sizing of an element. And to tamper with a DOM in such a way is not the best option for a third-party library which aims to cover this functionality. So it would probably be for the best if ResizeObserver accepted an optional parameter in it's constructor with one of the box types: "margin" | "border" | "padding" | "content". Like the getBoxQuads API does with it's box property. And I'm sorry if I'm missing something here. Maybe this option has been already ruled out for some reason? |
Main reasons for only observing content box size are:
This can be revisited for v2, if real world usage shows demand for measurement of other sizes. |
I have an observer where I only care about the element's width to do some layout calculations. Would it be reasonable to somehow also get the size of the element before the resize happened? That way I could do As an example, React has a |
@jacobrask But you can use content rectangle from the previous observation. // Replace with an empty rectangle if the "doSomethingHeavy" function
// needs to be invoked on the first observation.
let prevRect = null;
const observer = new ResizeObserver(entries => {
// Simplified case with only one element being observed.
const {contentRect} = entries[0];
if (!prevRect) {
prevRect = contentRect;
}
if (prevRect.width !== contentRect.width) {
doSomethingHeavy();
}
prevRect = contentRect;
});
observer.observe(document.documentElement); |
Thanks for your answer. This is also what I currently do. It makes it a bit more difficult to factor out the observer function though, since I'll then need to make a "factory" function to be able to access the memoized
I completely understand that this use case might not be worth making the API more complicated for, but it might still be good to know a potential use case/pattern. |
I'm fiddling with using ResizeObserver for element/container queries. I’m trying to concoct an example that shows whether/how ResizeObserver is more performant than window.onresize.
Based on these two pages, it seems like, if every element’s size is changing on every window resize, it isn’t, really? I turned Chrome CPU throttling up to 10x on my MBP and resized -- both pages stutter about the same amount. Is that true, or am I doing something wrong? |
You are not doing anything wrong. If all your divs are changing size, performance is dominated by layout, not by resize observation. ResizeObserver is not about raw performance, (although it can help by eliminating polling), it is about giving developer ability to respond to size changes inside components. For example Without ResizeObserver, you'd have to poll, or use mouseover, etc. |
Thanks! That's really helpful. Tangentially-related: for the element/container query use case, would there be any performance or usability benefit to setting IntersectionObserver-style thresholds on the ResizeObserver, so that events only fire at pre-set breakpoints? (Rather than on every resize, requiring the callback to check against breakpoints with an if). |
Performance benefit would be marginal. Layout and Paint that happen together with ResizeObserver and most often much more expensive. |
Re: #34 (comment) and margin/border/padding/content rects... More than three-quarters of Chrome-Platform-Status-observed pageloads use some form of |
@jacobrask I am curious about why exactly did you need to know previous reported size? Can you elaborate? |
@atotic To be very specific, I have a grid layout component which distributes items into columns. Whenever the grid component's width updates it should redistribute the items. This is obviously expensive, so I only want to do it when the width changes and not the height, which is why I want to compare it with the previous width. |
@jacobrask Thanks for being specific, this is the first real-world example of needing to know previous size. btw, is your site https://www.visitstockholm.com/? |
No, I work on the Prenly news reader. Adaptable page layout is important for a our application so ResizeObserver is being introduced at several places. |
@jacobrask Neat. Are there any publicly available pages that currently use ResizeObserver polyfill. I am profiling today for "Intent to Ship". |
Do you have any suggestions, or usage examples? Leave your comments here.
The text was updated successfully, but these errors were encountered: