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

Product Decisions This Supports

  • Component Library Standardization: Eliminates class conflicts in reusable Laravel components (e.g., buttons, modals, cards) to enforce consistent styling across applications. Reduces manual overrides and improves maintainability in shared design systems, especially in multi-tenant or modular applications.
  • Dynamic UI Composition: Critical for projects using Blade, Livewire, or API-driven styling where classes are merged conditionally (e.g., dark mode toggles, responsive breakpoints, or user-specific themes). Automates conflict resolution, reducing bugs and accelerating development in real-time or data-driven UIs.
  • Build vs. Buy Decision: Replaces ad-hoc solutions (regex, JavaScript workarounds, or custom logic) with a maintained, PHP-native library, reducing technical debt and aligning with Laravel’s ecosystem. Avoids reinventing the wheel for a common problem.
  • Tailwind CSS Scalability: Enables teams to adopt Tailwind at scale without collision bugs, particularly in large applications with shared components, frequent theming changes (e.g., dark mode, custom color palettes), or complex state management (e.g., hover, focus, group states).
  • Developer Experience (DX) Improvement: Abstracts manual class conflict resolution, allowing backend developers to focus on logic rather than styling edge cases. Especially valuable for teams with limited frontend resources or those using Tailwind as a primary styling tool.
  • Performance Optimization: Optional PSR-16 caching reduces runtime overhead for frequent merges (e.g., in loops, high-traffic pages, or Livewire real-time updates), improving response times and scalability.
  • API-Driven Styling: Simplifies server-side HTML generation (e.g., in APIs, emails, or PDFs) where classes are merged dynamically based on data, user roles, or business logic. Ensures consistency in non-template contexts.
  • Future-Proofing: Actively maintained and supports Tailwind CSS v3.0–v3.4, reducing the risk of breaking changes. The companion Laravel package further integrates seamlessly into existing workflows.
  • Cross-Team Collaboration: Standardizes class merging across frontend and backend teams, reducing friction in collaborative environments (e.g., designers providing Tailwind classes, developers implementing them dynamically).

When to Consider This Package

Adopt when:

  • Your Laravel project uses Tailwind CSS v3.0–v3.4 and relies on dynamic class generation in Blade templates, Livewire components, or API responses.
  • You encounter frequent styling bugs due to conflicting utilities (e.g., p-4 px-6 resolving unpredictably, or collisions in dark mode, hover states, or responsive breakpoints).
  • You’re building a component library, shared UI system, or design system where consistent class composition is critical for reusability and maintainability.
  • Your team lacks dedicated frontend resources to manually resolve class conflicts or lacks expertise in Tailwind’s utility system.
  • You need server-side merging for non-template contexts like dynamic HTML generation in services, emails, PDFs, or CLI tools.
  • You’re using Livewire, Inertia.js, or API-driven UIs with server-side or real-time class merging requirements (e.g., real-time updates, dynamic theming).
  • Your project uses PHP 8.1+ and requires a lightweight, maintained, PHP-native solution with minimal dependencies.
  • You want to reduce technical debt by replacing custom regex or JavaScript-based merging solutions with a battle-tested library.

Look elsewhere if:

  • Your project doesn’t use Tailwind CSS (consider the JavaScript version or another utility library like PostCSS).
  • Class merging is trivial (e.g., static classes in small projects with no dynamic styling).
  • You have highly custom Tailwind configurations (e.g., heavily modified class names, arbitrary values, or unsupported variants) that exceed the package’s default configuration.
  • Your team prefers client-side merging (e.g., Alpine.js, Vue, React, or JavaScript frameworks) and doesn’t need server-side resolution.
  • You’re using Laravel <8.0 or PHP <8.1, as the package requires these versions.
  • Styling conflicts are rare or nonexistent, and manual resolution is sustainable.
  • You’re already using a dedicated frontend framework (e.g., Vue, React) with built-in class merging capabilities.

How to Pitch It (Stakeholders)

For Executives: "TailwindMerge for PHP solves a critical scalability challenge in our Laravel applications: class conflicts in dynamic UI components. Currently, developers spend time manually resolving styling collisions—especially in shared components, dark mode, or responsive designs—which slows down development and introduces bugs. This package automates that process, ensuring consistent styling across the board while reducing the need for frontend resources. It’s a lightweight, maintained solution that aligns with our Laravel ecosystem, enabling us to scale Tailwind adoption without technical debt. By adopting this, we’ll accelerate feature delivery, improve code quality, and future-proof our styling system."

For Engineering Leaders/Managers: *"This package provides a robust, PHP-native solution for merging Tailwind CSS classes with intelligent conflict resolution, supporting dynamic styling in Blade, Livewire, and API-driven contexts. It’s easy to integrate, offers optional caching for performance-critical applications, and handles edge cases like dark mode, hover states, and arbitrary values—problems we’ve had to solve manually or with workarounds. By adopting TailwindMerge, we can:

  • Standardize class merging across the codebase, reducing bugs and inconsistencies.
  • Free up frontend resources to focus on higher-value work.
  • Improve scalability for large applications or component libraries.
  • Future-proof our styling system with active maintenance and Tailwind v3.4 support. The Laravel-specific wrapper further simplifies adoption, making this a no-brainer for our stack."*

For Developers: *"TailwindMerge for PHP lets you merge Tailwind classes in PHP with automatic conflict resolution, so you don’t have to manually handle cases like:

  • p-4 px-6px-6 (later class wins)
  • text-red-500 dark:text-blue-500 text-green-500text-green-500 dark:text-blue-500 (dark mode preserved)
  • hover:bg-blue-500 focus:bg-red-500focus:bg-red-500 (focus state wins)

Key benefits:Simple API: Just call $tw->merge('class1', 'class2')—no regex or manual logic. ✅ Works everywhere: Blade, Livewire, APIs, emails, PDFs—anywhere you generate HTML dynamically. ✅ Configurable: Customize for your Tailwind setup (e.g., custom colors, fonts). ✅ Caching support: Speed up frequent merges with PSR-16 cache. ✅ No frontend overhead: Resolves conflicts server-side, reducing client-side complexity.

Example use cases:

  • Dynamic buttons with conditional styling: $tw->merge('bg-blue-500', user_is_premium ? 'bg-green-500' : '').
  • Livewire components with real-time class updates.
  • API-generated HTML with user-specific themes.

Get started in minutes:

composer require gehrisandro/tailwind-merge-php

Then use it anywhere in your app:

$merged = TailwindMerge::instance()->merge('p-4 px-6', 'text-red-500 dark:text-blue-500');
// Result: 'px-6 text-red-500 dark:text-blue-500'

No more guessing which class wins—just reliable, automated merging."*

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
christhompsontldr/phpsdk
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