@carbon/icons-angular
Version:
Angular components for icons in digital and software products using the Carbon Design System
174 lines (171 loc) • 9.47 kB
JavaScript
import { __decorate, __metadata } from 'tslib';
import { Input, Component, ElementRef, Directive, NgModule } from '@angular/core';
import { getAttributes } from '@carbon/icon-helpers';
var StringTextDirective_1;
let StringTextComponent = class StringTextComponent {
constructor() {
this.focusable = false;
}
};
__decorate([
Input(),
__metadata("design:type", String)
], StringTextComponent.prototype, "ariaLabel", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], StringTextComponent.prototype, "ariaLabelledby", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], StringTextComponent.prototype, "ariaHidden", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], StringTextComponent.prototype, "title", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], StringTextComponent.prototype, "focusable", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], StringTextComponent.prototype, "innerClass", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], StringTextComponent.prototype, "size", void 0);
StringTextComponent = __decorate([
Component({
selector: "ibm-icon-string-text",
template: `
<svg
ibmIconStringText
[size]="size"
[ariaLabel]="ariaLabel"
[ariaLabelledby]="ariaLabelledby"
[ariaHidden]="ariaHidden"
[title]="title"
[isFocusable]="focusable"
[attr.class]="innerClass">
</svg>
`
})
], StringTextComponent);
let StringTextDirective = StringTextDirective_1 = class StringTextDirective {
constructor(elementRef) {
this.elementRef = elementRef;
this.isFocusable = false;
this.icons = {
"32": {
metadata: { "moduleName": "StringText32", "filepath": "string-text/32.js", "descriptor": { "elem": "svg", "attrs": { "xmlns": "http://www.w3.org/2000/svg", "viewBox": "0 0 32 32", "fill": "currentColor", "width": 32, "height": 32 }, "content": [{ "elem": "path", "attrs": { "d": "M29 22H24a2.0027 2.0027 0 01-2-2V14a2.0023 2.0023 0 012-2h5v2H24v6h5zM18 12H14V8H12V22h6a2.0027 2.0027 0 002-2V14A2.0023 2.0023 0 0018 12zm-4 8V14h4v6zM8 12H3v2H8v2H4a2 2 0 00-2 2v2a2 2 0 002 2h6V14A2.0023 2.0023 0 008 12zm0 8H4V18H8z" } }], "name": "string-text", "size": 32 }, "size": 32 },
svg: `<svg focusable="false" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><path d="M29 22H24a2.0027 2.0027 0 01-2-2V14a2.0023 2.0023 0 012-2h5v2H24v6h5zM18 12H14V8H12V22h6a2.0027 2.0027 0 002-2V14A2.0023 2.0023 0 0018 12zm-4 8V14h4v6zM8 12H3v2H8v2H4a2 2 0 00-2 2v2a2 2 0 002 2h6V14A2.0023 2.0023 0 008 12zm0 8H4V18H8z"></path></svg>`
},
"24": {
metadata: { "moduleName": "StringText24", "filepath": "string-text/24.js", "descriptor": { "elem": "svg", "attrs": { "xmlns": "http://www.w3.org/2000/svg", "viewBox": "0 0 32 32", "fill": "currentColor", "width": 24, "height": 24 }, "content": [{ "elem": "path", "attrs": { "d": "M29 22H24a2.0027 2.0027 0 01-2-2V14a2.0023 2.0023 0 012-2h5v2H24v6h5zM18 12H14V8H12V22h6a2.0027 2.0027 0 002-2V14A2.0023 2.0023 0 0018 12zm-4 8V14h4v6zM8 12H3v2H8v2H4a2 2 0 00-2 2v2a2 2 0 002 2h6V14A2.0023 2.0023 0 008 12zm0 8H4V18H8z" } }], "name": "string-text", "size": 24 }, "size": 24 },
svg: `<svg focusable="false" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="24" height="24" viewBox="0 0 32 32" aria-hidden="true"><path d="M29 22H24a2.0027 2.0027 0 01-2-2V14a2.0023 2.0023 0 012-2h5v2H24v6h5zM18 12H14V8H12V22h6a2.0027 2.0027 0 002-2V14A2.0023 2.0023 0 0018 12zm-4 8V14h4v6zM8 12H3v2H8v2H4a2 2 0 00-2 2v2a2 2 0 002 2h6V14A2.0023 2.0023 0 008 12zm0 8H4V18H8z"></path></svg>`
},
"20": {
metadata: { "moduleName": "StringText20", "filepath": "string-text/20.js", "descriptor": { "elem": "svg", "attrs": { "xmlns": "http://www.w3.org/2000/svg", "viewBox": "0 0 32 32", "fill": "currentColor", "width": 20, "height": 20 }, "content": [{ "elem": "path", "attrs": { "d": "M29 22H24a2.0027 2.0027 0 01-2-2V14a2.0023 2.0023 0 012-2h5v2H24v6h5zM18 12H14V8H12V22h6a2.0027 2.0027 0 002-2V14A2.0023 2.0023 0 0018 12zm-4 8V14h4v6zM8 12H3v2H8v2H4a2 2 0 00-2 2v2a2 2 0 002 2h6V14A2.0023 2.0023 0 008 12zm0 8H4V18H8z" } }], "name": "string-text", "size": 20 }, "size": 20 },
svg: `<svg focusable="false" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="20" height="20" viewBox="0 0 32 32" aria-hidden="true"><path d="M29 22H24a2.0027 2.0027 0 01-2-2V14a2.0023 2.0023 0 012-2h5v2H24v6h5zM18 12H14V8H12V22h6a2.0027 2.0027 0 002-2V14A2.0023 2.0023 0 0018 12zm-4 8V14h4v6zM8 12H3v2H8v2H4a2 2 0 00-2 2v2a2 2 0 002 2h6V14A2.0023 2.0023 0 008 12zm0 8H4V18H8z"></path></svg>`
},
"16": {
metadata: { "moduleName": "StringText16", "filepath": "string-text/16.js", "descriptor": { "elem": "svg", "attrs": { "xmlns": "http://www.w3.org/2000/svg", "viewBox": "0 0 32 32", "fill": "currentColor", "width": 16, "height": 16 }, "content": [{ "elem": "path", "attrs": { "d": "M29 22H24a2.0027 2.0027 0 01-2-2V14a2.0023 2.0023 0 012-2h5v2H24v6h5zM18 12H14V8H12V22h6a2.0027 2.0027 0 002-2V14A2.0023 2.0023 0 0018 12zm-4 8V14h4v6zM8 12H3v2H8v2H4a2 2 0 00-2 2v2a2 2 0 002 2h6V14A2.0023 2.0023 0 008 12zm0 8H4V18H8z" } }], "name": "string-text", "size": 16 }, "size": 16 },
svg: `<svg focusable="false" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="16" height="16" viewBox="0 0 32 32" aria-hidden="true"><path d="M29 22H24a2.0027 2.0027 0 01-2-2V14a2.0023 2.0023 0 012-2h5v2H24v6h5zM18 12H14V8H12V22h6a2.0027 2.0027 0 002-2V14A2.0023 2.0023 0 0018 12zm-4 8V14h4v6zM8 12H3v2H8v2H4a2 2 0 00-2 2v2a2 2 0 002 2h6V14A2.0023 2.0023 0 008 12zm0 8H4V18H8z"></path></svg>`
},
};
}
ngAfterViewInit() {
const svg = this.elementRef.nativeElement;
svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
const icon = this.icons[this.size];
const domParser = new DOMParser();
const rawSVG = icon.svg;
const svgElement = domParser.parseFromString(rawSVG, "image/svg+xml").documentElement;
let node = svgElement.firstChild;
while (node) {
// importNode makes a clone of the node
// this ensures we keep looping over the nodes in the parsed document
svg.appendChild(svg.ownerDocument.importNode(node, true));
node = node.nextSibling;
}
const attributes = getAttributes({
width: icon.metadata.descriptor.attrs.height,
height: icon.metadata.descriptor.attrs.height,
viewBox: icon.metadata.descriptor.attrs.viewBox,
title: this.title,
"aria-label": this.ariaLabel,
"aria-labelledby": this.ariaLabelledby,
"aria-hidden": this.ariaHidden,
focusable: this.isFocusable.toString()
});
const attrKeys = Object.keys(attributes);
for (let i = 0; i < attrKeys.length; i++) {
const key = attrKeys[i];
const value = attributes[key];
if (key === "title") {
continue;
}
if (value) {
svg.setAttribute(key, value);
}
}
if (attributes.title) {
const title = document.createElement("title");
title.textContent = attributes.title;
StringTextDirective_1.titleIdCounter++;
title.setAttribute("id", `string-text-${StringTextDirective_1.titleIdCounter}`);
svg.appendChild(title);
svg.setAttribute("aria-labelledby", `string-text-${StringTextDirective_1.titleIdCounter}`);
}
}
};
StringTextDirective.titleIdCounter = 0;
StringTextDirective.ctorParameters = () => [
{ type: ElementRef }
];
__decorate([
Input(),
__metadata("design:type", String)
], StringTextDirective.prototype, "ariaLabel", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], StringTextDirective.prototype, "ariaLabelledby", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], StringTextDirective.prototype, "ariaHidden", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], StringTextDirective.prototype, "title", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], StringTextDirective.prototype, "isFocusable", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], StringTextDirective.prototype, "size", void 0);
StringTextDirective = StringTextDirective_1 = __decorate([
Directive({
selector: "[ibmIconStringText]"
}),
__metadata("design:paramtypes", [ElementRef])
], StringTextDirective);
let StringTextModule = class StringTextModule {
};
StringTextModule = __decorate([
NgModule({
declarations: [
StringTextComponent,
StringTextDirective,
],
exports: [
StringTextComponent,
StringTextDirective,
]
})
], StringTextModule);
export { StringTextComponent, StringTextDirective, StringTextModule };