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

dom-slot-assign

v0.1.3

Published

A polyfill for imperative slot assignment

Downloads

341

Readme

DOM Slot Assign

npm version bundle size npm usage

DOM Slot Assign is a polyfill that allows you to use imperative slot assignment, following the HTML specification.

Usage

Include DOM Slot Assign in your project.

import "https://unpkg.com/[email protected]"
<script src="https://unpkg.com/[email protected]"></script>

If you're ok with slightly reduced browser support, you can include it conditionally, only when needed:

if (!globalThis?.HTMLSlotElement?.prototype.assign) {
  await import("https://unpkg.com/[email protected]");
}

You can also use npm:

// npm install dom-slot-assign
import "dom-slot-assign"

That’s it. Now use imperative slotting in your project.

Example

This example creates a <content-tabs> element. From its children, any <h1> - <h6> element generates a new <details> / <summary> in its shadow dom, with the heading slotted into the summary. Any content after that heading and before the next heading is added as content to the current details.

customElements.define('content-tabs', class extends HTMLElement {
  constructor() {
    let host = super()
    let root = host.attachShadow({ mode: 'open', slotAssignment: 'manual' })

    root.innerHTML += '<style>::slotted(:is(h1, h2, h3, h4, h5, h6)) { display: contents; pointer-events: none; }</style>'

    if (host.childNodes.length) host.contentChangedCallback()

    new MutationObserver(() => host.contentChangedCallback()).observe(host, { childList: true })
  }

  contentChangedCallback() {
    let root = this.shadowRoot
    let [ style ] = root.childNodes
    let details, summary, content

    // clear the shadow root
    root.replaceChildren(style)

    // for each slottable child
    for (let node of this.childNodes) {
      // ignore leading text nodes
      if (!details && node.nodeType !== 1) continue

      // create new summary details with headings
      if (node instanceof HTMLHeadingElement) {
        details = root.appendChild(document.createElement('details'))
        summary = details.appendChild(document.createElement('summary'))
        summary.appendChild(document.createElement('slot')).assign(node)
        content = undefined
      } else {
        // put adjacent comments in summary details below headings
        content = content || details.appendChild(document.createElement('slot'))
        content.assign(...content.assignedNodes(), node)
      }
    }
  }
})

Open this example on CodePen

How the polyfill works

When the <slot> element lacks an assign() method, the polyfill is activated.

The polyfilled assign() method

An assign() method works by detecting whether the given slot belongs to a shadow root with manual slot assignment If it does, it assigns a unique name to the given slot, which is never actually used. Then, each assigned Element node has its slot set a separate, private slot, which is then appended to the given slot.

<#shadow-root>
  <!-- public slot created by the user -->
  <slot name="">
    <slot style="display:contents!important" name="">
      <!-- private slot for the first element -->
    </slot>
    <slot style="display:contents!important" name="">
      <!-- private slot for the second element -->
    </slot>
    <slot style="display:contents!important" name="">
      <!-- private slot for the N element -->
    </slot>
  </slot>
</#shadow-root>

Assigned Text nodes are cloned and each clone is placed inside a private slot, which is also appended to the given slot. The original text nodes are observed for DOMCharacterDataModified and DOMNodeRemoved to update or remove the clone.

<#shadow-root>
  <!-- public slot created by the user -->
  <slot name="">
    <slot style="display:contents!important" name="">
      <!-- private slot for the first element -->
    </slot>
    <slot style="display:contents!important" name="">
      <!-- private slot for the second element -->
    </slot>
    <slot style="display:contents!important" name="">
      A cloned text node.
    </slot>
    <slot style="display:contents!important" name="">
      <!-- private slot for the N element -->
    </slot>
  </slot>
</#shadow-root>

The polyfilled assignedNodes() method

A new assignedNodes() method returns an array of all nodes privately assigned to the slot that are still connected.

The polyfilled assignedElements() method

A new assignedElements() method returns an array of all elements privately assigned to the slot that are still connected.

The polyfilled slotAssignment property

An overriding attachShadow() method detects the slotAssignment: "manual" option and enables slot elements within the given shadow root to support the assign method. To prevent any accidental ‘named’ slot assignments, the shadow root is observed for slotchange to enforce the manual assignment.