blade-ui-kit/blade-heroicons
Use Heroicons in Laravel Blade via simple SVG components. Supports passing classes/attributes, configuration through Blade Icons (defaults, caching), and works with PHP 8+ and Laravel 9+. Browse icons in the bundled SVGs or on heroicons.com.
Installation:
composer require blade-ui-kit/blade-heroicons
This adds Heroicons as Blade components and leverages Blade Icons under the hood.
First Use Case: Use an icon directly in a Blade view:
<x-heroicon-o-arrow-left class="w-6 h-6 text-gray-500" />
Replace o-arrow-left with any Heroicons name.
Key Resources:
Component-Based Usage:
Prefix Heroicons with x-heroicon- followed by the icon type (o-, s-, m-, c-) and name:
<!-- Outline -->
<x-heroicon-o-check-circle class="text-green-500" />
<!-- Solid -->
<x-heroicon-s-pencil class="w-5 h-5" />
<!-- Mini -->
<x-heroicon-m-home class="text-blue-600" />
<!-- Micro -->
<x-heroicon-c-bell class="text-gray-400" />
Dynamic Icons: Use Blade logic to switch icons dynamically:
@php
$icon = $isActive ? 'heroicon-s-check' : 'heroicon-o-x';
@endphp
<x-{{ $icon }} class="w-5 h-5" />
Inline Styling: Pass classes or styles directly:
<x-heroicon-o-search
class="w-6 h-6"
style="color: #6b7280; transition: all 0.2s"
/>
SVG Directive: For non-component usage (e.g., in JavaScript):
@svg('heroicon-o-arrow-left', 'w-4 h-4', ['fill' => 'currentColor'])
Raw SVG Assets: Publish and use SVGs directly:
php artisan vendor:publish --tag=blade-heroicons --force
<img src="{{ asset('vendor/blade-heroicons/o-arrow-left.svg') }}" alt="Icon" />
Tailwind CSS: Combine with Tailwind for consistent sizing/colors:
<x-heroicon-o-menu class="w-6 h-6 text-indigo-600 hover:text-indigo-800" />
Component Wrappers:
Create reusable components (e.g., ButtonIcon.blade.php):
<button class="p-2 rounded-md hover:bg-gray-100">
<x-heroicon-o-{{ $icon }} class="w-5 h-5" />
</button>
Icon Sets: Group related icons in a partial:
@component('icons.user')
<x-heroicon-o-user />
<x-heroicon-o-cog />
@endcomponent
Dark Mode: Use Tailwind’s dark mode classes:
<x-heroicon-o-moon class="w-6 h-6 dark:text-gray-300" />
Accessibility:
Add aria-hidden and role for non-interactive icons:
<x-heroicon-o-info-circle
class="w-5 h-5 text-blue-500"
aria-hidden="true"
role="img"
/>
Caching:
php artisan view:clear
config/blade-heroicons.php for production:
'cache' => env('ICON_CACHE_ENABLED', true),
Icon Naming:
arrow-left), not camelCase.Laravel Version:
SVG Publishing:
public/vendor/blade-heroicons.--force or back up files first.Component Autoloading:
BladeIconsServiceProvider is in config/app.php.Missing Icons:
php artisan view:clear if icons disappear after updates.Styling Issues:
!important sparingly; Heroicons SVGs may have inline styles.Performance:
'cache' => true).style="color: {{ $color }}")—use classes instead.Custom Icons:
BladeIconsServiceProvider or publishing SVGs manually.Global Defaults:
Configure defaults in config/blade-heroicons.php:
'default_class' => 'w-5 h-5 text-gray-500',
'default_attributes' => ['aria-hidden' => 'true'],
Icon Aliases: Create shortcuts in a macro:
use BladeUI\Icons\Factory;
BladeIcons::macro('user', fn() => 'heroicon-o-user');
Usage:
<x-heroicon-user class="w-6" />
Dynamic Icon Resolution:
Override the resolver in AppServiceProvider:
public function boot()
{
BladeIcons::resolveUsing(function ($icon) {
return "custom-{$icon}";
});
}
Testing: Mock icons in tests:
BladeIcons::shouldReceive('resolve')->andReturn('heroicon-o-check');
How can I help you explore Laravel packages today?