Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example of web component #57

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions popup-info-box-web-component/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Create a class for the element
class PopUpInfo extends HTMLElement {
#img;
#info;

constructor() {
// Always call super first in constructor
super();
Expand All @@ -14,26 +17,13 @@ class PopUpInfo extends HTMLElement {
const icon = document.createElement('span');
icon.setAttribute('class', 'icon');
icon.setAttribute('tabindex', 0);

this.#img = document.createElement('img');
icon.appendChild(this.#img);

const info = document.createElement('span');
info.setAttribute('class', 'info');

// Take attribute content and put it inside the info span
const text = this.getAttribute('data-text');
info.textContent = text;

// Insert icon
let imgUrl;
if(this.hasAttribute('img')) {
imgUrl = this.getAttribute('img');
} else {
imgUrl = 'img/default.png';
}

const img = document.createElement('img');
img.src = imgUrl;
icon.appendChild(img);

this.#info = document.createElement('span');
this.#info.setAttribute('class', 'info');

// Create some CSS to apply to the shadow dom
const style = document.createElement('style');
console.log(style.isConnected);
Expand Down Expand Up @@ -73,7 +63,22 @@ class PopUpInfo extends HTMLElement {
console.log(style.isConnected);
shadow.appendChild(wrapper);
wrapper.appendChild(icon);
wrapper.appendChild(info);
wrapper.appendChild(this.#info);
}

connectedCallback() {
// Take attribute content and put it inside the info span
const text = this.getAttribute('data-text');
this.#info.textContent = text;

// Update icon based on attribute, if any
let imgUrl;
if(this.hasAttribute('img')) {
imgUrl = this.getAttribute('img');
} else {
imgUrl = 'img/default.png';
}
this.#img.src = imgUrl;
}
}

Expand Down