Skip to content

Commit 2314e43

Browse files
committed
Added store methods API
Updated Readme Updated Docs
1 parent 3c5e6a2 commit 2314e43

File tree

5 files changed

+52
-20
lines changed

5 files changed

+52
-20
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ Install svelte-command-palette with npm
3131

3232
```javascript
3333
<script>
34-
import CommandPalette, { defineActions } from 'svelte-command-palette'
34+
import CommandPalette, { defineActions, createStoreMethods } from 'svelte-command-palette'
3535

3636
// define actions using the defineActions API
3737

38+
const paletteMethods = createStoreMethods();
39+
3840
const actions = defineActions([
3941
{
4042
id: 1,
@@ -50,8 +52,11 @@ Install svelte-command-palette with npm
5052
5153
// render your command palette at the root of your application, say _layout.svelte
5254
55+
<button on:click={() => paletteMethods.openPalette()}>Open Command Palette</button>
56+
5357
<CommandPalette {actions}>
5458
59+
5560
```
5661
5762
## API
@@ -97,3 +102,28 @@ The `<CommandPalette />` component accepts the following optional properties for
97102
keywords?: Array<string>;
98103
shortcut?: string;
99104
```
105+
106+
## Store Methods
107+
108+
Get palette methods from `createStoreMethods`.
109+
110+
```
111+
import { createStoreMethods } from 'svelte-command-palette`
112+
113+
const methods = createStoreMethods();
114+
115+
method.openPalette();
116+
```
117+
118+
### API
119+
120+
```
121+
togglePalette: () => void;
122+
getAllCalledActions: () => ActionId[];
123+
storeCalledAction: (id: ActionId) => void;
124+
revertLastAction: (id: ActionId) => void;
125+
resetActionLog: () => void;
126+
openPalette: () => void;
127+
closePalette: () => void;
128+
resetPaletteStore: () => void;
129+
```

snippets/command-palette.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<script>
2-
import CommandPalette, { defineActions } from 'svelte-command-palette';
2+
import CommandPalette, { defineActions, createStoreMethods } from 'svelte-command-palette';
33
{/* Render command palette at the root of your application */}
4+
5+
// define actions using the defineActions API
6+
7+
const paletteMethods = createStoreMethods();
8+
9+
<button on:click={() => paletteMethods.openPalette()}>Open Command Palette</button>
10+
411
<CommandPalette
512

613
actions={defineActions([

src/lib/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Portal from './components/Portal.svelte';
22
import CommandPalette from './components/CommandPalette.svelte';
33
import { defineActions } from './utils';
44
import { paletteStore } from './store/PaletteStore';
5+
import createStoreMethods from './utils/createStoreMethods';
56

67
export default CommandPalette;
7-
export { Portal, defineActions, paletteStore };
8+
export { Portal, defineActions, paletteStore, createStoreMethods };

src/routes/code/command-palette.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<pre class="shiki" style="background-color: #22272e"><code><span class="line"><span style="color: #ADBAC7">&lt;</span><span style="color: #8DDB8C">script</span><span style="color: #ADBAC7">&gt;</span></span>
2-
<span class="line"><span style="color: #ADBAC7"> </span><span style="color: #F47067">import</span><span style="color: #ADBAC7"> CommandPalette, { defineActions } </span><span style="color: #F47067">from</span><span style="color: #ADBAC7"> </span><span style="color: #96D0FF">&#39;svelte-command-palette&#39;</span><span style="color: #ADBAC7">;</span></span>
2+
<span class="line"><span style="color: #ADBAC7"> </span><span style="color: #F47067">import</span><span style="color: #ADBAC7"> CommandPalette, { defineActions, createStoreMethods } </span><span style="color: #F47067">from</span><span style="color: #ADBAC7"> </span><span style="color: #96D0FF">&#39;svelte-command-palette&#39;</span><span style="color: #ADBAC7">;</span></span>
33
<span class="line"><span style="color: #ADBAC7"> {</span><span style="color: #768390">/* Render command palette at the root of your application */</span><span style="color: #ADBAC7">}</span></span>
4+
<span class="line"></span>
5+
<span class="line"><span style="color: #ADBAC7"> </span><span style="color: #768390">// define actions using the defineActions API</span></span>
6+
<span class="line"></span>
7+
<span class="line"><span style="color: #ADBAC7"> </span><span style="color: #F47067">const</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">paletteMethods</span><span style="color: #ADBAC7"> </span><span style="color: #F47067">=</span><span style="color: #ADBAC7"> </span><span style="color: #DCBDFB">createStoreMethods</span><span style="color: #ADBAC7">();</span></span>
8+
<span class="line"></span>
9+
<span class="line"><span style="color: #ADBAC7"> &lt;</span><span style="color: #8DDB8C">button</span><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">on</span><span style="color: #ADBAC7">:</span><span style="color: #6CB6FF">click</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">{() </span><span style="color: #F47067">=&gt;</span><span style="color: #ADBAC7"> paletteMethods.</span><span style="color: #DCBDFB">openPalette</span><span style="color: #ADBAC7">()}&gt;Open Command Palette&lt;/</span><span style="color: #8DDB8C">button</span><span style="color: #ADBAC7">&gt;</span></span>
10+
<span class="line"></span>
411
<span class="line"><span style="color: #ADBAC7"> &lt;</span><span style="color: #8DDB8C">CommandPalette</span></span>
512
<span class="line"></span>
613
<span class="line"><span style="color: #ADBAC7"> </span><span style="color: #6CB6FF">actions</span><span style="color: #F47067">=</span><span style="color: #ADBAC7">{</span><span style="color: #DCBDFB">defineActions</span><span style="color: #ADBAC7">([</span></span>

src/routes/index.svelte

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import { goto } from '$app/navigation';
33
import { afterUpdate, onMount } from 'svelte';
44
import Hero from '../components/Hero.svelte';
5-
import CommandPalette, { defineActions, paletteStore } from '../lib';
5+
import CommandPalette, { defineActions, paletteStore, createStoreMethods } from '../lib';
66
import themeStore from '../store/themeStore';
77
import switchTheme from '../utils/switchTheme';
88
99
type Themes = string | null;
1010
1111
let currentTheme: Themes;
12+
const paletteMethods = createStoreMethods();
1213
1314
onMount(() => {
1415
themeStore.set(localStorage.getItem('theme'));
@@ -19,13 +20,6 @@
1920
2021
let counter = 0;
2122
22-
paletteStore.update((value) => {
23-
return {
24-
...value,
25-
profileType: 'user'
26-
};
27-
});
28-
2923
let actions = defineActions([
3024
{
3125
title: 'Go to docs',
@@ -104,10 +98,7 @@
10498
]);
10599
106100
const openCommandPalette = () => {
107-
paletteStore.update((val) => {
108-
val.isVisible = true;
109-
return val;
110-
});
101+
paletteMethods.openPalette();
111102
};
112103
113104
const incrementCounter = () => {
@@ -118,10 +109,6 @@
118109
alert('Look ma! I can run.');
119110
};
120111
121-
afterUpdate(() => {
122-
console.log({ currentTheme });
123-
});
124-
125112
$: paletteTheme =
126113
currentTheme === 'light'
127114
? {

0 commit comments

Comments
 (0)