tailwind-merge
Advanced tools
Comparing version 2.4.0-dev.38db48d362b45aab9ba7e2e05aef4c36abab6edb to 2.4.0-dev.488f9130f2ccc3696d37060104f967401b56da74
{ | ||
"name": "tailwind-merge", | ||
"version": "2.4.0-dev.38db48d362b45aab9ba7e2e05aef4c36abab6edb", | ||
"version": "2.4.0-dev.488f9130f2ccc3696d37060104f967401b56da74", | ||
"description": "Merge Tailwind CSS classes without style conflicts", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -6,3 +6,3 @@ <!-- This file is autogenerated. If you want to change this content, please do the changes in `./docs/README.md` instead. --> | ||
<a href="https://github.com/dcastil/tailwind-merge"> | ||
<img src="https://github.com/dcastil/tailwind-merge/raw/38db48d362b45aab9ba7e2e05aef4c36abab6edb/assets/logo.svg" alt="tailwind-merge" height="150px" /> | ||
<img src="https://github.com/dcastil/tailwind-merge/raw/488f9130f2ccc3696d37060104f967401b56da74/assets/logo.svg" alt="tailwind-merge" height="150px" /> | ||
</a> | ||
@@ -29,12 +29,12 @@ </div> | ||
- [What is it for](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/what-is-it-for.md) | ||
- [When and how to use it](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/when-and-how-to-use-it.md) | ||
- [Features](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/features.md) | ||
- [Limitations](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/limitations.md) | ||
- [Configuration](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/configuration.md) | ||
- [Recipes](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/recipes.md) | ||
- [API reference](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/api-reference.md) | ||
- [Writing plugins](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/writing-plugins.md) | ||
- [Versioning](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/versioning.md) | ||
- [Contributing](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/contributing.md) | ||
- [Similar packages](https://github.com/dcastil/tailwind-merge/tree/38db48d362b45aab9ba7e2e05aef4c36abab6edb/docs/similar-packages.md) | ||
- [What is it for](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/what-is-it-for.md) | ||
- [When and how to use it](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/when-and-how-to-use-it.md) | ||
- [Features](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/features.md) | ||
- [Limitations](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/limitations.md) | ||
- [Configuration](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/configuration.md) | ||
- [Recipes](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/recipes.md) | ||
- [API reference](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/api-reference.md) | ||
- [Writing plugins](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/writing-plugins.md) | ||
- [Versioning](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/versioning.md) | ||
- [Contributing](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/contributing.md) | ||
- [Similar packages](https://github.com/dcastil/tailwind-merge/tree/488f9130f2ccc3696d37060104f967401b56da74/docs/similar-packages.md) |
import { ConfigUtils } from './config-utils' | ||
import { IMPORTANT_MODIFIER, sortModifiers } from './parse-class-name' | ||
const SPLIT_CLASSES_REGEX = /\s+/ | ||
export const mergeClassList = (classList: string, configUtils: ConfigUtils) => { | ||
@@ -16,84 +14,64 @@ const { parseClassName, getClassGroupId, getConflictingClassGroupIds } = configUtils | ||
*/ | ||
const classGroupsInConflict = new Set<string>() | ||
const classGroupsInConflict: string[] = [] | ||
return ( | ||
classList | ||
.trim() | ||
.split(SPLIT_CLASSES_REGEX) | ||
.map((originalClassName) => { | ||
const { | ||
modifiers, | ||
hasImportantModifier, | ||
baseClassName, | ||
maybePostfixModifierPosition, | ||
} = parseClassName(originalClassName) | ||
let result = '' | ||
let hasPostfixModifier = Boolean(maybePostfixModifierPosition) | ||
let classGroupId = getClassGroupId( | ||
hasPostfixModifier | ||
? baseClassName.substring(0, maybePostfixModifierPosition) | ||
: baseClassName, | ||
) | ||
for (let i = classList.length - 1; i >= 0; ) { | ||
while (classList[i] === ' ') { | ||
--i | ||
} | ||
const nextI = classList.lastIndexOf(' ', i) | ||
const originalClassName = classList.slice(nextI === -1 ? 0 : nextI + 1, i + 1) | ||
i = nextI | ||
if (!classGroupId) { | ||
if (!hasPostfixModifier) { | ||
return { | ||
isTailwindClass: false as const, | ||
originalClassName, | ||
} | ||
} | ||
const { modifiers, hasImportantModifier, baseClassName, maybePostfixModifierPosition } = | ||
parseClassName(originalClassName) | ||
classGroupId = getClassGroupId(baseClassName) | ||
let hasPostfixModifier = Boolean(maybePostfixModifierPosition) | ||
let classGroupId = getClassGroupId( | ||
hasPostfixModifier | ||
? baseClassName.substring(0, maybePostfixModifierPosition) | ||
: baseClassName, | ||
) | ||
if (!classGroupId) { | ||
return { | ||
isTailwindClass: false as const, | ||
originalClassName, | ||
} | ||
} | ||
if (!classGroupId) { | ||
if (!hasPostfixModifier) { | ||
result = originalClassName + (result.length > 0 ? ' ' + result : result) | ||
continue | ||
} | ||
hasPostfixModifier = false | ||
} | ||
classGroupId = getClassGroupId(baseClassName) | ||
const variantModifier = sortModifiers(modifiers).join(':') | ||
if (!classGroupId) { | ||
result = originalClassName + (result.length > 0 ? ' ' + result : result) | ||
continue | ||
} | ||
const modifierId = hasImportantModifier | ||
? variantModifier + IMPORTANT_MODIFIER | ||
: variantModifier | ||
hasPostfixModifier = false | ||
} | ||
return { | ||
isTailwindClass: true as const, | ||
modifierId, | ||
classGroupId, | ||
originalClassName, | ||
hasPostfixModifier, | ||
} | ||
}) | ||
.reverse() | ||
// Last class in conflict wins, so we need to filter conflicting classes in reverse order. | ||
.filter((parsed) => { | ||
if (!parsed.isTailwindClass) { | ||
return true | ||
} | ||
const variantModifier = sortModifiers(modifiers).join(':') | ||
const { modifierId, classGroupId, hasPostfixModifier } = parsed | ||
const modifierId = hasImportantModifier | ||
? variantModifier + IMPORTANT_MODIFIER | ||
: variantModifier | ||
const classId = modifierId + classGroupId | ||
const classId = modifierId + classGroupId | ||
if (classGroupsInConflict.has(classId)) { | ||
return false | ||
} | ||
if (classGroupsInConflict.includes(classId)) { | ||
continue | ||
} | ||
classGroupsInConflict.add(classId) | ||
classGroupsInConflict.push(classId) | ||
getConflictingClassGroupIds(classGroupId, hasPostfixModifier).forEach((group) => | ||
classGroupsInConflict.add(modifierId + group), | ||
) | ||
const conflictGroups = getConflictingClassGroupIds(classGroupId, hasPostfixModifier) | ||
for (let i = 0; i < conflictGroups.length; ++i) { | ||
const group = conflictGroups[i]! | ||
classGroupsInConflict.push(modifierId + group) | ||
} | ||
return true | ||
}) | ||
.reverse() | ||
.map((parsed) => parsed.originalClassName) | ||
.join(' ') | ||
) | ||
result = originalClassName + (result.length > 0 ? ' ' + result : result) | ||
} | ||
return result | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
708816
15507