Skip to content

Commit 9ced39b

Browse files
authored
fix: remove state in theme changer (#145)
1 parent 57234f1 commit 9ced39b

2 files changed

Lines changed: 2 additions & 38 deletions

File tree

src/lib/components/navbar.test.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { render, screen } from '@testing-library/svelte';
22
import { readable } from 'svelte/store';
3-
import { beforeEach, describe, expect, it, vi } from 'vitest';
3+
import { describe, expect, it, vi } from 'vitest';
44

55
import Navbar from './navbar.svelte';
66

@@ -20,29 +20,6 @@ vi.mock('$app/paths', () => ({
2020
vi.mock('$lib/features/awesome-privacy/components/search.svelte');
2121

2222
describe('Navbar component', () => {
23-
// mock localStorage
24-
beforeEach(() => {
25-
globalThis.localStorage = (function () {
26-
let store: Record<string, string> = {};
27-
28-
return {
29-
getItem: (k: string) => (k in store ? store[k] : null),
30-
setItem: (k: string, v: string) => {
31-
store[k] = String(v);
32-
},
33-
removeItem: (k: string) => {
34-
delete store[k];
35-
},
36-
clear: () => {
37-
store = {};
38-
},
39-
40-
length: 0,
41-
key: (index: number) => Object.keys(store)[index] ?? null
42-
} as Storage;
43-
})();
44-
});
45-
4623
describe('URLs', () => {
4724
it('Privacy News link has correct href', () => {
4825
render(Navbar);

src/lib/components/theme-change.svelte

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,11 @@
77
import { THEMES } from '$lib/configs';
88
import { cn } from '$lib/utils/cn';
99
10-
const DEFAULT = 'kanagawa';
11-
1210
type ThemeChangeProps = HTMLAttributes<HTMLDivElement> & {
1311
variant?: 'dropdown' | 'select';
1412
};
1513
1614
let { variant = 'dropdown', class: klass, ...props }: ThemeChangeProps = $props();
17-
18-
let defaultTheme = $state(
19-
typeof window !== 'undefined' ? (localStorage.getItem('theme') ?? DEFAULT) : DEFAULT
20-
);
21-
2215
onMount(() => {
2316
themeChange(false);
2417
});
@@ -52,18 +45,12 @@
5245
class="theme-controller btn btn-block w-full justify-start capitalize btn-ghost sm:btn-sm"
5346
aria-label={theme}
5447
value={theme}
55-
checked={theme === defaultTheme}
5648
/>
5749
</li>
5850
{/each}
5951
</ul>
6052
{:else}
61-
<select
62-
data-choose-theme
63-
class="select appearance-none"
64-
aria-label="Select Theme"
65-
bind:value={defaultTheme}
66-
>
53+
<select data-choose-theme class="select appearance-none" aria-label="Select Theme">
6754
{#each THEMES as theme (theme)}
6855
<option class="theme-controller" aria-label={theme} value={theme}>{theme}</option>
6956
{/each}

0 commit comments

Comments
 (0)