devrelm / resize-observer

Other
98 stars 12 forks source link

Ponyfill #26

Open andershansander opened 5 years ago

andershansander commented 5 years ago

I am not sure about this but I think that since the index.ts file call the install function immediately, then window.ResizeObserver will always be overwritten, and that that should be avoided by ponyfills.

Feel free to close this if I have misunderstood your code or what ponyfill means. 🙂

devrelm commented 4 years ago

You're right about the index.ts, but the package.json defines lib/ResizeObserver.js (the compiled ResizeObserver.ts) as the main entrypoint:

https://github.com/pelotoncycle/resize-observer/blob/64efdf34dc577286fccb22a531b1b171e902b739/package.json#L5

So importing resize-observer as a module will load it as a ponyfill as described in the readme.

Alternatively, you can load resize-observer as a polyfill by loading dist/resize-observer.js directly in a script tag.

As-is, the only way to accidentally load resize-observer as a polyfill would be to import the index.js file directly by one of the following:

import 'resize-observer/lib';

// or:

import 'resize-observer/lib/index';

At that point, I'd almost like to rename it and call it a feature. We could move/build index.ts to polyfill/index.js so that import 'resize-observer/polyfill' installs resize-observer as a polyfill. Then users don't have to deal with adding another <script> tag to their html.

In fact, that really sounds like something that should be done. I'm going to keep this issue open to track that.

andershansander commented 4 years ago

Oh, I see. I missed that. Sorry for reporting on a non-existing issue.

sag1v commented 2 years ago

@devrelm This means that if the browser already support ResizeObserver we will use the native?

devrelm commented 2 years ago

@sag1v

No. When you use resize-observer you will always get this package's implementation. This package does no checks for a native implementation.

The reason for this is that no matter how hard we try to keep this package in line with the ResizeObserver spec, we have seen some differences pop up here and there. To keep from having strange behavior pop up on some browsers but not others, we decided it would be better to have resize-observer always use its own version.

sag1v commented 2 years ago

Thanks @devrelm 🙏 In our case (a shared library) we want to use the native RO and use a ponyfill/pollyfill in test environment (because jest doesn't support it)

So i wanted to understand if the logic of conditionally using the native already done or should we write it ourselves. I understand that the latter is the case 🙂