WARNING API CURRENTLY UNSTABLE. PLEASE WAIT FOR A MORE STABLE RELEASE
react-dom-observer
This package exposes interfaces for reacting to changes in Observer APIs, for example reacting to resizing.
To see a demo, check the github pages site.
Install
yarn add react-dom-observer
Usage
IntersectionObserver
The IntersectionObserver
component leverages a polyfilled IntersectionObserver
to track the visibility of elements relative to each other.
In the simplest case, an IntersectionObserver
can be used to provide visibility
information from a parent element to its children.
In the following example, we create a short div where the child components are aware of if they're visible or not relative to the parent element's scroll space:
import IntersectionObserver from 'react-dom-observer'; const Example1 = <IntersectionObserver />
The Intersection
, and Internals
parameters are specific to each
IntersectionObserver
, which allows multiple IntersectionObserver
s
to track different aspects of different parents without running into each
other.
In this example, we produce the same scroll-aware set of children, but attempt to render them only when visible to the browser window:
import IntersectionObserver from 'react-dom-observer'; const ScrollTracker = WindowIntersection <IntersectionObserver /> const Example2 = <IntersectionObserver
ResizeObserver
The ResizeObserver
component leverages a polyfilled ResizeObserver
to track the visibility of elements relative to each other.
A ResizeObserver
can be used to track the size of a container element so that
React can calculate the necessary pixel-size of internal elements e.g. SVGs.
In the following example, we create a resize-aware <textarea>
:
import ResizeObserver Size from 'react-dom-observer'; const Example3 = <ResizeObserver> <Size /> </ResizeObserver>
License
MIT © zemnmez