mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-12-02 16:49:06 +00:00
fix(lucide-solid|@lucide/solid): render Icon children in solid and lucide-solid (#5259)
* fix(packages): render Icon children in solid and lucide-solid * fix(solid): avoid extra marker nodes when Icon has no children * Fix solid render * Format code --------- Co-authored-by: grzdev <grzdev@users.noreply.github.com> Co-authored-by: Eric <eric.fennis@gmail.com>
This commit is contained in:
co-authored by
grzdev
Eric
parent
cd12948ef6
commit
a05d1700c9
@@ -49,25 +49,25 @@ const Icon = (props: LucideProps & IconProps) => {
|
||||
absoluteStrokeWidth: localProps.absoluteStrokeWidth ?? globalProps.absoluteStrokeWidth,
|
||||
nonScalingStroke: localProps.nonScalingStroke ?? globalProps.nonScalingStroke,
|
||||
className: mergeClasses('lucide-icon', globalProps.class, localProps.class),
|
||||
hasA11yProp: Boolean(localProps.children) || hasA11yProp(rest),
|
||||
hasA11yProp: hasA11yProp(rest),
|
||||
attributes: rest,
|
||||
}),
|
||||
);
|
||||
|
||||
return (
|
||||
<svg {...builtIcon()[1]}>
|
||||
<For each={builtIcon()[2] ?? []}>
|
||||
{([elementName, attrs]) => {
|
||||
return (
|
||||
<Dynamic
|
||||
component={elementName}
|
||||
{...attrs}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
</For>
|
||||
</svg>
|
||||
const paths = (
|
||||
<For each={builtIcon()[2] ?? []}>
|
||||
{([elementName, attrs]) => {
|
||||
return (
|
||||
<Dynamic
|
||||
component={elementName}
|
||||
{...attrs}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
</For>
|
||||
);
|
||||
|
||||
return <svg {...builtIcon()[1]}>{[paths, localProps.children]}</svg>;
|
||||
};
|
||||
|
||||
export default Icon;
|
||||
|
||||
@@ -122,19 +122,45 @@ describe('Icon Component Accessibility', () => {
|
||||
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
|
||||
});
|
||||
|
||||
it('should not have aria-hidden prop when there are children that could be a <title> element', async () => {
|
||||
it('should render a labelled title child and not have aria-hidden prop', async () => {
|
||||
const { container } = render(() => (
|
||||
<Icon
|
||||
iconNode={airVent}
|
||||
size={48}
|
||||
stroke="red"
|
||||
absoluteStrokeWidth
|
||||
role="img"
|
||||
aria-labelledby="warn-title"
|
||||
>
|
||||
<title>Some title</title>
|
||||
<title id="warn-title">Warning: Action cannot be undone</title>
|
||||
</Icon>
|
||||
));
|
||||
|
||||
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
|
||||
expect(container.firstChild).toHaveAttribute('role', 'img');
|
||||
expect(container.firstChild).toHaveAttribute('aria-labelledby', 'warn-title');
|
||||
expect(container.querySelector('title#warn-title')).toHaveTextContent(
|
||||
'Warning: Action cannot be undone',
|
||||
);
|
||||
});
|
||||
|
||||
it('should update reactive children', async () => {
|
||||
const [showTitle, setShowTitle] = createSignal(false);
|
||||
const { container } = render(() => (
|
||||
<Icon
|
||||
iconNode={airVent}
|
||||
role="img"
|
||||
aria-labelledby="dynamic-title"
|
||||
>
|
||||
{showTitle() && <title id="dynamic-title">Dynamic title</title>}
|
||||
</Icon>
|
||||
));
|
||||
|
||||
expect(container.querySelector('title')).toBeNull();
|
||||
|
||||
setShowTitle(true);
|
||||
|
||||
expect(container.querySelector('title#dynamic-title')).toHaveTextContent('Dynamic title');
|
||||
});
|
||||
|
||||
it('should never override aria-hidden prop', async () => {
|
||||
|
||||
@@ -87,4 +87,19 @@ describe('Using lucide icon components', () => {
|
||||
expect(container.firstChild).toHaveClass('lucide');
|
||||
expect(container.firstChild).toHaveClass('lucide-droplet');
|
||||
});
|
||||
|
||||
it('should render child elements inside icon components', () => {
|
||||
const { container } = render(() => (
|
||||
<Droplet
|
||||
role="img"
|
||||
aria-labelledby="droplet-title"
|
||||
>
|
||||
<title id="droplet-title">Water droplet</title>
|
||||
</Droplet>
|
||||
));
|
||||
|
||||
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
|
||||
expect(container.firstChild).toHaveAttribute('aria-labelledby', 'droplet-title');
|
||||
expect(container.querySelector('title#droplet-title')).toHaveTextContent('Water droplet');
|
||||
});
|
||||
});
|
||||
|
||||
+14
-14
@@ -51,25 +51,25 @@ const Icon = (props: LucideProps & IconProps) => {
|
||||
absoluteStrokeWidth: props.absoluteStrokeWidth ?? globalProps.absoluteStrokeWidth,
|
||||
nonScalingStroke: props.nonScalingStroke ?? globalProps.nonScalingStroke,
|
||||
className: mergeClasses('lucide-icon', globalProps.class, props.class),
|
||||
hasA11yProp: Boolean(props.children) || hasA11yProp(rest()),
|
||||
hasA11yProp: hasA11yProp(rest()),
|
||||
attributes: rest(),
|
||||
}),
|
||||
);
|
||||
|
||||
return (
|
||||
<svg {...builtIcon()[1]}>
|
||||
<For each={builtIcon()[2] ?? []}>
|
||||
{([elementName, attrs]) => {
|
||||
return (
|
||||
<Dynamic
|
||||
component={elementName}
|
||||
{...attrs}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
</For>
|
||||
</svg>
|
||||
const paths = (
|
||||
<For each={builtIcon()[2] ?? []}>
|
||||
{([elementName, attrs]) => {
|
||||
return (
|
||||
<Dynamic
|
||||
component={elementName}
|
||||
{...attrs}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
</For>
|
||||
);
|
||||
|
||||
return <svg {...builtIcon()[1]}>{[paths, props.children]}</svg>;
|
||||
};
|
||||
|
||||
export default Icon;
|
||||
|
||||
@@ -123,19 +123,46 @@ describe('Icon Component Accessibility', () => {
|
||||
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
|
||||
});
|
||||
|
||||
it('should not have aria-hidden prop when there are children that could be a <title> element', async () => {
|
||||
it('should render a labelled title child and not have aria-hidden prop', async () => {
|
||||
const { container } = render(() => (
|
||||
<Icon
|
||||
iconNode={airVent}
|
||||
size={48}
|
||||
stroke="red"
|
||||
absoluteStrokeWidth
|
||||
role="img"
|
||||
aria-labelledby="warn-title"
|
||||
>
|
||||
<title>Some title</title>
|
||||
<title id="warn-title">Warning: Action cannot be undone</title>
|
||||
</Icon>
|
||||
));
|
||||
|
||||
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
|
||||
expect(container.firstChild).toHaveAttribute('role', 'img');
|
||||
expect(container.firstChild).toHaveAttribute('aria-labelledby', 'warn-title');
|
||||
expect(container.querySelector('title#warn-title')).toHaveTextContent(
|
||||
'Warning: Action cannot be undone',
|
||||
);
|
||||
});
|
||||
|
||||
it('should update reactive children', async () => {
|
||||
const [showTitle, setShowTitle] = createSignal(false);
|
||||
const { container } = render(() => (
|
||||
<Icon
|
||||
iconNode={airVent}
|
||||
role="img"
|
||||
aria-labelledby="dynamic-title"
|
||||
>
|
||||
{showTitle() && <title id="dynamic-title">Dynamic title</title>}
|
||||
</Icon>
|
||||
));
|
||||
|
||||
expect(container.querySelector('title')).toBeNull();
|
||||
|
||||
setShowTitle(true);
|
||||
flush();
|
||||
|
||||
expect(container.querySelector('title#dynamic-title')).toHaveTextContent('Dynamic title');
|
||||
});
|
||||
|
||||
it('should never override aria-hidden prop', async () => {
|
||||
|
||||
@@ -87,4 +87,19 @@ describe('Using lucide icon components', () => {
|
||||
expect(container.firstChild).toHaveClass('lucide');
|
||||
expect(container.firstChild).toHaveClass('lucide-droplet');
|
||||
});
|
||||
|
||||
it('should render child elements inside icon components', () => {
|
||||
const { container } = render(() => (
|
||||
<Droplet
|
||||
role="img"
|
||||
aria-labelledby="droplet-title"
|
||||
>
|
||||
<title id="droplet-title">Water droplet</title>
|
||||
</Droplet>
|
||||
));
|
||||
|
||||
expect(container.firstChild).not.toHaveAttribute('aria-hidden');
|
||||
expect(container.firstChild).toHaveAttribute('aria-labelledby', 'droplet-title');
|
||||
expect(container.querySelector('title#droplet-title')).toHaveTextContent('Water droplet');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user