Skip to content

Commit c4adf5d

Browse files
authored
Merge pull request #371 from dcastil/bugfix/370/fix-stroke-width-merged-with-stroke-color
Fix stroke-color being incorrectly detected as stroke-width
2 parents ee02ebe + 1ca9250 commit c4adf5d

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/lib/validators.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const stringLengths = new Set(['px', 'full', 'screen'])
44
const tshirtUnitRegex = /^(\d+(\.\d+)?)?(xs|sm|md|lg|xl)$/
55
const lengthUnitRegex =
66
/\d+(%|px|r?em|[sdl]?v([hwib]|min|max)|pt|pc|in|cm|mm|cap|ch|ex|r?lh|cq(w|h|i|b|min|max))|\b(calc|min|max|clamp)\(.+\)|^0$/
7+
const colorFunctionRegex = /^(rgba?|hsla?|hwb|(ok)?(lab|lch))\(.+\)$/
78
// Shadow always begins with x and y offset separated by underscore
89
const shadowRegex = /^-?((\d+)?\.?(\d+)[a-z]+|0)_-?((\d+)?\.?(\d+)[a-z]+|0)/
910
const imageRegex =
@@ -84,7 +85,10 @@ function getIsArbitraryValue(
8485
}
8586

8687
function isLengthOnly(value: string) {
87-
return lengthUnitRegex.test(value)
88+
// `colorFunctionRegex` check is necessary because color functions can have percentages in them which which would be incorrectly classified as lengths.
89+
// For example, `hsl(0 0% 0%)` would be classified as a length without this check.
90+
// I could also use lookbehind assertion in `lengthUnitRegex` but that isn't supported widely enough.
91+
return lengthUnitRegex.test(value) && !colorFunctionRegex.test(value)
8892
}
8993

9094
function isNever() {

tests/colors.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ import { twMerge } from '../src'
33
test('handles color conflicts properly', () => {
44
expect(twMerge('bg-grey-5 bg-hotpink')).toBe('bg-hotpink')
55
expect(twMerge('hover:bg-grey-5 hover:bg-hotpink')).toBe('hover:bg-hotpink')
6+
expect(twMerge('stroke-[hsl(350_80%_0%)] stroke-[10px]')).toBe(
7+
'stroke-[hsl(350_80%_0%)] stroke-[10px]',
8+
)
69
})

0 commit comments

Comments
 (0)