tailwind-merge


Nametailwind-merge JSON
Version 0.3.3 PyPI version JSON
download
home_pageNone
SummaryMerge Tailwind CSS classes without conflicts in python.
upload_time2025-07-23 20:45:58
maintainerNone
docs_urlNone
authorWill Abbott
requires_python<4.0,>=3.8
licenseMIT
keywords python tailwind css merge
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Tailwind Merge Python

![PyPI](https://img.shields.io/pypi/v/tailwind-merge?color=blue&style=flat-square)
![PyPI - Downloads](https://img.shields.io/pypi/dm/tailwind-merge?color=blue&style=flat-square)

A Python utility for merging Tailwind CSS class lists intelligently, resolving conflicts based on Tailwind's principles. Inspired by the popular [tailwind-merge](https://github.com/dcastil/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:
```bash
pip install tailwind-merge 
```

## Usage

```python
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
```python
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-500` conflicts with `hover:text-green-500` but not with `focus:p-4` or `p-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-2` as belonging to `border-width-top` before matching the shorter `border-` 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_rule` method 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.

---
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tailwind-merge",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": "python, tailwind, css, merge",
    "author": "Will Abbott",
    "author_email": "willabb83@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ad/58/241aa4c1e4459170a4e0e938520181f3ddbcc8275c7b11f6d95b0b6baaca/tailwind_merge-0.3.3.tar.gz",
    "platform": null,
    "description": "# Tailwind Merge Python\n\n![PyPI](https://img.shields.io/pypi/v/tailwind-merge?color=blue&style=flat-square)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/tailwind-merge?color=blue&style=flat-square)\n\nA Python utility for merging Tailwind CSS class lists intelligently, resolving conflicts based on Tailwind's principles. Inspired by the popular [tailwind-merge](https://github.com/dcastil/tailwind-merge) JavaScript package.\n\nThis 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.\n\n## Installation\n\nUsing pip:\n```bash\npip install tailwind-merge \n```\n\n## Usage\n\n```python\nfrom tailwind_merge import TailwindMerge \n\n# Initialize \ntwm = TailwindMerge()\n\nresult = twm.merge(\n    \"p-4 w-6 text-blue-500\", # Initial classes\n    \"w-8 text-red-500\"       # Overrides for width and text color\n)\n# \"p-4 text-red-500 w-8\" \n\nresult = twm.merge(\"pl-4\", \"pr-6 pl-2\") \n# \"pl-2 pr-6\"\n\nresult = twm.merge(\"p-2 hover:p-4\", \"p-3\") \n# \"hover:p-4 p-3\"\n\nresult = twm.merge(\"hover:p-2\", \"focus:p-1 hover:p-4\") \n# \"hover:p-4 focus:p-1\"\n\nresult = twm.merge(\"p-1\", \"p-[2px]\")\n# \"p-[2px]\"\n\nresult = twm.merge(\n    \"flex items-center justify-center\", # Base layout\n    \"justify-between\",                  # Override justify\n    \"text-red-500\",                     # Add text color\n    \"hover:text-blue-500 text-lg\"       # Add hover color and text size\n)\n# \"flex items-center justify-between text-red-500 hover:text-blue-500 text-lg\"\n```\n### Custom Rules\n```python\ntwm.add_rule('custom-icon-size', ['icon-sm', 'icon-md', 'icon-lg'])\ntwm.merge(\"icon-sm icon-lg\")\n# \"icon-lg\"\n```\n\n## Features\n\n-   **Conflict Resolution:** Correctly identifies and resolves conflicting Tailwind classes based on their utility function, keeping the last applied class within a specific conflict group.\n-   **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-500` conflicts with `hover:text-green-500` but not with `focus:p-4` or `p-4`).\n-   **Arbitrary Value Support:** Recognizes and correctly groups classes with arbitrary values (e.g., `p-[3px]`, `w-[calc(100%-theme(spacing.4))]`, `text-[#FF0000]`).\n-   **Prefix Matching:** Uses longest-prefix matching to correctly categorize classes when prefixes might overlap (e.g., correctly identifies `border-t-2` as belonging to `border-width-top` before matching the shorter `border-` prefix).\n-   **Order Preservation:** Aims to preserve the relative order of the *final* classes as they appeared in the input strings.\n-   **Custom classes:** Allows adding custom conflict rules using the `add_rule` method for project-specific utilities or third-party libraries.\n-   **Zero Dependencies:** Pure Python implementation with no external library requirements.\n\n## Contributing\n\nContributions 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.\n\n---",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Merge Tailwind CSS classes without conflicts in python.",
    "version": "0.3.3",
    "project_urls": {
        "Bug Tracker": "https://github.com/wrabit/tailwind-merge-python/issues",
        "Homepage": "https://github.com/wrabit/tailwind-merge-python"
    },
    "split_keywords": [
        "python",
        " tailwind",
        " css",
        " merge"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cf560e580fb3459254cd02bd734029392743345d3c72306a0de8037f78091731",
                "md5": "a633983547399366e0f0c296d7808405",
                "sha256": "7e913e108040ae4b5bb906b90556c115a644f8ba6c0b78d8fcc86800125eb0ed"
            },
            "downloads": -1,
            "filename": "tailwind_merge-0.3.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a633983547399366e0f0c296d7808405",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 9262,
            "upload_time": "2025-07-23T20:45:57",
            "upload_time_iso_8601": "2025-07-23T20:45:57.380977Z",
            "url": "https://files.pythonhosted.org/packages/cf/56/0e580fb3459254cd02bd734029392743345d3c72306a0de8037f78091731/tailwind_merge-0.3.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ad58241aa4c1e4459170a4e0e938520181f3ddbcc8275c7b11f6d95b0b6baaca",
                "md5": "b8ce14474ba3fa17b56a893c2ee3cb9a",
                "sha256": "be19cde831cedafed24078f28f7014224398449fab436a651fd39e96a723a9c8"
            },
            "downloads": -1,
            "filename": "tailwind_merge-0.3.3.tar.gz",
            "has_sig": false,
            "md5_digest": "b8ce14474ba3fa17b56a893c2ee3cb9a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 8756,
            "upload_time": "2025-07-23T20:45:58",
            "upload_time_iso_8601": "2025-07-23T20:45:58.437830Z",
            "url": "https://files.pythonhosted.org/packages/ad/58/241aa4c1e4459170a4e0e938520181f3ddbcc8275c7b11f6d95b0b6baaca/tailwind_merge-0.3.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-23 20:45:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "wrabit",
    "github_project": "tailwind-merge-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "tailwind-merge"
}
        
Elapsed time: 1.45871s