PHP code example of nrep / filament-theme-switcher

1. Go to this page and download the library: Download nrep/filament-theme-switcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nrep / filament-theme-switcher example snippets


use Isura\FilamentThemeSwitcher\FilamentThemeSwitcherPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        // ... other configuration
        ->plugin(
            FilamentThemeSwitcherPlugin::make()
        );
}

use Isura\FilamentThemeSwitcher\Http\Middleware\SetTheme;

public function panel(Panel $panel): Panel
{
    return $panel
        // ... other configuration
        ->middleware([
            // ... other middleware
            SetTheme::class,
        ]);
}

// config/filament-theme-switcher.php
return [
    'mode' => 'global', // or 'user' for per-user themes
    'default_theme' => 'default',
];

FilamentThemeSwitcherPlugin::make()
    // Set the theme mode
    ->mode('user') // 'global' or 'user'
    
    // Customize navigation
    ->navigationGroup('Settings')
    ->navigationIcon('heroicon-o-paint-brush')
    ->navigationSort(100)
    ->navigationLabel('Appearance')
    
    // Control page access
    ->registerNavigation(true) // Show/hide from navigation
    ->canViewThemesPage(fn () => auth()->user()?->is_admin)
    
    // Register custom themes
    ->registerThemes([
        'my-theme' => MyCustomTheme::class,
    ])
    
    // Override default themes entirely
    ->registerThemes([
        'my-theme' => MyCustomTheme::class,
    ], override: true);



namespace App\Filament\Themes;

use Filament\Support\Colors\Color;
use Isura\FilamentThemeSwitcher\Themes\AbstractTheme;

class MyCustomTheme extends AbstractTheme
{
    public static function getName(): string
    {
        return 'my-custom-theme';
    }

    public function getLabel(): string
    {
        return 'My Custom Theme';
    }

    public function getColors(): array
    {
        return [
            'primary' => Color::Purple,
            'danger' => Color::Red,
            'gray' => Color::Slate,
            'info' => Color::Blue,
            'success' => Color::Green,
            'warning' => Color::Amber,
        ];
    }
}

FilamentThemeSwitcherPlugin::make()
    ->registerThemes([
        'my-custom-theme' => MyCustomTheme::class,
    ]);

public function getColors(): array
{
    return [
        'primary' => '#8b5cf6',
        'danger' => '#ef4444',
        'gray' => '#64748b',
        'info' => '#3b82f6',
        'success' => '#22c55e',
        'warning' => '#f59e0b',
    ];
}

use Filament\View\PanelsRenderHook;
use Illuminate\Support\Facades\Blade;

public function panel(Panel $panel): Panel
{
    return $panel
        ->renderHook(
            PanelsRenderHook::USER_MENU_BEFORE,
            fn () => Blade::render('@livewire(\'theme-switcher\')')
        );
}

FilamentThemeSwitcherPlugin::make()
    ->canViewThemesPage(fn () => auth()->user()?->hasPermission('manage-themes'));
bash
php artisan vendor:publish --tag="filament-theme-switcher-config"
bash
php artisan vendor:publish --tag="filament-theme-switcher-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="filament-theme-switcher-translations"