Command palette

A command palette over the app: one field that finds pages, customers and deals and runs actions, grouped with their shortcuts, opened with Ctrl K.

Open full screen

A panel opens over any screen and keeps its own look in every direction: it has no direction of its own.

npx wideset add block-command-palette

Import the default export from @/blocks/wideset/command-palette. The files below are relative to your project's source directory.

Installed with this block
  • app-shell
  • avatar
  • block-styles
  • breadcrumbs
  • card
  • divider
  • dropdown-menu
  • icon-button
  • list-item
  • modal
  • sidebar
  • status-badge
  • table
  • text-input
  • utils
blocks/wideset/command-palette/index.tsx
'use client';

import '@/styles/wideset-blocks.css';

import { RiSideBarLine } from '@remixicon/react';
import { AppShell } from '@/components/ui/wideset/app-shell/AppShell';
import { Breadcrumbs } from '@/components/ui/wideset/breadcrumbs/Breadcrumbs';
import { IconButton } from '@/components/ui/wideset/button/IconButton';
import { useEffect, useId, useState } from 'react';

import { CommandPalette } from './command-palette';
import { type Command, commands, recentAtStart, suggested } from './data';
import { HomePage } from './home-page';
import { WorkspaceSidebar } from './sidebar';

/** How many recent items the empty palette lists. */
const RECENT_LIMIT = 4;

/**
 * Command palette (panel over Home, Inset direction): Ctrl+K or ⌘K anywhere, or the sidebar's
 * search field, which shows the shortcut, open it: one door on the screen, not two. Running a
 * page, customer or deal moves it to the top of Recent; a starting point has nowhere to go, so
 * running only closes the palette.
 */
export default function CommandPaletteBlock() {
  const sidebarId = useId();
  const [collapsed, setCollapsed] = useState(false);
  const [open, setOpen] = useState(true);
  const [recent, setRecent] = useState(recentAtStart);

  useEffect(() => {
    const onKeyDown = (event: KeyboardEvent) => {
      if (event.key.toLowerCase() !== 'k' || !(event.metaKey || event.ctrlKey)) return;
      event.preventDefault();
      setOpen((current) => !current);
    };
    window.addEventListener('keydown', onKeyDown);
    return () => window.removeEventListener('keydown', onKeyDown);
  }, []);

  const run = (command: Command) => {
    if (command.group !== 'Actions') {
      setRecent((current) =>
        [command.id, ...current.filter((id) => id !== command.id)].slice(0, RECENT_LIMIT),
      );
    }
    setOpen(false);
  };

  return (
    <AppShell className="ws-block-utility" direction="inset">
      <WorkspaceSidebar
        id={sidebarId}
        collapsed={collapsed}
        onCollapsedChange={setCollapsed}
        onSearch={() => setOpen(true)}
      />
      <AppShell.Main>
        <div className="h-ws-18 border-ws-static-base flex shrink-0 items-center border-b">
          {/* The bar belongs to the surround: it runs edge to edge. The sidebar's toggle is pulled
              left so its glyph, not its box, sits 20px from the edge; the crumbs follow it. */}
          <div className="gap-ws-5 px-ws-11 flex w-full items-center">
            <IconButton
              icon={RiSideBarLine}
              variant="secondary"
              preset="ghost"
              size="sm"
              iconClassName="text-ws-static-subtle size-ws-9"
              aria-label={collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
              aria-expanded={!collapsed}
              aria-controls={sidebarId}
              onClick={() => setCollapsed((collapsed) => !collapsed)}
              className="-ml-ws-5"
            />
            <Breadcrumbs separator="arrow">
              <Breadcrumbs.Item>Home</Breadcrumbs.Item>
            </Breadcrumbs>
          </div>
        </div>

        <HomePage />
      </AppShell.Main>

      <CommandPalette
        open={open}
        onOpenChange={setOpen}
        commands={commands}
        recent={recent}
        suggested={suggested}
        onRun={run}
      />
    </AppShell>
  );
}