lucide
Version:
A Lucide icon library package for web and javascript applications.
67 lines (63 loc) • 2.1 kB
JavaScript
/**
* @license lucide v1.8.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';
import { hasA11yProp } from './shared/src/utils/hasA11yProp.js';
import { mergeClasses } from './shared/src/utils/mergeClasses.js';
import { toPascalCase } from './shared/src/utils/toPascalCase.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 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 ariaProps = hasA11yProp(elementAttrs) ? {} : { "aria-hidden": "true" };
const iconAttrs = {
...defaultAttributes,
"data-lucide": iconName,
...ariaProps,
...attrs,
...elementAttrs
};
const elementClassNames = getClassNames(elementAttrs);
const className = getClassNames(attrs);
const classNames = mergeClasses(
"lucide",
`lucide-${iconName}`,
...elementClassNames,
...className
);
if (classNames) {
Object.assign(iconAttrs, {
class: classNames
});
}
const svgElement = createElement(iconNode, iconAttrs);
return element.parentNode?.replaceChild(svgElement, element);
};
export { replaceElement as default, getAttrs, getClassNames };
//# sourceMappingURL=replaceElement.js.map