blade-ui-kit/blade-icons
Use SVG icons in Laravel Blade with simple components and directives. Convert SVG files into <x-icon-... /> tags or @svg() calls, add classes/attributes easily, and plug in many third‑party icon set packages for quick, consistent icons across your app.
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).Place SVGs:
camera.svg) into resources/svg/[icon-set]/ (e.g., resources/svg/heroicons/).camera.svg → <x-icon-camera />).First Use Case: Replace raw SVG markup in Blade:
<!-- Before -->
<svg ...>...</svg>
<!-- After -->
<x-icon-camera class="w-6 h-6" />
Or use the @svg directive:
@svg('camera', 'w-6 h-6')
config/blade-icons.php (define default and sets).Component-Based Usage:
<!-- Auto-generated component -->
<x-icon-[name] class="text-blue-500" />
php artisan vendor:publish --tag=blade-icons:views to customize component templates.Directive-Based Usage:
@svg('name', ['class' => 'w-4 h-4', 'fill' => 'currentColor'])
@foreach($icons as $icon)
@svg($icon, ['class' => 'w-5 h-5'])
@endforeach
Custom Icon Sets:
config/blade-icons.php:
'sets' => [
'heroicons' => resource_path('svg/heroicons'),
'custom' => resource_path('svg/custom'),
],
resources/svg/custom/ and use:
@svg('custom-icon', 'w-6 h-6', 'custom')
Dynamic Attributes:
<x-icon-camera :class="$isActive ? 'text-green-500' : 'text-gray-500'" />
@class directive for dynamic classes:
@svg('camera', ['class' => $isActive ? 'text-green-500' : 'text-gray-500'])
w-6 h-6) directly in Blade.dark: variants or inline styles:
<x-icon-moon class="dark:text-yellow-300" />
aria-hidden="true" or role="img" to SVG components.<div x-data="{ showIcon: false }" @mouseover="showIcon = true">
<x-icon-settings x-show="showIcon" class="w-4 h-4" />
</div>
Missing SVG Files:
Class 'App\View\Components\IconCamera' not found.resources/svg/heroicons/camera.svg).php artisan cache:clear and check storage/logs/laravel.log.Caching Issues:
php artisan view:clear
php artisan cache:clear
Namespace Conflicts:
App\View\Components\CustomIcon).SVG Optimization:
// In a route or Tinker
\BladeUI\Icons\Facades\BladeIcon::getIconSets();
php artisan config:clear
AppServiceProvider:
Blade::if('svg', function ($expression) {
list($name, $classes, $set) = $expression;
if (!file_exists(resource_path("svg/{$set}/{$name}.svg"))) {
Log::warning("Icon not found: {$set}/{$name}");
}
return true;
});
Custom Directives:
Extend the @svg directive in AppServiceProvider:
Blade::directive('icon', function ($expression) {
list($name, $classes, $set = config('blade-icons.default')) = $expression;
return "<x-icon-{$name} class=\"{$classes}\" />";
});
Usage:
@icon('camera', 'w-6 h-6')
Icon Set Validation:
Override validation in AppServiceProvider:
\BladeUI\Icons\Facades\BladeIcon::macro('validateIcon', function ($name, $set) {
if (!file_exists(resource_path("svg/{$set}/{$name}.svg"))) {
throw new \InvalidArgumentException("Icon {$name} not found in set {$set}");
}
});
Dynamic Icon Sets: Fetch icon sets dynamically (e.g., from a database):
config(['blade-icons.sets' => [
'dynamic' => storage_path('app/svg/dynamic'),
]]);
// mix.js
const svgLoader = require('laravel-mix-svg-loader');
mix.svgLoader();
<div x-data="{ loaded: false }">
<template x-if="!loaded">
<script src="/js/load-icons.js" x-on:load="loaded = true"></script>
</template>
<x-icon-custom x-show="loaded" />
</div>
default set in config/blade-icons.php to avoid runtime errors.Camera.svg ≠ camera.svg).if, else, component).How can I help you explore Laravel packages today?