gehrisandro/tailwind-merge-laravel
Merge Tailwind CSS classes in Laravel and automatically resolve conflicts (later classes win). Ideal for Blade components and directives. PHP/Laravel port of tailwind-merge. Supports Tailwind v3.0–v3.3 (Laravel 10+).
ComponentAttributesBag, enabling seamless integration into existing component architectures (e.g., Livewire, Inertia, or vanilla Blade).tailwind-merge, it leverages Tailwind’s utility-first philosophy, making it ideal for projects heavily invested in Tailwind CSS (e.g., SaaS dashboards, marketing sites, or internal tools).@apply).tailwind-merge.php).| Risk Area | Assessment | Mitigation |
|---|---|---|
| Tailwind Version Drift | Package may lag behind Tailwind v4.x or future versions. | Monitor tailwind-merge-php for updates; consider forking if critical features are missing. |
| Blade-Specific Limitations | Direct use of $attributes->merge() is unsupported. Workarounds (e.g., twMergeFor()) may require refactoring existing components. |
Document component migration paths; prioritize components with conflicting class logic. |
| Performance Overhead | Class merging adds minimal runtime cost (cached by default). However, complex merges (e.g., arbitrary values) may impact performance in edge cases. | Benchmark in staging; disable caching (TAILWIND_MERGE_CACHE=false) if needed. |
| Custom Config Complexity | Advanced Tailwind setups (e.g., custom class groups) may require deep configuration of tailwind-merge.php. |
Provide default-agnostic documentation; offer a config validator to catch misconfigurations early. |
| Dependency Bloat | Adds ~10KB to vendor size (negligible for most projects). | Justify inclusion via ROI analysis (e.g., reduced styling bugs, faster theming). |
Design System Maturity:
classGroups tweaks?Team Adoption:
@twMerge) vs. Facade/helper methods?$attributes->merge() that would need refactoring?Performance Sensitivity:
TAILWIND_MERGE_CACHE=false) for dynamic class generation?Future-Proofing:
Alternatives:
laravel-tailwind?| Phase | Action Items | Dependencies |
|---|---|---|
| Assessment | Audit existing Blade components for class conflicts (e.g., bg-red-500 vs. bg-blue-500). Identify high-priority components for migration. |
Tailwind CSS classes in Blade templates. |
| Pilot | Install package in a staging environment: composer require gehrisandro/tailwind-merge-laravel. Test with 1–2 components (e.g., buttons, cards). |
Laravel 10+; Tailwind v3.x. |
| Configuration | Publish config: php artisan vendor:publish --provider="TailwindMerge\Laravel\TailwindMergeServiceProvider". Customize tailwind-merge.php if using non-standard Tailwind setups. |
Custom Tailwind config (if applicable). |
| Component Refactor | Replace $attributes->class with $attributes->twMerge() in Blade components. Use twMergeFor() for multi-element merging. |
Blade components with conflicting classes. |
| Blade Directive | Replace manual class concatenation (e.g., class="{{ $class1 }} {{ $class2 }}") with @twMerge($class1, $class2). |
Blade templates with dynamic classes. |
| Facade/Helper Usage | Replace custom class-merging logic in controllers/services with TailwindMerge::merge() or twMerge(). |
Non-Blade PHP logic generating HTML classes. |
| Testing | Verify merged classes in unit tests (e.g., assertStringContainsString('bg-blue-500', $html)). Test edge cases (arbitrary values, dark mode, breakpoints). |
PHPUnit/BrowserKit for rendering tests. |
| Rollout | Deploy to production in phases (e.g., non-critical sections first). Monitor for regressions (e.g., broken layouts due to unexpected class merges). | CI/CD pipeline; error tracking (Sentry/Laravel logs). |
composer.json constraints).TailwindMerge::merge() in non-Blade logic (How can I help you explore Laravel packages today?