Code Monkey home page Code Monkey logo

resize-detector's Introduction

Resize Detector

This project is basically a modified version of sdecima/javascript-detect-element-resize including these changes:

  • Try to utilize native ResizeObserver first.
  • Adopt Mutation-based approach to track detaching/attaching in both DOM trees and render trees (see que-etc/resize-observer-polyfill).
  • Use ES Modules.
  • Put most CSS content inside a separate .css file.
  • Drop support for IE8 and below.
  • Make the package available from npm.

Installation

$ npm i --save resize-detector

Usage

import { addListener, removeListener } from 'resize-detector'

// adding listener
addListener(elem, callback)

// removing listener
removeListener(elem, callback)

this inside callback function is the element whose size has been changed.

Limitations and caveats

  • Is polyfill?

    No.

  • Native first

    Yes.

  • Strategy

    Scroll-based + Mutation-based.

  • Pros

    • Small size.
    • Minimal limitations.
  • Side effects

    • Targets with position: static will become position: relative.
    • Several hidden elements will be injected into the target elements.

Comparison with other projects

  • Is polyfill?

    No.

  • Native first

    No.

  • Strategy

    Scroll-based.

  • Pros

    • Small size.
    • Higher performance comparing to hidden <object>s.
    • Compatible with down to IE7.
  • Side effects

    • Targets with position: static will become position: relative.
    • Several hidden elements will be injected into the target elements.
  • Limitations

    • Cannot track detach/attach or visibility change on IE10 and below.
  • Is polyfill

    Yes.

  • Native first

    Yes.

  • Fallback Strategy

    Use MutationObserver to observe every mutation in a document. For IE9/10, use Mutation Events instead.

  • Pros

    • Small size.
    • Minimal side effects on target elements.
    • Can track detaching/attaching in both DOM trees and render trees as soon as it's triggered by DOM mutation.
  • Limitations

    • Need extra transition event handling to catch size change from user interaction pseudo classes like :hover.
    • Delayed transitions will receive only one notification with the latest dimensions of an element.
  • Is polyfill

    No.

  • Native first

    No.

  • Strategy

    Listen to resize events via hidden <iframe>s.

  • Pros

    Dead simple.

  • Side effects

    • Targets with position: static will become position: relative.
    • Several hidden elements will be injected into the target elements.
    • Relatively low performance.
  • Limitations

    • Inapplicable for void elements.
    • Cannot track detach/attach or visibility change.
  • Is polyfill?

    Yes.

  • Native first

    Yes.

  • Fallback Strategy

    Long polling through requestAnimationFrame or setTimeout.

  • Pros

    Dead simple.

  • Side effects

    • Might be not so performant by checking rendered sizes in each animation frame.
  • Is polyfill?

    No.

  • Native first

    No.

  • Strategy

    Either hidden <object>s or scroll-based.

  • Pros

    Two approaches available (Really, why?) with scroll-based approach being much faster than hidden <object>s.

  • Side effects

    • Targets with position: static will become position: relative.
    • Several hidden elements will be injected into the target elements.
  • Limitations

    • Package size is relatively large.
    • Inapplicable for void elements.
    • Cannot track detach/attach or visibility change.

resize-detector's People

Contributors

justineo avatar sorayama avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.