Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@juggle/resize-observer
Advanced tools
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.
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();
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.
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.
A minimal library which polyfills the ResizeObserver API and is entirely based on the latest Draft Specification. Essentially, it detects when an element's size changes, allowing you to deal with it!
Check out the Demo Playground
npm i @juggle/resize-observer
import ResizeObserver from '@juggle/resize-observer';
const ro = new ResizeObserver((entries, observer) => {
console.log('Body has resized!');
observer.disconnect(); // Stop observing
});
ro.observe(document.body); // Watch dimension changes on body
This will use the ponyfilled version of ResizeObserver, even if the browser supports ResizeObserver natively.
import ResizeObserver from '@juggle/resize-observer';
const ro = new ResizeObserver((entries, observer) => {
console.log('Elements resized:', entries.length);
entries.forEach((entry, index) => {
const { width, height } = entry.contentRect;
console.log(`Element ${index + 1}:`, `${width}x${height}`);
});
});
const els = docuent.querySelectorAll('.resizes');
[...els].forEach(el => ro.observe(el)); // Watch multiple!
The latest standards allow for watching different box sizes. The box size option can be specified when observing an element. Options inlcude border-box
, content-box
, scroll-box
, device-pixel-border-box
.
device-pixel-border-box
can only be used on canvas
elements.
import ResizeObserver from '@juggle/resize-observer';
const ro = new ResizeObserver((entries, observer) => {
console.log('Elements resized:', entries.length);
entries.forEach((entry, index) => {
const { inlineSize, blockSize } = entry.borderBoxSize;
console.log(`Element ${index + 1}:`, `${inlineSize}x${blockSize}`);
});
});
const observerOptions = {
box: 'border-box'
};
const els = docuent.querySelectorAll('.resizes');
[...els].forEach(el => ro.observe(el, observerOptions)); // Watch multiple!
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;
// Uses native or polyfill, depending on browser support
const ro = new ResizeObserver((entries, observer) => {
console.log('Something has resized!');
});
Warning: Browsers with native support may be behind on the latest specification.
Resize Observers have inbuilt protection against infinite resize loops.
If an element's observed box size changes again within the same resize loop, the observation will be skipped and an error event will be dispatched on the window.
import ResizeObserver from '@juggle/resize-observer';
const ro = new ResizeObserver((entries, observer) => {
// Changing the body size inside of the observer
// will cause a resize loop and the next observation will be skipped
document.body.style.width = '50%';
});
// Listen for errors
window.addEventListener('error', e => console.log(e.message));
// Observe the body
ro.observe(document.body);
To prevent constant polling, every frame. The DOM is queried whenever an event occurs which could cause an element to change its size. This could be when an element is clicked, a DOM Node is added, or, when an animation is running.
To cover these scenarios, there are two types of observation. The first is to listen to specific DOM events, including resize
, mousedown
and focus
to name a few. The second is to listen for any DOM mutations that occur. This detects when a DOM node is added or removed, an attribute is modified, or, even when some text has changed.
This allows for greater idle time, when the application itself is idle.
This library is written in TypeScript, however, it's compiled into JavaScript during release. Definition files are included in the package and should be picked up automatically to re-enable support in TypeScript projects.
FAQs
Polyfills the ResizeObserver API and supports box size options from the latest spec
The npm package @juggle/resize-observer receives a total of 1,363,619 weekly downloads. As such, @juggle/resize-observer popularity was classified as popular.
We found that @juggle/resize-observer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.