File size: 3,513 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Dashboard Command Palette

A command palette implementation for the dashboard, using WordPress's `@wordpress/commands` package and TanStack Router for navigation.

## Architecture

The command palette integrates with both WordPress Commands API and TanStack Router:

- Uses WordPress Commands for the UI, keyboard shortcuts, and command registration
- Uses TanStack Router for navigation commands
- Must be placed within the router context for navigation to work properly

## Placement Requirements

The command palette component **must** be placed within the Router context:

```jsx
// In root/index.tsx
import CommandPalette from '../command-palette';

function Root() {
  return (
    <div className="dashboard-root__layout">
      <Header />
      <main>
        <Outlet />
      </main>
      <CommandPalette />
    </div>
  );
}
```

> **Important**: The command palette must be a child of a component within the TanStack Router context (like Root). Placing it as a sibling to RouterProvider will break navigation functionality.

## Opening the Command Palette

The command palette opens automatically with Cmd+K / Ctrl+K, or programmatically:

```jsx
import { useOpenCommandPalette } from '../command-palette/utils';

function YourComponent() {
	const openCommandPalette = useOpenCommandPalette();
  return (
    <button onClick={openCommandPalette}>
      Open Command Palette
    </button>
  );
}
```

## Implementation Details

This command palette:

1. **Uses WordPress Commands API**: Leverages the WordPress commands system for UI and keyboard shortcuts
2. **Integrates with TanStack Router**: Routes defined in commands.tsx use absolute paths (/path) for consistent navigation
3. **Auto-registers Navigation Commands**: Registers dashboard navigation commands on component mount
4. **Self-cleans on Unmount**: Unregisters all dashboard commands when the component unmounts

## Context-Aware Commands

Navigation commands can be filtered based on feature flags from the app context:

```typescript
export const navigationCommands: Command[] = [
  {
    name: 'dashboard-go-to-sites',
    label: __( 'Go to Sites' ),
    searchLabel: __( 'Navigate to Sites dashboard' ),
    path: '/sites',  // Note the leading slash for absolute paths
    icon: home,
    feature: 'sites', // Only show if 'sites' feature is enabled in the app context
  },
  // ... other commands
];
```

The command palette will only register commands for features that are enabled in the current dashboard configuration. This allows different dashboards (e.g., `/v2` vs `/v2-a4a`) to show different commands without code duplication.

## Adding Custom Commands

Custom commands can be added from any component:

```jsx
import { useRouter } from '@tanstack/react-router';
import { store as commandsStore } from '@wordpress/commands';
import { dispatch } from '@wordpress/data';

function YourComponent() {
  const router = useRouter();

  useEffect(() => {
    const { registerCommand } = dispatch(commandsStore);

    registerCommand({
      name: 'your-command-name',
      label: 'Your Command Label',
      callback: ({ close }) => {
        // First close the palette
        close();
        // Then perform any action, like navigation
        router.navigate({ to: '/your-path' });
      },
      icon: yourIcon,
    });

    return () => {
      const { unregisterCommand } = dispatch(commandsStore);
      unregisterCommand('your-command-name');
    };
  }, [router]);

  return <div>Your component content</div>;
}
```