Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/samthor/viz-observer

Notifies your code on DOM node move or resize
https://github.com/samthor/viz-observer

dom javascript

Last synced: 3 months ago
JSON representation

Notifies your code on DOM node move or resize

Awesome Lists containing this project

README

        

This is an ES Module which exports the default method `vizObserver`.
It notifies you when an element is resized or moved on a page, including when it appears or disappears (similar to but _not quite_ being added/removed from the DOM).
See [this post](https://whistlr.info/2021/observing-dom/) for an explanation, or watch the animation:



## Usage

Install as "viz-observer".

```js
import vizObserver from 'viz-observer';

const cleanup = vizObserver(yourElement, (rect) => {
console.info('element is now at', rect);
});

// later
cleanup();
```

This returns a cleanup method which you _must_ call when done, otherwise you risk memory leaks.
You can instead pass an `AbortSignal` as `{signal}` in the third argument:

```js
const ac = new AbortController();

vizObserver(yourElement, yourCallback, {signal: ac.signal});

// later
ac.abort();
```

## Requirements

This requires `IntersectionObserver`, which [is pretty widely supported](https://caniuse.com/intersectionobserver).
It also requires `ResizeObserver` but this was released before `IntersectionObserver` in all browsers bar one.

For Safari 12.x, which was the only browser to introduce `IntersectionObserver` _before_ `ResizeObserver`, it supports working in a slightly restricted mode: it won't report elements shrinking.
It'll only report elements when they grow, move or are removed/added from the page.

## Notes

This works totally fine inside Shadow DOM.
It's how the author uses it: I report the location of interesting elements and "attach" unrelated elements to them, such as for a popup or tooltip.