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

children-changed-callback

v1.1.0

Published

Mixin to provide subclasses of HTMLElement with .childrenChangedCallback(newList, oldList) and .visibleChildren().

Downloads

8

Readme

.childrenChangedCallback

This mixin adds two methods to an HTMLElement

  • .childrenChangedCallback(newVisibleChildren, oldVisibleChildren)
  • .getVisibleChildren()

You use it as follows:

import {ChildrenChangedMixin} from "https://unpkg.com/children-changed-callback/src/ChildrenChangedMixin.js";

class MyWebComponent extends ChildrenChangedMixin(HTMLElement) {
                                               
  constructor(){
    super();
    const myVisibleChildren = this.getVisibleChildren(); //this can be called even when not connected
  }
  
  childrenChangedCallback(newChildren, oldChildren) {
    //this method is called everytime a visible child changes
    //but only while the instance of MyWebComponent is connected to the DOM.
  }
  
  
}
customElements.define("my-web-component", MyWebComponent);
const el = new MyWebComponent();
el.appendChild(document.createElement("div")); //.childrenChangedCallback is NOT triggered since el is not connected to DOM.
document.querySelector("body").appendChild(el);//.childrenChangedCallback is triggered when el gets connected to DOM.
el.appendChild(document.createElement("div")); //.childrenChangedCallback is triggered while el is connected and childList changes.

"visibleChildren" of HTMLElements

Using <slot> is a way to declaratively in the HTML arrange html components at composition time of multiple components, as opposed to at creation time of each and every component. But, outside of HTML, a slotted child and a normal child should in most cases be treated equally. A slotted child is primarily "just a child": it looks that way on screen, and should act that way in the DOM js environment too. Only very rarely, and more often in a platform context than an app context, does a developer need to know if the change of a visible child was slotted, as opposed to a direct change of the DOM.

Thus, visibleChildren of an HTMLElement are both normal .children and "slotted children" (.assignedNodes()). The visibleChildren is a list of the element's children where the slot elements has been replaced with their .assignedNodes(). In most cases where we talk about "children" we are thinking about these "visible children" and not just the "normal children". (cf. Web components gold standard on content assignment).

when visibleChildren changes

Currently, the platform provides observation of

  • "normal children" changes through MutationObserver(...).observe({childList: true})
  • "slotted children" changes through the slotchange Event.

However, in most cases, when we say "children changed" we refer to "visible children changed". We need to observe changes of both "normal children" and "slotted children". To observe such "visible children" changes, one must combine the two methods. This is what ChildrenChangedMixin is doing. Another approach would be to extend MutationObserver to provide something like a "visibleChildList" option that would react to any changes of the "visible children".

ref:

  • https://github.com/webcomponents/gold-standard/wiki/Content-Changes
  • https://github.com/webcomponents/gold-standard/wiki/Content-Assignment
  • https://github.com/webcomponents/gold-standard/wiki/Detachment
  • https://www.polymer-project.org/2.0/docs/devguide/shadow-dom#observe-nodes
  • https://www.polymer-project.org/2.0/docs/api/classes/Polymer.FlattenedNodesObserver

Some considerations about slotchange

  1. Use-case: observe slotchanges of children in the shadowDOM. this.shadowRoot.addEventListener("slotchange", ()=> doYourThing())));

  2. Requirement: Actions responding to slotchange events are queued in the event loop? Actions responding to MutationObserver( func ).observe({childList: true}) are queued in the microtask que? Should they not be queued in the same que?