From 3ace1df5a2fab26f489140bcc27554ca8dcfe6ff Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Wed, 10 Apr 2024 21:38:44 -0600 Subject: [PATCH] Begin work on an icon component --- module/components/icon.mjs | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 module/components/icon.mjs diff --git a/module/components/icon.mjs b/module/components/icon.mjs new file mode 100644 index 0000000..daadaa7 --- /dev/null +++ b/module/components/icon.mjs @@ -0,0 +1,68 @@ +/** +Attributes: +@property {string} name - The name of the icon, takes precedence over the path +@property {string} path - The path of the icon file +*/ +export class DotDungeonIcon extends HTMLElement { + static elementName = `dd-icon`; + static formAssociated = false; + + static #styles = ``; + _internals; + _shadow; + + _min; + _max; + _smallStep; + _largeStep; + + constructor() { + super(); + this._shadow = this.attachShadow({ mode: `open`, delegatesFocus: true }); + if (DotDungeonIcon.#styles) this.#embedStyles(); + }; + + + connectedCallback() { + this.replaceChildren(); + + /* + This converts all of the double-dash prefixed properties on the element to + CSS variables so that they don't all need to be provided by doing style="" + */ + for (const attrVar of this.attributes) { + if (attrVar.name?.startsWith(`--`)) { + this.style.setProperty(attrVar.name, attrVar.value); + }; + }; + + /* + Style fetching if we haven't gotten them yet + */ + if (!DotDungeonIcon.#styles) { + fetch(`./systems/dotdungeon/.styles/v3/components/incrementer.css`) + .then(r => r.text()) + .then(t => { + DotDungeonIcon.#styles = t; + this.#embedStyles(); + }); + }; + + /* + Try to retrieve the icon if it isn't present, try the path then default to + the slot content, as then we can have a default per-icon usage + */ + let content; + + if (!content) { + let slot = document.createElement(`slot`); + content ??= slot; + } + }; + + #embedStyles() { + const style = document.createElement(`style`); + style.innerHTML = DotDungeonIcon.#styles; + this._shadow.appendChild(style); + }; +};