Open nickschot opened 4 years ago
Sounds like a great idea. I came across this addon when looking for a ResizeObserver-based modifier -- that was my expectation of how this problem would likely be solved.
I'd like to see how well a modifier based on ResizeObserver
works too. :)
My application is for ember-container-query
, whereby O(10¹) resizes can happen at the same time (not just 1 or 2 resizes). I'm interested to see if ResizeObserver
performs as well as (or better than) what element-resize-detector
did.
So I looked into this a bit and found out a couple of things while tinkering with a ResizeObserver proof of concept for this addon:
getBoundingClientRect
manually in the callback forces another layout calculation). I'd prefer to simply pass the ResizeObserverEntry as the only argument, but that would be a breaking change.Thoughts are welcome!
I built ember-resize-observer-service to solve exactly the problem with multiple ResizeObserver
instances. Feel free to use it to migrate this addon to ResizeObserver API, if it makes sense.
Building a size-responsive modifier with it is pretty straight forward:
export default class OnResizeModifier extends Modifier {
@service resizeObserver;
didInstall() {
this.resizeObserver.observe(this.element, this.handleResize);
}
willRemove() {
this.resizeObserver.unobserve(this.element, this.handleResize);
}
@action
handleResize(...args) {
let [callback] = this.args.positional;
callback(...args);
}
}
@nickschot If you need a complete ResizeObserver based solution with a single instance optimization, I built ember-on-resize-modifier.
And in case if you need a polyfill:
ember install ember-resize-observer-polyfill
If it already exists it makes sense to migrate to that addon in my opinion. As far is I can see (after a very quick peek) the main feature difference is that your addon also runs on insert. For my use cases that doesn’t matter though.
I've noticed the element-resize-detector is quite a huge library for what it does. Were using the native ResizeObserver + a polyfill like https://www.npmjs.com/package/resize-observer-polyfill (2.44kB gzipped) considered? Seems like it would work just as well and save many KB. The polyfill could even be loaded conditionally based on ember's
config/targets.js
feature.