LivewireUI Spotlight is a Livewire component that provides Spotlight/Alfred-like functionality to your Laravel application. View demo video.
To get started, require the package via Composer:
composer require livewire-ui/spotlight
Add the Livewire directive @livewire('livewire-ui-spotlight')
:
<html>
<body>
<!-- content -->
@livewire('livewire-ui-spotlight')
</body>
</html>
To open the Spotlight input bar you can use one of the following shortcuts:
- CTRL + K
- CMD + K
- CTRL + /
- CMD + /
You can create your first Spotlight command by creating a new class and have it
extend LivewireUI\Spotlight\SpotlightCommand
. Start by defining a $name
and $description
for your command. The
name and description will be visible when searching through commands.
<?php
namespace LivewireUI\Spotlight\Commands;
use LivewireUI\Spotlight\SpotlightCommand;
class Logout extends SpotlightCommand
{
protected string $name = 'Logout';
protected string $description = 'Logout out of your account';
}
The execute
method is called when a command is chosen, and the command has no dependencies. Let's for example take a
look at the Logout
command execute
method:
<?php
namespace LivewireUI\Spotlight\Commands;
use Illuminate\Contracts\Auth\StatefulGuard;
use LivewireUI\Spotlight\Spotlight;
use LivewireUI\Spotlight\SpotlightCommand;
class Logout extends SpotlightCommand
{
protected string $name = 'Logout';
protected string $description = 'Logout out of your account';
public function execute(Spotlight $spotlight, StatefulGuard $guard): void
{
$guard->logout();
$spotlight->redirect('/');
}
}
As you can see, you can type-hint your dependencies and have them resolved by Laravel. If you
type-hint Spotlight $spotlight
, you will get access to the Livewire Spotlight component. This gives you access to all
the Livewire helpers, so you can redirect users, emit events, you name it.
In some cases your command might require dependencies. Let's say we want to create a new user and add it to a specific
team. In this case we would need to define a team dependency. To define any dependencies, add a new method to your
command and name the method dependencies
.
You can use the SpotlightCommandDependencies::collection()
method to create a new collection of dependencies. Call
the add
method to register a new dependency. You can add as many of dependencies as you like. The user input prompt
follows the order in which you add the commands.
SpotlightCommandDependencies::collection()
->add(SpotlightCommandDependency::make('team')->setPlaceholder('For which team do you want to create a user?'))
->add(SpotlightCommandDependency::make('foobar')->setPlaceholder('Some other dependency here?'));
For every dependency, Spotlight will check if a search{dependency-name}
method exists on the command. This method
provides the search query given by the user. For example, to search for our team dependency:
public function searchTeam($query)
{
return Team::where('name', 'like', "%$query%")
->get()
->map(function(Team $team) {
return new SpotlightSearchResult(
$team->id,
$team->name,
sprintf('Create license for %s', $team->name)
);
});
}
Spotlight expects a collection of SpotlightSearchResult
objects. The SpotlightSearchResult
object consists out of
the result identifier, name and description.
Every dependency will have access to the already defined dependencies. So in the example below, you can see
that searchFoobar
has access to the `Team the user has chosen. This allows for scoped dependency searching.
use LivewireUI\Spotlight\Spotlight;
use LivewireUI\Spotlight\SpotlightCommand;
use LivewireUI\Spotlight\SpotlightCommandDependencies;
use LivewireUI\Spotlight\SpotlightCommandDependency;
use LivewireUI\Spotlight\SpotlightSearchResult;
class CreateUser extends SpotlightCommand
{
protected string $name = 'Create user';
protected string $description = 'Create new team user';
public function dependencies(): ?SpotlightCommandDependencies
{
return SpotlightCommandDependencies::collection()
->add(SpotlightCommandDependency::make('team')->setPlaceholder('For which team do you want to create a user?'))
->add(SpotlightCommandDependency::make('foobar')->setPlaceholder('Some other dependency here?'));
}
public function searchTeam($query)
{
return Team::where('name', 'like', "%$query%")
->get()
->map(function(Team $team) {
return new SpotlightSearchResult(
$team->id,
$team->name,
sprintf('Create user for %s', $team->name)
);
});
}
public function searchFoobar(Team $team, $query)
{
return $team->foobar()->where('name', 'like', "%$query%")
->get()
->map(function(Foobar $foobar) {
return new SpotlightSearchResult(
$foobar->id,
$foobar->name,
sprintf('Create something for %s', $foobar->name)
);
});
}
public function execute(Spotlight $spotlight, Team $team)
{
$spotlight->emit('openModal', 'user-create', ['team' => $team->id]);
}
}
You can register commands by adding these to the livewire-ui-spotlight.php
config file:
<?php
return [
'placeholder' => 'What do you want to do?',
'commands' => [
\App\SpotlightCommands\CreateUser::class
]
];
It's also possible to register commands via one of your service providers:
use \App\SpotlightCommands\CreateUser;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Spotlight::registerCommand(CreateUser::class);
}
}
You can customize the placeholder for Spotlight via the livewire-ui-spotlight.php
config file. This includes some additional options like including CSS if you don't use TailwindCSS for your application. To publish the config run the vendor:publish command:
php artisan vendor:publish --tag=livewire-ui-spotlight-config
<?php
return [
/*
|--------------------------------------------------------------------------
| Shortcuts
|--------------------------------------------------------------------------
|
| Define which shortcuts will activate Spotlight CTRL / CMD + key
| The default is CTRL/CMD + K or CTRL/CMD + /
|
*/
'shortcuts' => [
'k',
'slash',
],
/*
|--------------------------------------------------------------------------
| Placeholder
|--------------------------------------------------------------------------
|
| Define the text you want to show by default when Spotlight is enabled
|
*/
'placeholder' => 'What do you want to do?',
/*
|--------------------------------------------------------------------------
| Placeholder
|--------------------------------------------------------------------------
|
| Define which commands you want to make available in Spotlight.
| Alternatively, you can also register commands in your AppServiceProvider
| with the Spotlight::registerCommand(Logout::class); method.
|
*/
'commands' => [
\LivewireUI\Spotlight\Commands\Logout::class
],
/*
|--------------------------------------------------------------------------
| Include CSS
|--------------------------------------------------------------------------
|
| Spotlight uses TailwindCSS, if you don't use TailwindCSS you will need
| to set this parameter to true. This includes the modern-normalize css.
|
*/
'include_css' => false,
];
Livewire UI is open-sourced software licensed under the MIT license.