feat: Implement dark mode switch

This commit is contained in:
Leni Aniva 2024-10-11 11:45:19 -07:00
parent f923b1ea68
commit 479690d2b2
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
6 changed files with 64 additions and 2 deletions

View File

@ -71,6 +71,10 @@
} }
} }
body {
transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out;
}
@layer base { @layer base {
* { * {
@apply border-border; @apply border-border;

View File

@ -0,0 +1,39 @@
<script lang="ts">
import { Moon, Sun } from 'lucide-svelte';
import { theme } from '$lib/theme'
function toggleTheme() {
theme.update((currentTheme) => {
const newTheme = currentTheme === 'tokiwa' ? 'star' : 'tokiwa'
document.documentElement.setAttribute('color-scheme', newTheme)
localStorage.setItem('color-scheme', newTheme)
if (newTheme === "tokiwa") {
document.documentElement.classList.remove('dark');
} else {
document.documentElement.classList.add('dark');
}
return newTheme
})
}
</script>
<button on:click={toggleTheme} aria-label="Toggle Theme">
{#if $theme === "tokiwa"}
<Sun />
{:else}
<Moon />
{/if}
</button>
<style>
button {
padding: 0;
font-weight: inherit;
background: none;
border: none;
box-shadow: none;
overflow: hidden;
}
</style>

13
src/lib/theme.ts Normal file
View File

@ -0,0 +1,13 @@
import { writable } from 'svelte/store'
import { browser } from "$app/environment"
type Theme = 'tokiwa' | 'star'
const userTheme = browser && localStorage.getItem('color-scheme')
export const theme = writable(userTheme ?? 'tokiwa')
// set the theme
export function setTheme(newTheme: Theme) {
theme.set(newTheme)
}

View File

@ -40,7 +40,7 @@
color: rgb(128,128,128); color: rgb(128,128,128);
pointer-events: none; pointer-events: none;
} }
:global(a) { :global(a), :global(button) {
color: theme('colors.eucalyptus.400'); color: theme('colors.eucalyptus.400');
&:hover, &:hover,
&:focus { &:focus {

View File

@ -2,6 +2,7 @@
import { routes } from "$lib/sitemap.ts" import { routes } from "$lib/sitemap.ts"
import { Separator } from "$lib/components/ui/separator/index.js"; import { Separator } from "$lib/components/ui/separator/index.js";
import { Rss } from "lucide-svelte"; import { Rss } from "lucide-svelte";
import ThemeSwitch from "$lib/components/ThemeSwitch.svelte"
let scrollPosition: number = .5; let scrollPosition: number = .5;
let scrollHeight: number = 1; let scrollHeight: number = 1;
let progress: number = 0; let progress: number = 0;
@ -58,6 +59,8 @@
<Separator class="bg-gray-500 dark:bg-gray-100" orientation="vertical" /> <Separator class="bg-gray-500 dark:bg-gray-100" orientation="vertical" />
{/if} {/if}
{/each} {/each}
<ThemeSwitch />
<Separator class="bg-gray-500 dark:bg-gray-100" orientation="vertical" />
<a class="icon" href="/rss.xml"><Rss /></a> <a class="icon" href="/rss.xml"><Rss /></a>
</div> </div>
</div> </div>
@ -65,7 +68,7 @@
</div> </div>
<main> <main>
<div id="bio" class="bg-background"> <div id="bio">
<div class="p-1"> <div class="p-1">
<div class="flex"> <div class="flex">
<div id="info flex-col"> <div id="info flex-col">
@ -130,6 +133,7 @@
main { main {
width: 100%; width: 100%;
@apply bg-background; @apply bg-background;
@apply dark:bg-gray-800;
box-shadow: box-shadow:
0px -5px 10px 10px rgba(200,200,200,0.4); 0px -5px 10px 10px rgba(200,200,200,0.4);
} }

View File

@ -2,6 +2,7 @@
import { page } from '$app/stores'; import { page } from '$app/stores';
import { routes } from "$lib/sitemap.ts" import { routes } from "$lib/sitemap.ts"
import { name } from '$content/metadata.json'; import { name } from '$content/metadata.json';
import ThemeSwitch from "$lib/components/ThemeSwitch.svelte"
import { Rss } from "lucide-svelte"; import { Rss } from "lucide-svelte";
function isCurrentLink(pathname, route) { function isCurrentLink(pathname, route) {
@ -19,6 +20,7 @@
class:disabled-link={item.disabled} class:disabled-link={item.disabled}
href={item.route}>{item.name}</a> href={item.route}>{item.name}</a>
{/each} {/each}
<div><ThemeSwitch /></div>
<a class="icon" href="/rss.xml"><Rss /></a> <a class="icon" href="/rss.xml"><Rss /></a>
</div> </div>