Skip to content

feat: add Template Attributes polyfill #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ packages/webcomponentsjs/webcomponents-bundle.js*
packages/scoped-custom-element-registry/**/.wireit/
packages/scoped-custom-element-registry/**/.tsbuildinfo
packages/scoped-custom-element-registry/**/build/

packages/template-attributes/**/.wireit/
packages/template-attributes/**/.tsbuildinfo
packages/template-attributes/**/build/
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ packages/scoped-custom-element-registry/**/.wireit/
packages/scoped-custom-element-registry/**/.tsbuildinfo
packages/scoped-custom-element-registry/**/build/

packages/template-attributes/**/.wireit/
packages/template-attributes/**/.tsbuildinfo
packages/template-attributes/**/build/

LICENSE
packages/*/LICENSE.md
packages/tests/html-imports/html/imports/unclosed-import.html
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ The current version of the Web Components loader does **not** automatically
polyfill HTML Imports. Applications that still depend on HTML Imports are
recommended to install `@webcomponents/html-imports` and load it separately.

---

### [![Published on npm](https://img.shields.io/npm/v/@webcomponents/template-attributes.svg)](https://www.npmjs.com/package/@webcomponents/template-attributes) [template-attributes](https://github.com/webcomponents/polyfills/tree/master/packages/template-attributes)

##### [Documentation](https://github.com/webcomponents/polyfills/tree/master/packages/template-attributes#readme) | [Changelog](https://github.com/webcomponents/polyfills/blob/master/packages/template-attributes/CHANGELOG.md) | [Issues](https://github.com/webcomponents/polyfills/issues?q=is%3Aissue+is%3Aopen+label%3A%22Package%3A+template-attributes%22)

Polyfill for Template Attributes (Spec TBD)

## Roadmap

The following APIs are on the roadmap for 2020:
Expand Down
3 changes: 3 additions & 0 deletions packages/template-attributes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.wireit/
.tsbuildinfo
build/
11 changes: 11 additions & 0 deletions packages/template-attributes/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<!-- ## Unreleased -->
<!-- ### Added -->
<!-- ### Changed -->
<!-- ### Fixed -->
21 changes: 21 additions & 0 deletions packages/template-attributes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Template Attributes polyfill prototype

## 🚨 Work in progress

This polyfill explores implementation of a non-finalized spec proposal and is
currently a work in progress.

## Overview

Template Attributes polyfill based on the <https://github.com/Symbitic/webcomponents/blob/template-attribute/proposals/Template-Attribute.md> proposal.

Technique: modifies HTMLElement to patch `attachShadow()` to attach a mutation observer and setup a proxy on the `.templates` property. While the mutation observer isn't required and is strictly an implementation detail, it is considerably more performant than running `querySelectorAll("*")` every time the `templates` property is changed.

## Demo

Run two commands in different windows/tabs inside the `./packages/template-attributes` folder:

npx tsc --watch
npx serve ./

Then open `http://<host>:<port>/demo` in a web browser. Edit `demo/index.html` and refresh the page to see the results.
24 changes: 24 additions & 0 deletions packages/template-attributes/demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template id="foo">
<p><span template-classname="colors" template-textcontent="msg"></span></p>
</template>
<script src="../build/template-attributes.js"></script>
<my-hello id="hello"></my-hello>

<script type="module">
class MyHello extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});
this.shadowRoot.appendChild(
document.querySelector('#foo').content.cloneNode(true)
);
}

connectedCallback() {
this.templates.msg = 'Hello, World!';
this.templates.colors = 'red';
}
}

customElements.define('my-hello', MyHello);
</script>
Loading