Socket
Book a DemoInstallSign in
Socket

@webcomponents/custom-elements

Package Overview
Dependencies
Maintainers
4
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@webcomponents/custom-elements

HTML Custom Elements Polyfill

1.6.0
latest
Source
npmnpm
Version published
Weekly downloads
198K
3.74%
Maintainers
4
Weekly downloads
 
Created

What is @webcomponents/custom-elements?

@webcomponents/custom-elements is a polyfill for the Custom Elements v1 specification. It allows developers to define new HTML elements in a framework-agnostic way, enabling the creation of reusable and encapsulated components.

What are @webcomponents/custom-elements's main functionalities?

Defining a Custom Element

This feature allows you to define a new custom HTML element by extending the HTMLElement class. The custom element can then be used in HTML just like any standard element.

class MyElement extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.innerHTML = '<p>Hello, World!</p>';
  }
}
customElements.define('my-element', MyElement);

Using Lifecycle Callbacks

This feature allows you to use lifecycle callbacks such as connectedCallback and disconnectedCallback to run code when the element is added or removed from the DOM.

class MyElement extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
  }
  connectedCallback() {
    this.shadowRoot.innerHTML = '<p>Element added to the DOM</p>';
  }
  disconnectedCallback() {
    console.log('Element removed from the DOM');
  }
}
customElements.define('my-element', MyElement);

Attribute Change Handling

This feature allows you to handle changes to the element's attributes using the attributeChangedCallback method. You can specify which attributes to observe using the static observedAttributes getter.

class MyElement extends HTMLElement {
  static get observedAttributes() { return ['data-value']; }
  attributeChangedCallback(name, oldValue, newValue) {
    if (name === 'data-value') {
      this.shadowRoot.innerHTML = `<p>Value: ${newValue}</p>`;
    }
  }
}
customElements.define('my-element', MyElement);

Other packages similar to @webcomponents/custom-elements

Keywords

custom-elements

FAQs

Package last updated on 30 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.