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

@dannymoerkerke/custom-element

v1.0.0

Published

Web Components base class which provides data binding and convenience methods

Downloads

20

Readme

Custom Element

A base class for Custom Elements which provides simple data binding.

This is a simple POC to demonstrate how data binding can be easily implemented using Web Components. It is not a replacement for React or other frameworks, nor is it intended to be.

See my article on medium.com for an in depth explanation.

How it works

Inside the Shadow Root of any Web Component that extends CustomElement data binding can be provided through the data-bind attribute on any HTML element, including other Web Components. Data binding is done through the properties of the state object inside CustomElement.

For example:

<p data-bind="title"></p>

means the value of the state.title property of CustomElement is bound to this <p>. If the value of state.title changes the textContent of this <p> will change with it. If the value of state.title needs to be bound to a property of another Web Component it should be prefixed with the name of that property and a :.

For example:

<web-component data-bind="name:title"></web-component>

means the property name of <web-component> is bound to state.title.

If <web-component> also extends CustomElement then its property state.name is bound to state.title, which allows for multiple levels of data binding. This is because internally the setState method of <web-component> will be invoked.

CustomElement also provides some convenience methods:

  • show(elements?: HTMLElement[]), show the element itself or multiple child elements when elements argument is given
  • hide(elements?: HTMLElement[]), hide the element itself or multiple child elements when elements argument is given
  • select(selector: string), select an element from the component's Shadow DOM (calls querySelector internally)
  • selectAll(selector: string), select multiple elements from the component's Shadow DOM (calls querySelectorAll internally)
  • css(elements: HTMLElement | HTMLElement[], styles: Object), set multiple styles on one element or an array of elements at once
  • addTemplate(element: HTMLElement, selector: string, replaceContents = false): add the contents of the <template> which is selected by selector to element. When replaceContents is true the existing contents of element will be replaced.

This only works for templates inside the shadowRoot of the component

Example:

const container = this.shadowRoot.querySelector('#container');

this.css(container, {
  display: 'flex'  
  paddingBottom: '10px',
  backgroundColor: '#ff0000'
});
  • multiSelect({property: selector}), selects an element in Shadow DOM and sets it to a property on the element

Example:

this.multiSelect({
  container: '#container',
  header: 'h1'
});

is equivalent to:

this.container = this.shadowRoot.querySelector('#container');
this.header = this.shadowRoot.querySelector('h1');
  • addTemplate(element: HTMLElement, selector: string, replaceContents?: boolean), adds the template selected by selector to the element, optionally replacing existing content so the added template will be the only child

Demo

To run the demo, run npm install once and then npm start and view the demo on http://localhost:8080/

Testing

Run npm test and view the results on http://localhost:8080/ or run npm run test:headless to run the tests on the command line.

This repo also contains the configuration file wallaby.js to run the tests from your IDE using Wallaby.js

Browser support

  • Chrome 53+
  • Firefox 63+
  • Safari 10+
  • iOS Safari 10+
  • Chrome Android 71+
  • Firefox Android 64+