-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon-link.js
37 lines (28 loc) · 1.03 KB
/
icon-link.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class IconLink extends HTMLElement {
constructor() {
super()
let hasError = false;
let attributes = { href: '', title: '' };
Object.keys(attributes).map(attribute => {
try {
if (!this.hasAttribute(attribute)) throw Error(`Attribute '${attribute}' is required for this custom element.`);
else attributes[attribute] = this.getAttribute(attribute);
} catch (e) {
console.error(e);
hasError = true;
}
});
if (hasError) return;
const [href, title] = Object.values(attributes);
this.attachShadow({ mode: 'open' });
const anchor = document.createElement('a');
anchor.setAttribute('href', href);
anchor.setAttribute('target', '_blank');
const icon = anchor.appendChild(document.createElement('img'));
icon.setAttribute('src', `assets/icons/${title.toLowerCase().replaceAll(' ', '')}.svg`);
icon.setAttribute('alt', title);
icon.setAttribute('title', title);
this.shadowRoot.append(anchor);
}
}
customElements.define('icon-link', IconLink);