driesvints/blade-icons
Blade Icons lets you use SVG icons in Laravel Blade with simple components and directives. Convert files like camera.svg into or @svg('camera') with easy class/attribute control, and plug in community icon set packages.
Installation:
composer require blade-ui-kit/blade-icons
php artisan vendor:publish --tag=blade-icons
default icon set in config/blade-icons.php (e.g., heroicons).resources/svg (or the configured directory).First Use Case:
<x-icon-home class="w-6 h-6" />
@svg('camera', 'w-6 h-6')
Where to Look First:
config/blade-icons.php (configuration)resources/svg (icon storage)Component-Based Icons:
<x-icon-home />).<x-icon-home class="w-8 h-8 text-red-500" />
Direct SVG Injection:
@svg directive for dynamic or inline usage:
@svg('camera', ['class' => 'w-6 h-6', 'fill' => 'currentColor'])
Dynamic Icon Selection:
@php($icon = 'home')
<x-icon-{{ $icon }} class="w-6 h-6" />
Icon Sets:
config/blade-icons.php:
'sets' => [
'heroicons' => [
'path' => 'vendor/blade-ui-kit/blade-heroicons/resources/svg',
'prefix' => 'heroicon',
],
],
Custom Icon Sets:
composer require davidhsianturi/blade-bootstrap-icons
Then update config/blade-icons.php to include the new set.Reusable Icon Components:
@component('icons.button', ['icon' => 'check', 'size' => 'lg'])
@endcomponent
Icon Discovery:
resources/svg and reference it.Theming:
<x-icon-home class="text-blue-500 dark:text-blue-400" />
Accessibility:
aria-hidden="true" or screen-reader-only text:
<x-icon-home aria-hidden="true" />
<span class="sr-only">Home</span>
Performance:
Integration with UI Libraries:
<x-icon-bell class="cursor-pointer" x-on:click="toggleNotification" />
Missing SVG Files:
storage/logs/laravel.log for missing file errors.Caching Issues:
php artisan view:clear
// config/view.php
'cache' => env('VIEW_CACHE', false),
Namespace Conflicts:
<x-icon-home> vs. <x-home-icon>).Dynamic Icon Names:
{{ }} for dynamic icon names to prevent syntax errors:
<x-icon-{{ $dynamicIcon }} />
SVG Optimization:
Check Icon Existence:
config/blade-icons.php matches the actual location.Inspect Rendered Output:
Log Missing Icons:
// app/Providers/BladeIconsServiceProvider.php
BladeIcons::missing(function ($icon) {
Log::warning("Icon '{$icon}' not found.");
});
Default Icon Set:
default key in config/blade-icons.php is set to an existing icon set:
'default' => 'heroicons',
Custom Directories:
public/svg), update the path in the config:
'sets' => [
'custom' => [
'path' => 'public/svg',
'prefix' => 'custom-icon',
],
],
Prefix Handling:
prefix in the config determines the Blade component name (e.g., heroicon-home becomes <x-icon-home>).Custom Icon Sets:
'my-icons' => [
'path' => 'resources/svg/my-icons',
'prefix' => 'my-icon',
],
resources/svg/my-icons and use them as <x-icon-my-icon />.Modify Icon Rendering:
// app/Providers/BladeIconsServiceProvider.php
BladeIcons::macro('customSvg', function ($icon, $attributes) {
return '<svg custom-attribute="value">' . file_get_contents(resource_path("svg/{$icon}.svg")) . '</svg>';
});
Usage:
@customSvg('camera', ['class' => 'w-6 h-6'])
Add Icon Metadata:
BladeIcons::extend(function ($view, $icon) {
$metadata = collect([
'camera' => ['category' => 'media', 'description' => 'Camera icon'],
]);
$view->withIconMetadata($metadata->get($icon, []));
});
Usage:
@php($metadata = $iconMetadata ?? [])
<x-icon-camera title="{{ $metadata['description'] ?? '' }}" />
Lazy Loading:
<div class="icon-container" data-icon="camera">
<noscript><x-icon-camera /></noscript>
</div>
JavaScript:
document.querySelectorAll('.icon-container').forEach(el => {
const icon = el.dataset.icon;
el.innerHTML = `<x-icon-${icon} />`;
});
Dark Mode Support:
@if(config('app.dark_mode'))
<x-icon-home-dark class="w-6 h-6" />
@else
<x-icon-home class="w-6 h-6" />
@endif
How can I help you explore Laravel packages today?