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

@browserkids/dom

v0.6.0

Published

Non-transpiled ES6+ DOM helper functions.

Downloads

12

Readme

Installation

Using a CDN

Fastest way to get going. See for yourself:

<script type="module">
import { isElementInViewport } from 'https://unpkg.com/@browserkids/dom';

console.log(isElementInViewport(document.body))
</script>

Self hosted

Semi-fast way. Download the files and upload them to your server. Just make sure your import path is correct.

import { isElementInViewport } from './assets/@browserkids/dom/index.js';

Using a bundler

Semi-fast way as well. Just install it via npm.

npm install -S @browserkids/dom

Import the functions where you need them.

import { isElementInViewport } from '@browserkids/dom';

Single import

Just want to import a single function?

<script type="module">
import isElementInViewport from 'https://unpkg.com/@browserkids/dom/isElementInViewport';

console.log(isElementInViewport(document.body))
</script>

:warning: Some functions depend on other @browserkids/dom functions. See API chapter for details.

<script type="module">
import findAttributes from 'https://unpkg.com/@browserkids/dom/findAttributes';
import findReferences from 'https://unpkg.com/@browserkids/dom/findReferences';

// Provide necessary functions via settings object.
console.log(findReferences(document.body, { findAttributes }))
</script>

Browser support

Almost every function uses at least one feature of ECMAScript 9 or above, but no ESNext features — promised. So support should be fine for “evergreen” browsers at the time of writing. This means that Internet Explorer is out of the game.

As this library is not transpiled nor ever will be you should use polyfills in case you need to support a specific browser version.

API

  1. createShadowRoot($el, template = '', settings = {})
    Creates a Shadow DOM for this element and uses the given template as content.

    By default this creates an open Shadow DOM. A very simple example on how to use this would be:

    <my-custom-element>rock!</my-custom-element>
       
    <script type="module">
      import { createShadowRoot } from 'https://unpkg.com/@browserkids/dom';
       
      customElements.define('my-custom-element', class MyCustomElement extends HTMLElement {
        constructor() {
          super();
               
          createShadowRoot(this, `
            <style>
              :host::before {
                content: 'My scoped styles…';
              }
            </style>
               
            <slot></slot>
          `);
        }
      });
    </script>
  2. dispatch($el, name, detail, settings = {})
    Triggers a custom event with the given data.

    This function has some sensible defaults like bubbling enabled or no trespassing of the event between the boundary of shadow and regular DOM.

  3. findAttributes($el, name))
    Tries to find attributes that name is matching a given regular expression.

    <main #container class="random-class">Just a random element.</main>
      
    <script type="module">
      import { findAttributes } from 'https://unpkg.com/@browserkids/dom';
      
      // [{ name: '#container', value: '' }]
      console.log(findAttributes(document.querySelector('main'), /^#.+/));
    </script>
  4. findReferences($el, settings = {})
    Finds all elements within a given element that have #referenceId (by default) attributes.

    :warning: If you single-import this function, make sure to provide a findAttributes() function.

    <body>
      <header #header></header>
      <main #content>
        <h1 #headline></h1>
      </main>
      <footer #footer></footer>
    </body>
       
    <script type="module">
      import { findReferences } from 'https://unpkg.com/@browserkids/dom';
       
      // { header: header, headline: h1, content: main, footer: footer }
      console.log(findReferences(document.body));
    </script>

    Available settings:

    • pattern (default: /^#(?<id>.+)/), adjust the RegEx pattern for finding references.
    • cleanUp (default: true), remove attributes after finding reference.
    • findAttributes, function for finding attributes. This is only mandatory if you single-import this function.
  5. isElementInViewport($el, settings = {})
    Returns true if the given element is within the boundaries of the given viewport coordinates or at least the amount specified.

    You may adjust the following settings:

    • amount, specify minimum amount of overlapping/intersection between target element and viewport.
    • viewport, provide a custom viewport bounding rectangle. Default is window rectangle.
  6. bindEventListeners($el, scope = $el, settings = {})
    Finds all elements that have @event[.modifier]="function" (by default) attributes and automatically registers the event listeners to the elements.

    :warning: If you single-import this function, make sure to provide a findAttributes() function.

    <main @click="onClick">Just a random element.</main>
       
    <script type="module">
      import { bindEventListeners } from 'https://unpkg.com/@browserkids/dom';
       
      bindEventListeners(document.body, {
        onClick() {
          console.log('I just got clicked.');
        }
      });
    </script>

    There are a few modifiers available:

    • prevent calls e.preventDefault() before running the event listener.
    • self, calls only when event.target === event.currentTarget.
    • away, calls only when the event.target is not a child of event listener element.
    • once, calls the event listener only once.
    • window, attaches the event listener to the window object.
    • document, attaches the event listener to the document object.

    Available settings:

    • pattern (default: /^@(?<event>[^.]+).?(?<modifier>.+)?/), adjust the RegEx pattern for finding event hooks.
    • cleanUp (default: true), remove attributes after finding reference.
    • findAttributes, function for finding attributes. This is only mandatory if you single-import this function.
  7. upgrade($el, template)

    All-in-one function that will run createShadowRoot, bindEventListeners, findReferences.

    :warning: If you single-import this function, make sure all necessary functions are known to the global scope.

    <my-custom-element></my-custom-element>
      
    <script type="module">
      import { upgrade } from 'https://unpkg.com/@browserkids/dom';
       
      customElements.define('my-custom-element', class MyCustomElement extends HTMLElement {
        constructor() {
          super();
               
          upgrade(this, '<button @click="onClick" #button>Hit me</button>');
    
          console.log(this.$refs);
        }
    
        onClick() {
          console.log('I was clicked.');
        }
      });
    </script>