DEV Community

Cover image for A Cmd+K command palette for Laravel, built in PHP
Amjad Iqbal
Amjad Iqbal

Posted on

A Cmd+K command palette for Laravel, built in PHP

Laravel Ninja Keys adds a keyboard-first Cmd+K / Ctrl+K command palette to a Laravel app. You define the actions in PHP and render the ninja-keys web component [2] with one Blade tag. It works with Blade, Livewire and Alpine.

The problem

Using ninja-keys by hand means a script in every layout, an action list built in JavaScript, and remembering which actions each user may see. The package handles those parts.

How it works

  • Register actions with a fluent builder, using route() or href() so navigation needs no JavaScript.
  • Render the palette with before.
  • define() callbacks run at render time, so they can use the logged-in user and current route.
NinjaKeys::define(function (Manager $palette) {
    $palette->addAction('users')->title('Manage users')
        ->route('users.index')->can('manage-users');
});
Enter fullscreen mode Exit fullscreen mode

Permission-aware

can() runs your Gate abilities [1] on the server. Hidden actions and their sub-actions are removed, so they never appear in the page source.

Safety and limits

  • ninja-keys 1.2.2 is bundled and self-hosted; no third-party script requests unless you choose the CDN.
  • Survives Livewire wire:navigate [3] without duplicate shortcuts.
  • ninja-keys' last release is 1.2.2 (July 2022); the package works around three bugs.

Install

composer require amjadiqbal/laravel-ninja-keys
Enter fullscreen mode Exit fullscreen mode

MIT licensed. Requires PHP 8.3+ and Laravel 12 or 13. Docs and demo: https://urlshortly.com/UlcqQ. Source: https://urlshortly.com/etAOf.

References

  1. Laravel: Authorization
  2. ninja-keys on GitHub
  3. Livewire: wire:navigate

Top comments (0)