Files
chatpapers/src/utils/icons.ts
T

42 lines
1.2 KiB
TypeScript

import type { IconNode } from "lucide";
export type LucideIcon = IconNode;
export function createLucideIcon(
doc: Document,
icon: LucideIcon,
options: {
size?: number;
strokeWidth?: number;
className?: string;
} = {},
): Element {
const size = options.size ?? 16;
const svg = doc.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("width", String(size));
svg.setAttribute("height", String(size));
svg.setAttribute("viewBox", "0 0 24 24");
svg.setAttribute("fill", "none");
svg.setAttribute("stroke", "currentColor");
svg.setAttribute("stroke-width", String(options.strokeWidth ?? 2));
svg.setAttribute("stroke-linecap", "round");
svg.setAttribute("stroke-linejoin", "round");
svg.setAttribute("aria-hidden", "true");
svg.classList.add("chatpapers-icon");
if (options.className) {
for (const c of options.className.split(/\s+/)) {
if (c) svg.classList.add(c);
}
}
for (const node of icon) {
const [tag, attrs] = node;
const el = doc.createElementNS("http://www.w3.org/2000/svg", tag);
for (const [key, value] of Object.entries(attrs)) {
el.setAttribute(key, String(value));
}
svg.appendChild(el);
}
return svg;
}