Files
lucide/packages/lucide-react/src/createLucideIcon.ts
T
Han Yeong-woo eb035fe370 Improve formatting (#1814)
* Ignore linting for examples in docs

* Formatting JSX single attribute per line

* Separte `format` and `lint:format` in package.json

* Bump prettier version

* Run format
2024-02-01 14:38:21 +01:00

78 lines
1.9 KiB
TypeScript

import {
forwardRef,
createElement,
ReactSVG,
SVGProps,
ForwardRefExoticComponent,
RefAttributes,
} from 'react';
import defaultAttributes from './defaultAttributes';
export type IconNode = [elementName: keyof ReactSVG, attrs: Record<string, string>][];
export type SVGAttributes = Partial<SVGProps<SVGSVGElement>>;
type ComponentAttributes = RefAttributes<SVGSVGElement> & SVGAttributes;
export interface LucideProps extends ComponentAttributes {
size?: string | number;
absoluteStrokeWidth?: boolean;
}
export type LucideIcon = ForwardRefExoticComponent<LucideProps>;
/**
* Converts string to KebabCase
* Copied from scripts/helper. If anyone knows how to properly import it here
* then please fix it.
*
* @param {string} string
* @returns {string} A kebabized string
*/
export const toKebabCase = (string: string) =>
string
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
.toLowerCase()
.trim();
const createLucideIcon = (iconName: string, iconNode: IconNode): LucideIcon => {
const Component = forwardRef<SVGSVGElement, LucideProps>(
(
{
color = 'currentColor',
size = 24,
strokeWidth = 2,
absoluteStrokeWidth,
className = '',
children,
...rest
},
ref,
) => {
return createElement(
'svg',
{
ref,
...defaultAttributes,
width: size,
height: size,
stroke: color,
strokeWidth: absoluteStrokeWidth
? (Number(strokeWidth) * 24) / Number(size)
: strokeWidth,
className: ['lucide', `lucide-${toKebabCase(iconName)}`, className].join(' '),
...rest,
},
[
...iconNode.map(([tag, attrs]) => createElement(tag, attrs)),
...(Array.isArray(children) ? children : [children]),
],
);
},
);
Component.displayName = `${iconName}`;
return Component;
};
export default createLucideIcon;