(() => {
- const HeightMap = new Map<number, number>([
+ type IconSize = number;
+ type RenderSize = number;
+ const HeightMap = new Map<IconSize, RenderSize>([
[16, 14],
[24, 18],
[32, 28],
]);
class FaBrand extends HTMLElement {
+ private root?: ShadowRoot = undefined;
+ private svgStyle: HTMLStyleElement = document.createElement("style");
+
connectedCallback() {
this.validate();
- const root = this.prepareRoot();
+ const root = this.getRoot();
const slot = document.createElement("slot");
slot.name = "svg";
}
}
- private prepareRoot(): ShadowRoot {
- const root = this.attachShadow({ mode: "open" });
+ private getRoot(): ShadowRoot {
+ if (this.root === undefined) {
+ this.root = this.attachShadow({ mode: "open" });
+
+ this.updateRenderSize();
+ this.root.append(this.svgStyle);
+ }
+
+ return this.root;
+ }
- const iconHeight = HeightMap.get(this.size)!;
- const style = document.createElement("style");
- style.textContent = `
+ private updateRenderSize(): void {
+ const renderSize = HeightMap.get(this.size)!;
+ this.svgStyle.textContent = `
::slotted(svg) {
fill: currentColor;
- height: ${iconHeight}px;
+ height: ${renderSize}px;
shape-rendering: geometricprecision;
}
`;
- root.append(style);
-
- return root;
}
- get size(): number {
+ get size(): IconSize {
const size = this.getAttribute("size");
if (size === null) {
return 0;
return parseInt(size);
}
+
+ set size(size: number) {
+ if (!HeightMap.has(size)) {
+ throw new Error(`Refused to set the invalid icon size '${size}'.`);
+ }
+
+ this.setAttribute("size", size.toString());
+ this.updateRenderSize();
+ }
}
window.customElements.define("fa-brand", FaBrand);
return isFA6Free;
}
- const HeightMap = new Map<number, number>([
+ type IconSize = number;
+ type RenderSize = number;
+ const HeightMap = new Map<IconSize, RenderSize>([
[16, 14],
[24, 18],
[32, 28],
this.setAttribute("name", name);
}
- get size(): number {
+ get size(): IconSize {
const size = this.getAttribute("size");
if (size === null) {
return 0;
return parseInt(size);
}
+ set size(size: number) {
+ if (!HeightMap.has(size)) {
+ throw new Error(`Refused to set the invalid icon size '${size}'.`);
+ }
+
+ this.setAttribute("size", size.toString());
+ }
+
static get observedAttributes() {
return ["name"];
}
[144, 130],
]);
class FaBrand extends HTMLElement {
+ constructor() {
+ super(...arguments);
+ this.root = undefined;
+ this.svgStyle = document.createElement("style");
+ }
connectedCallback() {
this.validate();
- const root = this.prepareRoot();
+ const root = this.getRoot();
const slot = document.createElement("slot");
slot.name = "svg";
root.append(slot);
throw new TypeError("Must provide a valid icon size.");
}
}
- prepareRoot() {
- const root = this.attachShadow({ mode: "open" });
- const iconHeight = HeightMap.get(this.size);
- const style = document.createElement("style");
- style.textContent = `
+ getRoot() {
+ if (this.root === undefined) {
+ this.root = this.attachShadow({ mode: "open" });
+ this.updateRenderSize();
+ this.root.append(this.svgStyle);
+ }
+ return this.root;
+ }
+ updateRenderSize() {
+ const renderSize = HeightMap.get(this.size);
+ this.svgStyle.textContent = `
::slotted(svg) {
fill: currentColor;
- height: ${iconHeight}px;
+ height: ${renderSize}px;
shape-rendering: geometricprecision;
}
`;
- root.append(style);
- return root;
}
get size() {
const size = this.getAttribute("size");
}
return parseInt(size);
}
+ set size(size) {
+ if (!HeightMap.has(size)) {
+ throw new Error(`Refused to set the invalid icon size '${size}'.`);
+ }
+ this.setAttribute("size", size.toString());
+ this.updateRenderSize();
+ }
}
window.customElements.define("fa-brand", FaBrand);
})();
}
return parseInt(size);
}
+ set size(size) {
+ if (!HeightMap.has(size)) {
+ throw new Error(`Refused to set the invalid icon size '${size}'.`);
+ }
+ this.setAttribute("size", size.toString());
+ }
static get observedAttributes() {
return ["name"];
}
$type = $tagArgs['type'] ?? '';
if (!\in_array($size, self::SIZES)) {
- throw new \InvalidArgumentException("An unsupported size `{$size}` was requested.");
+ throw new \InvalidArgumentException("An unsupported size '{$size}' was requested.");
}
if ($name === '') {
- throw new \InvalidArgumentException("The `name` attribute must be present and non-empty.");
+ throw new \InvalidArgumentException("The 'name' attribute must be present and non-empty.");
}
if ($type !== '' && !\in_array($type, self::TYPES)) {
- throw new \InvalidArgumentException("An unsupported type `${type}` was specified.");
+ throw new \InvalidArgumentException("An unsupported type '{$type}' was specified.");
}
if ($type === 'brand') {
$svgFile = \WCF_DIR . "icon/font-awesome/v6/brands/{$name}.svg";
if (!\file_exists($svgFile)) {
- throw new \InvalidArgumentException("Unable to locate the icon for brand `${name}`.");
+ throw new \InvalidArgumentException("Unable to locate the icon for brand '{$name}'.");
}
$content = \file_get_contents($svgFile);