Skip to content

Commit 759f850

Browse files
fix: return null on invalid icon name exception instead of throwing (#5)
* feat: return `null` on invalid icon name * chore: update tests * chore: update README
1 parent 89e3049 commit 759f850

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Apart from the requirements above, it is required that you use Statamic's new Ru
2626

2727
First, require `statamic-heroicons` as a Composer dependency:
2828

29-
```
29+
```shell
3030
composer require stefangalescu/statamic-heroicons
3131
```
3232

src/Tags/Heroicon.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ class Heroicon extends Tags
1313
{
1414
protected static $handle = 'heroicon';
1515

16-
private function renderBladeToHtml(string $variant, string $icon, Collection $attrs): string
16+
private function renderBladeToHtml(string $variant, string $icon, Collection $attrs): string|null
1717
{
1818
$attrsString = $attrs->map(function ($value, $key) {
1919
$parsedValue = gettype($value) === 'string' ? $value : var_export($value, true);
2020

2121
return $key.'='.'"'.$parsedValue.'"';
2222
})->join(' ');
2323

24-
return Blade::render('<x-heroicon-'.$variant[0].'-'.$icon.' '.$attrsString.' />');
24+
try {
25+
return Blade::render('<x-heroicon-'.$variant[0].'-'.$icon.' '.$attrsString.' />');
26+
} catch (\Throwable $e) {
27+
return null;
28+
}
2529
}
2630

27-
private function render(string $variant = null, string|null $icon = null): string
31+
private function render(string $variant = null, string|null $icon = null): string|null
2832
{
2933
$variant = $variant ?? Str::lower($this->params->get('variant'));
3034
$icon = $icon ?? Str::lower($this->params->get('icon'));
@@ -37,39 +41,39 @@ private function render(string $variant = null, string|null $icon = null): strin
3741
/**
3842
* The {{ heroicon }} tag.
3943
*/
40-
public function index(): string
44+
public function index(): string|null
4145
{
4246
return $this->render();
4347
}
4448

4549
/**
4650
* The {{ heroicon:mini }} tag.
4751
*/
48-
public function mini(): string
52+
public function mini(): string|null
4953
{
5054
return $this->render('mini');
5155
}
5256

5357
/**
5458
* The {{ heroicon:outline }} tag.
5559
*/
56-
public function outline(): string
60+
public function outline(): string|null
5761
{
5862
return $this->render('outline');
5963
}
6064

6165
/**
6266
* The {{ heroicon:solid }} tag.
6367
*/
64-
public function solid(): string
68+
public function solid(): string|null
6569
{
6670
return $this->render('solid');
6771
}
6872

6973
/**
7074
* The {{ heroicon:{variant}:{icon} }} tag.
7175
*/
72-
public function wildcard(string $tag): string
76+
public function wildcard(string $tag): string|null
7377
{
7478
[$variant, $icon] = Str::of($tag)->split('/:/')->toArray();
7579
$icon = Str::kebab($icon);

tests/HeroiconTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace StefanGalescu\Heroicons\Tests;
44

55
use function PHPUnit\Framework\assertEquals;
6+
use function PHPUnit\Framework\assertNull;
67
use function PHPUnit\Framework\assertStringContainsString;
78
use Statamic\Statamic;
89

@@ -75,4 +76,12 @@ public function can_add_dynamically_binded_attributes_to_svg()
7576

7677
assertStringContainsString('x-bind:class="true ? \'w-6 h-6\' : \'w-5 h-5\'"', $render);
7778
}
79+
80+
/** @test */
81+
public function will_not_throw_when_icon_name_is_invalid()
82+
{
83+
$render = $this->render('outline', 'invalid-icon-name');
84+
85+
assertNull($render);
86+
}
7887
}

0 commit comments

Comments
 (0)