wire-elements/spotlight
Livewire Spotlight adds an Alfred-like command palette to Laravel. Open with configurable keyboard shortcuts or toggle via events, then search, navigate, and run actions from a sleek input bar. Easy Composer install and Livewire directive.
Installation:
composer require wire-elements/spotlight
Add the Livewire directive to your layout (e.g., resources/views/layouts/app.blade.php):
@livewire('livewire-ui-spotlight')
For Livewire v2, include Alpine.js via CDN:
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
First Command: Generate a basic command:
php artisan make:spotlight Logout
Edit the generated file (app/SpotlightCommands/Logout.php):
use LivewireUI\Spotlight\SpotlightCommand;
class Logout extends SpotlightCommand {
protected string $name = 'Logout';
protected string $description = 'Logout of your account';
public function execute() {
Auth::logout();
redirect('/');
}
}
Register Command:
Add the command to config/livewire-ui-spotlight.php:
'commands' => [
\App\SpotlightCommands\Logout::class,
],
Test Spotlight:
Open Spotlight with Ctrl+K (or Cmd+K on Mac) and search for "Logout".
Command Creation:
php artisan make:spotlight for boilerplate.LivewireUI\Spotlight\SpotlightCommand and define:
$name (displayed in search results).$description (tooltip text).execute() (logic when command is selected).Dynamic Commands:
shouldBeShown() to conditionally hide/show commands (e.g., role-based access):
public function shouldBeShown(): bool {
return auth()->user()->isAdmin();
}
Dependency Handling:
dependencies():
public function dependencies() {
return SpotlightCommandDependencies::collection()
->add(SpotlightCommandDependency::make('user')->setPlaceholder('Search for a user...'));
}
search{DependencyName}() to fetch dynamic results:
public function searchUser($query) {
return User::where('name', 'like', "%$query%")
->limit(5)
->get()
->map(fn ($user) => new SpotlightSearchResult($user->id, $user->name, 'Select user'));
}
Integration with Livewire:
execute() to trigger Livewire updates:
public function execute(Spotlight $spotlight) {
$spotlight->emit('showModal', ['type' => 'create-user']);
}
protected $listeners = ['showModal' => 'handleModal'];
Global Shortcuts:
config/livewire-ui-spotlight.php:
'shortcuts' => ['k', 'slash', 'shift+k'], // Adds Shift+K as a trigger
<button @click="$dispatch('toggle-spotlight')">Open Spotlight</button>
Search Synonyms:
protected array $synonyms = ['billing', 'payment', 'invoice'];
Conditional Registration:
Spotlight::registerCommandIf(
auth()->check(),
\App\SpotlightCommands\UserProfile::class
);
Custom Views:
php artisan vendor:publish --tag=livewire-ui-spotlight-views
resources/views/vendor/livewire-ui-spotlight/spotlight.blade.php.Localization:
php artisan vendor:publish --tag=livewire-ui-spotlight-translations
resources/lang/en/spotlight.php:
'placeholder' => 'Search actions...',
Livewire v2 vs v3:
dispatchBrowserEvent('toggle-spotlight').dispatch('toggle-spotlight').Dependency Resolution:
execute() dependencies fail, verify:
Team model must be resolvable).search{Dependency} method returns SpotlightSearchResult objects (not raw data).Command Visibility:
Spotlight::registerCommand()) and pass shouldBeShown() if defined.public function shouldBeShown(): bool {
\Log::info('Checking visibility...');
return true; // Temporarily force-show for testing
}
CSS/JS Conflicts:
'include_css' => true in config if not using Tailwind.'include_js' => false and manually bundle fuse.js if using a custom build system.Shortcut Conflicts:
Ctrl+K used elsewhere) may require customizing the config or using JavaScript to intercept events.Log Command Execution:
Add debug logs to execute() or shouldBeShown():
public function execute() {
\Log::info('Logout command executed');
Auth::logout();
}
Inspect Search Results:
Dump search{Dependency} results to verify data:
public function searchUser($query) {
$results = User::where('name', 'like', "%$query%")->get();
\Log::info($results);
return $results->map(...);
}
Check Registration: Verify commands are loaded by inspecting the config or using:
\LivewireUI\Spotlight\Facades\Spotlight::getCommands();
Alpine/JS Issues:
fuse.js).Custom Search Logic:
Override the default search behavior by extending the Spotlight component or using middleware to filter commands.
Theme Customization: Modify the published Blade view to change styling or layout (e.g., add icons, adjust spacing).
Event Listeners: Listen for Spotlight events globally (e.g., in a service provider) to log usage or trigger analytics:
\LivewireUI\Spotlight\Facades\Spotlight::on('command-executed', function ($command) {
\Log::info("Command executed: {$command->getName()}");
});
Dynamic Command Loading: Fetch commands via API (e.g., for admin panels) by implementing a custom command resolver.
How can I help you explore Laravel packages today?