Improve the behavior of the `<fa-icon>` name attribute
authorAlexander Ebert <ebert@woltlab.com>
Wed, 10 Aug 2022 10:58:35 +0000 (12:58 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 12 Aug 2022 19:25:55 +0000 (21:25 +0200)
ts/WoltLabSuite/WebComponent/fa-icon.ts
wcfsetup/install/files/js/WoltLabSuite/WebComponent/fa-icon.js

index 2df931ed6dc274115caaac6f22771b34a0215b9d..39509326a6a2f06e45ce4a126c015855313e5815 100644 (file)
       if (!isSolid && isFontAwesome6Free()) {
         const [, styles] = window.getFontAwesome6IconMetadata(name)!;
         if (!styles.includes("regular")) {
+          // Font Awesome 6 Free only includes solid icons with the
+          // the exception to some special icons that use the weight
+          // to differentiate two related icons. One such example is
+          // the `bell` icon that comes in `solid` and `regular` flavor.
           return false;
         }
       }
       root.append(codepoint);
     }
 
-    attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void {
-      switch (name) {
-        case "name":
-          if (newValue !== null && this.isValidIconName(newValue)) {
-            this.updateIcon();
-          }
-          break;
-      }
-    }
-
     get solid(): boolean {
       return this.hasAttribute("solid");
     }
     }
 
     set name(name: string) {
+      if (!this.isValidIconName(name)) {
+        throw new Error(`Refused to set the unknown icon name '${name}'.`);
+      }
+
       this.setAttribute("name", name);
+      this.updateIcon();
     }
 
     get size(): IconSize {
 
       this.setAttribute("size", size.toString());
     }
-
-    static get observedAttributes() {
-      return ["name"];
-    }
   }
 
   window.customElements.define("fa-icon", FaIcon);
index 044de90923ddca1cf57434da102ac7e739014685..ceceabcc467f6ef541ddec1f620edbb7ec47c593 100644 (file)
             if (!isSolid && isFontAwesome6Free()) {
                 const [, styles] = window.getFontAwesome6IconMetadata(name);
                 if (!styles.includes("regular")) {
+                    // Font Awesome 6 Free only includes solid icons with the
+                    // the exception to some special icons that use the weight
+                    // to differentiate two related icons. One such example is
+                    // the `bell` icon that comes in `solid` and `regular` flavor.
                     return false;
                 }
             }
             const [codepoint] = window.getFontAwesome6IconMetadata(this.name);
             root.append(codepoint);
         }
-        attributeChangedCallback(name, oldValue, newValue) {
-            switch (name) {
-                case "name":
-                    if (newValue !== null && this.isValidIconName(newValue)) {
-                        this.updateIcon();
-                    }
-                    break;
-            }
-        }
         get solid() {
             return this.hasAttribute("solid");
         }
             return this.getAttribute("name") || "";
         }
         set name(name) {
+            if (!this.isValidIconName(name)) {
+                throw new Error(`Refused to set the unknown icon name '${name}'.`);
+            }
             this.setAttribute("name", name);
+            this.updateIcon();
         }
         get size() {
             const size = this.getAttribute("size");
             }
             this.setAttribute("size", size.toString());
         }
-        static get observedAttributes() {
-            return ["name"];
-        }
     }
     window.customElements.define("fa-icon", FaIcon);
 })();