Skip to content

Support conditional classes in Blade directive and attribute macro #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TailwindMerge\Laravel;

use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\ComponentAttributeBag;
Expand Down Expand Up @@ -46,14 +47,14 @@ protected function registerBladeDirectives(): void
return;
}

$bladeCompiler->directive($name, fn (?string $expression) => "<?php echo twMerge($expression); ?>");
$bladeCompiler->directive($name, fn (?string $expression) => "<?php echo twMerge(\Illuminate\Support\Arr::map([$expression], fn (\$arg) => \Illuminate\Support\Arr::toCssClasses(\$arg))); ?>");
});
}

protected function registerAttributesBagMacro(): void
{
ComponentAttributeBag::macro('twMerge', function (...$args): static {
$this->attributes['class'] = resolve(TailwindMergeContract::class)->merge($args, ($this->attributes['class'] ?? ''));
$this->attributes['class'] = resolve(TailwindMergeContract::class)->merge(Arr::map($args, fn ($arg) => Arr::toCssClasses($arg)), ($this->attributes['class'] ?? ''));

return $this;
});
Expand Down
1 change: 1 addition & 0 deletions tests/Arch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
->expect('TailwindMerge\Laravel\ServiceProvider')
->toOnlyUse([
'Illuminate\Contracts\Support\DeferrableProvider',
'Illuminate\Support\Arr',
'Illuminate\Support\ServiceProvider',
'Illuminate\View\Compilers\BladeCompiler',
'Illuminate\View\ComponentAttributeBag',
Expand Down
21 changes: 20 additions & 1 deletion tests/Feature/BladeComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@

use Illuminate\Support\Facades\Blade;
use Tests\Fixtures\Circle;
use Tests\Fixtures\Rectangle;
use Tests\Fixtures\Square;

it('provides a blade directive to merge tailwind classes', function () {
it('provides a blade attribute macro to merge tailwind classes', function () {
Blade::component('circle', Circle::class);

$this->blade('<x-circle class="bg-blue-500" />')
->assertSee('<div class="w-10 h-10 rounded-full bg-blue-500"></div>', false);
});

test('blade attribute macro supports conditional classes', function () {
Blade::component('square', Square::class);

$this->blade('<x-square class="bg-blue-500" />')
->assertSee('<div class="w-10 h-10 bg-blue-500"></div>', false);

$this->blade('<x-square class="bg-blue-500" rounded />')
->assertSee('<div class="w-10 h-10 rounded-lg bg-blue-500"></div>', false);
});

test('blade attribute macro supports multiple arguments', function () {
Blade::component('rectangle', Rectangle::class);

$this->blade('<x-rectangle />')
->assertSee('<div class="h-6 rounded-lg border pl-4"></div>', false);
});
19 changes: 19 additions & 0 deletions tests/Feature/BladeDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@
->assertSee('class="h-6"', false);
});

test('blade directive supports conditional classes', function () {
$this->blade('<div class="@twMerge([
"h-4 h-6",
"rounded-lg" => false,
])"></div>')
->assertSee('class="h-6"', false);

$this->blade('<div class="@twMerge([
"h-4 h-6",
"rounded-lg" => true,
])"></div>')
->assertSee('class="h-6 rounded-lg"', false);
});

test('blade directive supports multiple arguments', function () {
$this->blade('<div class="@twMerge("h-4 h-6", "rounded-lg border", ["pl-4", "rounded-full" => false])"></div>')
->assertSee('class="h-6 rounded-lg border pl-4"', false);
});

test('name ot the blade directive is configurable', function () {
Config::set('tailwind-merge.blade_directive', 'myMerge');

Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/Rectangle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace tests\Fixtures;

use Illuminate\View\Component;

class Rectangle extends Component
{
public function render()
{
return <<<'blade'
<div {{ $attributes->twMerge("h-4 h-6", "rounded-lg border", ["pl-4", "rounded-full" => false]) }}></div>
blade;
}
}
23 changes: 23 additions & 0 deletions tests/Fixtures/Square.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace tests\Fixtures;

use Illuminate\View\Component;

class Square extends Component
{
public function __construct(
public bool $rounded = false,
) {
}

public function render()
{
return <<<"blade"
<div {{ \$attributes->twMerge([
'w-10 h-10 bg-red-500',
'rounded-lg' => '{$this->rounded}',
]) }}></div>
blade;
}
}