Coder Social home page Coder Social logo

async-rect's Introduction

DEPRECATION NOTICE

This library was built on faulty assumptions. You are better off calling getBoundingClientRect() inside of requestAnimationFrame(), which has essentially the same effect while avoiding the cost of creating an IntersectionObserver, as this benchmark demonstrates.

Original documentation follows.

async-rect Build Status

Asynchronous version of getBoundingClientRect(), built on top of IntersectionObserver.

It works the same as getBoundingClientRect(), except you don't need to worry about layout thrashing or forcing layouts/reflows, because this function asynchronously waits for the next browser layout pass instead of synchronously forcing layout.

This library also works around some browser bugs.

Usage

npm install async-rect
import asyncGetBoundingClientRect from 'async-rect'

(async () => {
  let element = document.querySelector('#myElement')
  let rect = await asyncGetBoundingClientRect(element)
  console.log(rect)
  // { width: 100, height: 50, left: 0, top: 0, bottom: 50, right: 100 }
})()

Or use as a script tag:

<script src="https://unpkg.com/async-rect/dist/async-rect.min.js"></script>
<script>
 asyncGetBoundingClientRect(element).then(/* ... */)
</script>

Requirements

This library requires both IntersectionObserver and Promises. If you need to support browsers without those features, please use the polyfills:

This library is written in ES5, so it will work in older browsers assuming you have the polyfills.

Limitations

This library creates a new IntersectionObserver object and then disconnect()s it when it's done. As an optimization, you may want to re-use the same IntersectionObserver object and manage its lifecycle yourself. (Note though that re-using the same IntersectionObserver object will not work if you want to invoke the function twice on the same element.) You may also want to do this if you are interested in using a root element other than the viewport, or if you want to know the rootBounds as well, or if you have some other particular use case.

In those cases, the library is quite small, so feel free to fork as you see fit! ๐Ÿ˜Š

Building

npm run build

Testing

npm test

async-rect's People

Contributors

nolanlawson avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

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