npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

position-observer

v1.0.3

Published

The PositionObserver interface provides an asynchronous way to observe changes in the position, size, and intersection state of elements relative to their containing root element.

Downloads

20,309

Readme

PositionObserver

The PositionObserver interface provides an asynchronous way to observe changes in the position, size, and intersection state of elements relative to their containing root element.

Overview

PositionObserver asynchronously monitors changes to element positioning, sizing, and visibility within a specified root container or the viewport. It extends the functionality of the native IntersectionObserver API, and provides detailed position and size information whenever these properties change.

The underlying implementation avoids polling mechanisms and optimizes performance by minimizing calls to getBoundingClientRect(), relying instead on a combination of native browser observation APIs.

Key capabilities

  • Observes element position changes within the viewport or custom root containers
  • Detects element size modifications, including changes due to CSS transformations
  • Monitors intersection state between observed elements and their root container
  • Detects changes to the position of an element when the viewport or root resizes

Installation

npm install position-observer

Constructor

new PositionObserver(callback, options?)

Parameters

  • callback - A function called when position changes are detected

    (entries: PositionObserverEntry[]) => void
  • options (optional) - Configuration object

    {
      root?: Element | Document | null;
    }

Return value

A new PositionObserver instance.

Instance methods

observe()

Begins observing position changes for the specified element.

observer.observe(target);

Parameters

  • target - The Element to observe for position changes.

unobserve()

Stops observing position changes for the specified element.

observer.unobserve(target);

Parameters

  • target - The Element to stop observing.

disconnect()

Stops observing all target elements and releases all references.

observer.disconnect();

PositionObserverEntry

Represents a single observation result containing geometric information about an observed element.

Properties

  • target (Element) - The observed element
  • boundingClientRect (DOMRectReadOnly) - The element's current position and dimensions
  • intersectionRect (DOMRectReadOnly) - The visible portion of the element within the root
  • isIntersecting (boolean) - Whether the element intersects with the root
  • rootBounds (DOMRectReadOnly) - The root container's dimensions

Examples

Basic usage

import { PositionObserver } from "position-observer";

const observer = new PositionObserver((entries) => {
  entries.forEach((entry) => {
    console.log("Element:", entry.target);
    console.log("Position:", entry.boundingClientRect);
    console.log("Is intersecting:", entry.isIntersecting);
    console.log("Intersection area:", entry.intersectionRect);
  });
});

const element = document.querySelector("#my-element");
observer.observe(element);

Position tracking

import { PositionObserver } from "position-observer";

const observer = new PositionObserver((entries) => {
  entries.forEach((entry) => {
    const { target, boundingClientRect, isIntersecting } = entry;

    if (isIntersecting) {
      console.log(`${target.id} position:`, {
        x: boundingClientRect.left,
        y: boundingClientRect.top,
        width: boundingClientRect.width,
        height: boundingClientRect.height,
      });
    }
  });
});

document.querySelectorAll(".tracked-elements").forEach((el) => {
  observer.observe(el);
});

Custom root container

const container = document.querySelector("#scroll-container");

const observer = new PositionObserver(
  (entries) => {
    entries.forEach((entry) => {
      console.log("Position within container:", entry.intersectionRect);
    });
  },
  { root: container }
);

observer.observe(document.querySelector("#item-in-container"));

Limitations

Visibility within the root

PositionObserver only triggers callbacks for elements that intersect with the root container. Position changes that occur while an element is completely outside the root bounds will not be detected until the element re-enters the observable area.

Precision of small position changes

PositionObserver may not detect very small position changes (approximately 1 pixel or less) due to the threshold-based nature of the underlying IntersectionObserver API. The detection sensitivity is influenced by the relative size of the observed element and its root container.

Development

Clone the repository and install dependencies:

npm install

Project structure

position-observer/
├── src/                    # Source code
│   ├── PositionObserver.ts # Main PositionObserver class
│   ├── observers/          # Internal observer implementations
│   ├── utilities/          # Helper functions and utilities
│   └── index.ts           # Public API exports
├── tests/                  # Test suite
│   ├── fixtures.ts        # Test utilities and helpers
│   ├── assertions.ts      # Custom test assertions
│   └── test.spec.ts       # Test specifications
├── playground/            # Interactive development environment
├── dist/                  # Built output (generated)
└── package.json          # Project configuration

Available scripts

Building

# Build for production
npm run build

# Build in watch mode for development
npm run dev

Development

# Start both build watcher and playground
npm start

# Start only the playground development server
npm run dev:playground

# Lint and fix code style
npm run lint

Testing

The test suite uses Playwright for cross-browser testing and includes comprehensive coverage of PositionObserver functionality.

Running tests

# Run all tests across all browsers
npm test

# Run tests with interactive UI
npm run test:ui

Acknowledgements

  • @samthor for earlier exploration of position observation techniques using IntersectionObserver: https://samthor.au/2021/observing-dom/
  • @runjuu for transferring ownership of the position-observer package name on npm.

License

MIT License, refer to LICENSE.md