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

Add themetoggle #7

Merged
merged 16 commits into from
Apr 14, 2023
5 changes: 5 additions & 0 deletions src/components/Footer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SPONSOR_URL,
} from "../scripts/constants";
import { Icon } from "astro-icon";
import ThemeToggle from "./ThemeToggle.svelte";
---

<footer>
Expand All @@ -31,6 +32,10 @@ import { Icon } from "astro-icon";
<a href="/support" title="Support Vencord's Development">
<Icon name="fa-regular:heart" />
</a>
<ThemeToggle client:load>
<Icon name="fa-regular:sun" slot="light" title="Switch to Light theme" />
<Icon name="fa-regular:moon" slot="dark" title="Switch to Dark theme" />
</ThemeToggle>
</div>
</div>
</footer>
Expand Down
8 changes: 1 addition & 7 deletions src/components/NavBar.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
const navLinks = {
"download": ["Download", "accentPurple"],
"plugins": ["Plugins", "accentBlue"],
"faq": ["FAQ", "accentAqua"],
"docs": ["Docs", "accentGreen"],
Expand All @@ -17,13 +18,6 @@ const page = Astro.url.pathname.split("/")[1];
<div id="oneko" aria-hidden="true"></div>
</a>
</div>
<div id="btnsleft" class="p-label-m">
<ul>
<li>
<a href="/download" style="--accent:var(--accentPurple)" class:list={{ active: page === "download" }}>Download</a>
</li>
</ul>
</div>
<div id="btns" class="p-label-m">
<ul id="right">
{Object.entries(navLinks).map(([link, [display, color]]) =>
Expand Down
63 changes: 63 additions & 0 deletions src/components/ThemeToggle.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<script lang="ts">
import { writable } from "svelte/store";

import { IS_SERVER } from "../scripts/constants";

const options = ["Light", "Dark"] as const;

const initialValue = IS_SERVER
? "Dark"
: (() => {
const stored = localStorage.theme;
if (stored && options.includes(stored)) return stored;

if (window.matchMedia("(prefers-color-scheme: light)").matches)
return "Light";
else return "Dark";
})();

const selected = writable(initialValue);
if (!IS_SERVER) {
selected.subscribe(v => {
localStorage.theme = v;
v = v.toLowerCase();
const prev = v === "light" ? "dark" : "light";
document.body.classList.remove(prev);
document.body.classList.add(v);
});
}

function themeSwitch() {
selected.update(x => (x = x === "Light" ? "Dark" : "Light"));
}
</script>

<!-- Probably not the best way to go about this tbh -->
<button on:click={themeSwitch}>
{#if $selected === "Light"}
<slot name="dark" />
{:else if $selected === "Dark"}
<slot name="light" />
{/if}
</button>

<style>
button {
all: unset;
cursor: pointer;
}
button:focus-visible {
/* Should stylize this to be less ugly,
along with other elements - Tyler */
outline: auto;
}
button slot {
display: inline-block;
width: 1.5em;
height: 1.5em;
color: var(--fg0);
}
button slot {
color: var(--accent);
}
</style>
Loading