Skip to content

Commit 63f5938

Browse files
committed
Added custom ComponentAttributeBag
1 parent 64feb6f commit 63f5938

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

src/BemClassesServiceProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ class BemClassesServiceProvider extends ServiceProvider
1414
*/
1515
public function boot()
1616
{
17-
ComponentAttributeBag::macro('bem', fn(string $base, string|array $extraModifiers = []) => call_user_func($this->bemResolver, $base, $extraModifiers));
17+
ComponentAttributeBag::macro(
18+
'bem',
19+
function(string $base, string|array $extraModifiers = []) {
20+
return $this->resolveBemClasses($base, $extraModifiers);
21+
}
22+
);
1823
}
1924

2025
/**

src/BemComponentAttributeBag.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Whitecube\BemComponents;
4+
5+
use Closure;
6+
use Illuminate\View\ComponentAttributeBag;
7+
8+
class BemComponentAttributeBag extends ComponentAttributeBag
9+
{
10+
/**
11+
* The BEM macro callback function
12+
*/
13+
protected Closure $bemResolver;
14+
15+
/**
16+
* Define the BEM resolver callback.
17+
*/
18+
public function setBemResolver(Closure $bemResolver): static
19+
{
20+
$this->bemResolver = $bemResolver;
21+
22+
return $this;
23+
}
24+
25+
/**
26+
* Resolve the BEM attributes and merge the resulting CSS classes
27+
* in this bag.
28+
*/
29+
public function resolveBemClasses(string $base, string|array $extraModifiers = []): static
30+
{
31+
call_user_func($this->bemResolver, $base, $extraModifiers);
32+
33+
return $this;
34+
}
35+
36+
/**
37+
* Only include the given attribute from the attribute array.
38+
*/
39+
public function only($keys)
40+
{
41+
$instance = parent::only($keys);
42+
$instance->setBemResolver($this->bemResolver);
43+
44+
return $instance;
45+
}
46+
47+
/**
48+
* Exclude the given attribute from the attribute array.
49+
*/
50+
public function except($keys)
51+
{
52+
$instance = parent::except($keys);
53+
$instance->setBemResolver($this->bemResolver);
54+
55+
return $instance;
56+
}
57+
58+
/**
59+
* Filter the attributes, returning a bag of attributes that pass the filter.
60+
*/
61+
public function filter($callback)
62+
{
63+
$instance = parent::filter($callback);
64+
$instance->setBemResolver($this->bemResolver);
65+
66+
return $instance;
67+
}
68+
69+
/**
70+
* Merge additional attributes / values into the attribute bag.
71+
*/
72+
public function merge(array $attributeDefaults = [], $escape = true)
73+
{
74+
$instance = parent::merge($attributeDefaults, $escape);
75+
$instance->setBemResolver($this->bemResolver);
76+
77+
return $instance;
78+
}
79+
}

src/HasBemClasses.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,9 @@ protected function mergeAllClassesInAttributeBag(string $base, string|array $ext
189189
*/
190190
protected function newAttributeBag(array $attributes = [])
191191
{
192-
$bag = parent::newAttributeBag($attributes);
193-
194-
$bag->bemResolver = fn(string $base, string|array $extraModifiers = []) => $this->mergeAllClassesInAttributeBag($base, $extraModifiers);
195-
196-
return $bag;
192+
return (new BemComponentAttributeBag($attributes))
193+
->setBemResolver(function(string $base, string|array $extraModifiers = []) {
194+
return $this->mergeAllClassesInAttributeBag($base, $extraModifiers);
195+
});
197196
}
198197
}

0 commit comments

Comments
 (0)