From c1a02a04d4f3f7049d44567d62b8b06cff0b9103 Mon Sep 17 00:00:00 2001 From: Ken Kunz Date: Fri, 20 Nov 2026 14:43:11 +0100 Subject: [PATCH] fix(packages): allow string values for `size` and `strokeWidth` in global icon config (#4870) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Allow string values for `size` and `strokeWidth` in icon contexts The global icon context types declare `size` and `strokeWidth` as `number`, while the component props they supply defaults for accept `number | string`. The context value is passed through to the component untouched, so a CSS length that works on a single icon is rejected when set as a default: setLucideProps({ size: '1em' }); // Type 'string' is not assignable to 'number' // same value, same code path, no error * Accept a string `size` on vue icon components @lucide/vue types the component prop as `size?: 24 | number`, so it rejects the CSS lengths its own `strokeWidth?: number | string` accepts, and that every other framework accepts. The value goes straight to the `width`/`height` attributes, so a string already works at runtime. * Allow string `size` and `strokeWidth` in the icon context Copilot's PR comment correctly surfaced a gap in the PR - the same type bug exists in the lucide-react-native project and wasn't fixed there. Same narrowing as the other framework contexts: `LucideProps` accepts `size?: string | number`, and `strokeWidth` resolves to react-native-svg's `NumberProp` (`string | number`), but the context and `LucideProviderProps` declared both as `number`. `Icon.ts` passes the context value straight through — `width: width ?? size ?? contextSize` — so nothing downstream needs a number. * 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. * fix(packages/angular): allow a string `strokeWidth` in the global config `LucideConfig` already types `size` as `string | number` but left `strokeWidth` as `number`, while `LucideIconBase` accepts `string | number` for both and passes the configured value straight to the shared builder. So `provideLucideConfig({ strokeWidth: '1em' })` was rejected for no reason. * refactor(shared): keep absolute stroke scaling inside its branch The finite-value fallback added in b65ee945 computed the scaled width before testing `absoluteStrokeWidth`, so the default path paid for three `Number` conversions and a division it never used. Start from the unscaled width and overwrite it only when scaling both applies and yields a finite result, which also states the rule more directly. --------- Co-authored-by: Eric Fennis --- packages/angular/src/lucide-config.ts | 2 +- packages/lucide-preact/src/context.ts | 8 ++--- packages/lucide-react-native/src/context.ts | 8 ++--- packages/lucide-react/src/context.ts | 4 +-- packages/lucide-solid/src/context.tsx | 8 ++--- .../shared/src/build/buildLucideIconNode.ts | 15 +++++++--- .../shared/tests/buildLucideIconNode.spec.ts | 29 +++++++++++++++++++ packages/svelte/src/context.ts | 4 +-- packages/vue/src/context.ts | 4 +-- packages/vue/src/types.ts | 2 +- 10 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 packages/shared/tests/buildLucideIconNode.spec.ts diff --git a/packages/angular/src/lucide-config.ts b/packages/angular/src/lucide-config.ts index 3944638f1..66d0ef6ae 100644 --- a/packages/angular/src/lucide-config.ts +++ b/packages/angular/src/lucide-config.ts @@ -18,7 +18,7 @@ export interface LucideConfig { * Stroke width * @default 2 */ - strokeWidth: number; + strokeWidth: string | number; /** * Whether to use absolute stroke width. * @deprecated Use `nonScalingStroke` instead. diff --git a/packages/lucide-preact/src/context.ts b/packages/lucide-preact/src/context.ts index f839258b6..fd67838c4 100644 --- a/packages/lucide-preact/src/context.ts +++ b/packages/lucide-preact/src/context.ts @@ -2,9 +2,9 @@ import { createContext, h, type ComponentChildren } from 'preact'; import { useContext, useMemo } from 'preact/hooks'; const LucideContext = createContext<{ - size?: number; + size?: number | string; color?: string; - strokeWidth?: number; + strokeWidth?: number | string; /** * @deprecated Use `nonScalingStroke` instead. */ @@ -22,9 +22,9 @@ const LucideContext = createContext<{ interface LucideProviderProps { children: ComponentChildren; - size?: number; + size?: number | string; color?: string; - strokeWidth?: number; + strokeWidth?: number | string; /** * @deprecated Use `nonScalingStroke` instead. */ diff --git a/packages/lucide-react-native/src/context.ts b/packages/lucide-react-native/src/context.ts index 347f90592..31eea1561 100644 --- a/packages/lucide-react-native/src/context.ts +++ b/packages/lucide-react-native/src/context.ts @@ -1,9 +1,9 @@ import { createContext, createElement, type ReactNode, useContext, useMemo } from 'react'; const LucideContext = createContext<{ - size?: number; + size?: number | string; color?: string; - strokeWidth?: number; + strokeWidth?: number | string; /** * @deprecated Use `nonScalingStroke` instead. */ @@ -21,9 +21,9 @@ const LucideContext = createContext<{ interface LucideProviderProps { children: ReactNode; - size?: number; + size?: number | string; color?: string; - strokeWidth?: number; + strokeWidth?: number | string; /** * @deprecated Use `nonScalingStroke` instead. */ diff --git a/packages/lucide-react/src/context.ts b/packages/lucide-react/src/context.ts index ceb5c79d0..2c261dd83 100644 --- a/packages/lucide-react/src/context.ts +++ b/packages/lucide-react/src/context.ts @@ -4,9 +4,9 @@ import { createContext, createElement, type ReactNode, useContext, useMemo } fro import { LucideProps } from './types'; type LucideConfig = { - size: number; + size: number | string; color: string; - strokeWidth: number; + strokeWidth: number | string; /** * @deprecated Use `nonScalingStroke` instead. */ diff --git a/packages/lucide-solid/src/context.tsx b/packages/lucide-solid/src/context.tsx index c12f31109..63996ed7b 100644 --- a/packages/lucide-solid/src/context.tsx +++ b/packages/lucide-solid/src/context.tsx @@ -1,9 +1,9 @@ import { createContext, splitProps, type JSXElement } from 'solid-js'; export const LucideContext = createContext<{ - size?: number; + size?: number | string; color?: string; - strokeWidth?: number; + strokeWidth?: number | string; /** * @deprecated Use `nonScalingStroke` instead. */ @@ -21,9 +21,9 @@ export const LucideContext = createContext<{ interface LucideProviderProps { children: JSXElement; - size?: number; + size?: number | string; color?: string; - strokeWidth?: number; + strokeWidth?: number | string; /** * @deprecated Use `nonScalingStroke` instead. */ diff --git a/packages/shared/src/build/buildLucideIconNode.ts b/packages/shared/src/build/buildLucideIconNode.ts index a88f323e7..914146317 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']) * + let calculatedStrokeWidth = params.strokeWidth ?? defaultAttributes['stroke-width']; + + if (params.absoluteStrokeWidth) { + const scaled = + (Number(calculatedStrokeWidth) * Number(icon.size ?? icon.width ?? defaultAttributes['width'])) / - Number(params.size ?? params.width ?? defaultAttributes['width']) - : (params.strokeWidth ?? defaultAttributes['stroke-width']); + Number(params.size ?? params.width ?? defaultAttributes['width']); + + // Only numeric lengths can be scaled; a CSS unit such as `1em`, or a size of `0`, makes the + // conversion non-finite, so the unscaled width stands rather than emitting `NaN`. + if (Number.isFinite(scaled)) calculatedStrokeWidth = scaled; + } 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); + }); +}); diff --git a/packages/svelte/src/context.ts b/packages/svelte/src/context.ts index 03828114b..ffd391e27 100644 --- a/packages/svelte/src/context.ts +++ b/packages/svelte/src/context.ts @@ -4,8 +4,8 @@ const LucideContext = Symbol('lucide-context'); export interface LucideGlobalContext { color?: string; - size?: number; - strokeWidth?: number; + size?: number | string; + strokeWidth?: number | string; /** * @deprecated Use `nonScalingStroke` instead. */ diff --git a/packages/vue/src/context.ts b/packages/vue/src/context.ts index 0435ed55c..7c2a2851b 100644 --- a/packages/vue/src/context.ts +++ b/packages/vue/src/context.ts @@ -3,9 +3,9 @@ import { provide, inject } from 'vue'; export const LUCIDE_CONTEXT = Symbol('lucide-icons'); interface LucideIconsContext { - size?: number; + size?: number | string; color?: string; - strokeWidth?: number; + strokeWidth?: number | string; /** * @deprecated Use `nonScalingStroke` instead. */ diff --git a/packages/vue/src/types.ts b/packages/vue/src/types.ts index f292be26a..2dd2a787e 100644 --- a/packages/vue/src/types.ts +++ b/packages/vue/src/types.ts @@ -5,7 +5,7 @@ import type { } from '@lucide/shared/types'; export interface LucideProps extends Partial { - size?: 24 | number; + size?: 24 | number | string; strokeWidth?: number | string; /** * @deprecated Use `nonScalingStroke` instead.