Skip to content

Commit

Permalink
feat(web): simplify theme handling
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Shatford <[email protected]>
  • Loading branch information
jordanshatford committed Aug 29, 2023
1 parent eecc00d commit d0dd3cc
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
ci:
name: CI (Build, Lint, Format, Check)
name: CI (Lint, Format, Check, Build)
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand Down
6 changes: 1 addition & 5 deletions apps/web/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AudioFormat, Theme } from '$lib/utils/types';
import { AudioFormat } from '$lib/utils/types';

export const env = {
serverAddress: import.meta.env.VITE_SERVER_ADDR as string
Expand All @@ -15,10 +15,6 @@ const config = {
format: AudioFormat.MP3
}
},
theme: {
key: 'theme',
default: Theme.DARK
},
github: 'https://github.com/jordanshatford/youtube-audio-downloader',
copyright: {
owner: 'Jordan Shatford',
Expand Down
23 changes: 9 additions & 14 deletions apps/web/src/lib/stores/theme.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { browser } from '$app/environment';
import { writable } from 'svelte/store';
import { Theme } from '$lib/utils/types';
import config from '$lib/config';

function createThemeStore() {
const THEME_KEY = config.theme.key;
const DEFAULT_THEME_VALUE = config.theme.default;
const THEME_KEY = 'theme';

const { subscribe, set, update } = writable(DEFAULT_THEME_VALUE);
function createThemeStore() {
const { subscribe, set, update } = writable<'light' | 'dark'>('light');

if (browser) {
const data = localStorage?.getItem(THEME_KEY) as Theme;
const data = localStorage?.getItem(THEME_KEY) as 'light' | 'dark';

if (data) {
set(data);
if (data === 'dark') {
document.querySelector('html')?.classList.add('dark');
}
}
}

Expand All @@ -23,22 +23,17 @@ function createThemeStore() {
}
});

function applyDark() {
document.querySelector('html')?.classList.add(Theme.DARK);
}

function toggle() {
update((state) => {
state = state === Theme.DARK ? Theme.LIGHT : Theme.DARK;
state = state === 'dark' ? 'light' : 'dark';
return state;
});
document.querySelector('html')?.classList.toggle(Theme.DARK);
document.querySelector('html')?.classList.toggle('dark');
}

return {
subscribe,
set,
applyDark,
toggle
};
}
Expand Down
5 changes: 0 additions & 5 deletions apps/web/src/lib/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ export enum Status {

export type Variant = 'default' | 'info' | 'success' | 'warning' | 'danger';

export enum Theme {
LIGHT = 'light',
DARK = 'dark'
}

export enum AudioFormat {
MP3 = 'mp3',
AAC = 'aac',
Expand Down
5 changes: 0 additions & 5 deletions apps/web/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@
import Loading from '$lib/components/Loading.svelte';
import { session } from '$lib/stores/session';
import { downloads } from '$lib/stores/downloads';
import { theme } from '$lib/stores/theme';
import { Theme } from '$lib/utils/types';
$: if ($session) {
downloads.setupStatusListener();
}
onMount(async () => {
if ($theme === Theme.DARK) {
theme.applyDark();
}
await session.setup();
});
</script>
Expand Down

1 comment on commit d0dd3cc

@vercel
Copy link

@vercel vercel bot commented on d0dd3cc Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.