4 ms·
You can use @import to include shared CSS files. HTML import is still pending in the standard committee; should come soon. For now HTML template needs to go w
by ww520 6y ago
You can use @import to include shared CSS files. HTML import is still pending in the standard committee; should come soon. For now HTML template needs to go with the Javascript.
I usually structure a component like the following. The nice thing about WebComponent is everything is scoped to the component and you can have pretty generic names for things except the defined component name ("component1" in this case).
/components/component1.js
import cu from "/util/comp-util.js";
import comp2 from "/components/component2.js";
import comp3 from "/components/component3.js";
(function() {
let template = `
<template>
<style>
@import "/styles/shared.css";
@import "/components/component1.css";
</style>
<div id="the_component" class="component1">
<button />
<component2 attr1="green" attr2="10" ... />
<component3 ... />
...
</div>
</template>`;
class ComponentElement extends HTMLElement {
constructor() {
super();
cu.attachTemplate(this, templete); // the attachment boiler-plate code in util.
}
...
}
window.customElements.define("component1", ComponentElement);
}());
- clairity 6y agointeresting, i look forward to when html imports come about. it's strange that this most fundamental component weren't among the first things you could import (i guess plain old links subsumed some of that need).
- cletusw 6y agoHTML Imports were actually among the first (shipped in Chrome), but then ES Modules/import/export took off and now spec authors want to reconcile the two before standarizing.