lucide
Version:
A Lucide icon library package for web and javascript applications.
60 lines (56 loc) • 2.09 kB
JavaScript
/**
* @license lucide v0.525.0 - ISC
*
* This source code is licensed under the ISC license.
* See the LICENSE file in the root directory of this source tree.
*/
import createElement from './createElement.js';
import defaultAttributes from './defaultAttributes.js';
const getAttrs = (element) => Array.from(element.attributes).reduce((attrs, attr) => {
attrs[attr.name] = attr.value;
return attrs;
}, {});
const getClassNames = (attrs) => {
if (typeof attrs === "string") return attrs;
if (!attrs || !attrs.class) return "";
if (attrs.class && typeof attrs.class === "string") {
return attrs.class.split(" ");
}
if (attrs.class && Array.isArray(attrs.class)) {
return attrs.class;
}
return "";
};
const combineClassNames = (arrayOfClassnames) => {
const classNameArray = arrayOfClassnames.flatMap(getClassNames);
return classNameArray.map((classItem) => classItem.trim()).filter(Boolean).filter((value, index, self) => self.indexOf(value) === index).join(" ");
};
const toPascalCase = (string) => string.replace(/(\w)(\w*)(_|-|\s*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase());
const replaceElement = (element, { nameAttr, icons, attrs }) => {
const iconName = element.getAttribute(nameAttr);
if (iconName == null) return;
const ComponentName = toPascalCase(iconName);
const iconNode = icons[ComponentName];
if (!iconNode) {
return console.warn(
`${element.outerHTML} icon name was not found in the provided icons object.`
);
}
const elementAttrs = getAttrs(element);
const iconAttrs = {
...defaultAttributes,
"data-lucide": iconName,
...attrs,
...elementAttrs
};
const classNames = combineClassNames(["lucide", `lucide-${iconName}`, elementAttrs, attrs]);
if (classNames) {
Object.assign(iconAttrs, {
class: classNames
});
}
const svgElement = createElement(iconNode, iconAttrs);
return element.parentNode?.replaceChild(svgElement, element);
};
export { combineClassNames, replaceElement as default, getAttrs, getClassNames };
//# sourceMappingURL=replaceElement.js.map