Last active
January 31, 2019 01:05
-
-
Save pinkhominid/748a24638e293efd709060abaa971867 to your computer and use it in GitHub Desktop.
Minimal Web Component Demo App Starter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html><html lang=en><meta charset=utf-8><title>Demo</title> | |
<script type=module src=./index.js async></script> | |
<demo-app></demo-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
npm i && npm start