From b65ee94555c07536ed9b3111e38c45904f63cc74 Mon Sep 17 00:00:00 2001 From: Ken Kunz Date: Wed, 16 Sep 2026 10:18:01 -0500 Subject: [PATCH] Don't emit NaN stroke width for non-numeric absolute lengths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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. --- .../shared/src/build/buildLucideIconNode.ts | 17 +++++++---- .../shared/tests/buildLucideIconNode.spec.ts | 29 +++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 packages/shared/tests/buildLucideIconNode.spec.ts diff --git a/packages/shared/src/build/buildLucideIconNode.ts b/packages/shared/src/build/buildLucideIconNode.ts index a88f323e7..b9039d7e7 100644 --- a/packages/shared/src/build/buildLucideIconNode.ts +++ b/packages/shared/src/build/buildLucideIconNode.ts @@ -30,11 +30,18 @@ function buildLucideIconNode(icon: LucideIconData, params: LucideBuildParams = { ? mergeClasses(...classNamesFromClassName) : mergeClasses('lucide', ...iconClassNames, ...classNamesFromClassName); - const calculatedStrokeWidth = params.absoluteStrokeWidth - ? (Number(params.strokeWidth ?? defaultAttributes['stroke-width']) * - Number(icon.size ?? icon.width ?? defaultAttributes['width'])) / - Number(params.size ?? params.width ?? defaultAttributes['width']) - : (params.strokeWidth ?? defaultAttributes['stroke-width']); + const scaledStrokeWidth = + (Number(params.strokeWidth ?? defaultAttributes['stroke-width']) * + Number(icon.size ?? icon.width ?? defaultAttributes['width'])) / + Number(params.size ?? params.width ?? defaultAttributes['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 = { ...Object.entries(defaultAttributes).reduce((attrs, [attrName, value]) => { diff --git a/packages/shared/tests/buildLucideIconNode.spec.ts b/packages/shared/tests/buildLucideIconNode.spec.ts new file mode 100644 index 000000000..aeecc001b --- /dev/null +++ b/packages/shared/tests/buildLucideIconNode.spec.ts @@ -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[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); + }); +});