codeat3/blade-icon-generation-helpers
Laravel package that adds helper directives and utilities to generate Blade icon components quickly. Streamlines converting icon sets into reusable Blade components, reducing boilerplate and keeping icon usage consistent across your views.
Installation:
composer require codeat3/blade-icon-generation-helpers
Publish the config (if available) or register the service provider in config/app.php under providers:
Codeat3\BladeIconHelpers\BladeIconHelpersServiceProvider::class,
First Use Case: Generate a basic SVG icon in a Blade template:
@icon('user', '24px', '#333')
This renders a user icon with a height of 24px and black color.
Where to Look First:
README.md for basic syntax and examples.BladeIconHelpersServiceProvider for registered helpers and aliases.helpers.php file (if auto-loaded) or the BladeIconHelper class for available methods.Dynamic Icon Generation: Use Blade directives to dynamically generate icons based on conditions:
@if($user->isAdmin)
@icon('admin', '32px', '#20c997')
@else
@icon('user', '24px', '#6c757d')
@endif
Reusable Icon Components: Create a Blade component for consistent icon usage:
<!-- resources/views/components/icon.blade.php -->
<x-icon :name="$name" :size="$size" :color="$color" />
Usage:
<x-icon name="settings" size="20px" color="#6f42c1" />
Integration with Tailwind CSS: Combine with Tailwind for responsive and themed icons:
@icon('bell', '24px', 'currentColor')
<style>
.text-primary { color: #0d6efd; }
</style>
Usage:
@icon('bell', '24px', 'text-primary')
SVG Customization: Pass additional SVG attributes for advanced customization:
@icon('home', '32px', '#333', ['fill-opacity' => '0.8', 'class' => 'hover:fill-blue-500'])
Asset Optimization: Use the package’s helpers to generate inline SVGs, reducing HTTP requests for icons. Example:
@icon('logo', '48px', '#000', ['viewBox' => '0 0 24 24'])
Localization: Combine with Laravel’s localization features for multi-language icon labels:
<div class="flex items-center">
@icon('globe', '16px', '#6c757d')
<span>{{ __('messages.language') }}</span>
</div>
Dark Mode Support: Use dynamic color switching based on user preferences:
@icon('moon', '20px', request()->wantsDarkMode() ? '#fff' : '#333')
Caching Issues: If icons appear stale, clear Blade cache:
php artisan view:clear
Or disable caching for development:
// config/view.php
'compiled' => env('APP_DEBUG') ? false : true,
SVG Attribute Conflicts:
Avoid passing conflicting SVG attributes (e.g., width and height if size is already set). The package may override these.
Namespace Collisions:
If using other icon packages (e.g., blade-icons), ensure helper names don’t clash. Prefix or alias methods:
// config/app.php
'aliases' => [
'BladeIcon' => Codeat3\BladeIconHelpers\Facades\BladeIcon::class,
];
Usage:
@BladeIcon('user', '24px')
Browser Caching: Inline SVGs may not update immediately due to caching. Append a query string to force refresh:
@icon('refresh', '24px', '#333', ['class' => 'cache-bust-' . time()])
Check Registered Helpers: Dump the available helpers in a Blade template:
@dump(app('blade')->getCompiler()->getDirectives())
Look for @icon or related directives.
Log Helper Output: Temporarily modify the helper to log its arguments:
// In a helper file or service provider
if (env('APP_DEBUG')) {
\Log::debug('Icon args:', ['name' => $name, 'size' => $size, 'color' => $color]);
}
Custom Icon Sets:
Extend the package by adding your own icon sets. Override the getIconDefinition method in a service provider:
public function register()
{
$this->app->extend('blade-icon.definitions', function ($definitions) {
$definitions['custom'] = collect([
'home' => '<svg>...</svg>',
'user' => '<svg>...</svg>',
]);
return $definitions;
});
}
Dynamic Icon Loading: Load icons from a database or API. Create a custom helper:
// app/Helpers/IconHelper.php
function dynamicIcon($id, $size = '24px', $color = '#333')
{
$icon = Icon::find($id);
return str_replace(['{size}', '{color}'], [$size, $color], $icon->svg);
}
Register it in AppServiceProvider:
Blade::directive('dynamicIcon', function ($expression) {
return "<?php echo dynamicIcon($expression); ?>";
});
Usage:
@dynamicIcon($iconId)
Reactivity with Alpine.js: Make icons reactive to user interactions:
<div x-data="{ isActive: false }">
@icon('star', '24px', x-bind:style="isActive ? '#ffc107' : '#6c757d'")
<button @click="isActive = !isActive">Toggle</button>
</div>
Default Values:
Check if the package has default values for size or color in its config. Override them in config/blade-icon.php (if published):
'defaults' => [
'size' => '16px',
'color' => '#333',
],
Icon Naming Conventions:
Ensure icon names match the package’s expected format (e.g., user vs. fa-user). Refer to the package’s definitions file for valid names.
Performance:
For large icon sets, consider lazy-loading or splitting definitions into multiple files. Override the getIconDefinition method to load definitions dynamically:
$definitions = Cache::remember("blade-icon.definitions", now()->addHours(1), function () {
return collect(file_get_contents(storage_path('app/icon-definitions.json')));
});
How can I help you explore Laravel packages today?