63 lines
2.1 KiB
JavaScript
63 lines
2.1 KiB
JavaScript
/**
|
|
* @license lucide v0.331.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';
|
|
|
|
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 [tag, iconAttributes, children] = iconNode;
|
|
const iconAttrs = {
|
|
...iconAttributes,
|
|
"data-lucide": iconName,
|
|
...attrs,
|
|
...elementAttrs
|
|
};
|
|
const classNames = combineClassNames(["lucide", `lucide-${iconName}`, elementAttrs, attrs]);
|
|
if (classNames) {
|
|
Object.assign(iconAttrs, {
|
|
class: classNames
|
|
});
|
|
}
|
|
const svgElement = createElement([tag, iconAttrs, children]);
|
|
return element.parentNode?.replaceChild(svgElement, element);
|
|
};
|
|
|
|
export { combineClassNames, replaceElement as default, getAttrs, getClassNames };
|
|
//# sourceMappingURL=replaceElement.js.map
|