Skip to content

Instantly share code, notes, and snippets.

@kitsune7
Last active August 15, 2018 06:19
Show Gist options
  • Save kitsune7/2e9cc2a25acec8a4c5f5e97492a296f9 to your computer and use it in GitHub Desktop.
Save kitsune7/2e9cc2a25acec8a4c5f5e97492a296f9 to your computer and use it in GitHub Desktop.
Web component using html imports
<template id="hello-world-template">
<div class="hello-world">
<h1>Hello, World!</h1>
</div>
</template>
class HelloWorld extends HTMLElement {
createdCallback () {
console.log('RUNNING -- createdCallback')
const templates = document.getElementById('templates').import
const template = templates.getElementById('hello-world-template')
this.attachShadow({ mode: 'open' })
.appendChild(template.content.cloneNode(true))
}
}
document.registerElement('hello-world', HelloWorld)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Native component test</title>
<link rel="import" href="hello-world.html" id="templates" />
<script src="HelloWorld.js" defer></script>
</head>
<body>
<hello-world />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment