Laravel Tailwind Merge is a package that automatically resolves Tailwind CSS class conflicts in Laravel. This allows you to merge multiple Tailwind classes and resolves conflicts.
Here's a basic example from the README. Given the following code in a blade component:
// circle.blade.php<div {{ $attributes->twMerge('w-10 h-10 rounded-full bg-red-500') }}></div>
Here's a usage example with the above circle
component:
{{-- your-view.blade.php --}}<x-circle class="bg-blue-500" /> {{-- Output --}}<div class="w-10 h-10 rounded-full bg-blue-500"></div>
Here are some more examples from the readme of the inner workings of the merge
method. You can use the provided TailwindMerge
facade:
use TailwindMerge\Laravel\Facades\TailwindMerge; // block and inline are conflicting; The last rule wins.TailwindMerge::merge('block inline'); // inline // px-6 overrides pl-4TailwindMerge::merge('pl-4 px-6'); // px-6 // with breakpointsTailwindMerge::merge('h-10 lg:h-12 lg:h-20'); // h-10 lg:h-20 // dark modeTailwindMerge::merge('text-black dark:text-white dark:text-gray-700');// text-black dark:text-gray-700 // etc.
You can also use the @twMerge
Blade directive as well:
@twMerge('w-10 h-10 rounded-full bg-red-500 bg-blue-500'){{-- w-10 h-10 rounded-full bg-blue-500 --}}
You can learn more about this package, get full installation instructions, and view the source code on GitHub.