mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-12-03 16:59:42 +00:00
feat(packages): extract build logic to @lucide/shared, still wip
This commit is contained in:
@@ -1,22 +1,16 @@
|
||||
import base64SVG from '@lucide/build-icons/utils/base64SVG';
|
||||
import defineExportTemplate from '@lucide/build-icons/utils/defineExportTemplate';
|
||||
|
||||
export default defineExportTemplate(async({
|
||||
componentName,
|
||||
iconName,
|
||||
children,
|
||||
getSvg,
|
||||
deprecated,
|
||||
deprecationReason,
|
||||
}) => {
|
||||
const svgContents = await getSvg();
|
||||
const svgBase64 = base64SVG(svgContents);
|
||||
export default defineExportTemplate(
|
||||
async ({ componentName, iconName, iconData, getSvg, deprecated, deprecationReason }) => {
|
||||
const svgContents = await getSvg();
|
||||
const svgBase64 = base64SVG(svgContents);
|
||||
|
||||
return `
|
||||
return `
|
||||
import createLucideIcon from '../createLucideIcon';
|
||||
import { IconNode } from '../types';
|
||||
import type { LucideIconData } from '../types';
|
||||
|
||||
export const __iconNode: IconNode = ${JSON.stringify(children)}
|
||||
export const __iconData: LucideIconData = ${JSON.stringify(iconData)}
|
||||
|
||||
/**
|
||||
* @component @name ${componentName}
|
||||
@@ -29,8 +23,9 @@ export const __iconNode: IconNode = ${JSON.stringify(children)}
|
||||
* @returns {JSX.Element} JSX Element
|
||||
* ${deprecated ? `@deprecated ${deprecationReason}` : ''}
|
||||
*/
|
||||
const ${componentName} = createLucideIcon('${iconName}', __iconNode);
|
||||
const ${componentName} = createLucideIcon(__iconData);
|
||||
|
||||
export default ${componentName};
|
||||
`;
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
'use client';
|
||||
|
||||
import { createElement, forwardRef, useEffect, useState } from 'react';
|
||||
import { IconNode, LucideIcon, LucideProps } from './types';
|
||||
import { LucideIcon, LucideIconData, LucideIconNode, LucideProps } from './types';
|
||||
import dynamicIconImports from './dynamicIconImports';
|
||||
import Icon from './Icon';
|
||||
|
||||
export type DynamicIconModule = { default: LucideIcon; __iconNode: IconNode };
|
||||
export type DynamicIconModule = {
|
||||
default: LucideIcon;
|
||||
__iconNode?: LucideIconNode;
|
||||
__iconData?: LucideIconData;
|
||||
};
|
||||
|
||||
export type IconName = keyof typeof dynamicIconImports;
|
||||
|
||||
@@ -24,7 +28,15 @@ async function getIconNode(name: IconName) {
|
||||
// TODO: Replace this with a generic iconNode package.
|
||||
const icon = (await dynamicIconImports[name]()) as DynamicIconModule;
|
||||
|
||||
return icon.__iconNode;
|
||||
if (icon.__iconData != null) {
|
||||
return icon.__iconData.node;
|
||||
}
|
||||
|
||||
if (icon.__iconNode != null) {
|
||||
return icon.__iconNode;
|
||||
}
|
||||
|
||||
throw new Error('[lucide-react]: Failed to resolve icon data for DynamicIcon');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,14 +49,14 @@ async function getIconNode(name: IconName) {
|
||||
* @param {number} props.strokeWidth - The stroke width of the icon
|
||||
* @param {boolean} props.absoluteStrokeWidth - Whether to use absolute stroke width
|
||||
* @param {string} props.className - The class name of the icon
|
||||
* @param {IconNode} props.children - The children of the icon
|
||||
* @param {IconNode} props.iconNode - The icon node of the icon
|
||||
* @param {LucideIconNode} props.children - The children of the icon
|
||||
* @param {LucideIconNode} props.iconNode - The icon node of the icon
|
||||
*
|
||||
* @returns {ForwardRefExoticComponent} LucideIcon
|
||||
*/
|
||||
const DynamicIcon = forwardRef<SVGSVGElement, DynamicIconComponentProps>(
|
||||
({ name, fallback: Fallback, ...props }, ref) => {
|
||||
const [iconNode, setIconNode] = useState<IconNode>();
|
||||
const [iconNode, setIconNode] = useState<LucideIconNode>();
|
||||
|
||||
useEffect(() => {
|
||||
getIconNode(name)
|
||||
|
||||
@@ -1,15 +1,40 @@
|
||||
'use client';
|
||||
|
||||
import { createElement, forwardRef } from 'react';
|
||||
import { buildLucideIconNode, hasA11yProp, mergeClasses } from '@lucide/shared';
|
||||
import defaultAttributes from './defaultAttributes';
|
||||
import { IconNode, LucideProps } from './types';
|
||||
import { mergeClasses, hasA11yProp } from '@lucide/shared';
|
||||
import { LucideIconNode, LucideProps } from './types';
|
||||
import { useLucideContext } from './context';
|
||||
|
||||
interface IconComponentProps extends LucideProps {
|
||||
iconNode: IconNode;
|
||||
name?: string;
|
||||
aliases?: string[];
|
||||
iconNode: LucideIconNode;
|
||||
}
|
||||
|
||||
const toReactAttributeName = (attributeName: string) => {
|
||||
if (attributeName === 'class') {
|
||||
return 'className';
|
||||
}
|
||||
|
||||
if (attributeName.startsWith('data-') || attributeName.startsWith('aria-')) {
|
||||
return attributeName;
|
||||
}
|
||||
|
||||
return attributeName.replace(/-([a-z])/g, (_, character: string) => character.toUpperCase());
|
||||
};
|
||||
|
||||
const toReactAttributes = (attributes: Record<string, any>) => {
|
||||
return Object.entries(attributes).reduce<Record<string, any>>((acc, [attributeName, value]) => {
|
||||
if (value === undefined) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
acc[toReactAttributeName(attributeName)] = value;
|
||||
return acc;
|
||||
}, {});
|
||||
};
|
||||
|
||||
/**
|
||||
* Lucide icon component
|
||||
*
|
||||
@@ -20,14 +45,25 @@ interface IconComponentProps extends LucideProps {
|
||||
* @param {number} props.strokeWidth - The stroke width of the icon
|
||||
* @param {boolean} props.absoluteStrokeWidth - Whether to use absolute stroke width
|
||||
* @param {string} props.className - The class name of the icon
|
||||
* @param {IconNode} props.children - The children of the icon
|
||||
* @param {IconNode} props.iconNode - The icon node of the icon
|
||||
* @param {LucideIconNode} props.children - The children of the icon
|
||||
* @param {LucideIconNode} props.iconNode - The icon node of the icon
|
||||
*
|
||||
* @returns {ForwardRefExoticComponent} LucideIcon
|
||||
*/
|
||||
const Icon = forwardRef<SVGSVGElement, IconComponentProps>(
|
||||
(
|
||||
{ color, size, strokeWidth, absoluteStrokeWidth, className = '', children, iconNode, ...rest },
|
||||
{
|
||||
color,
|
||||
size,
|
||||
strokeWidth,
|
||||
absoluteStrokeWidth,
|
||||
className = '',
|
||||
children,
|
||||
iconNode,
|
||||
name,
|
||||
aliases,
|
||||
...rest
|
||||
},
|
||||
ref,
|
||||
) => {
|
||||
const {
|
||||
@@ -43,21 +79,33 @@ const Icon = forwardRef<SVGSVGElement, IconComponentProps>(
|
||||
? (Number(strokeWidth ?? contextStrokeWidth) * 24) / Number(size ?? contextSize)
|
||||
: strokeWidth ?? contextStrokeWidth;
|
||||
|
||||
const hasAccessibleProp = Boolean(children) || hasA11yProp(rest);
|
||||
|
||||
const [, svgAttributes, builtIconNode = []] = buildLucideIconNode(
|
||||
{
|
||||
name,
|
||||
aliases,
|
||||
size: 24,
|
||||
node: iconNode,
|
||||
},
|
||||
{
|
||||
color: color ?? contextColor,
|
||||
size: size ?? contextSize ?? defaultAttributes.width,
|
||||
strokeWidth: calculatedStrokeWidth,
|
||||
className: mergeClasses(contextClass, className),
|
||||
hasA11yProp: hasAccessibleProp,
|
||||
},
|
||||
);
|
||||
|
||||
return createElement(
|
||||
'svg',
|
||||
{
|
||||
ref,
|
||||
...defaultAttributes,
|
||||
width: size ?? contextSize ?? defaultAttributes.width,
|
||||
height: size ?? contextSize ?? defaultAttributes.height,
|
||||
stroke: color ?? contextColor,
|
||||
strokeWidth: calculatedStrokeWidth,
|
||||
className: mergeClasses('lucide', contextClass, className),
|
||||
...(!children && !hasA11yProp(rest) && { 'aria-hidden': 'true' }),
|
||||
...toReactAttributes(svgAttributes),
|
||||
...rest,
|
||||
},
|
||||
[
|
||||
...iconNode.map(([tag, attrs]) => createElement(tag, attrs)),
|
||||
...builtIconNode.map(([tag, attrs]) => createElement(tag, attrs)),
|
||||
...(Array.isArray(children) ? children : [children]),
|
||||
],
|
||||
);
|
||||
|
||||
@@ -1,31 +1,54 @@
|
||||
import { createElement, forwardRef } from 'react';
|
||||
import { mergeClasses, toKebabCase, toPascalCase } from '@lucide/shared';
|
||||
import { IconNode, LucideProps } from './types';
|
||||
import { toLucideIconData, toPascalCase } from '@lucide/shared';
|
||||
import { LucideIcon, LucideIconData, LucideIconNode, LucideProps } from './types';
|
||||
import Icon from './Icon';
|
||||
|
||||
/**
|
||||
* Create a Lucide icon component
|
||||
* @param {string} iconName
|
||||
* @param {array} iconNode
|
||||
* @returns {ForwardRefExoticComponent} LucideIcon
|
||||
* Creates a Lucide icon component from icon data.
|
||||
*
|
||||
* @param iconData Icon data object.
|
||||
* @returns Lucide icon component.
|
||||
*/
|
||||
const createLucideIcon = (iconName: string, iconNode: IconNode) => {
|
||||
function createLucideIcon(iconData: LucideIconData): LucideIcon;
|
||||
|
||||
/**
|
||||
* Creates a Lucide icon component from the legacy icon arguments.
|
||||
*
|
||||
* @param iconName Icon name.
|
||||
* @param iconNode Icon node.
|
||||
* @param aliases Optional icon aliases.
|
||||
* @returns Lucide icon component.
|
||||
*/
|
||||
function createLucideIcon(
|
||||
iconName: string,
|
||||
iconNode: LucideIconNode,
|
||||
aliases?: string[],
|
||||
): LucideIcon;
|
||||
|
||||
function createLucideIcon(
|
||||
iconDataOrName: LucideIconData | string,
|
||||
iconNode?: LucideIconNode,
|
||||
aliases: string[] = [],
|
||||
): LucideIcon {
|
||||
const iconData =
|
||||
typeof iconDataOrName === 'string'
|
||||
? toLucideIconData(iconDataOrName, iconNode as LucideIconNode, aliases)
|
||||
: iconDataOrName;
|
||||
|
||||
const Component = forwardRef<SVGSVGElement, LucideProps>(({ className, ...props }, ref) =>
|
||||
createElement(Icon, {
|
||||
ref,
|
||||
iconNode,
|
||||
className: mergeClasses(
|
||||
`lucide-${toKebabCase(toPascalCase(iconName))}`,
|
||||
`lucide-${iconName}`,
|
||||
className,
|
||||
),
|
||||
name: iconData.name,
|
||||
aliases: iconData.aliases,
|
||||
iconNode: iconData.node,
|
||||
className,
|
||||
...props,
|
||||
}),
|
||||
);
|
||||
|
||||
Component.displayName = toPascalCase(iconName);
|
||||
Component.displayName = toPascalCase(iconData.name);
|
||||
|
||||
return Component;
|
||||
};
|
||||
}
|
||||
|
||||
export default createLucideIcon;
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import type { SVGProps, ForwardRefExoticComponent, RefAttributes } from 'react';
|
||||
import type {
|
||||
LucideIconData as SharedLucideIconData,
|
||||
LucideIconNode as SharedLucideIconNode,
|
||||
} from '@lucide/shared';
|
||||
|
||||
export type LucideIconNode = SharedLucideIconNode[];
|
||||
|
||||
export type LucideIconData = SharedLucideIconData & {
|
||||
name: string;
|
||||
node: LucideIconNode;
|
||||
};
|
||||
|
||||
/**
|
||||
* A reduced version of `SVGElementType` from @types/react. This type was added
|
||||
* with the release of React 19, and is included here in order to support usage
|
||||
* with older versions.
|
||||
* @deprecated Use LucideIconNode instead.
|
||||
*/
|
||||
type SVGElementType =
|
||||
| 'circle'
|
||||
| 'ellipse'
|
||||
| 'g'
|
||||
| 'line'
|
||||
| 'path'
|
||||
| 'polygon'
|
||||
| 'polyline'
|
||||
| 'rect';
|
||||
|
||||
export type IconNode = [elementName: SVGElementType, attrs: Record<string, string>][];
|
||||
export type IconNode = LucideIconNode;
|
||||
|
||||
export type SVGAttributes = Partial<SVGProps<SVGSVGElement>>;
|
||||
type ElementAttributes = RefAttributes<SVGSVGElement> & SVGAttributes;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
exports[`Using LucideProvider > should render the icon with LucideProvider 1`] = `
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
class="lucide lucide-house"
|
||||
class="lucide lucide-house lucide-home"
|
||||
fill="none"
|
||||
height="48"
|
||||
stroke="red"
|
||||
|
||||
@@ -1,35 +1,6 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`Using createLucideIcon > should create a component from an iconNode 1`] = `
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
class="lucide lucide-air-vent lucide-AirVent"
|
||||
fill="none"
|
||||
height="24"
|
||||
stroke="currentColor"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
viewBox="0 0 24 24"
|
||||
width="24"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path
|
||||
d="M6 12H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"
|
||||
/>
|
||||
<path
|
||||
d="M6 8h12"
|
||||
/>
|
||||
<path
|
||||
d="M18.3 17.7a2.5 2.5 0 0 1-3.16 3.83 2.53 2.53 0 0 1-1.14-2V12"
|
||||
/>
|
||||
<path
|
||||
d="M6.6 15.6A2 2 0 1 0 10 17v-5"
|
||||
/>
|
||||
</svg>
|
||||
`;
|
||||
|
||||
exports[`Using createLucideIcon > should create a component from an iconNode with iconName 1`] = `
|
||||
exports[`Using createLucideIcon > should create a component from icon data 1`] = `
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
class="lucide lucide-air-vent"
|
||||
@@ -58,10 +29,39 @@ exports[`Using createLucideIcon > should create a component from an iconNode wit
|
||||
</svg>
|
||||
`;
|
||||
|
||||
exports[`Using createLucideIcon > should include backwards compatible className 1`] = `
|
||||
exports[`Using createLucideIcon > should include aliases from icon data 1`] = `
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
class="lucide lucide-layout2 lucide-layout-2"
|
||||
class="lucide lucide-circle-check lucide-check-circle"
|
||||
fill="none"
|
||||
height="24"
|
||||
stroke="currentColor"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
viewBox="0 0 24 24"
|
||||
width="24"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path
|
||||
d="M6 12H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"
|
||||
/>
|
||||
<path
|
||||
d="M6 8h12"
|
||||
/>
|
||||
<path
|
||||
d="M18.3 17.7a2.5 2.5 0 0 1-3.16 3.83 2.53 2.53 0 0 1-1.14-2V12"
|
||||
/>
|
||||
<path
|
||||
d="M6.6 15.6A2 2 0 1 0 10 17v-5"
|
||||
/>
|
||||
</svg>
|
||||
`;
|
||||
|
||||
exports[`Using createLucideIcon > should merge className prop with icon classes 1`] = `
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
class="lucide lucide-circle-check lucide-check-circle custom-class"
|
||||
fill="none"
|
||||
height="24"
|
||||
stroke="currentColor"
|
||||
|
||||
@@ -10,7 +10,7 @@ exports[`Using lucide icon components > should adjust the size, stroke color and
|
||||
stroke-width="4"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
class="lucide lucide-grid3x3 lucide-grid-3x3"
|
||||
class="lucide lucide-grid-3x3 lucide-grid lucide-grid-3-x-3"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<rect width="18"
|
||||
@@ -41,7 +41,7 @@ exports[`Using lucide icon components > should not scale the strokeWidth when ab
|
||||
stroke-width="1"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
class="lucide lucide-grid3x3 lucide-grid-3x3"
|
||||
class="lucide lucide-grid-3x3 lucide-grid lucide-grid-3-x-3"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<rect width="18"
|
||||
@@ -72,7 +72,7 @@ exports[`Using lucide icon components > should render an component 1`] = `
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
class="lucide lucide-grid3x3 lucide-grid-3x3"
|
||||
class="lucide lucide-grid-3x3 lucide-grid lucide-grid-3-x-3"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<rect width="18"
|
||||
|
||||
@@ -94,6 +94,9 @@ describe('Using LucideProvider', () => {
|
||||
|
||||
const IconComponent = container.firstElementChild;
|
||||
|
||||
expect(IconComponent).toHaveAttribute('class', 'lucide provider-class lucide-house icon-class');
|
||||
expect(IconComponent).toHaveAttribute(
|
||||
'class',
|
||||
'lucide lucide-house lucide-home provider-class icon-class',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,30 +4,58 @@ import { airVent } from './testIconNodes';
|
||||
import { render } from '@testing-library/react';
|
||||
|
||||
describe('Using createLucideIcon', () => {
|
||||
it('should create a component from an iconNode', () => {
|
||||
it('should create a component from icon data', () => {
|
||||
const AirVent = createLucideIcon({
|
||||
name: 'air-vent',
|
||||
node: airVent,
|
||||
});
|
||||
|
||||
const { container } = render(<AirVent />);
|
||||
|
||||
expect(container.firstChild).toMatchSnapshot();
|
||||
expect(container.firstChild).toBeDefined();
|
||||
});
|
||||
|
||||
it('should include aliases from icon data', () => {
|
||||
const CircleCheck = createLucideIcon({
|
||||
name: 'circle-check',
|
||||
aliases: ['check-circle'],
|
||||
node: airVent,
|
||||
});
|
||||
|
||||
const { container } = render(<CircleCheck />);
|
||||
|
||||
expect(container.firstChild).toMatchSnapshot();
|
||||
expect(container.firstChild).toBeDefined();
|
||||
});
|
||||
|
||||
it('should merge className prop with icon classes', () => {
|
||||
const CircleCheck = createLucideIcon({
|
||||
name: 'circle-check',
|
||||
aliases: ['check-circle'],
|
||||
node: airVent,
|
||||
});
|
||||
|
||||
const { container } = render(<CircleCheck className="custom-class" />);
|
||||
|
||||
expect(container.firstChild).toMatchSnapshot();
|
||||
expect(container.firstChild).toBeDefined();
|
||||
});
|
||||
|
||||
it('should create a component from legacy icon arguments', () => {
|
||||
const AirVent = createLucideIcon('AirVent', airVent);
|
||||
|
||||
const { container } = render(<AirVent />);
|
||||
|
||||
expect(container.firstChild).toMatchSnapshot();
|
||||
expect(container.firstChild).toBeDefined();
|
||||
expect(container.firstChild).toHaveClass('lucide-air-vent');
|
||||
});
|
||||
|
||||
it('should create a component from an iconNode with iconName', () => {
|
||||
const AirVent = createLucideIcon('air-vent', airVent);
|
||||
it('should include aliases from legacy icon arguments', () => {
|
||||
const CircleCheck = createLucideIcon('CircleCheck', airVent, ['check-circle']);
|
||||
|
||||
const { container } = render(<AirVent />);
|
||||
const { container } = render(<CircleCheck />);
|
||||
|
||||
expect(container.firstChild).toMatchSnapshot();
|
||||
expect(container.firstChild).toBeDefined();
|
||||
});
|
||||
|
||||
it('should include backwards compatible className', () => {
|
||||
const Layout2 = createLucideIcon('layout-2', airVent);
|
||||
|
||||
const { container } = render(<Layout2 />);
|
||||
|
||||
expect(container.firstChild).toMatchSnapshot();
|
||||
expect(container.firstChild).toBeDefined();
|
||||
expect(container.firstChild).toHaveClass('lucide-circle-check');
|
||||
expect(container.firstChild).toHaveClass('lucide-check-circle');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user