Skip to content

Commit 0f056c1

Browse files
committed
Added theme store with reactive context
1 parent 5b92f54 commit 0f056c1

File tree

12 files changed

+191
-60
lines changed

12 files changed

+191
-60
lines changed

src/components/Counter.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
export let counter: number = 0;
33
</script>
44

5-
<div class="mt-8 py-16 shadow-md flex items-center justify-center text-6xl">
5+
<div
6+
class="mt-8 py-16 shadow-md flex items-center justify-center text-6xl dark:bg-dark-mode-gray dark:text-light-gray"
7+
>
68
{counter}
79
</div>

src/components/Hero.svelte

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
</script>
99

1010
<main class="pt-20 grid grid-cols-2 gap-12 pb-20">
11-
<div class="">
12-
<h1 class="text-5xl text-gray-600 font-light leading-tight">
11+
<div>
12+
<h1 class="text-5xl text-gray-600 dark:text-light-gray font-light leading-tight">
1313
Increase your productivity exponentially.
1414
</h1>
15-
<h3 class="text-2xl mt-6 text-gray-600">
15+
<h3 class="text-2xl mt-6 text-gray-600 dark:text-light-gray">
1616
Use <KeyboardShortcut
1717
on:onShortcutClick={openCommandPalette}
1818
shortcuts={['', 'K']}
@@ -21,9 +21,11 @@
2121
</h3>
2222
</div>
2323
<div>
24-
<h1 class="text-4xl text-gray-600 font-light leading-tight">Keyboard Shorcuts</h1>
24+
<h1 class="text-4xl text-gray-600 dark:text-light-gray font-light leading-tight">
25+
Keyboard Shorcuts
26+
</h1>
2527
<div class="mt-4">
26-
<h3 class="text-lg mt-6 text-gray-600">
28+
<h3 class="text-lg mt-6 text-gray-600 dark:text-light-gray">
2729
Press or hold <KeyboardShortcut
2830
shortcuts={['C', 'C']}
2931
theme="secondary"
@@ -34,20 +36,24 @@
3436
</div>
3537
</div>
3638
<div>
37-
<h1 class="text-4xl text-gray-600 font-light leading-tight">Conditional Actions</h1>
39+
<h1 class="text-4xl text-gray-600 dark:text-light-gray font-light leading-tight">
40+
Conditional Actions
41+
</h1>
3842
<div class="mt-4">
39-
<h3 class="text-lg mt-6 text-gray-600">
43+
<h3 class="text-lg mt-6 text-gray-600 dark:text-light-gray">
4044
This will only run when the counter is greater than 2.
4145
</h3>
4246
</div>
43-
<div class="mt-4 flex items-center flex-col justify-center bg-slate-50 shadow text-xl p-20">
47+
<div
48+
class="mt-4 flex items-center flex-col justify-center bg-slate-50 dark:bg-gray-700 shadow text-xl p-20"
49+
>
4450
{#if counter < 3}
4551
<div class:text-red-500={counter < 3}>
4652
Just need {3 - counter} more
4753
</div>
4854
{/if}
4955
{#if counter > 2}
50-
<div class="mt-4">
56+
<div class="mt-4 dark:text-light-gray">
5157
Press <KeyboardShortcut
5258
on:onShortcutClick={runConditionalAction}
5359
shortcuts={['F', 'B', 'I']}
@@ -58,26 +64,30 @@
5864
</div>
5965
</div>
6066
<div>
61-
<h1 class="text-4xl text-gray-600 font-light leading-tight">Advanced Conditional Actions</h1>
67+
<h1 class="text-4xl text-gray-600 dark:text-light-gray font-light leading-tight">
68+
Advanced Conditional Actions
69+
</h1>
6270
<div class="mt-4">
63-
<h3 class="text-lg mt-6 text-gray-600">
71+
<h3 class="text-lg mt-6 text-gray-600 dark:text-light-gray">
6472
You can run an action based on the current state of your command palette. Press <KeyboardShortcut
6573
shortcuts={['C', 'I', 'A']}
6674
theme="secondary"
6775
/> to run this action
6876
</h3>
6977
</div>
7078
<div class="mt-8">
71-
<div class="text-lg">
79+
<div class="text-lg dark:text-light-gray">
7280
You must run <KeyboardShortcut
7381
on:onShortcutClick={runConditionalAction}
7482
shortcuts={['F', 'B', 'I']}
7583
theme="secondary"
7684
/> atleast once to enable this action
7785
</div>
78-
<div class="text-base mt-4">
86+
<div class="text-base mt-4 dark:text-light-gray ">
7987
Note: You can update your
80-
<div class="inline text-xl font-bold p-1 rounded font-mono">paletteStore</div>
88+
<div class="inline text-xl font-bold p-1 dark:text-light-gray rounded font-mono">
89+
paletteStore
90+
</div>
8191
from anywhere!
8292
</div>
8393
</div>

src/components/KeyboardShortcut.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
});
1414
};
1515
16-
$: bgColor = theme === 'primary' ? 'bg-gray-800 text-white' : 'bg-gray-100 shadow text-black';
16+
$: bgColor =
17+
theme === 'primary'
18+
? 'bg-gray-800 dark:bg-dark-mode-gray text-white dark:text-light-gray'
19+
: 'bg-gray-100 shadow text-black dark:text-light-gray dark:bg-dark-mode-gray';
1720
</script>
1821

1922
<button

src/components/Navbar.svelte

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
<nav class="flex items-center justify-between shadow-sm py-6 px-8">
1+
<script lang="ts">
2+
import themeStore from '../store/themeStore';
3+
const LIGHT = 'light';
4+
const DARK = 'dark';
5+
6+
const handleThemeSwitch = () => {
7+
const theme = localStorage.getItem('theme') || 'light';
8+
const root = window.document.documentElement;
9+
const isDark = theme === DARK;
10+
root.classList.remove(theme);
11+
root.classList.add(isDark ? LIGHT : DARK);
12+
localStorage.setItem('theme', isDark ? LIGHT : DARK);
13+
themeStore.set(isDark ? LIGHT : DARK);
14+
};
15+
</script>
16+
17+
<nav class="flex items-center justify-between shadow-sm py-6 px-8 dark:bg-dark-mode-gray">
218
<a href="/" class="text-svelte-brand text-xl font-bold">Svelte Command Palette</a>
3-
<div class="flex gap-12">
19+
<div class="flex gap-12 dark:text-svelte-brand">
20+
<button on:click={() => handleThemeSwitch()} type="button">Switch Theme</button>
421
<a href="/docs">Docs</a>
522
<a target="_blank" rel="noreferrer" href="https://github.com/rohitpotato/svelte-command-palette"
623
>Github</a

src/lib/components/CommandPalette.svelte

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
import { onDestroy, onMount, afterUpdate } from 'svelte';
33
import { setContext as setThemeContext } from 'svelte';
4+
import { writable } from 'svelte/store';
45
import { paletteStore } from '../store/PaletteStore';
56
import Portal from './Portal.svelte';
67
import ResultPanel from './ResultPanel.svelte';
@@ -58,29 +59,9 @@
5859
5960
// set themes to context to pass down to deeply nested components
6061
61-
setThemeContext(THEME_CONTEXT, {
62-
inputClass,
63-
overlayClass,
64-
paletteWrapperInnerClass,
65-
resultsContainerClass,
66-
resultContainerClass,
67-
optionSelectedClass,
68-
titleClass,
69-
subtitleClass,
70-
descriptionClass,
71-
keyboardButtonClass,
72-
unstyled,
73-
inputStyle: toCssString(inputStyle),
74-
overlayStyle: toCssString(overlayStyle),
75-
paletteWrapperInnerStyle: toCssString(paletteWrapperInnerStyle),
76-
resultsContainerStyle: toCssString(resultsContainerStyle),
77-
resultContainerStyle: toCssString(resultContainerStyle),
78-
optionSelectedStyle: toCssString(optionSelectedStyle),
79-
titleStyle: toCssString(titleStyle),
80-
subtitleStyle: toCssString(subtitleStyle),
81-
descriptionStyle: toCssString(descriptionStyle),
82-
keyboardButtonStyle: toCssString(keyboardButtonStyle)
83-
});
62+
const themeStore = writable({});
63+
64+
setThemeContext(THEME_CONTEXT, themeStore);
8465
8566
const storeMethods = createStoreMethods();
8667
const actionMap = createActionMap(commands);
@@ -216,6 +197,30 @@
216197
if (!isPaletteVisible) {
217198
isWrapperClickHandlerSet = false;
218199
}
200+
201+
themeStore.set({
202+
inputClass,
203+
overlayClass,
204+
paletteWrapperInnerClass,
205+
resultsContainerClass,
206+
resultContainerClass,
207+
optionSelectedClass,
208+
titleClass,
209+
subtitleClass,
210+
descriptionClass,
211+
keyboardButtonClass,
212+
unstyled,
213+
inputStyle: toCssString(inputStyle),
214+
overlayStyle: toCssString(overlayStyle),
215+
paletteWrapperInnerStyle: toCssString(paletteWrapperInnerStyle),
216+
resultsContainerStyle: toCssString(resultsContainerStyle),
217+
resultContainerStyle: toCssString(resultContainerStyle),
218+
optionSelectedStyle: toCssString(optionSelectedStyle),
219+
titleStyle: toCssString(titleStyle),
220+
subtitleStyle: toCssString(subtitleStyle),
221+
descriptionStyle: toCssString(descriptionStyle),
222+
keyboardButtonStyle: toCssString(keyboardButtonStyle)
223+
});
219224
});
220225
221226
onDestroy(() => {

src/lib/components/KeyboardButton.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script lang="ts">
2+
import type { Writable } from 'svelte/store';
23
import { THEME_CONTEXT } from '$lib/constants';
34
import type { themeContext } from '$lib/types';
45
import { createEventDispatcher, getContext } from 'svelte';
@@ -10,8 +11,8 @@
1011
});
1112
};
1213
13-
const themeContext: themeContext = getContext(THEME_CONTEXT);
14-
const { unstyled, keyboardButtonClass, keyboardButtonStyle } = themeContext;
14+
const themeContext: Writable<themeContext> = getContext(THEME_CONTEXT);
15+
const { unstyled, keyboardButtonClass, keyboardButtonStyle } = $themeContext;
1516
</script>
1617

1718
<button

src/lib/components/Result.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import KeyboardButton from './KeyboardButton.svelte';
66
import { THEME_CONTEXT } from '../constants';
77
import type { action, themeContext } from '$lib/types';
8+
import type { Writable } from 'svelte/store';
89
910
export let action: action;
1011
let elRef: HTMLElement;
1112
let isActive: boolean;
1213
let formattedShortcut: Array<string[] | string> = [];
1314
14-
const themeContext: themeContext = getContext(THEME_CONTEXT);
15+
const themeContext: Writable<themeContext> = getContext(THEME_CONTEXT);
1516
const {
1617
resultContainerClass,
1718
unstyled,
@@ -24,7 +25,7 @@
2425
subtitleStyle,
2526
descriptionStyle,
2627
optionSelectedStyle
27-
} = themeContext;
28+
} = $themeContext;
2829
2930
afterUpdate(() => {
3031
if (action.actionId === $paletteStore.activeCommandId && elRef) {

src/lib/components/ResultPanel.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import { getNonEmptyArray } from '../utils';
66
import { THEME_CONTEXT } from '../constants';
77
import type { commands, storeParams, themeContext } from '$lib/types';
8+
import type { Writable } from 'svelte/store';
89
910
let actions: commands = [];
1011
const unsubscribe = paletteStore.subscribe((value: storeParams) => {
1112
actions = getNonEmptyArray(value.results);
1213
});
1314
14-
const themeContext: themeContext = getContext(THEME_CONTEXT);
15-
const { resultsContainerClass, unstyled, resultsContainerStyle } = themeContext;
15+
const themeContext = getContext(THEME_CONTEXT) as Writable<themeContext>;
16+
const { resultsContainerClass, unstyled, resultsContainerStyle } = $themeContext;
1617
1718
onDestroy(unsubscribe);
1819
</script>

src/routes/__layout.svelte

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,21 @@
33
import '../app.css';
44
</script>
55

6+
<svelte:head>
7+
<script>
8+
const getThemeFromLocalStorage = () => {
9+
const theme = localStorage.getItem('theme') || 'light';
10+
return theme;
11+
};
12+
13+
const applyTheme = () => {
14+
const theme = getThemeFromLocalStorage();
15+
const root = window.document.documentElement;
16+
root.classList.add(theme);
17+
};
18+
applyTheme();
19+
</script>
20+
</svelte:head>
21+
622
<Navbar />
723
<slot />

0 commit comments

Comments
 (0)