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';
|
2023-06-22 08:50:27 +00:00
|
|
|
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');
|
|
|
|
|
2023-02-21 07:04:06 +00:00
|
|
|
// 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
|
2023-06-22 08:50:27 +00:00
|
|
|
const html = iconToHTML(data.body, attr);
|
|
|
|
node.innerHTML = cleanUpInnerHTML(html);
|
2022-04-29 20:19:22 +00:00
|
|
|
return node.firstChild as HTMLElement;
|
|
|
|
}
|