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

@open-wc/scoped-elements

v3.0.5

Published

Allows to auto define custom elements scoping them

Downloads

413,904

Readme

Development >> Scoped Elements ||40

Installation

npm i --save @open-wc/scoped-elements

This package requires using the Scoped Custom Element Registry polyfill.

Usage

@open-wc/scoped-elements supports both vanilla HTMLElement, as well as LitElement (both [email protected] and [email protected] are supported) based components. You can use the mixin as follows:

HTMLElement

import { ScopedElementsMixin } from '@open-wc/scoped-elements/html-element.js';
import { MyButton } from './MyButton.js';

class MyElement extends ScopedElementsMixin(HTMLElement) {
  static scopedElements = {
    'my-button': MyButton,
  };

  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
  }

  connectedCallback() {
    this.shadowRoot.innerHTML = '<my-button>click</my-button>';
  }
}

LitElement

import { ScopedElementsMixin } from '@open-wc/scoped-elements/lit-element.js';
import { LitElement, html } from 'lit';
import { MyButton } from './MyButton.js';

class MyElement extends ScopedElementsMixin(LitElement) {
  static scopedElements = {
    'my-button': MyButton,
  };

  render() {
    return html`<my-button>click</my-button>`;
  }
}

Polyfill

This package requires use of the Scoped Custom Element Registry polyfill. Make sure to load it as the first thing in your application:

import '@webcomponents/scoped-custom-element-registry';

If you're using @web/rollup-plugin-polyfills-loader, you can use it in your rollup config like this:

polyfillsLoader({
  polyfills: {
    scopedCustomElementRegistry: true,
  },
});

If you're using @web/dev-server for local development, you can use the @web/dev-server-polyfill plugin:

polyfill({
  scopedCustomElementRegistry: true,
});

API

Lazy scoped element definition

If you're lazily importing custom elements, you can define them by accessing the this.registry directly on your element as per spec behavior:

onClick() {
  import('./LazyElement.js').then(m => {
    this.registry.define('lazy-element', m.LazyElement);
  });
}

Imperative scoped element creation

If you need to imperatively create elements that have been scoped via the ScopedElementsMixin, you can use this.shadowRoot.createElement as per spec behavior:

class MyElement extends ScopedElementsMixin(HTMLElement) {
  static scopedElements = {
    'foo-element': FooElement,
  };

  onClick() {
    const el = this.shadowRoot.createElement('foo-element');
    this.shadowRoot.appendChild(el);
  }
}

Scope level

By default, elements are scoped on the constructor level for performance reasons. For most usecase this should be fine. However, for some usecases, like for example when component registrations are provided from an external source, it can be useful to scope on the instance level instead. To achieve this, you can override the registry getter/setter pair like this:

class UserFlowFramework extends ScopedElementsMixin(LitElement) {
  set registry(r) {
    this.__registry = r;
  }

  get registry() {
    return this.__registry;
  }
}

Notes

When using @open-wc/scoped-elements, its important that the modules containing your custom element classes are side effect free, and don't call customElements.define themself. The consumer of your custom elements is responsible for registering them via the ScopedElementsMixin.

This means you should avoid code like:

class MyElement extends HTMLElement {}
// ❌
customElements.define('my-element', MyElement);

You can, instead, consider splitting up the export of your component class and the registration of your component, or don't export a self-registering module at all:

// ✅
export class MyElement extends HTMLElement {}

You should also avoid using the @customElement decorator, because it calls customElements.define internally:

// ❌
@customElement('my-element')
export class MyElement extends LitElement {}

Motivation

In large applications, it can be the case that you need to support multiple versions of a component on the same page, like for example design system components.

Consider the following example:

<my-app>
  <feature-a>
    #shadowroot
    <!-- uses [email protected] -->
    <my-button>click</my-button>
  </feature-a>
  <feature-b>
    #shadowroot
    <!-- uses [email protected] -->
    <my-button>click</my-button>
  </feature-b>
</my-app>

If you're using the global customElements registry, you would have run into name clashes, because my-button would have already been defined in the global registry. Using scoped custom element registries, we can assign a registry per shadowroot, and scope our custom elements to those registries instead.

In this case, if feature-a and feature-b use ScopedElementsMixin, the mixin will create a separate registry for each of their shadowroots so that the elements used internally by feature-a and feature-b will be scoped to that registry, rather than the global registry, and avoid nameclashes.