Skip to content

Commit 947aa04

Browse files
committed
add support for arbitrary variants in modifier sorting
1 parent 43b9dfa commit 947aa04

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/lib/merge-classlist.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { ConfigUtils } from './config-utils'
22

33
const SPLIT_CLASSES_REGEX = /\s+/
44
const IMPORTANT_MODIFIER = '!'
5-
const MODIFIER_SEPARATOR = ':'
65

76
export function mergeClassList(classList: string, configUtils: ConfigUtils) {
87
const { getClassGroupId, getConflictingClassGroupIds } = configUtils
@@ -33,18 +32,15 @@ export function mergeClassList(classList: string, configUtils: ConfigUtils) {
3332
}
3433
}
3534

36-
const variantModifier =
37-
modifiers.length === 0
38-
? ''
39-
: modifiers.sort().concat('').join(MODIFIER_SEPARATOR)
35+
const variantModifier = sortModifiers(modifiers).join('')
4036

41-
const fullModifier = hasImportantModifier
37+
const modifierId = hasImportantModifier
4238
? variantModifier + IMPORTANT_MODIFIER
4339
: variantModifier
4440

4541
return {
4642
isTailwindClass: true as const,
47-
modifier: fullModifier,
43+
modifierId,
4844
classGroupId,
4945
originalClassName,
5046
}
@@ -56,9 +52,9 @@ export function mergeClassList(classList: string, configUtils: ConfigUtils) {
5652
return true
5753
}
5854

59-
const { modifier, classGroupId } = parsed
55+
const { modifierId, classGroupId } = parsed
6056

61-
const classId = `${modifier}${classGroupId}`
57+
const classId = `${modifierId}${classGroupId}`
6258

6359
if (classGroupsInConflict.has(classId)) {
6460
return false
@@ -67,7 +63,7 @@ export function mergeClassList(classList: string, configUtils: ConfigUtils) {
6763
classGroupsInConflict.add(classId)
6864

6965
getConflictingClassGroupIds(classGroupId).forEach((group) =>
70-
classGroupsInConflict.add(`${modifier}${group}`)
66+
classGroupsInConflict.add(`${modifierId}${group}`)
7167
)
7268

7369
return true
@@ -111,3 +107,32 @@ function splitModifiers(className: string) {
111107
baseClassName,
112108
}
113109
}
110+
111+
/**
112+
* Sorts modifiers according to following schema:
113+
* - Predefined modifiers are sorted alphabetically
114+
* - When an arbitrary variant appears, it's important to preserve which modifiers are before and after it
115+
*/
116+
function sortModifiers(modifiers: string[]) {
117+
if (modifiers.length <= 1) {
118+
return modifiers
119+
}
120+
121+
const sortedModifiers = []
122+
let unsortedModifiers: string[] = []
123+
124+
modifiers.forEach((modifier) => {
125+
const isArbitraryVariant = modifier[0] === '['
126+
127+
if (isArbitraryVariant) {
128+
sortedModifiers.push(...unsortedModifiers.sort(), modifier)
129+
unsortedModifiers = []
130+
} else {
131+
unsortedModifiers.push(modifier)
132+
}
133+
})
134+
135+
sortedModifiers.push(...unsortedModifiers.sort())
136+
137+
return sortedModifiers
138+
}

0 commit comments

Comments
 (0)