Palette Store

The palette store is a standard Svelte store that holds all command palette state. Subscribe to it, update it, and extend it with your own properties.

Store Structure

The store contains the following properties:

PropertyTypeDescription
isVisiblebooleanWhether the palette is currently open
textInputstringCurrent search query
commandsaction[]All registered actions
resultsaction[]Filtered search results
activeCommandIdstring | number | nullCurrently highlighted action ID
selectedCommandIdstring | number | nullLast selected action ID
calledActionsArray<ActionId>History of executed action IDs
actionMapobjectMap of actionId to action object
storeMethodsobjectMethods to control the palette

Subscribing & Updating

Use standard Svelte store patterns to interact with the palette:

Store Usage
import { paletteStore } from '$lib';

// Subscribe to store changes
paletteStore.subscribe((state) => {
  console.log('Visibility:', state.isVisible);
  console.log('Search text:', state.textInput);
  console.log('Active command:', state.activeCommandId);
  console.log('Called actions:', state.calledActions);
});

// Update the store
paletteStore.update((state) => ({
  ...state,
  textInput: 'prefilled search',
  isVisible: true
}));

Store Methods

Use createStoreMethods() to get helper functions:

Store Methods
import { createStoreMethods } from '$lib';

// Create store methods
const { openPalette, closePalette, togglePalette } = createStoreMethods();

// Open programmatically
<button onclick={() => openPalette()}>
  Open Command Palette
</button>

// Or toggle
document.addEventListener('keydown', (e) => {
  if (e.key === 'p' && e.metaKey) {
    togglePalette();
  }
});

Available Methods

openPalette()

Opens the command palette and focuses the search input

closePalette()

Closes the palette and clears the search

togglePalette()

Toggles the palette visibility

Extending the Store

Add your own properties to track custom state:

Custom State
// Add custom state to track user preferences
paletteStore.update((state) => ({
  ...state,
  // Your custom properties
  lastUsedActions: [],
  favoriteActions: ['action-1', 'action-2'],
  settings: {
    showDescriptions: true,
    maxResults: 10
  }
}));

// Access in action callbacks
const actions = defineActions([
  {
    title: 'Toggle Descriptions',
    onRun: ({ storeProps }) => {
      paletteStore.update(s => ({
        ...s,
        settings: {
          ...s.settings,
          showDescriptions: !storeProps.settings.showDescriptions
        }
      }));
    }
  }
]);
💡
Pro tip: The calledActions array persists across palette opens. Use it to implement features like "recently used" or conditional action chains.

Reactive Store in Components

Access the store reactively in Svelte components:

+page.svelte
<script>
  import { paletteStore } from '$lib'; // Use 'svelte-command-palette' in your project
  
  // Reactive access
  $: isOpen = $paletteStore.isVisible;
  $: searchQuery = $paletteStore.textInput;
  $: resultCount = $paletteStore.results.length;
</script>

{#if isOpen}
  <div class="status">
    Searching: "{searchQuery}" ({resultCount} results)
  </div>
{/if}