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

shadow-dom-crawler

v1.2.4

Published

A zero-dependency script for crawling deeply nested shadow DOMs

Downloads

14

Readme

Shadow DOM Crawler

This is a zero-dependency script for crawling deeply nested shadow DOMs. This can only crawl shadow structures created with { mode: 'open' } because "closed" structures are inaccessable to JavaScript.

npm install shadow-dom-crawler
yarn add shadow-dom-crawler

Use cases

This library was developed for a very limited set of use cases and should NOT be used to violate the intentions of component developers. Please remember that components use a shadow DOM in order to isolate themselves from the outside world. DO NOT interfere with, modify, or otherwise disrupt the DOMs in 3rd party components. With that said, here are some of the reasons why I built this script:

  • bring focus to a particular element within a modal/popover for ARIA compliance
    • https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/dialog_role
    • https://www.w3.org/TR/wai-aria-practices/examples/dialog-modal/dialog.html
  • inject shared styles inside of components to reduce duplicity and bundle sizes
    • NOTE: this is a controversial use case. Components should ship with everything they need to work. However, when building an application I believe it's both ethical and sane to allow a grandmother component inject a set of shared utility styles into known grandchild components.
  • if you have other use cases, please file an issue so I can list them here.

What NOT to do

  • modify the DOM inside a shadow root
  • share any type of application state between components
  • set properties or attributes on nested components
  • pretty much anything else which is not listed in the use cases above... seriously, don't do it.

API

findNode(selector: string, startNode?: Node | Node[] | NodeList)

This method is analagous to Element.prototype.querySelector, but works with open shadow DOMs.

Given a valid CSS selector string, will start crawling the document in search of a matching element and will stop crawling as soon as one is found. If no item is found, this will return null. By default this will start crawling at the document.body. You can pass an optional second parameter of Node | Node[] | NodeList from where to start crawling:

import { findNode } from 'shadow-dom-crawler';

const node = findNode('.some-valid-css-selector');

const root = document.getElementById('#root');
const node = findNode('.some .nested .css-selector', root);

const items = document.querySelector('li.items');
const node = findNode('.some complex-css[ selector ]', items);

Roadmap

  • findAll('selector', startNode?) - Find all nodes matching a selector.
  • injectStyles('selector' | node(s), styles, startNode?) - will find all elements matching the selector and inject styles into those elements. One or more elements can optionally be passed in order to skip the crawling step. This will be useful for injecting shared styles into shadow elements.

Testing

This uses @stencil/core for testing as it provides convenient wrappers around jest for testing web components. Stencil components are also compiled to standards-compliant components which makes it convenient for creating and nesting multiple components used for testing.

yarn test