Skip to content

Instantly share code, notes, and snippets.

@joelalejandro
Created September 5, 2020 00:11
Show Gist options
  • Save joelalejandro/11ca9d83c2b4e112cafb98f64a272a9a to your computer and use it in GitHub Desktop.
Save joelalejandro/11ca9d83c2b4e112cafb98f64a272a9a to your computer and use it in GitHub Desktop.
Web Components: Usando <slot>
<template id="tabifit-boton">
<button>
<slot name="icono-izquierdo"></slot>
<slot name="etiqueta">Botón</slot>
<slot name="icono-derecho"></slot>
</button>
</template>
<tabifit-boton>
<span slot="icono-izquierdo">🦜</span>
<label slot="etiqueta">Adoptar un loro</label>
</tabifit-boton>
<tabifit-boton>
<span slot="icono-derecho">🦜</span>
<label slot="etiqueta">Adoptar un loro</label>
</tabifit-boton>
function cargarTemplate(referenciaTemplate, instanciaComponente) {
const template = document.querySelector(referenciaTemplate);
const templateContent = template.content;
instanciaComponente.attachShadow({ mode: "open" })
.appendChild(templateContent.cloneNode(true));
}
class TabifitBoton extends HTMLElement {
constructor() {
super();
cargarTemplate('#tabifit-boton', this);
}
}
customElements.define('tabifit-boton', TabifitBoton);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment