All Projects → DannyMoerkerke → Custom Element

DannyMoerkerke / Custom Element

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Custom Element

Omi
Front End Cross-Frameworks Framework - 前端跨框架跨平台框架
Stars: ✭ 12,153 (+20155%)
Mutual labels:  webcomponents, custom-elements, shadow-dom
Switzerland
🇨🇭Switzerland takes a functional approach to Web Components by applying middleware to your components. Supports Redux, attribute mutations, CSS variables, React-esque setState/state, etc… out-of-the-box, along with Shadow DOM for style encapsulation and Custom Elements for interoperability.
Stars: ✭ 261 (+335%)
Mutual labels:  webcomponents, custom-elements, shadow-dom
Nutmeg
Build, test, and publish vanilla Web Components with a little spice
Stars: ✭ 111 (+85%)
Mutual labels:  webcomponents, custom-elements, shadow-dom
Hybrids
Extraordinary JavaScript framework with unique declarative and functional architecture
Stars: ✭ 2,529 (+4115%)
Mutual labels:  webcomponents, custom-elements, shadow-dom
material-webcomponents
Material Design implemented in Web Components (Custom Elements v1)
Stars: ✭ 110 (+83.33%)
Mutual labels:  webcomponents, custom-elements, shadow-dom
Dna
Progressive Web Components.
Stars: ✭ 22 (-63.33%)
Mutual labels:  webcomponents, custom-elements, shadow-dom
crab
JavaScript library for building user interfaces with Custom Elements, Shadow DOM and React like API
Stars: ✭ 22 (-63.33%)
Mutual labels:  webcomponents, custom-elements, shadow-dom
Crab
JavaScript library for building user interfaces with Custom Elements, Shadow DOM and React like API
Stars: ✭ 22 (-63.33%)
Mutual labels:  webcomponents, custom-elements, shadow-dom
Reactshadow
🔰 Utilise Shadow DOM in React with all the benefits of style encapsulation.
Stars: ✭ 948 (+1480%)
Mutual labels:  webcomponents, shadow-dom
custom-elements-manifest
Custom Elements Manifest is a file format that describes custom elements in your project.
Stars: ✭ 81 (+35%)
Mutual labels:  webcomponents, custom-elements
svelte-webcomponents
A ready-to-use project template to build custom elements (web components) with Svelte 3 with support and examples for web components, jest, sass, nested components with props, eslinting, stylelinting, Github actions, propagating custom events from shadow-DOM to real-DOM etc.
Stars: ✭ 22 (-63.33%)
Mutual labels:  custom-elements, shadow-dom
cwco
Powerful and Fast Web Component Library with a Simple API
Stars: ✭ 27 (-55%)
Mutual labels:  webcomponents, custom-elements
vite-plugin-sloth
Fresh take on static site generation, using HTML-first approach to build website. Powered by ⚡️ Vite
Stars: ✭ 39 (-35%)
Mutual labels:  webcomponents, custom-elements
vaadin-text-field
The themable Web Component providing input controls. Part of the Vaadin components.
Stars: ✭ 29 (-51.67%)
Mutual labels:  webcomponents, custom-elements
domy
Custom Elements Storage
Stars: ✭ 77 (+28.33%)
Mutual labels:  webcomponents, custom-elements
Snuggsi
snuggsi ツ - Easy Custom Elements in ~1kB
Stars: ✭ 288 (+380%)
Mutual labels:  webcomponents, custom-elements
highcharts-webcomponent
Highcharts Web Component usable with any Framework
Stars: ✭ 21 (-65%)
Mutual labels:  webcomponents, custom-elements
anywhere-webcomponents
A UI work in progress based on custom elements (web components) for use in anywhere.
Stars: ✭ 17 (-71.67%)
Mutual labels:  webcomponents, custom-elements
Vaadin Grid
vaadin-grid is a free, high quality data grid / data table Web Component. Part of the Vaadin components.
Stars: ✭ 383 (+538.33%)
Mutual labels:  webcomponents, custom-elements
Remount
Mount React components to the DOM using custom elements
Stars: ✭ 522 (+770%)
Mutual labels:  custom-elements, shadow-dom

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+
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].