Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
resize-observer-polyfill
Advanced tools
The resize-observer-polyfill package provides a polyfill for the ResizeObserver API, which allows you to be notified when an element's content rectangle has changed its size, and thus react accordingly. This is particularly useful for responsive designs and element-specific layout updates.
Observing element size changes
This feature allows you to observe changes in the size of an element. The callback function is executed whenever the size of the observed element changes, providing you with the new dimensions and allowing you to perform any necessary adjustments.
var ro = new ResizeObserver(entries => {
for (let entry of entries) {
const cr = entry.contentRect;
console.log('Element:', entry.target);
console.log(`Element size: ${cr.width}px x ${cr.height}px`);
console.log(`Element padding: ${cr.top}px ; ${cr.left}px`);
}
});
ro.observe(document.getElementById('myElement'));
element-resize-detector is an npm package that offers a way to detect when an element has been resized. It differs from resize-observer-polyfill in that it uses scroll events and other tricks to detect size changes instead of the native ResizeObserver API, which can be useful for broader browser support.
css-element-queries provides a way to attach event listeners to elements for various purposes, including element resize detection. It includes a ResizeSensor class that can be used to detect when an element's size changes. This package offers additional functionalities like element query conditions, which go beyond the scope of resize-observer-polyfill.
A polyfill for the Resize Observer API.
Implementation is based on the MutationObserver and uses Mutation Events as a fall back if the first one is not supported, so there will be no polling unless DOM changes. Doesn't modify observed elements. Handles CSS transitions/animations and can possibly observe changes caused by dynamic CSS pseudo-classes, e.g. by :hover
.
Follows the spec and the native implementation. The size is 2.44 KiB when minified and gzipped.
Live demo (has style problems in IE10 and lower).
From NPM:
npm install resize-observer-polyfill --save-dev
From Bower: (will be removed with the next major release)
bower install resize-observer-polyfill --save-dev
Polyfill has been tested in the following browsers:
NOTE: Internet Explorer 8 and its earlier versions are not supported.
It's recommended to use this library in the form of a ponyfill, which doesn't inflict modifications of the global object.
import ResizeObserver from 'resize-observer-polyfill';
const ro = new ResizeObserver((entries, observer) => {
for (const entry of entries) {
const {left, top, width, height} = entry.contentRect;
console.log('Element:', entry.target);
console.log(`Element's size: ${ width }px x ${ height }px`);
console.log(`Element's paddings: ${ top }px ; ${ left }px`);
}
});
ro.observe(document.body);
Package's main file is a ES5 UMD bundle that will be swapped with the ES6 modules version for those bundlers that are aware of the module field, e.g. for Rollup or webpack 2+.
Note: global version of the polyfill (dist/ResizeObserver.global
) is deprecated and will be removed in the next major release.
As mentioned above, this implementation primarily (but not solely) relies on Mutation Observer with a fallback to Mutation Events for IE 9 and IE 10.
Speaking of Mutation Events as a fallback approach: they might not be as ugly as they are being rendered, particularly when their calls are batched, throttled and there is no need to analyze changes. Given that, they won't interrupt browser's reflow/repaint cycles (same for MutationObserver) and may even outperform Internet Explorer's implementation of MO causing little to no performance degradation. In contemporary browsers (Chrome, Firefox, etc.) Mutation Observer slows down the suite that includes 200 iterations of adding/removing elements, changing attributes and modifying text data by less than 1%. Internet Explorer gives different results with MO slowing down the same suite by 2-3% while Mutation Events show the difference of ~0.6%.
As for the reasons why other approaches, namely the iframe/object and scroll
strategies, were ruled out:
<img>
, <input>
, <textarea>
, <canvas>
, <tr>
, <tbody>
, <thead>
, <table>
, etc. For most of them you would need to keep an extra <div>
wrapper and almost all instances of the SVGGraphicsElement will be out of scope.display: none
state. Same goes for when it's being removed from or added to the DOM. It's not possible to handle these cases merely by using former approaches, so you'd still need to either subscribe for DOM mutations or to continuously check the element's state.And though every approach has its own limitations, I reckon that it'd be too much of a trade-off to have those constraints when building a polyfill.
:hover
and :focus
, are not tracked. As a workaround you could add a short transition which would trigger the transitionend
event when an element receives one of the former classes (example).To build polyfill. Creates UMD bundle in the dist
folder:
npm run build
To run a code style test:
npm run test:lint
Running unit tests:
npm run test:spec
To test in a browser that is not present in karma's config file:
npm run test:spec:custom
Testing against a native implementation:
npm run test:spec:native
NOTE: after you invoke spec:native
and spec:custom
commands head to the http://localhost:9876/debug.html
page.
FAQs
A polyfill for the Resize Observer API
The npm package resize-observer-polyfill receives a total of 6,336,114 weekly downloads. As such, resize-observer-polyfill popularity was classified as popular.
We found that resize-observer-polyfill 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.