yeejiawei/laravel-essential-icon
Installation:
composer require yeejiawei/laravel-essential-icon
Publish the config file (if needed):
php artisan vendor:publish --provider="YeeJiaWei\EssentialIcon\EssentialIconServiceProvider"
Register the Blade directive:
Add the following to app/Providers/BladeServiceProvider.php (or create it if missing):
public function boot()
{
$this->directive('icon', function ($expression) {
return "<?php echo YeeJiaWei\\EssentialIcon\\EssentialIcon::render($expression); ?>";
});
}
First Use Case: Use the directive in Blade templates:
@icon('icon-name', ['class' => 'text-blue-500'])
Or via a component (if configured):
<x-icon::icon-name class="text-red-500" />
config/essential-icon.php for default paths and icon sets.YeeJiaWei\EssentialIcon\EssentialIconServiceProvider for registration logic.YeeJiaWei\EssentialIcon\EssentialIcon for rendering logic and supported parameters.Blade Directive:
@icon('home', ['class' => 'w-6 h-6'])
@icon($dynamicIconName)).@icon('settings', class: 'text-gray-500')
Component-Based Usage: If configured, use components like:
<x-icon::home class="text-green-500" />
Custom Icon Sets: Extend the default icon set by publishing and modifying the config:
'icons' => [
'custom' => resource_path('views/vendor/essential-icon/custom'),
],
Dynamic Icon Selection: Use logic to select icons dynamically:
$iconName = auth()->user()->role === 'admin' ? 'admin' : 'user';
@icon($iconName)
Icon Discovery:
resources/views/vendor/essential-icon directory for available icons.php artisan vendor:publish to add new icon sets.Theming Icons: Override icon styles via Tailwind/CSS classes:
@icon('bell', ['class' => 'text-yellow-500 hover:text-yellow-700'])
Reusable Icon Components: Create a Blade component for consistent icon usage:
<!-- resources/views/components/Icon.blade.php -->
<x-icon::{{ $name }} {{ $attributes }} />
Usage:
<x-icon name="search" class="w-5 h-5" />
Laravel Mix/Webpack:
Ensure icon SVG files are processed if using custom icon sets (e.g., optimize SVGs with laravel-mix-svg).
Livewire/Alpine: Combine with Livewire for dynamic icon updates:
<div x-data="{ active: false }">
<button @click="active = !active">
@icon(active ? 'check' : 'x', ['class' => 'w-4 h-4'])
</button>
</div>
Form Requests: Use icons in form buttons or inputs:
<button type="submit" class="flex items-center">
@icon('paper-plane', ['class' => 'mr-2'])
Submit
</button>
Missing Icons:
resources/views/vendor/essential-icon/fontawesome/home.svg).dd(YeeJiaWei\EssentialIcon\EssentialIcon::getIconPath('home'));
Blade Directive Not Registered:
BladeServiceProvider is registered in config/app.php under providers.php artisan view:clear
Component Naming Conflicts:
<x-app-icon::home />).SVG Optimization:
svgo to optimize icon files.Check Icon Paths: Dump the resolved icon path to verify correctness:
dd(config('essential-icon.icons.fontawesome') . '/home.svg');
View Source: Inspect the rendered HTML to confirm the SVG is loaded:
<svg class="...">...</svg>
Log Errors: Add error handling to the directive:
$this->directive('icon', function ($expression) {
try {
return "<?php echo YeeJiaWei\\EssentialIcon\\EssentialIcon::render($expression); ?>";
} catch (\Exception $e) {
return "<?php echo '<span class=\"text-red-500\">Icon Error</span>'; ?>";
}
});
Custom Icon Sets:
'icons' => [
'custom' => base_path('resources/views/vendor/essential-icon/custom'),
'material' => base_path('resources/views/vendor/essential-icon/material-icons'),
],
laravel-essential-icon-generator (if available) to auto-generate icon files.Icon Size Consistency: Standardize icon sizes using Tailwind classes:
@icon('user', ['class' => 'w-5 h-5'])
Dark Mode Support: Use CSS variables or Tailwind’s dark mode classes:
@icon('moon', ['class' => 'dark:text-gray-300'])
Fallback Icons: Provide a fallback for missing icons:
@icon($iconName ?? 'question', ['class' => 'w-4 h-4'])
Performance:
static $iconPaths = [];
if (!isset($iconPaths[$name])) {
$iconPaths[$name] = EssentialIcon::getIconPath($name);
}
Testing: Test icon rendering in PHPUnit:
public function test_icon_rendering()
{
$this->blade('@icon("home")')
->assertSee('<svg');
}
Extension Points:
EssentialIcon class to add custom logic (e.g., icon modifiers):
public static function render($name, $attributes = [])
{
$attributes['data-custom'] = 'value';
return parent::render($name, $attributes);
}
How can I help you explore Laravel packages today?