- How do I install TailwindMerge for PHP in a Laravel project?
- Run `composer require gehrisandro/tailwind-merge-php` to install the package. No additional Laravel-specific setup is required, though you can bind it to the service container for dependency injection. The package works standalone or alongside the Laravel wrapper.
- Does this package support Tailwind v3.4? What if I’m using an older version?
- Yes, it supports Tailwind v3.0–3.4 out of the box. For older versions (e.g., v2.x), you’ll need to manually configure `classGroups` to match your Tailwind setup, as the package is tailored for v3.x. Check the [original tailwind-merge config guide](https://github.com/dcastil/tailwind-merge) for reference.
- Can I use this for dynamic class merging in Livewire or Alpine.js components?
- Absolutely. The package resolves conflicts server-side, so you can merge classes dynamically in Livewire methods or Alpine.js reactive properties without client-side JavaScript. Example: Return merged classes from a Livewire method to update UI seamlessly.
- How does conflict resolution work? For example, merging `bg-red-500` and `bg-blue-500`.
- The package follows Tailwind’s logic: later classes overwrite earlier ones. In your example, `bg-blue-500` wins, and `bg-red-500` is removed. This matches the behavior of the JavaScript `tailwind-merge` library, ensuring consistency between frontend and backend.
- Is there a performance impact if I merge classes in loops (e.g., for 100+ items)?
- For most cases, the impact is negligible. However, you can enable PSR-16 caching (e.g., Redis or file cache) via `TailwindMerge::factory()->withCache($cache)` to optimize repeated merges. Test with your workload to confirm.
- How do I configure custom Tailwind setups, like arbitrary values or non-standard variants?
- Use `TailwindMerge::factory()->withConfiguration(['classGroups' => [...]])` to extend the default configuration. Refer to the [original library’s config guide](https://github.com/dcastil/tailwind-merge#configuration) for schema details. For arbitrary values, ensure they’re included in `classGroups`.
- Can I integrate this with Laravel Blade templates or view composers?
- Yes. Bind the instance to Laravel’s container (e.g., in a service provider) and inject it into Blade views or view composers. Example: `{{ $tw->merge($dynamicClasses) }}` in Blade. This enables server-side merging for dynamic templates.
- What’s the difference between `TailwindMerge::instance()` and `TailwindMerge::factory()`?
- `instance()` returns a stateless, default-configured instance for quick use. `factory()` lets you customize the instance (e.g., with caching or config) before making it. Use `factory()` for reusable, configured instances in your app’s lifecycle.
- Does this package work with Laravel’s email or PDF generation (e.g., Spatie Mail, DomPDF)?
- Yes. Merge classes dynamically in email templates or PDF generators (e.g., `Mail::send()` or DomPDF) to resolve conflicts server-side. Example: `$message->withTailwindClasses($tw->merge('p-4', 'dark:p-8'))`.
- Are there alternatives to this package? When would I choose another?
- Alternatives include client-side JavaScript solutions (e.g., `tailwind-merge` npm package) or custom PHP logic. Choose this package if you need server-side merging (e.g., for SEO, PDFs, or performance-critical paths) or Laravel integration. For frontend-only use, the JS version may suffice.