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

web_ui_components

v1.0.8

Published

A Web Component is a way to create an encapsulated, single-responsibility code block that can be reused on any page.

Downloads

22

Readme

Web Components

A Web Component is a way to create an encapsulated, single-responsibility code block that can be reused on any page.

Lifecycle Methods

The browser automatically calls six methods throughout the lifecycle of the Web Component state.

  1. constructor(): It’s called when the component is first initialized. It must call super() and can set any defaults or perform other pre-rendering processes.

super(): make sure that the component inherits the correct prototype chain and all properties and methods of the class it extends

  1. connectedCallback(): When element is added to the DOM, the connectedCallback method is triggered. From that moment we can be sure that its available in the DOM and we’re able to safely set attributes, fetch resources, run setup code or render templates.

connectedCallback() { ... }

  1. disconnectedCallback() This lifecycle hook is triggered when the element is removed from the DOM and is the ideal place to add cleanup logic.

disconnectedCallback() { ... }

  1. static observedAttributes(): Returns an array of attributes that the browser will observe.

static get observedAttributes() { return ['prop1', 'prop2', 'prop3']; }

  1. attributeChangedCallback(attrName, oldVal, newVal)

Called whenever an observed attribute is changed. Those defined in HTML are passed immediately, but JavaScript can modify them:

attributeChangedCallback(name, oldValue, newValue) { console.log(${'prop1'}' value has been changed from ${oldValue} to ${newValue}); }

  1. adoptedCallback()

This function is called when a Web Component is moved from one document to another.

In general, this will only occur when dealing with elements where each iframe has its own DOM, but when it happens the adoptedCallback lifecycle hook is triggered. We can use it to interact with the owner document, the main document or other elements.

adoptedCallback() { ... }

How Web Components Interact With Other Elements

Web Components offer some unique functionality you won’t find in JavaScript frameworks.

  1. The Shadow DOM

While the Web Component we’ve built above works, it’s not immune to outside interference, and CSS or JavaScript could modify it. Similarly, the styles you define for your component could leak out and affect others.

The Shadow DOM solves this encapsulation problem by attaching a separated DOM to the Web Component with:

const shadow = this.attachShadow({ mode: 'closed/open' });

  1. HTML Templates

Defining HTML inside a script can become impractical for more complex Web Components. A template allows you to define a chunk of HTML in your page that your Web Component can use.

This has several benefits:

  • You can tweak HTML code without having to rewrite strings inside your JavaScript.

  • Components can be customized without having to create separate JavaScript classes for each type.

  • It’s easier to define HTML in HTML — and it can be modified on the server or client before the component renders.

Templates are defined in a tag, and it’s practical to assign an ID so you can reference it within the component class. This example three paragraphs to display the “Hello” message:

The Web Component class can access this template, get its content, and clone the elements to ensure you’re creating a unique DOM fragment everywhere it’s used:

const template = document.getElementById('hello-world').content.cloneNode(true);

The DOM can be modified and added directly to the Shadow DOM:

connectedCallback() { const shadow = this.attachShadow({ mode: 'closed' }) const template = document.getElementById('hello-world').content.cloneNode(true) shadow.append( template ); }

Template Slots

Slots allow you to customize a template.

In addition, you cannot directly style the inserted elements, although you can target specific slots within your Web Component:

Shadow DOM Events

Your Web Component can attach events to any element in the Shadow DOM just like you would in the page DOM, such as to listen for click events on all inner children:

shadow.addEventListener('click', e => {

// do something

});


Few more important Concepts:

getAttribute('text') hasAttribute('text') setAttribute('text') is="confirm-link" :host(.wrapper-container) :host-context(p .wrapper-container) ::slotted(.heighlight)


Series of Medium articles

Showcase Your Medium Articles with Web Components: Part 1 — Basics.

Showcase Your Medium Articles with Web Components: Part 2 — Data Flow.

Showcase Your Medium Articles with Web Components: Part 3 — Webpack.

Demo

Live Demo