capricorn86 / happy-dom

A JavaScript implementation of a web browser without its graphical user interface
MIT License
3.3k stars 201 forks source link

Add ResizeObserver support #1497

Open csantos1113 opened 2 months ago

csantos1113 commented 2 months ago

Is your feature request related to a problem? Please describe. Could it be possible to add support for ResizeObserver?

I'm currently using vitest + happy-dom and I had a poor stub for the ResizeObserver; although it wasn't working but I didn't really know why, at first.

Then I noticed this in my code:

if (typeof global.ResizeObserver !== 'undefined') {
  return global.ResizeObserver;
}

So it made me realize that happy-dom already has a ResizeObserver stub with a bunch of // TODO 😅

https://github.com/capricorn86/happy-dom/blob/e6f91276d7b4549d17f5d67bd2f909723f4940b3/packages/happy-dom/src/resize-observer/ResizeObserver.ts#L13

Describe the solution you'd like My poor stub is far from optimal so I'm not going to share it here as it's not worth it; and I'm unsure how hard it'd be to build one from scratch.

But I could suggest something like https://github.com/juggle/resize-observer which I found by looking at the code of one of the libraries I use: https://github.com/ZeeCoder/use-resize-observer/blob/465fbc11c92e1b97a1dcb526b44ce4d889956951/tests/ie/polyfilled.tsx#L15-L25

Describe alternatives you've considered For now the solution for my case is deleting this code

if (typeof global.ResizeObserver !== 'undefined') {
  return global.ResizeObserver;
}

and forcing my poor stub to be applied regardless if there is a ResizeObserver or not

webJose commented 2 months ago

I need this too, to test Svelte v5 libraries.