2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-14 14:38:25 +00:00
iconify/iconify-icon/icon/src/render/svg.ts

29 lines
740 B
TypeScript
Raw Normal View History

2022-04-29 20:19:22 +00:00
import type { IconifyIconBuildResult } from '@iconify/utils/lib/svg/build';
import { iconToHTML } from '@iconify/utils/lib/svg/html';
import { cleanUpInnerHTML } from '@iconify/utils/lib/svg/inner-html';
2022-04-29 20:19:22 +00:00
/**
* Render node as <svg>
*/
export function renderSVG(data: IconifyIconBuildResult): Element {
const node = document.createElement('span');
// Add style if needed
const attr = data.attributes as Record<string, string>;
let style = '';
if (!attr.width) {
style = 'width: inherit;';
}
if (!attr.height) {
style += 'height: inherit;';
}
if (style) {
attr.style = style;
}
2022-04-29 20:19:22 +00:00
// Generate SVG
const html = iconToHTML(data.body, attr);
node.innerHTML = cleanUpInnerHTML(html);
2022-04-29 20:19:22 +00:00
return node.firstChild as HTMLElement;
}