createStoreMethods API

The createStoreMethods function provides methods to programmatically control the command palette's visibility.

Basic Usage

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

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

// Open programmatically
function handleButtonClick() {
  openPalette();
}

// Close with custom logic
function handleSave() {
  saveData();
  closePalette();
}

// Toggle visibility
function handleKeyPress(event) {
  if (event.key === 'p' && event.metaKey) {
    togglePalette();
  }
}

Available Methods

openPalette()

Opens the command palette and focuses the search input

Returns: void

closePalette()

Closes the palette and clears the search input

Returns: void

togglePalette()

Toggles the palette visibility

Returns: void

With Palette Store

Combine with paletteStore for reactive state:

Reactive Store
<script lang="ts">
  import { paletteStore, createStoreMethods } from '$lib';
  
  const { openPalette, closePalette } = createStoreMethods();
  
  // React to store changes
  $: if ($paletteStore.isVisible) {
    console.log('Palette opened');
  }
  
  // Access called actions history
  $: recentActions = $paletteStore.calledActions;
</script>

<button on:click={openPalette}>
  Open Palette
</button>

{#if recentActions.length > 0}
  <p>Last action: {recentActions[recentActions.length - 1]}</p>
{/if}

Inside Action Callbacks

Store methods are automatically available in action callbacks:

Action Callbacks
const actions = defineActions([
  {
    title: 'Close and Navigate',
    onRun: ({ storeMethods }) => {
      // storeMethods is available in callbacks
      storeMethods.closePalette();
      goto('/dashboard');
    }
  },
  {
    title: 'Keep Open',
    onRun: ({ action, storeProps }) => {
      // Access current state
      console.log('Current search:', storeProps.textInput);
      console.log('Active command:', storeProps.activeCommandId);
      // Don't close - palette stays open
    }
  }
]);

Store Properties

The paletteStore contains:

PropertyTypeDescription
isVisiblebooleanWhether palette is currently open
textInputstringCurrent search query
commandsaction[]All registered actions
resultsaction[]Filtered search results
activeCommandIdstring | number | nullCurrently highlighted action
calledActionsArray<ActionId>History of executed action IDs