0
I’m doing a Webcomponent and I came across a situation: Content is not inside the component template.
If you inspect below, the "Alert Test" is positioned below the template instead of inside. I searched about slots but didn’t understand how to use it.
I ask for help to clarify the use and if I am doing something wrong. Thank you!
class Alert extends HTMLElement {
constructor() {
super();
this.shadow = this.attachShadow({ mode: 'open' });
this._priority = null;
this._typeClass = '';
this._iconClass = '';
this._alertTypeName = '';
this._alertPriority = null;
}
get type() {
return this.getAttribute('type');
}
get priority() {
return this.getAttribute('priority');
}
static get observedAttributes() {
return ['type', 'priority'];
}
attributeChangedCallback(type, oldVal, newVal) {
console.log(this.shadow)
}
connectedCallback() {
switch (this.type) {
case "success":
this._alertTypeName = "Sucesso";
this._typeClass = "alert-success";
this._iconClass = "glyphicon-ok";
this._alertPriority = this.priority;
break;
case "info":
this._alertTypeName = "Informação";
this._typeClass = "alert-info";
this._iconClass = "glyphicon-info-sign";
this._alertPriority = this.priority;
break;
case "warning":
this._alertTypeName = "Aviso";
this._typeClass = "alert-warning";
this._iconClass = "glyphicon-warning-sign";
this._alertPriority = this.priority;
break;
case "error":
this._alertTypeName = "Erro";
this._typeClass = "alert-danger";
this._iconClass = "glyphicon-exclamation-sign";
this._alertPriority = "alert";
break;
}
var template = `
<div class="alert alert-dismissible fade show" role>
<span class="glyphicon" aria-hidden="true"></span>
<button type="button" class="close" data-dismiss="alert" aria-label="Fechar"><span aria-hidden="true">×</span></button>
</div>
`;
this.shadow.innerHTML = template;
var alertElement = this.shadow.querySelector('.alert');
alertElement.setAttribute("role", this._alertPriority);
alertElement.children[0].setAttribute("aria-label", this._alertTypeName);
alertElement.classList.add(this._typeClass);
alertElement.children[0].classList.add(this._iconClass);
}
}
window.customElements.define('meu-alert', Alert);
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<meu-alert type="success" priority="alert">
Teste de alerta
</meu-alert>
Wonderful guy, clarified a lot for me! Thank you!
– Douglas Teles