Skip to content

Commit

Permalink
Added custom ComponentAttributeBag
Browse files Browse the repository at this point in the history
  • Loading branch information
toonvandenbos committed Dec 13, 2023
1 parent 64feb6f commit 63f5938
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/BemClassesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ class BemClassesServiceProvider extends ServiceProvider
*/
public function boot()
{
ComponentAttributeBag::macro('bem', fn(string $base, string|array $extraModifiers = []) => call_user_func($this->bemResolver, $base, $extraModifiers));
ComponentAttributeBag::macro(
'bem',
function(string $base, string|array $extraModifiers = []) {
return $this->resolveBemClasses($base, $extraModifiers);
}
);
}

/**
Expand Down
79 changes: 79 additions & 0 deletions src/BemComponentAttributeBag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Whitecube\BemComponents;

use Closure;
use Illuminate\View\ComponentAttributeBag;

class BemComponentAttributeBag extends ComponentAttributeBag
{
/**
* The BEM macro callback function
*/
protected Closure $bemResolver;

/**
* Define the BEM resolver callback.
*/
public function setBemResolver(Closure $bemResolver): static
{
$this->bemResolver = $bemResolver;

return $this;
}

/**
* Resolve the BEM attributes and merge the resulting CSS classes
* in this bag.
*/
public function resolveBemClasses(string $base, string|array $extraModifiers = []): static
{
call_user_func($this->bemResolver, $base, $extraModifiers);

return $this;
}

/**
* Only include the given attribute from the attribute array.
*/
public function only($keys)
{
$instance = parent::only($keys);
$instance->setBemResolver($this->bemResolver);

return $instance;
}

/**
* Exclude the given attribute from the attribute array.
*/
public function except($keys)
{
$instance = parent::except($keys);
$instance->setBemResolver($this->bemResolver);

return $instance;
}

/**
* Filter the attributes, returning a bag of attributes that pass the filter.
*/
public function filter($callback)
{
$instance = parent::filter($callback);
$instance->setBemResolver($this->bemResolver);

return $instance;
}

/**
* Merge additional attributes / values into the attribute bag.
*/
public function merge(array $attributeDefaults = [], $escape = true)
{
$instance = parent::merge($attributeDefaults, $escape);
$instance->setBemResolver($this->bemResolver);

return $instance;
}
}
9 changes: 4 additions & 5 deletions src/HasBemClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,9 @@ protected function mergeAllClassesInAttributeBag(string $base, string|array $ext
*/
protected function newAttributeBag(array $attributes = [])
{
$bag = parent::newAttributeBag($attributes);

$bag->bemResolver = fn(string $base, string|array $extraModifiers = []) => $this->mergeAllClassesInAttributeBag($base, $extraModifiers);

return $bag;
return (new BemComponentAttributeBag($attributes))
->setBemResolver(function(string $base, string|array $extraModifiers = []) {
return $this->mergeAllClassesInAttributeBag($base, $extraModifiers);
});
}
}

0 comments on commit 63f5938

Please sign in to comment.