Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Tailwind Merge Php Laravel Package

gehrisandro/tailwind-merge-php

Merge Tailwind CSS class strings in PHP with automatic conflict resolution (later classes win). PHP port of dcastil/tailwind-merge, compatible with Tailwind v3.0–v3.4 and PHP 8.1+. Create instances and customize configuration as needed.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Laravel Alignment: The package is designed for PHP/Tailwind ecosystems, with a Laravel-specific companion package (tailwind-merge-laravel), ensuring seamless integration with Laravel’s Blade templating, Livewire, and API-driven workflows.
  • Conflict Resolution: Directly addresses Tailwind’s utility-first paradigm by resolving class collisions (e.g., text-red-500 text-blue-500text-blue-500), which is critical for dynamic UI composition in Laravel.
  • Extensibility: Supports custom Tailwind configurations via classGroups, enabling adaptation to non-standard setups (e.g., custom color palettes, arbitrary values).
  • Performance: Optional PSR-16 caching (e.g., Laravel’s cache system) reduces runtime overhead for frequent merges, aligning with Laravel’s caching abstractions.

Integration Feasibility

  • Low Friction: Single Composer dependency (gehrisandro/tailwind-merge-php) with minimal setup (PHP 8.1+ required).
  • Blade/Livewire Compatibility: Ideal for server-side class merging in Blade templates or Livewire components, where dynamic classes are common.
  • API/Service Layer: Useful for generating HTML dynamically (e.g., emails, PDFs, or API responses) with merged Tailwind classes.
  • Non-Tailwind Classes: Gracefully handles non-Tailwind classes (e.g., non-tailwind-class block), reducing risk of unintended side effects.

Technical Risk

  • Tailwind Version Lock: Supports v3.0–v3.4 only. Projects using v2.x or v4.x+ may require custom configuration or forks.
  • Custom Config Complexity: Non-standard Tailwind setups (e.g., heavily modified tailwind.config.js) may need manual classGroups tuning.
  • Caching Dependencies: PSR-16 cache integration adds minor complexity but is optional. Laravel’s built-in cache (e.g., Illuminate\Cache) works out-of-the-box.
  • Arbitrary Values: Edge cases (e.g., [&>*]:underline) are supported but may require testing for project-specific arbitrary variants.
  • PHP Version: Requires PHP 8.1+, which may exclude legacy Laravel projects (<8.0).

Key Questions

  1. Tailwind Compatibility:

    • Does your project use Tailwind v3.0–v3.4? If not, can the package be adapted or is a fork needed?
    • Are there custom Tailwind configurations (e.g., renamed classes, non-standard utilities) that might require classGroups adjustments?
  2. Use Cases:

    • Where will merging occur? (Blade templates, Livewire components, API responses, etc.)
    • Are there performance-critical paths (e.g., high-traffic pages, real-time updates) where caching would be beneficial?
  3. Team Adoption:

    • Does the team have experience with Tailwind CSS and its utility-first approach?
    • Are developers comfortable with server-side class merging (vs. client-side solutions like Alpine.js)?
  4. Alternatives:

    • Have you evaluated client-side merging (e.g., JavaScript tailwind-merge) or manual solutions (regex, custom logic)?
    • Is the Laravel-specific wrapper (tailwind-merge-laravel) a better fit for your project’s needs?
  5. Testing:

    • Have you validated edge cases like dark mode, hover/focus states, and arbitrary values in your Tailwind setup?
    • Are there non-Tailwind classes in your codebase that could interfere with merging?

Integration Approach

Stack Fit

  • Laravel Ecosystem: Optimized for Laravel with Blade, Livewire, and API integrations. The companion package (tailwind-merge-laravel) further reduces friction.
  • PHP 8.1+: Leverages modern PHP features (e.g., typed properties, enums) for performance and maintainability.
  • Tailwind CSS: Deep integration with Tailwind’s utility system, including support for breakpoints, dark mode, and arbitrary values.
  • Caching: Compatible with Laravel’s PSR-16 cache implementations (e.g., file, redis, database), enabling performance optimizations.

Migration Path

  1. Assessment Phase:

    • Audit Tailwind usage to confirm compatibility with v3.0–v3.4.
    • Identify custom configurations that may need classGroups adjustments.
    • Map use cases (Blade, Livewire, API, etc.) to integration points.
  2. Proof of Concept (PoC):

    • Install the package: composer require gehrisandro/tailwind-merge-php.
    • Test basic merging in a Blade template or Livewire component:
      use TailwindMerge\TailwindMerge;
      $mergedClasses = TailwindMerge::instance()->merge('p-4 px-6'); // 'px-6'
      
    • Validate edge cases (dark mode, hover states, arbitrary values).
  3. Integration:

    • Blade Templates: Replace manual class concatenation with merged classes:
      <button class="{{ TailwindMerge::instance()->merge('bg-blue-500 hover:bg-blue-700', 'bg-red-500') }}">
          Click Me
      </button>
      
    • Livewire Components: Merge classes dynamically in properties or methods:
      public function getButtonClasses(): string
      {
          return TailwindMerge::instance()->merge(
              'py-2 px-4 rounded',
              $this->isPrimary ? 'bg-blue-600' : 'bg-gray-600'
          );
      }
      
    • API/Service Layer: Use in services generating HTML (e.g., emails):
      $html = '<button class="' . TailwindMerge::instance()->merge('text-white', 'text-black') . '">...</button>';
      
    • Optional Caching: Enable caching for performance-critical paths:
      $instance = TailwindMerge::factory()
          ->withCache(app('cache'))
          ->make();
      
  4. Custom Configuration:

    • If using non-standard Tailwind setups, extend classGroups in the factory:
      $instance = TailwindMerge::factory()->withConfiguration([
          'classGroups' => [
              'font-size' => [['text' => ['custom-size']]],
          ],
      ])->make();
      

Compatibility

  • Tailwind CSS: Works with v3.0–v3.4. For other versions, assess compatibility or fork.
  • PHP Extensions: No additional extensions required beyond PHP 8.1+.
  • Laravel Services: Integrates with Laravel’s cache, Blade, and Livewire without conflicts.
  • Third-Party Packages: No known conflicts with popular Laravel packages (e.g., Livewire, Inertia).

Sequencing

  1. Phase 1: Core Integration (1–2 sprints):
    • Install and test basic merging in Blade templates.
    • Validate conflict resolution for common use cases (buttons, cards, modals).
  2. Phase 2: Advanced Use Cases (1 sprint):
    • Integrate with Livewire components and API services.
    • Test edge cases (dark mode, arbitrary values, custom configs).
  3. Phase 3: Optimization (0.5 sprint):
    • Enable caching for performance-critical paths.
    • Document patterns and anti-patterns for the team.
  4. Phase 4: Rollout (Ongoing):
    • Gradually replace manual class merging across the codebase.
    • Monitor for edge cases and refine classGroups as needed.

Operational Impact

Maintenance

  • Low Overhead: Minimal maintenance required; the package is actively updated (last release: 2026-03-21).
  • Dependency Management: Single Composer dependency with no transitive conflicts.
  • Configuration Drift: Custom classGroups may need updates if Tailwind config changes, but this is rare.
  • Deprecation Risk: Low risk of breaking changes, as the package mirrors the stable tailwind-merge JS library.

Support

  • Documentation: Comprehensive README with usage examples, configuration guides, and changelog.
  • Community: Active maintainer (Sandro Gehri) and GitHub community for issues/feature requests.
  • Laravel-Specific: Companion package (tailwind-merge-laravel) provides Laravel-focused support.
  • Debugging: Clear error messages for unsupported classes or configurations.

Scaling

  • Performance: Optional PSR-16 caching reduces runtime overhead for frequent merges (e.g., in loops or real-time updates).
  • Concurrency: Thread-safe for Laravel’s request lifecycle (stateless singleton or cached instances).
  • Horizontal Scaling: No shared state; caching can be distributed (e.g., Redis) for multi-server setups.
  • Load Testing: Test caching under high traffic to ensure no bottlenecks (e.g., 1000+ merges/sec
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests