Don't emit NaN stroke width for non-numeric absolute lengths
`absoluteStrokeWidth` scales the stroke by `Number(size)`, so a CSS unit such as `1em` produced `NaN` and a size of `0` produced `Infinity` – both invalid as a `stroke-width` attribute. Fall back to the unscaled stroke width whenever the conversion isn't finite, so the deprecated prop degrades rather than breaking the rendered icon.
This commit is contained in:
parent
69d2476af3
commit
b65ee94555
2 changed files with 41 additions and 5 deletions
|
|
@ -30,11 +30,18 @@ function buildLucideIconNode(icon: LucideIconData, params: LucideBuildParams = {
|
||||||
? mergeClasses(...classNamesFromClassName)
|
? mergeClasses(...classNamesFromClassName)
|
||||||
: mergeClasses('lucide', ...iconClassNames, ...classNamesFromClassName);
|
: mergeClasses('lucide', ...iconClassNames, ...classNamesFromClassName);
|
||||||
|
|
||||||
const calculatedStrokeWidth = params.absoluteStrokeWidth
|
const scaledStrokeWidth =
|
||||||
? (Number(params.strokeWidth ?? defaultAttributes['stroke-width']) *
|
(Number(params.strokeWidth ?? defaultAttributes['stroke-width']) *
|
||||||
Number(icon.size ?? icon.width ?? defaultAttributes['width'])) /
|
Number(icon.size ?? icon.width ?? defaultAttributes['width'])) /
|
||||||
Number(params.size ?? params.width ?? defaultAttributes['width'])
|
Number(params.size ?? params.width ?? defaultAttributes['width']);
|
||||||
: (params.strokeWidth ?? defaultAttributes['stroke-width']);
|
|
||||||
|
// `absoluteStrokeWidth` can only scale numeric lengths. A CSS unit such as `1em`, or a size of
|
||||||
|
// `0`, makes the conversion non-finite, so fall back to the unscaled width rather than emitting
|
||||||
|
// `NaN` as the stroke width.
|
||||||
|
const calculatedStrokeWidth =
|
||||||
|
params.absoluteStrokeWidth && Number.isFinite(scaledStrokeWidth)
|
||||||
|
? scaledStrokeWidth
|
||||||
|
: (params.strokeWidth ?? defaultAttributes['stroke-width']);
|
||||||
|
|
||||||
const attributes = {
|
const attributes = {
|
||||||
...Object.entries(defaultAttributes).reduce((attrs, [attrName, value]) => {
|
...Object.entries(defaultAttributes).reduce((attrs, [attrName, value]) => {
|
||||||
|
|
|
||||||
29
packages/shared/tests/buildLucideIconNode.spec.ts
Normal file
29
packages/shared/tests/buildLucideIconNode.spec.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import buildLucideIconNode from '../src/build/buildLucideIconNode';
|
||||||
|
|
||||||
|
const icon = { name: 'circle', size: 24, node: [] };
|
||||||
|
|
||||||
|
const strokeWidthOf = (params: Parameters<typeof buildLucideIconNode>[1]) =>
|
||||||
|
buildLucideIconNode(icon, params)[1]['stroke-width'];
|
||||||
|
|
||||||
|
describe('buildLucideIconNode absoluteStrokeWidth', () => {
|
||||||
|
it('scales the stroke width for numeric sizes', () => {
|
||||||
|
expect(strokeWidthOf({ absoluteStrokeWidth: true, strokeWidth: 2, size: 48 })).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to the unscaled width for a CSS unit size', () => {
|
||||||
|
expect(strokeWidthOf({ absoluteStrokeWidth: true, strokeWidth: 2, size: '1em' })).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to the unscaled width for a non-numeric stroke width', () => {
|
||||||
|
expect(strokeWidthOf({ absoluteStrokeWidth: true, strokeWidth: '1em', size: 48 })).toBe('1em');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to the unscaled width for a zero size', () => {
|
||||||
|
expect(strokeWidthOf({ absoluteStrokeWidth: true, strokeWidth: 2, size: 0 })).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('leaves the stroke width alone when not absolute', () => {
|
||||||
|
expect(strokeWidthOf({ strokeWidth: 2, size: 48 })).toBe(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue