Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ebidel/declarative-web-components

Author web components, declaratively
https://github.com/ebidel/declarative-web-components

Last synced: about 2 months ago
JSON representation

Author web components, declaratively

Awesome Lists containing this project

README

        

# Author web components, declaratively

Custom elements and shadow DOM should be declarative. Unfortunately, both APIs
are JavaScript-based.

This repo shows how to define two custom elements, `` and `` which can be used to declaratively author a web component. Custom elements should be declarative

See [demo](demo/index.html) for full examples and live demos.

## Examples

**Example** - `` tag

```


Eric's


declarative shadow dom!



```

What this does:

- Creates Shadow DOM from a child `` and attaches the shadow root to the parent element. In this example, `

`.

**Example** - `` tag:

```html


content appended by element's author (in light dom)

```

What this does:

- Element does not use Shadow DOM
- Element does not register itself. The page should called `customElements.define('basic-element')`.

**Example** - `` with styles

```html



/* CSS selectors are prefixed/scoped b/c we're not using shadow dom. */
basic-element h2 {
color: blue
}

basic-element: some content should be blue


```

What this does:

- Element does not use Shadow DOM
- Element does not register itself. The page should called `customElements.define('basic-element-styles')`.
- Element defines styles for its light dom.

**Example** - `` that uses shadow dom and registers itself

```html




:host {
display: block;
color: red;
}
::slotted(span) {
color: blue;
}


click me (in shadow dom)






(class extends HTMLElement {
constructor() {
super();
this.addEventListener('click', this.sayHi);
}
sayHi() {
alert('Hi!');
}
})

```

What this does:

- Element uses Shadow DOM
- Element auto-registers itself when defining a script in ``.