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

@somehow-digital/web-component-loader

v0.0.1-rc.9

Published

Simple web component loader for loading web components lazily.

Downloads

3

Readme

Web Component Loader

Utility for lazy-loading web components.

Web Component Loader uses the IntersectionObserver API to defer loading components until they enter the viewport. It also uses the MutationObserver API to observe the DOM for newly inserted components. This allows to dynamically insert components into the DOM and have them loaded automatically.

  • 📦 small: 1.5kb minified
  • 🚀 fast: Uses IntersectionObserver to defer loading components
  • 🧩 flexible: Provides a class and a function for loading components
  • 🎨 customizable: Define various options for loading components

Setup

npm install @somehow-digital/web-component-loader

Usage

Web Component Loader provides a class and a function for loading components. The utility function is a wrapper around the class. The class is more flexible, but the function is more convenient.

Class

The class is a wrapper around the customElements.define function. It allows you to define components and then run the loader. This is useful if you want to define components in one place and then run the loader in another.

import Loader from '@somehow-digital/web-component-loader';

const loader = new Loader(/* options */);
loader.register('component-one', () => import('component-one.js'));
loader.register('component-two', () => import('component-two.js'), /* options */);

Function

The function returns a function that can be called to run the loader. This is useful to run the loader independently of the component definitions.

import { register } from '@somehow-digital/web-component-loader';

register({
  'component-one': () => import('component-one.js'),
  'component-two': [() => import('component-two.js'), /* options */],
})(/* options */);

Options

Options can be passed to the register function or the Loader class. Options can be set globally or some can be set per component definition. See file loader.ts for the default values.

| Option | Type | Default | Global | Element | Description | |--------------|---------------|------------|:------:|:-------:|---------------------------------------------------------------| | context | HTMLElement | document | ✅ | | The DOM context to search for components in. | | init | boolean | true | ✅ | | Whether to run the loader immediately. | | contextual | boolean | true | ✅ | ✅ | Whether to load components found in the DOM context. | | defer | boolean | true | ✅ | ✅ | Whether to defer loading components on entering the viewport. | | observe | boolean | true | ✅ | | Whether to observe the DOM for newly inserted components. | | define | boolean | true | ✅ | ✅ | Whether to define components when loading them. | | margin | string | 0px | ✅ | | The margin used when loading deferred elements. | | selector | function | ... | ✅ | ✅ | Selector to use when searching for components. | | excludes | string[] | [...] | ✅ | | An array of element names to exclude from processing. |


somehow.digital