gehrisandro/tailwind-merge-php
Merge Tailwind CSS class strings in PHP with automatic conflict resolution (last class wins), ported from tailwind-merge. Supports Tailwind v3.0–v3.4, configurable and cacheable. Requires PHP 8.1+.
Illuminate/Contracts/Cache). It integrates seamlessly with Blade templates, Livewire, and server-side rendering.classGroups).TailwindMerge::instance()) and factory patterns enable flexible instantiation (e.g., per-request caching or shared instances).{!! $mergedClasses !!}) with $tw->merge($classA, $classB) in directives or components.| Risk Area | Mitigation |
|---|---|
| Custom Tailwind Configs | Validate against default Tailwind v3.x rules. If unsupported, extend classGroups in config (documented in original JS package). |
| Performance | Enable PSR-16 caching for high-frequency merges (e.g., Livewire). Benchmark with/without cache to justify overhead. |
| Arbitrary Values | Test edge cases (e.g., [&>*]:underline [&>*]:line-through). Package handles these, but validate against your custom arbitrary classes. |
| Backward Compatibility | Monitor Tailwind CSS updates. Package supports v3.0–v3.4; upgrade path exists if Tailwind v4+ diverges. |
| Dependency Bloat | No external deps (beyond optional cache). Composer footprint: ~1MB. |
| Thread Safety | Stateless by default. If using cached instance, ensure thread-safe cache (e.g., Redis, Laravel’s array driver). |
classGroups configuration.!important, arbitrary variants)?Cache facade or third-party drivers like Redis).| Phase | Action | Tools/Examples |
|---|---|---|
| Assessment | Audit current class merging patterns (manual, JS, or regex). Identify top conflict hotspots (e.g., p-4 px-6, dark mode). |
git grep for class=".*" + tailwindcss; review Livewire/Blade templates. |
| Pilot | Replace 1–2 high-impact components (e.g., a Livewire modal or themed button) with TailwindMerge. |
Blade: @php echo $tw->merge($classes); @endphp; Livewire: Merge classes in mount() or render(). |
| Configuration | Extend classGroups if using custom Tailwind configs (e.g., ['font-size' => [['text' => ['custom-size']]]]). |
Test with TailwindMerge::factory()->withConfiguration([...])->make(). |
| Caching | Enable PSR-16 cache for performance-critical paths (e.g., Livewire updates). | TailwindMerge::factory()->withCache(app('cache'))->make(). |
| Testing | Validate merged classes against: |
!important, dark mode).assertEquals('expected-class', $tw->merge('a', 'b'));; Load test with symfony/stopwatch. |
| Rollout | Gradually replace manual merging in:bootstrap.js; Livewire: Update render() methods. |
| Monitoring | Track:illuminate/support (removed in v1.0.0).mount() or render().@php directives or custom components.laravel-tailwind).classGroups covers your custom Tailwind classes.classGroups may need updates if Tailwind config changes (e.g., new custom classes).classGroups or validate against Tailwind’s default rules.[...]) and !important modifiers.How can I help you explore Laravel packages today?