Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Persist theme and main links now use href #86

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import theme from './theme';

export { theme };
14 changes: 14 additions & 0 deletions src/lib/stores/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { browser } from '$app/environment';
import { writable } from 'svelte/store';

const defaultValue = 'light';
const initialValue = browser ? window.localStorage.getItem('theme') ?? defaultValue : defaultValue;

const theme = writable<string>(initialValue);
theme.subscribe((value) => {
if (browser) {
window.localStorage.setItem('theme', value === 'dark' ? 'dark' : 'light');
}
});

export default theme;
19 changes: 6 additions & 13 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import '../app.css';
import { goto } from '$app/navigation';

import { theme } from '$lib/stores';
import { page } from '$app/stores';
import { Button, Drawer, Swap, Icon, Layout, Portal, Row, Toggle, Col, Divider } from '../lib';
import { browser } from '$app/environment';
Expand All @@ -10,7 +9,6 @@
import { menu, close } from '../lib/icons';

let openMenu = false;
let darkTheme = false;

function handleOpenMenu() {
openMenu = !openMenu;
Expand All @@ -20,12 +18,15 @@
openMenu = false;
}

$: darkTheme = $theme === 'dark';
$: if (browser && darkTheme) {
const htmlElement = document.documentElement;
htmlElement.classList.add('dark');
theme.set('dark');
} else if (browser) {
const htmlElement = document.documentElement;
htmlElement.classList.remove('dark');
theme.set('light');
}

let path = $page.url.pathname;
Expand All @@ -44,14 +45,6 @@
}

$: scrollToTop($page.url.pathname);

function redirectToGithub() {
goto('https://github.com/N00nDay/stwui');
}

function redirectToDiscord() {
goto('https://discord.gg/YVgwp48Tcm');
}
</script>

<svelte:head>
Expand Down Expand Up @@ -112,7 +105,7 @@

<Button
ariaLabel="open discord"
on:click={redirectToDiscord}
href="https://discord.gg/YVgwp48Tcm"
shape="circle"
class="ml-2 bg-light-icon-background dark:bg-dark-icon-background text-light-icon dark:text-dark-icon"
>
Expand All @@ -132,7 +125,7 @@

<Button
ariaLabel="open github"
on:click={redirectToGithub}
href="https://github.com/N00nDay/stwui"
shape="circle"
class="ml-2 bg-light-icon-background dark:bg-dark-icon-background text-light-icon dark:text-dark-icon"
>
Expand Down