Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Blade Icon Generation Helpers Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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,
    
  2. 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.

  3. Where to Look First:

    • Documentation: Check the package’s README.md for basic syntax and examples.
    • Service Provider: Inspect BladeIconHelpersServiceProvider for registered helpers and aliases.
    • Helper Methods: Review the helpers.php file (if auto-loaded) or the BladeIconHelper class for available methods.

Implementation Patterns

Common Workflows

  1. 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
    
  2. 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" />
    
  3. 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')
    
  4. SVG Customization: Pass additional SVG attributes for advanced customization:

    @icon('home', '32px', '#333', ['fill-opacity' => '0.8', 'class' => 'hover:fill-blue-500'])
    

Integration Tips

  • 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')
    

Gotchas and Tips

Pitfalls

  1. 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,
    
  2. SVG Attribute Conflicts: Avoid passing conflicting SVG attributes (e.g., width and height if size is already set). The package may override these.

  3. 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')
    
  4. 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()])
    

Debugging

  • 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]);
    }
    

Extension Points

  1. 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;
        });
    }
    
  2. 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)
    
  3. 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>
    

Configuration Quirks

  • 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')));
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views