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 🙏

© 2024 – Pkg Stats / Ryan Hefner

computed-style-observer

v1.0.0

Published

This prototype `ComputedStyleObserver` provides the ability to watch for changes being made to the style properties of DOM elements.

Downloads

4

Readme

A Computed Style Observer

This prototype ComputedStyleObserver provides the ability to watch for changes being made to the style properties of DOM elements.

Example

<style>
  #test {
    padding: 1em;
    background-color: yellow;
  }
  #test:hover {
    background-color: red;
  }
</style>

<div id="test">
  Hover over me and watch the console
</div>

<!-- load the script -->
<script src="dist/computedStyleObserver.min.js"></script>

<script>
  // Setup a callback
  const callback = entries => {
    entries.forEach(entry => {
      console.log(`Property '${entry.property}' changed from '${entry.previousValue}' to '${entry.value}'`);
    });
  }

  // create the observer
  let computedStyleObserver = new ComputedStyleObserver(callback, ['background-color']);

  // observe an element
  computedStyleObserver.observe(document.getElementById('test'));
</script>

:warning: A note on performance

Internally, ComputedStyleObserver calls getComputedStyle for each observed element, every animation frame. Since getComputedStyle will trigger a style recalc (and sometimes reflows), performance will suffer as the number of observed elements increases.


ComputedStyleObserver

Constructor

ComputedStyleObserver(callback, properties)

Creates and returns a new ComputedStyleObserver which will invoke a specified callback function when style changes for the given properties occur.

The callback function will receive an array of ComputedStyleObserverEntry objects, one for each property change that occured.

properties is an array of strings specifying which CSS property names should be observed. (i.e. "background-color", "border-radius" etc.)

Methods

disconnect()

Prevents the ComputedStyleObserver instance from receiving further notifications until observe() is called again.

observe(targetElement)

Configures the ComputedStyleObserver instance to begin receiving notifications for the specificed targetElement through its callback function when style changes occur.

unobserve(targetElement)

Prevents the ComputedStyleObserver instance from receiving further notifications for the specificed targetElement.

ComputedStyleObserverEntry

A ComputedStyleObserverEntry represents an individual style mutation. It is the object that is passed to ComputedStyleObserver's callback.

Properties

target

The DOM node the mutation affected

property

The style property that mutated

value

The current value of the property

previousValue

The previous value of the property


Contributing

Requirements

  • Node / NPM

Setup

  1. Clone this repo.
  2. Install dependencies: npm install
  3. Build the project with the watch task: npm run dev
  4. Start editing...

Other build options

  • npm run dist - builds the both the unminified and minified distribution files to the /dist/ folder.