Gathering detailed insights and metrics for @thednp/position-observer
Gathering detailed insights and metrics for @thednp/position-observer
Gathering detailed insights and metrics for @thednp/position-observer
Gathering detailed insights and metrics for @thednp/position-observer
🏯 The PositionObserver provides a way to asynchronously observe changes in the position of a target element with an ancestor element or with a top-level document's viewport.
npm install @thednp/position-observer
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.7
Supply Chain
100
Quality
91.7
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
3,881,248
Last Day
5,569
Last Week
204,393
Last Month
885,874
Last Year
3,881,248
MIT License
31 Stars
71 Commits
1 Forks
2 Watchers
2 Branches
2 Contributors
Updated on Jun 11, 2025
Minified
Minified + Gzipped
Latest Version
1.1.0
Package Id
@thednp/position-observer@1.1.0
Unpacked Size
54.98 kB
Size
11.21 kB
File Count
12
NPM Version
11.4.1
Node Version
23.5.0
Published on
Jun 11, 2025
Cumulative downloads
Total Downloads
Last Day
-2.2%
5,569
Compared to previous day
Last Week
-8.8%
204,393
Compared to previous week
Last Month
10.3%
885,874
Compared to previous month
Last Year
0%
3,881,248
Compared to previous year
1
The PositionObserver is a lightweight utility that replaces traditional resize
and scroll
event listeners. Built on the IntersectionObserver API, it provides a way to asynchronously observe changes in the position of a target element with an ancestor element or with a top-level document
's viewport.
1npm i @thednp/position-observer
1yarn add @thednp/position-observer
1pnpm add @thednp/position-observer
1deno add npm:@thednp/position-observer@latest
1import PositionObserver from '@thednp/position-observer'; 2 3// Find a target element 4const myTarget = document.getElementById('myElement'); 5 6// Define a callback 7const callback = (entries: IntersectionObserverEntry[], currentObserver: PositionObserver) => { 8 // Access the observer inside your callback 9 // console.log(currentObserver); 10 entries.forEach((entry) => { 11 if (entry.isIntersecting/* and your own conditions apply */) { 12 // Handle position changes 13 console.log(entry.boundingClientRect); 14 } 15 }) 16}; 17 18// Set options 19const options = { 20 root: document.getElementById('myModal'), // Defaults to document.documentElement 21 rootMargin: '0px', // Margin around the root, this applies to IntersectionObserver only 22 threshold: 0, // Trigger when any part of the target is visible, this applies to IntersectionObserver only 23 callbackMode: 'intersecting', // Options: 'all', 'intersecting', 'update' 24}; 25 26// Create the observer 27const observer = new PositionObserver(callback, options); 28 29// Start observing 30observer.observe(myTarget); 31 32// Example callback entries 33[{ 34 target: <div#myElement>, 35 boundingClientRect: DOMRectReadOnly, 36 intersectionRatio: number, 37 isIntersecting: boolean, 38 // ... other IntersectionObserverEntry properties 39}] 40 41// Get an entry 42observer.getEntry(myTarget); 43 44// Stop observing a target 45observer.unobserve(myTarget); 46 47// Resume observing 48observer.observe(myTarget); 49 50// Stop all observation 51observer.disconnect();
Option | Type | Description |
---|---|---|
root | Element | undefined | The element used as the viewport for checking target visibility. Defaults to document.documentElement . |
callbackMode | "all" | "intersecting" | "update" | undefined | Controls PositionObserver callback behavior. Defaults to "intersecting". See below for details. |
rootMargin | string | undefined | Margin around the root of the IntersectionObserver . Uses same format as CSS margins (e.g., "10px 20px"). |
threshold | number | number[] | undefined | Percentage of the target's visibility required to trigger the IntersectionObserver callback. |
The PositionObserver instance.root
identifies the Element
whose bounds are treated as the bounding box of the viewport for the element which is the observer's target. Since we're observing for its width and height changes, this root can only be an instance of Element
, so Document
cannot be the root of your PositionObserver instance.
The IntersectionObserver instance.root
is always the default, which is Document
. The two observers really care for different things: one cares about intersection the other cares about position, which is why the two observers cannot use the same root.
When observing targets from a scrollable parent element, that parent must be set as root. The same applies to embeddings and IFrame
s. See the ScrollSpy example for implementation details.
The two initialization options specifically for the IntersectionObserver are rootMargin
and threshold
and only apply when using "intersecting" or "update" modes.
all
: Triggers the callback for all observed targets, regardless of visibility or position changes.intersecting
: Triggers the callback only for targets that are intersecting with the document's viewport and have changed position or root dimensions.update
: Triggers the callback for targets with position/root dimension changes or when a target's intersection status changes (e.g., from intersecting to non-intersecting).observe()
method requires a valid Element
, or it throws an Error. Targets not attached to the DOM are ignored.clientWidth
and clientHeight
.callbackMode
:
all
: Includes every observed target's entry.intersecting
: Includes only intersecting targets with position or root dimension changes.update
: Includes targets with position/root dimension changes or a change in intersection status.IntersectionObserver
with the document
as the root to determine isIntersecting
. The rootMargin
and threshold
options apply to these checks but have no impact in all
mode.entry.boundingClientRect
from observer.getEntry(target)
to avoid redundant getBoundingClientRect()
calls.requestAnimationFrame
and IntersectionObserver
for efficient, asynchronous operation. Consider wrapping callbacks in requestAnimationFrame
for synchronization and to eliminate any potential observation errors.display: none
or visibility: hidden
) for actual accurate bounding box measurements.entry.boundingClientRect.width
or height changes to mimic ResizeObserver
.entry.boundingClientRect.top
or left
.IntersectionObserver
uses the document
as its root, while the PositionObserver
's root option defines the reference Element
for position tracking.callbackMode
based on your use case:
intersecting
for most scenarios where only visible elements matter.update
to track intersection state changes.all
for comprehensive monitoring of all targets.all
mode, as non-intersecting targets are still processed. They are however relevant in intersecting
or update
modes for defining visibility conditions.The PositionObserver is released under the MIT license.
No vulnerabilities found.
No security vulnerabilities found.