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 (last class wins), ported from tailwind-merge. Supports Tailwind v3.0–v3.4, configurable and cacheable. Requires PHP 8.1+.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Laravel Native: The package is designed for PHP/Laravel ecosystems, with no external dependencies (beyond PSR-16 cache, which Laravel already supports via Illuminate/Contracts/Cache). It integrates seamlessly with Blade templates, Livewire, and server-side rendering.
  • Tailwind CSS Alignment: Directly mirrors Tailwind’s class resolution logic, ensuring compatibility with v3.0–v3.4. Custom Tailwind configs can be accommodated via configuration (e.g., classGroups).
  • Modular Design: Singleton (TailwindMerge::instance()) and factory patterns enable flexible instantiation (e.g., per-request caching or shared instances).
  • Stateful vs. Stateless: Stateless by default (pure function), but supports PSR-16 caching for performance-critical paths (e.g., real-time UI updates).

Integration Feasibility

  • Blade Templates: Replace manual class concatenation (e.g., {!! $mergedClasses !!}) with $tw->merge($classA, $classB) in directives or components.
  • Livewire/Inertia: Merge classes server-side before sending to the client, avoiding flicker from client-side resolution.
  • APIs/Emails/PDFs: Dynamically generate conflict-free HTML/CSS classes from runtime data (e.g., user preferences, tenant themes).
  • Shared Libraries: Enforce consistent class composition in reusable components (e.g., buttons, modals) across micro-frontends or modular apps.

Technical Risk

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).

Key Questions

  1. Tailwind Version: Is your project using Tailwind CSS v3.0–v3.4? If not, can you upgrade or extend the config to support custom classes?
  2. Dynamic Styling Needs: Where are conflicts most painful? (e.g., Livewire, dark mode, responsive breakpoints, theming).
  3. Performance Requirements: Will you need caching? If so, which PSR-16 cache driver (e.g., Redis, file)?
  4. Custom Configs: Do you use non-standard Tailwind classes (e.g., custom colors, fonts)? If yes, test the classGroups configuration.
  5. Testing Coverage: Can you validate merged classes against your design system’s edge cases (e.g., !important, arbitrary variants)?
  6. Adoption Path: Will this replace manual class merging (e.g., in Blade templates) or supplement it (e.g., in Livewire components)?
  7. Monitoring: How will you track false positives/negatives in merged classes (e.g., via QA or automated testing)?

Integration Approach

Stack Fit

  • Laravel Ecosystem: Zero friction with Blade, Livewire, Inertia.js, and server-side rendering.
  • PHP 8.1+: Native support; no polyfills or compatibility layers.
  • Tailwind CSS: Optimized for v3.0–v3.4. Custom configs require minimal extension.
  • Caching: PSR-16 compatible (Laravel’s Cache facade or third-party drivers like Redis).
  • Testing: PHPUnit-ready; includes comprehensive test suite for regression safety.

Migration Path

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:
  • Design system specs (e.g., no unintended overrides).
  • Edge cases (arbitrary values, !important, dark mode).
  • Performance (with/without cache). | PHPUnit: assertEquals('expected-class', $tw->merge('a', 'b'));; Load test with symfony/stopwatch. | | Rollout | Gradually replace manual merging in:
  1. Blade templates (directives or components).
  2. Livewire/Inertia components (server-side).
  3. APIs/Emails/PDFs (dynamic HTML generation). | Laravel Mix: Add to bootstrap.js; Livewire: Update render() methods. | | Monitoring | Track:
  • Reduction in styling bugs (e.g., via Sentry or QA tickets).
  • Cache hit ratio (if enabled).
  • Developer productivity (time saved on class conflict resolution). | Laravel Horizon for cache metrics; survey devs on pain points. |

Compatibility

  • Tailwind CSS: Supports v3.0–v3.4. For v4+, check for breaking changes in original JS package.
  • PHP: Requires 8.1+. Tested up to 8.3.
  • Laravel: No version dependency (works with Laravel 8+). Avoids illuminate/support (removed in v1.0.0).
  • Frameworks:
    • Livewire: Merge classes in mount() or render().
    • Inertia.js: Resolve server-side before sending props to Vue/React.
    • Blade: Use @php directives or custom components.
  • Third-Party: No conflicts with other Tailwind tools (e.g., laravel-tailwind).

Sequencing

  1. Start Small: Pilot with 1–2 components where conflicts are most frequent (e.g., a themed card or dark-mode toggle).
  2. Validate Config: Ensure classGroups covers your custom Tailwind classes.
  3. Enable Caching: Add PSR-16 cache for performance-critical paths (e.g., Livewire).
  4. Automate Testing: Add unit tests for merged classes in CI.
  5. Gradual Rollout: Replace manual merging in Blade → Livewire → APIs.
  6. Monitor: Track bug reduction and cache efficiency.

Operational Impact

Maintenance

  • Dependencies: Minimal (PSR-16 cache optional). No breaking changes since v1.0.0 (2024).
  • Updates: Monitor Tailwind CSS v4+ for compatibility. Package follows semver.
  • Configuration: classGroups may need updates if Tailwind config changes (e.g., new custom classes).
  • Deprecations: None planned. Original JS package is actively maintained.

Support

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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai