Socket
Socket
Sign inDemoInstall

@juggle/resize-observer

Package Overview
Dependencies
0
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @juggle/resize-observer

ResizeObserver - Based on the official draft specification


Version published
Weekly downloads
4.2M
increased by5.9%
Maintainers
1
Install size
40.9 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

ResizeObserver

A minimal library which polyfills the ResizeObserver API and is entirely based on the latest Draft Specification.

This library observes elements and dispatches notifications when their dimensions change. Differences are only calculated during animation, or, after DOM mutation or user interaction has occurred, keeping CPU and power consumption minimal.

Demo Playground

Installation

npm i @juggle/resize-observer

Basic usage

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.

Watching multiple elements

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!

Watching different box sizes

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 a canvas element.

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!

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;

// Uses native or polyfill, depending on browser support
const ro = new ResizeObserver((entries, observer) => {
  console.log('Something has resized!');
});

What's it good for?

  • Building responsive websites.
  • Creating 'self-aware' Web Components.
  • Making 3rd party libraries more responsive. e.g. charts and grids.
  • Many other things!

Limitations

  • No support for IE10 and below. IE11 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).

TypeScript support

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.

Keywords

FAQs

Last updated on 25 Jan 2019

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc