Skip to content

desmondinho/tailwind-merge-dotnet

Repository files navigation

tailwind-merge-dotnet

A utility to efficiently merge Tailwind CSS classes in C# without style conflicts.

  • Supports Tailwind v3.4
  • Thread-safe LRU caching

Prerequisites

.NET SDK Support
.NET 8 ✔️

Usage

Register the service in the DI container:

// Program.cs

using TailwindMerge.Extensions;

var builder = WebApplication.CreateBuilder( args );

// Add TailwindMerge to the container
builder.Services.AddTailwindMerge();

Inject the service into component using one of the following approaches:

@* Page.razor *@

@inject TwMerge TwMerge

@TwMerge.Merge("px-2 py-1 bg-red hover:bg-dark-red", "p-3 bg-[#B91C1C]")
@* → "hover:bg-dark-red p-3 bg-[#B91C1C]" *@
// Page.razor.cs

[Inject] private TwMerge TwMerge {get; set; } = default!;

TwMerge.Merge("px-2 py-1 bg-red hover:bg-dark-red", "p-3 bg-[#B91C1C]")
// → "hover:bg-dark-red p-3 bg-[#B91C1C]"

Contributing

Contributions are always welcome!

See CONTRIBUTING.md for ways to get started. Please adhere to this project's CODE_OF_CONDUCT.

Acknowledgements 🙏

This project is a C# adaptation of tailwind-merge originally developed by dcastil.