What is @juggle/resize-observer?
The @juggle/resize-observer package is a polyfill for the ResizeObserver API, which allows developers to observe changes to the size of DOM elements and react accordingly. It provides a way to receive notifications when an element's content rectangle has changed its size, which is useful for responsive designs and element-specific layout updates.
What are @juggle/resize-observer's main functionalities?
Observing size changes in elements
This feature allows you to create a new ResizeObserver instance and observe size changes in DOM elements. When a change is detected, the callback function is executed with the new size information.
const resizeObserver = new ResizeObserver(entries => {
for (let entry of entries) {
console.log('Size changed:', entry.contentRect);
}
});
resizeObserver.observe(document.querySelector('.resizable-element'));
Unobserving elements
This feature allows you to stop observing size changes in a DOM element that was previously being observed by the ResizeObserver instance.
resizeObserver.unobserve(document.querySelector('.resizable-element'));
Disconnecting the observer
This feature allows you to completely disconnect the ResizeObserver instance, which stops observing all elements and clears its references, allowing for garbage collection.
resizeObserver.disconnect();
Other packages similar to @juggle/resize-observer
resize-observer-polyfill
This package is another polyfill for the ResizeObserver API, similar to @juggle/resize-observer. It provides the same basic functionality, allowing developers to observe changes in the size of DOM elements.
react-resize-detector
This package is specifically designed for React and provides components and hooks to listen to resize events on DOM elements. It's a higher-level abstraction compared to @juggle/resize-observer, which is more general-purpose and not tied to a specific framework.
ResizeObserver
A polyfill entirely based on the current ResizeObserver Draft Specification.
This is currently a work in progress and should be used for testing only at this stage.
Installation
npm i @juggle/resize-observer
Usage
import ResizeObserver from '@juggle/resize-observer';
const resizeObserver = new ResizeObserver((entries, observer) => {
console.log('Something has resized!');
});
This will always use the polyfilled version of ResizeObserver, even if the browser supports ResizeObserver natively.
Switching between native and polyfilled versions
You can check to see if the native version is available and switch between this and the polyfill to improve porformance on browsers with native support.
import ResizeObserverPolyfill from '@juggle/resize-observer';
const ResizeObserver = window.ResizeObserver || ResizeObserverPolyfill;
const resizeObserver = new ResizeObserver((entries, observer) => {
console.log('Something has resized!');
});
Limitations
- No support for IE 10 and below (sorry all). IE 11 is supported.
- Dynamic stylesheet changes may not be noticed and updates will occur on the next user interaction.
- Currently no support for observations when
display:none
is toggled (coming soon).
Similar Libraries
https://github.com/que-etc/resize-observer-polyfill
https://github.com/pelotoncycle/resize-observer