All Projects → geocine → Custom Elements Ts

geocine / Custom Elements Ts

Create native custom elements using Typescript

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Custom Elements Ts

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 (-57.69%)
Mutual labels:  web-components, custom-elements, shadow-dom
Omi
Front End Cross-Frameworks Framework - 前端跨框架跨平台框架
Stars: ✭ 12,153 (+23271.15%)
Mutual labels:  web-components, custom-elements, shadow-dom
Nutmeg
Build, test, and publish vanilla Web Components with a little spice
Stars: ✭ 111 (+113.46%)
Mutual labels:  web-components, custom-elements, shadow-dom
grid-container
A grid for the future, CSS Grid Layout + Web Components (Custom Elements v1 + Shadow DOM v1)
Stars: ✭ 51 (-1.92%)
Mutual labels:  web-components, custom-elements, shadow-dom
Xy Ui
🎨面向未来的原生 web components UI组件库
Stars: ✭ 603 (+1059.62%)
Mutual labels:  web-components, custom-elements, shadow-dom
element
Fast and simple custom elements.
Stars: ✭ 65 (+25%)
Mutual labels:  web-components, custom-elements, shadow-dom
Shadow Dom In Depth
Everything you need to know about Shadow DOM
Stars: ✭ 191 (+267.31%)
Mutual labels:  web-components, custom-elements, shadow-dom
focus-trap
A lightweight web component that traps focus within a DOM node
Stars: ✭ 44 (-15.38%)
Mutual labels:  web-components, custom-elements, shadow-dom
Remount
Mount React components to the DOM using custom elements
Stars: ✭ 522 (+903.85%)
Mutual labels:  web-components, custom-elements, shadow-dom
Synergy
Synergy is a tiny runtime library for building web user interfaces
Stars: ✭ 296 (+469.23%)
Mutual labels:  web-components, custom-elements
Three Elements
Web Components-powered custom HTML elements for building Three.js-powered games and interactive experiences. 🎉
Stars: ✭ 331 (+536.54%)
Mutual labels:  web-components, custom-elements
Include Fragment Element
A client-side includes tag.
Stars: ✭ 380 (+630.77%)
Mutual labels:  web-components, custom-elements
Weightless
High-quality web components with a small footprint
Stars: ✭ 284 (+446.15%)
Mutual labels:  web-components, custom-elements
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 (+401.92%)
Mutual labels:  custom-elements, shadow-dom
Monogatari
Monogatari is a simple web visual novel engine, created to bring Visual Novels to the web.
Stars: ✭ 357 (+586.54%)
Mutual labels:  web-components, custom-elements
anywhere-webcomponents
A UI work in progress based on custom elements (web components) for use in anywhere.
Stars: ✭ 17 (-67.31%)
Mutual labels:  web-components, custom-elements
Details Menu Element
A menu opened with <details>.
Stars: ✭ 455 (+775%)
Mutual labels:  web-components, custom-elements
Vanilla Colorful
A tiny color picker custom element for modern web apps (2.7 KB) 🎨
Stars: ✭ 467 (+798.08%)
Mutual labels:  web-components, custom-elements
storify
Instagram/Whatsapp stories clone built on Web Components and Web Animations API. 🔥
Stars: ✭ 64 (+23.08%)
Mutual labels:  web-components, custom-elements
Lume
Create CSS3D/WebGL applications declaratively with HTML. Give regular DOM elements shadow and lighting.
Stars: ✭ 445 (+755.77%)
Mutual labels:  web-components, custom-elements

custom-elements-ts

Coverage Status Build Status npm version License: MIT

Create native custom elements using Typescript without using any third party libraries.

npm install custom-elements-ts

Usage

import { CustomElement } from 'custom-elements-ts';

@CustomElement({
  tag: 'counter-element',
  templateUrl: 'counter-element.html',
  styleUrl: 'counter-element.scss'
})
export class CounterElement extends HTMLElement {
  // code as you would when creating a native HTMLElement
  // full source code is at demo/counter
}
<!--index.html-->
<counter-element></counter-element>
<script src="counter.umd.js"></script>

Decorators

Decorator Target Parameters Description
@Prop() property - custom attribute/properties, reflects primitive properties to attributes
@Toggle() property - boolean attribute/properties, it is based on the presence of the attribute but also works with "true" and "false"
@Dispatch() property (event?) used to declare a CustomEvent which you could dispatch using the .emit method of its type DispatchEmitter. The event parameter is used to set the name of the CustomEvent
@Watch() method (property) triggers the method when a property is changed
@Listen() method (event, selector?) listens to an event on the host element or on the selector if specified

@Prop()

import { CustomElement, Prop } from 'custom-elements-ts';

@CustomElement({
  tag: 'todo-list',
  ...
})
export class TodoList extends HTMLElement {
  @Prop() color: string;
  @Prop() list: TodoItem[];
}

Since color is a primitive type of string it can be accessed via attributes and properties

const element = document.querySelector('todo-list');
// accessing value via attribute
const attrValue = element.getAttribute('color');
// setting value via attribute
element.setAttribute('color', 'red');
 
// accessing value via property
const propertyValue = element.color;
// setting via property
element.color = 'red';

On the other hand list is a rich data type (objects or arrays) can only be accessed/set via property

@Toggle()

Toggle attributes work the same way as HTML boolean attributes as defined by W3C for the most part. We changed a few things to overcome confusion. Check the table below for reference:

Markup disabled Description
<c-input /> false Follows W3C standard
<c-input disabled/> true Follows W3C standard
<c-input disabled="true"/> true Follows W3C standard
<c-input disabled="asd"/> false false since asd does not evaluate to a valid boolean
<c-input disabled="false"/> false false since the boolean false converted to a string is "false"
<c-input disabled="true"/> true true since the boolean true converted to a string is "true"

@Dispatch()

Creating a custom event

import { CustomElement, Dispatch, DispatchEmitter } from 'custom-elements-ts';

...
export class TodoList extends HTMLElement {
  // Creating a CustomEvent
  // custom event name will be `on.change`
  @Dispatch() onChange: DispatchEmitter;

  // Creating a CustomEvent with custom name `ce.select` 
  @Dispatch('ce.select') onSelect: DispatchEmitter;
}

Triggering the custom event from the example above:

  triggerOnChange() {
    // adding more data to the event object
    this.onChange.emit({detail: 'event changed'});
    this.onSelect.emit({detail: 'select triggered'});
  }

@Watch()

import { CustomElement, Dispatch, Prop } from 'custom-elements-ts';

...
export class TodoList extends HTMLElement {
  @Prop() color: string;

  @Watch('color')
  colorChanged() {
    // trigger when color property color changes
    // either via property or attribute
  }
}

@Listen()

Listen has parameters event and selector. Event is any valid javascript event. Selector is anything that works with querySelector()

import { CustomElement, Dispatch, Prop } from 'custom-elements-ts';

...
export class TodoList extends HTMLElement {
  @Listen('click')
  elementClicked() {
    // triggers when the element is clicked
  }

  @Listen('click','a')
  anchorClicked() {
    // triggers when an `a` inside the element is clicked
  }
}

Setup

Running the demos

npm start <element-name>

Building the demo

npm run build <element-name>

If you want to create a minified bundle

npm run build -- <element-name> --prod
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].