PHPackages                             yabasha/laravel-clsx - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yabasha/laravel-clsx

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yabasha/laravel-clsx
====================

A Laravel utility for conditional class name concatenation, inspired by the JS clsx package.

v1.1.0(1y ago)03MITPHPPHP ^8.0

Since Apr 26Pushed 1y ago1 watchersCompare

[ Source](https://github.com/yabasha/laravel-clsx)[ Packagist](https://packagist.org/packages/yabasha/laravel-clsx)[ RSS](/packages/yabasha-laravel-clsx/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (3)Used By (0)

Laravel Clsx
============

[](#laravel-clsx)

A Laravel utility for conditional class name concatenation, inspired by the popular [clsx](https://github.com/lukeed/clsx) JavaScript package.

Features
--------

[](#features)

- Concatenate class names based on conditions
- Supports arbitrarily nested arrays of class names
- Optional transformation/filter callback for class names
- Use as a global helper (`clsx()`) or as a static class (`Clsx::make()`)
- Macroable: extend with your own methods
- Blade directive: `@clsx` for easy usage in Blade templates
- Validation helper: `clsx_with_error()` for error class toggling
- Perfect for Blade templates and PHP code

Installation
------------

[](#installation)

```
composer require yabasha/laravel-clsx
```

Usage
-----

[](#usage)

### Nested Arrays

[](#nested-arrays)

You can pass deeply nested arrays and all class names will be flattened:

```
$classString = clsx(['foo', ['bar', ['baz']]]); // "foo bar baz"
```

### Filtering/Transformation Callback

[](#filteringtransformation-callback)

Optionally pass a callable as the last argument to transform class names:

```
$classString = clsx('foo', 'bar', function($c) { return strtoupper($c); }); // "FOO BAR"
```

### Macroable (Extending Clsx)

[](#macroable-extending-clsx)

You can add your own macros to Clsx:

```
use Yabasha\Clsx\Clsx;
Clsx::macro('withPrefix', function ($prefix, ...$args) {
    return Clsx::make(...array_map(fn($c) => $prefix.$c, $args));
});
// Usage:
$classes = Clsx::withPrefix('tw-', 'foo', 'bar'); // "tw-foo tw-bar"
```

### Blade Directive

[](#blade-directive)

Register the Blade directive by ensuring the service provider is loaded (auto-discovered):

```

```

### Validation Helper

[](#validation-helper)

Add error classes easily:

```

```

### In Blade Templates

[](#in-blade-templates)

```

    Example

```

### In PHP

[](#in-php)

```
use Yabasha\Clsx\Clsx;

$classString = Clsx::make('foo', ['bar' => $isBar, 'baz' => $isBaz], $moreClasses);
```

### Advanced Examples

[](#advanced-examples)

**Conditional route:**

```

    Dashboard

```

**With validation errors:**

```

```

**Mixing arrays and strings:**

```
@php
    $extra = 'rounded shadow';
@endphp

    Mixed usage

```

**Using the static class in Blade:**

```
@php
    use Yabasha\Clsx\Clsx;
@endphp

    Card Content

```

**Looping over items:**

```
@foreach($items as $item)

        {{ $item->name }}

@endforeach
```

License
-------

[](#license)

MIT

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance49

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

2

Last Release

377d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/dfd85c7f2344f5959e2dba8ed345b277e49c7ec38f063afd70f930f1b356213a?d=identicon)[yabasha](/maintainers/yabasha)

### Embed Badge

![Health badge](/badges/yabasha-laravel-clsx/health.svg)

```
[![Health](https://phpackages.com/badges/yabasha-laravel-clsx/health.svg)](https://phpackages.com/packages/yabasha-laravel-clsx)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
