mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-11-18 14:38:24 +00:00
feat(packages): extract build logic to @lucide/shared, still wip
This commit is contained in:
@@ -1,32 +1,50 @@
|
||||
---
|
||||
import defaultAttributes from './defaultAttributes';
|
||||
import buildLucideIconNode from './utils/buildLucideIconNode';
|
||||
import type { IconProps as Props } from './types';
|
||||
import { hasA11yProp } from './utils/hasA11yProp';
|
||||
|
||||
|
||||
const {
|
||||
color = 'currentColor',
|
||||
size = 24,
|
||||
'stroke-width': strokeWidth = 2,
|
||||
absoluteStrokeWidth = false,
|
||||
name,
|
||||
aliases,
|
||||
iconNode = [],
|
||||
class: className,
|
||||
...rest
|
||||
} = Astro.props;
|
||||
|
||||
const calculatedStrokeWidth =
|
||||
absoluteStrokeWidth ? (Number(strokeWidth) * 24) / Number(size) : strokeWidth;
|
||||
|
||||
const [, rawIconAttributes, builtIconNode = []] = buildLucideIconNode(
|
||||
{
|
||||
name,
|
||||
aliases,
|
||||
size: 24,
|
||||
node: iconNode,
|
||||
},
|
||||
{
|
||||
color,
|
||||
size,
|
||||
strokeWidth: calculatedStrokeWidth,
|
||||
className,
|
||||
hasA11yProp: hasA11yProp(rest),
|
||||
},
|
||||
);
|
||||
|
||||
const { class: iconClassName, ...builtIconAttributes } = rawIconAttributes;
|
||||
const iconAttributes = {
|
||||
...builtIconAttributes,
|
||||
...rest,
|
||||
};
|
||||
---
|
||||
|
||||
<svg
|
||||
{...{
|
||||
...defaultAttributes,
|
||||
width: size,
|
||||
height: size,
|
||||
stroke: color,
|
||||
'stroke-width': absoluteStrokeWidth ? (Number(strokeWidth) * 24) / Number(size) : strokeWidth,
|
||||
...(!hasA11yProp(rest) && { 'aria-hidden': 'true' }),
|
||||
...rest,
|
||||
}}
|
||||
class:list={['lucide', className]}
|
||||
{...iconAttributes}
|
||||
class={iconClassName}
|
||||
>
|
||||
{iconNode.map(([Tag, attrs]) => <Tag {...attrs} />)}
|
||||
{builtIconNode.map(([Tag, attrs]) => <Tag {...attrs} />)}
|
||||
<slot />
|
||||
</svg>
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import type { AstroComponentFactory } from 'astro/runtime/server/render/astro/factory.js';
|
||||
import type { IconNode } from './types';
|
||||
import type { LucideIconData } from './types';
|
||||
import { render, renderSlot, createComponent, renderComponent } from 'astro/compiler-runtime';
|
||||
import Icon from './Icon.astro';
|
||||
import { mergeClasses } from './utils/mergeClasses';
|
||||
import { toKebabCase } from './utils/toKebabCase';
|
||||
|
||||
export default (iconName: string, iconNode: IconNode): AstroComponentFactory => {
|
||||
export default (iconData: LucideIconData): AstroComponentFactory => {
|
||||
const Component = createComponent(
|
||||
($$result, $$props: Record<string, any>, $$slots) => {
|
||||
const { class: className, ...restProps } = $$props;
|
||||
@@ -14,11 +12,10 @@ export default (iconName: string, iconNode: IconNode): AstroComponentFactory =>
|
||||
'Icon',
|
||||
Icon,
|
||||
{
|
||||
class: mergeClasses(
|
||||
Boolean(iconName) && `lucide-${toKebabCase(iconName)}`,
|
||||
Boolean(className) && className,
|
||||
),
|
||||
iconNode,
|
||||
class: className,
|
||||
name: iconData.name,
|
||||
aliases: iconData.aliases,
|
||||
iconNode: iconData.node,
|
||||
...restProps,
|
||||
},
|
||||
{ default: () => render`${renderSlot($$result, $$slots['default'])}` },
|
||||
|
||||
@@ -9,6 +9,8 @@ export interface IconProps extends SVGAttributes {
|
||||
'stroke-width'?: number | string;
|
||||
absoluteStrokeWidth?: boolean;
|
||||
class?: string;
|
||||
name?: string;
|
||||
aliases?: string[];
|
||||
iconNode?: IconNode;
|
||||
title?: string;
|
||||
}
|
||||
@@ -17,6 +19,12 @@ export type SVGAttributes = HTMLAttributes<'svg'>;
|
||||
|
||||
export type IconNode = IconNodeChild[];
|
||||
|
||||
export interface LucideIconData {
|
||||
name: string;
|
||||
node: IconNode;
|
||||
aliases?: string[];
|
||||
}
|
||||
|
||||
type IconNodeChild = [elementName: SVGElements, attrs: SVGAttributes];
|
||||
|
||||
// All possible svg elements according to the Astro definitions
|
||||
|
||||
Reference in New Issue
Block a user