From 7f6a17633392968baacdf5491ac45c044d010399 Mon Sep 17 00:00:00 2001 From: stu Date: Sun, 14 Apr 2024 02:41:49 +0800 Subject: [PATCH 1/2] 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; } From 0edd65ff733568c05582549b666659df0cb0cdfd Mon Sep 17 00:00:00 2001 From: stu Date: Sun, 14 Apr 2024 03:03:20 +0800 Subject: [PATCH 2/2] fix a test that was broken with corrected output from this change --- .../@mantine/core/src/core/utils/units-converters/rem.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@mantine/core/src/core/utils/units-converters/rem.test.ts b/packages/@mantine/core/src/core/utils/units-converters/rem.test.ts index 01476254ae2..138ce6c1b44 100644 --- a/packages/@mantine/core/src/core/utils/units-converters/rem.test.ts +++ b/packages/@mantine/core/src/core/utils/units-converters/rem.test.ts @@ -70,7 +70,7 @@ describe('@mantine/units-converters/rem', () => { it('correctly transforms a list of coma separated values', () => { expect(rem('0 0, 0 4px, 4px -4px, -4px 0')).toBe( - '0rem 0rem,0rem 0rem calc(0.25rem * var(--mantine-scale)),0rem calc(0.25rem * var(--mantine-scale)) calc(-0.25rem * var(--mantine-scale)),0rem calc(-0.25rem * var(--mantine-scale)) 0rem' + '0rem 0rem, 0rem calc(0.25rem * var(--mantine-scale)), calc(0.25rem * var(--mantine-scale)) calc(-0.25rem * var(--mantine-scale)), calc(-0.25rem * var(--mantine-scale)) 0rem' ); }); });