Skip to content

Commit

Permalink
Convert to methods
Browse files Browse the repository at this point in the history
This makes it possible to register only the one you need.
  • Loading branch information
francoism90 authored Oct 10, 2024
1 parent 10cff80 commit 92aaf61
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Support/Blade/Concerns/WithBladeMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

trait WithBladeMacros
{
protected function registerBladeMacros(): static
protected function cssClass(): void
{
ComponentAttributeBag::macro('cssClass', function (array $values = []): mixed {
foreach ($values as $key => $value) {
Expand All @@ -21,7 +21,10 @@ protected function registerBladeMacros(): static

return $this;
});
}

protected function classMerge(): void
{
ComponentAttributeBag::macro('classMerge', function (?array $values = null): mixed {
/** @var ComponentAttributeBag $this */
$classes = Bladeable::classMerged($this, $values)
Expand All @@ -33,19 +36,28 @@ protected function registerBladeMacros(): static
return $this
->withoutClass();
});
}

protected function withoutClass(): void
{
ComponentAttributeBag::macro('withoutClass', function (): mixed {
/** @var ComponentAttributeBag $this */
return $this
->whereDoesntStartWith('class:');
});
}

protected function withoutWireModel(): void
{
ComponentAttributeBag::macro('withoutWireModel', function (): mixed {
/** @var ComponentAttributeBag $this */
return $this
->whereDoesntStartWith('wire:model');
});
}

protected function mergeAttributes(): void
{
ComponentAttributeBag::macro('mergeAttributes', function (array $values = []): mixed {
foreach ($values as $key => $value) {
/** @var ComponentAttributeBag $this */
Expand All @@ -54,24 +66,32 @@ protected function registerBladeMacros(): static

return $this;
});
}

protected function classFor(): void
{
ComponentAttributeBag::macro('classFor', function (string $key, string $default = ''): mixed {
/** @var ComponentAttributeBag $this */
$class = Bladeable::classKeys($key)->first();

return $this->get($class, $default);
});

}

protected function wireModel(): void
{
ComponentAttributeBag::macro('wireModel', function (): mixed {
/** @var ComponentAttributeBag $this */
return $this->whereStartsWith('wire:model')->first();
});
}

protected function wireKey(): void
{
ComponentAttributeBag::macro('wireKey', function (): mixed {
/** @var ComponentAttributeBag $this */
return $this->wireModel() ?: $this->first('id') ?: $this->first('name');
});

return $this;
}
}

0 comments on commit 92aaf61

Please sign in to comment.