Skip to content

Commit

Permalink
fix: Persist theme and main links now use href (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominic-schmid committed Mar 6, 2023
1 parent aea17a0 commit 2f88b91
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
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

1 comment on commit 2f88b91

@vercel
Copy link

@vercel vercel bot commented on 2f88b91 Mar 6, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

stwui – ./

stwui.vercel.app
stwui-n00nday.vercel.app
stwui-git-main-n00nday.vercel.app

Please sign in to comment.