Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
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 ResizeObserver API.
Implements event based tracking of changes in the dimensions of elements. Uses MutationsObserver and falls back to an infinite dirty checking cycle if the first one is not supported. Handles long running CSS transitions/animations, attributes and nodes mutations along with changes made by :hover pseudo-class (optional).
Compliant with the spec. Doesn't contain any publicly available methods except for those described in the spec. Size is 3.4kb when minified and gzipped.
Live demo (won't run in IE9).
From NPM:
npm install --save resize-observer-polyfill
From Bower:
bower install --save resize-observer-polyfill
Or just grab one of the pre-built versions from dist
.
Polyfill has been tested and known to work in the following browsers:
NOTE: Internet Explorer 8 and its earlier versions are not supported.
If you are using ES6 modules with bundlers like Webpack or JSPM:
import ResizeObserver from 'resize-observer-polyfill';
const observer = new ResizeObserver((entries, observer) => {});
observer.observe(document.body);
// ...
Alternatively you can take a pre-built UMD version.
With AMD:
define([
'resize-observer-polyfill/dist/ResizeObserver'
], function (ResizeObserver) {
// ...
});
With CommonJS:
var ResizeObserver = require('resize-observer-polyfill/dist/ResizeObserver');
As a browsers' global object:
<script src="resize-observer-polyfill/dist/ResizeObserver.js"></script>
<script>
(function () {
var observer = new ResizeObserver(function () {});
})();
</script>
You can also take a version that always extends browsers' global object.
With ES6 modules:
import 'resize-observer-polyfill/index.global';
const observer = new ResizeObserver(() => {});
With AMD/CommonJS:
require('resize-observer-polyfill/dist/ResizeObserver.global');
ResizeObserver
class additionally implements following static accessor properties:
After the mutations of DOM attributes, like class
or style
, an update cycle will be started which will run either until the dimensions of observed elements keep changing or the idleTimeout
is reached. This approach is used to handle transitions and animations.
Default timeout value is 50
milliseconds and you can increase it to match the delay of transitions, e.g. when transition starts with the delay of 500
milliseconds you can set ResizeObserver.idleTimeout = 500
to the corresponding value.
Note that even if transitions don't have a delay it's still better to leave this value as it is.
By default possible changes in dimensions of elements caused by CSS :hover
class are not tracked. To handle them you can set ResizeObserver.continuousUpdtaes = true
which in turn will start a continuous update cycle with an interval of 200
milliseconds (using RAF, of course). Keep in mind that this is going to affect the performance.
NOTE: Changes made to these properties will affect all existing and future instances of ResizeObserver.
First make sure that you have all dependencies installed by running npm install
. Then you can execute following tasks either with a gulp CLI or with npm run gulp
command.
To make a production build:
gulp build:production
To make a development build of polyfill itself (including sourcemap).
Files will be located in tmp
folder:
gulp build:dev
To run a code style test:
gulp test:lint
Functional tests for all available browsers that are
defined in karma.config.js
file:
gulp test:spec
If you want to test some specific browser that is not present in karmas' config file you'll need
to run gulp test:spec:manual
and then navigate to the http://localhost:9876/debug.html
page.
To run tests against native implementation you'll need to remove top imports from the ResizeObserver.spec.js
test suite in the first place.
Also don't forget to make sure that ResizeObserver API is enabled in a browser that is being tested.
FAQs
A polyfill for the Resize Observer API
The npm package resize-observer-polyfill receives a total of 5,215,660 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.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.