Skip to content

Instantly share code, notes, and snippets.

@aliafshar
Created December 15, 2016 13:38
Show Gist options
  • Select an option

  • Save aliafshar/ce99e3e10675994ec20c5acfe2c055ea to your computer and use it in GitHub Desktop.

Select an option

Save aliafshar/ce99e3e10675994ec20c5acfe2c055ea to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="material.indigo-pink.min.css">
</head>
<body>
<h1>HTML Style copying demo</h1>
<the-shizz>I am a button</the-shizz>
<script>
function copyStyles(styleElement, selectorText) {
for (let sheet of document.styleSheets) {
if (sheet.cssRules) {
for (let rule of sheet.cssRules) {
if (rule.selectorText && rule.selectorText.indexOf(selectorText) > -1) {
styleElement.sheet.insertRule(rule.cssText, styleElement.sheet.cssRules.length);
}
}
}
}
}
class TheShizz extends HTMLElement {
constructor() {
super(); // duper
const shadowRoot = this.attachShadow({mode: 'open'});
const styleElement = document.createElement('style');
const buttonElement = document.createElement('button');
buttonElement.classList.add('mdl-button', 'mdl-js-button');
shadowRoot.append(styleElement);
shadowRoot.append(buttonElement);
buttonElement.append(document.createElement('slot'));
copyStyles(styleElement, 'button');
}
}
customElements.define('the-shizz', TheShizz);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment