• Stars
    star
    156
  • Rank 239,589 (Top 5 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Element resize detection.

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, callback can be omitted to indicate that
// all event listeners should be removed
removeListener(elem, callback)

this inside callback function is the element whose size has been changed, also callback receive element as first argument.

Heads up

As resize-detector is published in both ES Module & CommonJS format and when you use webpack to bundle your app, the ESM version will be imported. It is not transpiled by Babel or similar tools so you have to transpile it in your build process.

For webpack with babe-loader you need to add it to the include field of the options:

// ...
{
  test: /\.js$/,
  loader: 'babel-loader',
  include: [
    // other stuff to be transpiled
    // ...
    path.resolve('node_modules/resize-detector')
  ]
}
// ...

If you are using other toolchain, just configure your bundler similarly so that resize-detector will be transpiled during build process.


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

sdecima/javascript-detect-element-resize

  • 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.

que-etc/resize-observer-polyfill

  • 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.

developit/simple-element-resize-detector

  • 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.

pelotoncycle/resize-observer

  • 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.

wnr/element-resize-detector

  • 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.

More Repositories

1

vue-awesome

Awesome SVG icon component for Vue.js, built-in with Font Awesome icons.
JavaScript
2,422
star
2

github-hovercard

Neat hovercards for GitHub.
JavaScript
1,823
star
3

coplay

Synchronizing video play between two peers.
JavaScript
772
star
4

vue-clamp

Clamping multiline text with ease.
Vue
697
star
5

vue-octicon

Octicon component for Vue.js, using inline SVG.
JavaScript
125
star
6

kolor

Color manipulation in sexy syntax.
JavaScript
94
star
7

slideshows

My slideshows about techical stuff.
HTML
79
star
8

lucky

A dead simple raffle app.
JavaScript
65
star
9

make-github-greater

Let's make GitHub greater.
JavaScript
57
star
10

makedown

Enable Markdown editing on Zhihu. / 在知乎上使用 Markdown 进行创作。
JavaScript
48
star
11

highlightable-input

A light-weight highlightable input.
TypeScript
38
star
12

fi

A snippet for inspecting font-family usage in a webpage.
JavaScript
34
star
13

color-converters

Tiny modular color converters.
TypeScript
23
star
14

clone-element

Clone HTML elements with styles.
TypeScript
21
star
15

postcss-discard-overridden

PostCSS plugin to discard overridden keyframes or counter-style.
JavaScript
16
star
16

postcss-sort-style-rules

PostCSS plugin to sort style rules according to selector specificity.
JavaScript
12
star
17

vue-directive-normalizer

Normalizing directive binding into an option bag.
JavaScript
11
star
18

vue-coupled

Create coupled parent-child component pairs with composition API.
TypeScript
10
star
19

vue-awesome-material-icons

Material icons for Vue-Awesome.
JavaScript
9
star
20

vue-void

A void Vue component to override a (scoped) slot with nothing.
JavaScript
8
star
21

triangle.less

Handy Less mixin for creating CSS triangles.
CSS
7
star
22

w3c-userstyle

Highly opinionated user stylesheet for W3C specifications.
6
star
23

zhihu-insight

在知乎问题页添加已有回答的索引,方便浏览热门问题。
JavaScript
5
star
24

spotlight

A simplest finder.
JavaScript
5
star
25

veui-theme-spectre

Spectre.css based theme for VEUI.
Vue
4
star
26

esm-pkg

Find all dependent packages with an ESM output.
JavaScript
4
star
27

short-circuit-loader

Short circuit specified side-effect-only modules for webpack.
JavaScript
4
star
28

justineo.github.io

HTML
3
star
29

v-html-svg

Manual test case for v-html support for SVG in Vue.js.
HTML
3
star
30

singles

3
star
31

npmmirror-sync

Triggers sync on npmmirror.com.
TypeScript
2
star
32

nuxt-generate-bug

A reproduction for nuxt/nuxt.js#4562
Vue
1
star
33

simpledark

Automatically exported from code.google.com/p/simpledark
PHP
1
star
34

babel-plugin-import-peer

Import peer dependencies for component modules with ease.
JavaScript
1
star
35

vscode-dls

Baidu DSL tooling for VS Code.
TypeScript
1
star
36

draw

Lucky draw for SSG.
JavaScript
1
star
37

vue-esnext-in-template

A minimal repro for vuejs/vue/#6307.
JavaScript
1
star
38

drag

Super compatible draggable implementation.
JavaScript
1
star