Introduction to TailwindMerge for PHP
TailwindMerge for PHP is a powerful utility that provides a seamless way to merge multiple Tailwind CSS classes, ensuring that any conflicting classes are resolved automatically. This means that when you use multiple classes that may clash, only the most recent or overriding class is retained, streamlining your CSS management process.
This project is inspired by the JavaScript library tailwind-merge created by dcastil. With support for Tailwind CSS from version 3.0 to 3.4, TailwindMerge for PHP is ideal for developers working in a PHP environment who wish to harness the power of Tailwind CSS with enhanced simplicity and efficiency.
Starting with TailwindMerge
To get started, ensure your PHP version is 8.1 or higher. Installation is straightforward using the Composer package manager:
composer require gehrisandro/tailwind-merge-php
Once installed, merging Tailwind CSS classes is as simple as using the TailwindMerge
class. Here's an example:
use TailwindMerge\TailwindMerge;
$tw = TailwindMerge::instance();
$tw->merge('text-red-500', 'text-blue-500'); // Result: 'text-blue-500'
This concise operation efficiently resolves class conflicts by keeping the last specified class.
Flexibility and Usage
The utility is not limited to resolving basic conflicts. TailwindMerge for PHP is equipped to handle complex scenarios:
- Resolving multiple conflicting classes, like
block
andinline
, or spacing utilities such aspl-4
andpx-6
. - Managing classes within different contexts, such as breakpoints (
lg:h-12
) or states likehover
,focus
, and even dark mode settings. - Using the
!important
modifier and arbitrary values or attributes is effortlessly managed.
Additionally, classes can be passed as strings, arrays, or a combination, allowing for flexible input:
$tw->merge(['h-10', 'h-20'], 'h-30', ['h-40']); // Result: 'h-40'
Enhancing Performance with Cache
For optimizations, TailwindMerge supports caching the results of merge operations. Leveraging any PSR-16 compatible cache implementation, developers can improve performance:
TailwindMerge::factory()
->withCache($cache)
->make();
If there are changes to your configuration, remember to clear the cache to ensure up-to-date merge results.
Configuration
By default, TailwindMerge is ready to use with standard Tailwind CSS configurations. However, if your Tailwind setup includes custom configurations, TailwindMerge can be adjusted to fit these needs.
Here is how to add a custom font size class:
TailwindMerge::factory()->withConfiguration([
'classGroups' => [
'font-size' => [
['text' => ['very-large']]
],
],
])->make();
You can delve into further configuration by exploring the detailed documentation provided by the original package.
Contributing to TailwindMerge
Contributions to the development and enhancement of TailwindMerge for PHP are highly encouraged. If you are interested in contributing, please refer to the guidelines in the CONTRIBUTING.md file.
Licensed under the MIT License, TailwindMerge for PHP is an open-source project that aims to continuously improve and support the development community.