Skip to content

Commit 45a2796

Browse files
committed
move context setter and getter
1 parent 6df8904 commit 45a2796

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

src/contexts/ThemeContext.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getContext, setContext } from 'svelte';
2+
3+
type ThemeStore = {
4+
currentTheme: string;
5+
handleThemeChange: (theme: string) => void;
6+
};
7+
8+
const themeKey = Symbol('theme');
9+
10+
export const setThemeContext = (storeCreator: () => ThemeStore) =>
11+
setContext(themeKey, storeCreator());
12+
13+
export const getThemeContext = (): ThemeStore => getContext(themeKey);

src/lib/components/ThemeToggle.svelte

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
<script lang="ts">
2-
import { getContext } from 'svelte';
3-
42
import { Sun, Moon } from '@lucide/svelte';
53
import * as Dialog from '@/lib/components/ui/dialog/index.js';
64
import { buttonVariants } from '@/lib/components/ui/button/index.js';
75
import * as RadioGroup from '@/lib/components/ui/radio-group/index';
86
import { Label } from '@/lib/components/ui/label';
97
import { themes } from '@/lib/utils/themes';
10-
import type { ThemeContext } from '@/stores/ThemeStove.svelte';
8+
import { getThemeContext } from '@/contexts/ThemeContext';
119
1210
let isOpen = $state(false);
1311
14-
let { currentTheme, handleThemeChange } = $derived(getContext<ThemeContext>('theme'));
12+
let { currentTheme, handleThemeChange } = $derived(getThemeContext());
1513
1614
const handleThemeSelection = (value: string) => {
1715
handleThemeChange(value);

src/lib/components/showcase/ThemeCard.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import { Sun, Moon } from '@lucide/svelte';
66
import * as RadioGroup from '@/lib/components/ui/radio-group/index';
77
import Label from '@/lib/components/ui/label/label.svelte';
8-
import { getContext } from 'svelte';
9-
import type { ThemeContext } from '@/stores/ThemeStove.svelte';
8+
import { getThemeContext } from '@/contexts/ThemeContext';
109
11-
let { currentTheme, handleThemeChange } = $derived(getContext<ThemeContext>('theme'));
10+
let { currentTheme, handleThemeChange } = $derived(getThemeContext());
1211
</script>
1312

1413
<Card.Root>

src/routes/+layout.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<script lang="ts">
22
import '../app.css';
3-
import { setContext, type Snippet } from 'svelte';
3+
import { type Snippet } from 'svelte';
44
import { ClerkProvider } from 'svelte-clerk';
55
import Footer from '@/lib/components/Footer.svelte';
66
import { Toaster } from '@/lib/components/ui/sonner/index.js';
77
import { PUBLIC_CLERK_PUBLISHABLE_KEY } from '$env/static/public';
88
import { useTheme } from '@/stores/ThemeStove.svelte';
9+
import { setThemeContext } from '@/contexts/ThemeContext';
910
10-
setContext('theme', useTheme());
11+
setThemeContext(useTheme);
1112
1213
const { children }: { children: Snippet } = $props();
1314
</script>

0 commit comments

Comments
 (0)