Merge Tailwind CSS classes without conflicts in python.
Project description
Tailwind Merge Python
A Python utility for merging Tailwind CSS class lists intelligently, resolving conflicts based on Tailwind's principles. Inspired by the popular tailwind-merge JavaScript package.
This utility ensures that when you combine multiple strings of Tailwind classes (e.g., from different component states or logic branches), the resulting string is clean, minimal, and applies the intended styles by removing redundant or conflicting classes based on their function.
Installation
Using pip:
pip install tailwind-merge
Usage
from tailwind_merge import TailwindMerge
# Initialize
twm = TailwindMerge()
result = twm.merge(
"p-4 w-6 text-blue-500", # Initial classes
"w-8 text-red-500" # Overrides for width and text color
)
# "p-4 text-red-500 w-8"
result = twm.merge("pl-4", "pr-6 pl-2")
# "pl-2 pr-6"
result = twm.merge("p-2 hover:p-4", "p-3")
# "hover:p-4 p-3"
result = twm.merge("hover:p-2", "focus:p-1 hover:p-4")
# "hover:p-4 focus:p-1"
result = twm.merge("p-1", "p-[2px]")
# "p-[2px]"
result = twm.merge(
"flex items-center justify-center", # Base layout
"justify-between", # Override justify
"text-red-500", # Add text color
"hover:text-blue-500 text-lg" # Add hover color and text size
)
# "flex items-center justify-between text-red-500 hover:text-blue-500 text-lg"
Custom Rules
twm.add_rule('custom-icon-size', ['icon-sm', 'icon-md', 'icon-lg'])
twm.merge("icon-sm icon-lg")
# "icon-lg"
Features
- Conflict Resolution: Correctly identifies and resolves conflicting Tailwind classes based on their utility function, keeping the last applied class within a specific conflict group.
- Modifier Support: Handles Tailwind modifiers (
hover:,focus:,md:,dark:, etc.). Conflicts are resolved independently for base styles and each unique modifier combination (e.g.,hover:text-red-500conflicts withhover:text-green-500but not withfocus:p-4orp-4). - Arbitrary Value Support: Recognizes and correctly groups classes with arbitrary values (e.g.,
p-[3px],w-[calc(100%-theme(spacing.4))],text-[#FF0000]). - Prefix Matching: Uses longest-prefix matching to correctly categorize classes when prefixes might overlap (e.g., correctly identifies
border-t-2as belonging toborder-width-topbefore matching the shorterborder-prefix). - Order Preservation: Aims to preserve the relative order of the final classes as they appeared in the input strings.
- Custom classes: Allows adding custom conflict rules using the
add_rulemethod for project-specific utilities or third-party libraries. - Zero Dependencies: Pure Python implementation with no external library requirements.
Contributing
Contributions are welcome! If you find a bug, have a feature request, or want to improve the class definitions, please feel free to open an issue or submit a Pull Request. Ensure tests pass and consider adding new tests for your changes.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tailwind_merge-0.3.3.tar.gz.
File metadata
- Download URL: tailwind_merge-0.3.3.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.18 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be19cde831cedafed24078f28f7014224398449fab436a651fd39e96a723a9c8
|
|
| MD5 |
b8ce14474ba3fa17b56a893c2ee3cb9a
|
|
| BLAKE2b-256 |
ad58241aa4c1e4459170a4e0e938520181f3ddbcc8275c7b11f6d95b0b6baaca
|
File details
Details for the file tailwind_merge-0.3.3-py3-none-any.whl.
File metadata
- Download URL: tailwind_merge-0.3.3-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.18 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e913e108040ae4b5bb906b90556c115a644f8ba6c0b78d8fcc86800125eb0ed
|
|
| MD5 |
a633983547399366e0f0c296d7808405
|
|
| BLAKE2b-256 |
cf560e580fb3459254cd02bd734029392743345d3c72306a0de8037f78091731
|