website/public/assets/js/CustomElement.mjs
vlw 7a8fc36ec0 feat: new website design and update to Vegvisir 3.1 (#2)
Brand new design for the website following the new design language I used for [version 2.0 of my personal website](https://codeberg.org/vlw/vlw.se/releases/tag/2.0.0).

Reviewed-on: https://codeberg.org/vegvisir/website/pulls/2
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
2025-03-05 11:16:54 +00:00

45 lines
No EOL
996 B
JavaScript

const PUBLIC_ELEMENT_STYLESHEET_DIR = "/assets/css/snippets/";
export class CustomElement extends HTMLElement {
constructor() {
super();
}
/**
* Return a pathname to a custom element CSS stylesheet
* @param {String} stylesheet
* @returns {String}
*/
static #getElementStylesheetHref(stylesheet) {
return `${PUBLIC_ELEMENT_STYLESHEET_DIR}${stylesheet}.css`;
}
/**
* Include a stylesheet for a custom element
* @param {String} stylesheet
*/
importElementStylesheet(stylesheet) {
if (document.head.querySelector(`link[href="${CustomElement.#getElementStylesheetHref(stylesheet)}"]`)) {
return;
}
const element = document.createElement("link");
element.href = CustomElement.#getElementStylesheetHref(stylesheet);
element.rel = "stylesheet";
document.head.appendChild(element);
}
connectedCallback() {
if ("connected" in this) {
this.connected();
}
}
disconnectedCallback() {
if ("disconnected" in this) {
this.disconnected();
}
}
}