createStoreMethods API
The createStoreMethods function provides methods to programmatically
control the command palette's visibility.
Basic Usage
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: voidclosePalette()
Closes the palette and clears the search input
Returns: voidtogglePalette()
Toggles the palette visibility
Returns: voidWith Palette Store
Combine with paletteStore for reactive state:
<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:
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:
| Property | Type | Description |
|---|---|---|
isVisible | boolean | Whether palette is currently open |
textInput | string | Current search query |
commands | action[] | All registered actions |
results | action[] | Filtered search results |
activeCommandId | string | number | null | Currently highlighted action |
calledActions | Array<ActionId> | History of executed action IDs |