Skip to content

Instantly share code, notes, and snippets.

@pinkhominid
Last active January 31, 2019 01:05
Show Gist options
  • Save pinkhominid/748a24638e293efd709060abaa971867 to your computer and use it in GitHub Desktop.
Save pinkhominid/748a24638e293efd709060abaa971867 to your computer and use it in GitHub Desktop.
Minimal Web Component Demo App Starter
const tagName = 'demo-app';
const template = document.createElement('template');
template.innerHTML = `
<style>
:host {
display: block;
contain: content;
}
:host([hidden]) {
display: none;
}
*, *::after, *::before {
box-sizing: border-box;
}
</style>
<p>Hello World!</p>
`;
class DemoApp extends HTMLElement {
constructor(props) {
super();
this.props = props;
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
console.log(props);
}
}
customElements.define(tagName, DemoApp);
export {
DemoApp as default,
DemoApp,
tagName as demoAppTagName,
}
<!doctype html><html lang=en><meta charset=utf-8><title>Demo</title>
<script type=module src=./index.js async></script>
<demo-app></demo-app>
import DemoAppDefault from './demo-app.js';
import { DemoApp, demoAppTagName as tagName } from './demo-app.js';
const elems = [
document.createElement(tagName),
new DemoApp({ name: 'Jane' }),
new DemoAppDefault({ name: 'Cruel World' }),
];
elems.forEach(elem => document.body.appendChild(elem));
{
"name": "demo-app",
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "http-server -o",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"http-server": "^0.11.1"
}
}
@pinkhominid
Copy link
Author

npm i && npm start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment