From 7f6a17633392968baacdf5491ac45c044d010399 Mon Sep 17 00:00:00 2001 From: stu Date: Sun, 14 Apr 2024 02:41:49 +0800 Subject: [PATCH] Spacing: fix resolver for empty strings, and strings with spaces --- .../resolvers/spacing-resolver/spacing-resolver.test.ts | 5 +++++ .../@mantine/core/src/core/utils/units-converters/rem.ts | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/packages/@mantine/core/src/core/Box/style-props/resolvers/spacing-resolver/spacing-resolver.test.ts b/packages/@mantine/core/src/core/Box/style-props/resolvers/spacing-resolver/spacing-resolver.test.ts index 84e59cc964c..cfa43dc450a 100644 --- a/packages/@mantine/core/src/core/Box/style-props/resolvers/spacing-resolver/spacing-resolver.test.ts +++ b/packages/@mantine/core/src/core/Box/style-props/resolvers/spacing-resolver/spacing-resolver.test.ts @@ -20,4 +20,9 @@ describe('@mantine/core/Box/spacing-resolver', () => { expect(spacingResolver('-10px', DEFAULT_THEME)).toBe(rem(-10)); expect(spacingResolver('1rem', DEFAULT_THEME)).toBe(rem('1rem')); }); + + it('resolves empty strings correctly', () => { + expect(spacingResolver('', DEFAULT_THEME)).toBe(rem('')); + expect(spacingResolver(' 10px', DEFAULT_THEME)).toBe(` ${rem(10)}`); + }); }); diff --git a/packages/@mantine/core/src/core/utils/units-converters/rem.ts b/packages/@mantine/core/src/core/utils/units-converters/rem.ts index 5c96b7b1434..9aa14cd5947 100644 --- a/packages/@mantine/core/src/core/utils/units-converters/rem.ts +++ b/packages/@mantine/core/src/core/utils/units-converters/rem.ts @@ -18,6 +18,11 @@ function createConverter(units: string, { shouldScale = false } = {}) { } if (typeof value === 'string') { + // Number("") === 0 so exit early + if (value === '') { + return value; + } + if (value.startsWith('calc(') || value.startsWith('clamp(') || value.includes('rgba(')) { return value; }