
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
@thednp/position-observer
Advanced tools
🏯 PositionObserver is a JavaScript tool that provides a way to asynchronously observe changes in the position of a target element within its viewport.
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.
npm i @thednp/position-observer
bun add @thednp/position-observer
pnpm add @thednp/position-observer
deno add npm:@thednp/position-observer@latest
import PositionObserver from '@thednp/position-observer';
// Find a target element
const myTarget = document.getElementById('myElement');
// Define a callback
const callback = (entries: IntersectionObserverEntry[], currentObserver: PositionObserver) => {
// Access the observer inside your callback
// console.log(currentObserver);
entries.forEach((entry) => {
if (entry.isIntersecting/* and your own conditions apply */) {
// Handle position changes
console.log(entry.boundingClientRect);
}
})
};
// Set options
const options = {
root: document.getElementById('myModal'), // Defaults to document.documentElement
rootMargin: '0px', // Margin around the root, this applies to IntersectionObserver only
threshold: 0, // Trigger when any part of the target is visible, this applies to IntersectionObserver only
};
// Create the observer
const observer = new PositionObserver(callback, options);
// Start observing
observer.observe(myTarget);
// Example callback entries
[{
target: <div#myElement>,
boundingClientRect: DOMRectReadOnly,
intersectionRatio: number,
isIntersecting: boolean,
// ... other IntersectionObserverEntry properties
}]
// Get an entry
observer.getEntry(myTarget);
// Stop observing a target
observer.unobserve(myTarget);
// Resume observing
observer.observe(myTarget);
// Stop all observation
observer.disconnect();
| Option | Type | Description |
|---|---|---|
root | Element | undefined | The element used as the viewport for checking target visibility. Defaults to document.documentElement. |
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 IFrames. See the ScrollSpy example for implementation details.
The two initialization options specifically for the IntersectionObserver are rootMargin and threshold; they control the behavior of PositionObserver in the sense that when conditions are met for intersection, the PositionObserver entries are updated or skipped.
observe() method requires a valid Element, or it throws an Error. Targets not attached to the DOM are ignored.clientWidth and clientHeight.IntersectionObserver with the document as the root to determine isIntersecting. The rootMargin and threshold options apply to these checks.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.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.
FAQs
🏯 PositionObserver is a JavaScript tool that provides a way to asynchronously observe changes in the position of a target element within its viewport.
The npm package @thednp/position-observer receives a total of 258,644 weekly downloads. As such, @thednp/position-observer popularity was classified as popular.
We found that @thednp/position-observer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.