Coder Social home page Coder Social logo

ivmarcos / custom-element Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dannymoerkerke/custom-element

0.0 0.0 0.0 104 KB

A base class for Web Components (Custom Elements) which provides simple data binding.

License: Other

JavaScript 93.99% HTML 6.01%

custom-element's Introduction

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+

custom-element's People

Contributors

dannymoerkerke avatar stefannieuwenhuis avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.