defineActions API

The defineActions helper function creates properly typed action arrays for the command palette.

Function Signature

Signature
function defineActions(actions: action[]): action[]

Full Example

Complete Action
import { defineActions } from '$lib';
import type { action } from '$lib';

const actions = defineActions([
  {
    // Required
    title: 'Go to Dashboard',
    
    // Optional - Unique identifier
    actionId: 'nav-dashboard',
    
    // Optional - Secondary text
    subTitle: 'View your analytics',
    
    // Optional - Additional description  
    description: 'Opens the main dashboard with all your metrics',
    
    // Optional - Icon (emoji or image URL)
    icon: '📊',
    
    // Optional - Group header
    group: 'Navigation',
    
    // Optional - Search keywords
    keywords: ['analytics', 'metrics', 'home', 'main'],
    
    // Optional - Keyboard shortcut
    shortcut: 'G D',
    
    // Optional - Action handler
    onRun: ({ action, storeProps, storeMethods }) => {
      console.log('Executing:', action.title);
      goto('/dashboard');
    },
    
    // Optional - Conditional execution
    canActionRun: ({ action, storeProps, storeMethods }) => {
      return storeProps.isAuthenticated;
    }
  }
]);

Type Definition

TypeScript Types
interface action {
  title: string;
  actionId?: string | number;
  subTitle?: string;
  description?: string;
  icon?: string;
  group?: string;
  keywords?: string[];
  shortcut?: string;
  onRun?: (params: onRunParams) => void;
  canActionRun?: (params: onRunParams) => boolean;
}

interface onRunParams {
  action: action;
  storeProps: storeParams;
  storeMethods: StoreMethods;
}

Property Reference

PropertyTypeRequiredDescription
titlestring✓Main display text for the action
actionIdstring | numberUnique identifier (auto-generated if omitted)
subTitlestringSecondary text below title
descriptionstringAdditional descriptive text
iconstringEmoji or image URL
groupstringGroup header name
keywordsstring[]Additional search terms
shortcutstringKeyboard shortcut (tinykeys format)
onRunfunctionCallback when action executes
canActionRunfunctionConditional check before execution

Callback Parameters

Both onRun and canActionRun receive:

ParameterTypeDescription
actionactionThe current action being executed
storePropsstoreParamsCurrent palette state (commands, results, etc.)
storeMethodsStoreMethodsMethods to control the palette