Skip to main content

Commands

The admin panel has a command menu that opens with ⌘K on macOS and Ctrl+K everywhere else. It lists commands, like switching the theme, that admins can search and run from the keyboard. You can add your own commands to it.

Registering commands

Register commands in a Larasell.booting() callback. For an app, that's resources/js/admin/app.tsx, which the panel loads on every admin page. For an extension, it's the script the extension registers with AdminPanel::vite().

Each command needs a unique id, a label, and a run function that runs when the admin picks it:

 1import { router } from '@inertiajs/react';
 2import { Larasell } from '@larasell-dev/admin';
 3
 4Larasell.booting(({ commands }) => {
 5    commands.register({
 6        id: 'orders.archived',
 7        label: 'Show archived orders',
 8        keywords: ['history', 'old'],
 9        run: () => router.visit('/admin/orders?archived=1'),
10    });
11});

Commands registered this way appear on every page. The menu lists the panel's own commands first, then yours in the order you registered them. The menu closes after a command runs.

Search matches a command's label and its keywords, so add words admins might type that aren't in the label.

Ids must be unique, and registering an id twice throws an error. In an extension, prefix each id with the extension's name, such as larasell-reviews.publish-all, so it can't clash with another extension.

Icons

Show an icon before the label with icon. It takes a Remix icon from @larasell-dev/ui/icons:

1import { ArchiveLine } from '@larasell-dev/ui/icons';
2
3commands.register({
4    id: 'orders.archived',
5    label: 'Show archived orders',
6    icon: ArchiveLine,
7    run: () => router.visit('/admin/orders?archived=1'),
8});

Icons are bundled with your own code, so install @larasell-dev/ui in your app or extension to import them. Labels of commands without an icon still line up with the others.

Nested commands

A command with items instead of run opens a nested list when the admin picks it. Each item has its own id, label and run. Set checked on the item that is currently in use, and the menu marks it with a check:

 1commands.register({
 2    id: 'acme.density',
 3    label: 'Change table density',
 4    placeholder: 'Search densities',
 5    items: [
 6        { id: 'compact', label: 'Compact', run: () => setDensity('compact') },
 7        {
 8            id: 'comfortable',
 9            label: 'Comfortable',
10            checked: true,
11            run: () => setDensity('comfortable'),
12        },
13    ],
14});

placeholder replaces the text in the search field while the nested list is open.

Searching from the top level also finds nested items. Typing "compact" shows Change table density › Compact, which runs straight away when picked.

Pressing Esc clears the search, then goes back to the top level, then closes the menu.

Commands that depend on the page

Give a command a use hook instead of fixed fields when it depends on the current page or other state. The menu calls the hook while it's open, so the hook can use React and Inertia hooks. It returns the command's fields, or null to hide the command:

 1import { router, usePage } from '@inertiajs/react';
 2import { defineCommand, Larasell } from '@larasell-dev/admin';
 3
 4const exportOrders = defineCommand({
 5    id: 'acme.export-orders',
 6    use() {
 7        const { url } = usePage();
 8
 9        if (!url.startsWith('/admin/orders')) {
10            return null;
11        }
12
13        return {
14            label: 'Export orders',
15            run: () => router.post('/admin/orders/export'),
16        };
17    },
18});
19
20Larasell.booting(({ commands }) => commands.register(exportOrders));

defineCommand adds types to a command you define in its own module. It returns the command unchanged.

Disabling commands

Set disabled to explain why a command can't run right now. The menu shows the reason in place of the command's check mark and won't run the command, open its nested list, or find its items:

1return {
2    label: 'Export orders',
3    disabled: orders.length === 0 && 'No orders to export',
4    run: () => router.post('/admin/orders/export'),
5};

Commands for a single page

To offer a command only on one page, call useCommand in that page's component. It takes the same fields as commands.register(). The command is in the menu while the component is mounted and goes away when the admin leaves the page:

 1import { router } from '@inertiajs/react';
 2import { SidebarLayout, useCommand } from '@larasell-dev/admin';
 3
 4export default function Report({ downloadUrl }: { downloadUrl: string }) {
 5    useCommand({
 6        id: 'acme.report.download',
 7        label: 'Download report',
 8        run: () => router.visit(downloadUrl),
 9    });
10
11    return <SidebarLayout>{/* … */}</SidebarLayout>;
12}

The menu lists page commands after the global ones. A page command always uses the latest props of its component, so you don't need to pass dependencies.