Skip to content

Commit 3723f96

Browse files
committed
Allow global attributes to get merged in.
1 parent e3eed14 commit 3723f96

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

docs/Helper/Icon.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ In this case make sure to use an array instead of just the class string:
4646
],
4747
```
4848

49+
You can also set a global attributes config that would be merged in with every icon:
50+
```php
51+
'Icon' => [
52+
'sets' => [
53+
...
54+
],
55+
'attributes' => [
56+
'data-custom' => 'some-default',
57+
...
58+
],
59+
],
60+
```
61+
4962
Don't forget to also set up the necessary stylesheets (CSS files) and alike.
5063

5164
## Usage

src/View/Icon/IconCollection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public function __construct(array $config = []) {
4646

4747
/** @var class-string<\Tools\View\Icon\IconInterface> $className */
4848
$className = $setConfig['class'];
49+
if (isset($config['attributes']) && isset($setConfig['attributes'])) {
50+
$setConfig['attributes'] += $config['attributes'];
51+
}
4952
$setConfig += $config;
5053
$this->iconSets[$set] = new $className($setConfig);
5154
}
@@ -115,6 +118,9 @@ public function render(string $icon, array $options = [], array $attributes = []
115118
}
116119
}
117120

121+
$attributes += $options['attributes'] ?? [];
122+
unset($options['attributes']);
123+
118124
return $this->iconSets[$set]->render($icon, $options, $attributes);
119125
}
120126

tests/TestCase/View/Icon/IconCollectionTest.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,48 @@ class IconCollectionTest extends TestCase {
1212
/**
1313
* @return void
1414
*/
15-
public function testCollect(): void {
15+
public function testRender(): void {
16+
$config = [
17+
'sets' => [
18+
'feather' => [
19+
'class' => FeatherIcon::class,
20+
],
21+
],
22+
'separator' => ':',
23+
];
24+
$result = (new IconCollection($config))->render('foo');
25+
26+
$this->assertSame('<span data-feather="foo" title="Foo"></span>', $result);
27+
}
28+
29+
/**
30+
* @return void
31+
*/
32+
public function testRenderNamespaced(): void {
33+
$config = [
34+
'sets' => [
35+
'feather' => [
36+
'class' => FeatherIcon::class,
37+
],
38+
'material' => [
39+
'class' => MaterialIcon::class,
40+
'namespace' => 'material-symbols',
41+
],
42+
],
43+
'separator' => ':',
44+
'attributes' => [
45+
'data-default' => 'some-default',
46+
],
47+
];
48+
$result = (new IconCollection($config))->render('material:foo');
49+
50+
$this->assertSame('<span class="material-symbols" title="Foo" data-default="some-default">foo</span>', $result);
51+
}
52+
53+
/**
54+
* @return void
55+
*/
56+
public function testNames(): void {
1657
$config = [
1758
'sets' => [
1859
'feather' => [

0 commit comments

Comments
 (0)