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
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 Theme from "./Theme.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>
<Theme client:load>
ggtylerr marked this conversation as resolved.
Show resolved Hide resolved
<Icon name="fa-regular:sun" slot="light"/>
ggtylerr marked this conversation as resolved.
Show resolved Hide resolved
<Icon name="fa-regular:moon" slot="dark"/>
</Theme>
</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/Theme.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 && window.matchMedia('(prefers-color-scheme: light').matches)
return "Light";
else return "Dark";
})();

const selected = writable(initialValue);
let first = true;
// Note - Theme switcher does not work on dev mode
if (!IS_SERVER) {
selected.subscribe(v => {
localStorage.theme = v;
v = v.toLowerCase();
const prev = (v === "light") ? "dark" : "light";
if (first) {
document.body.classList.add(v);
first = false;
}
else document.body.classList.replace(prev, 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} title="Switch to Dark">
ggtylerr marked this conversation as resolved.
Show resolved Hide resolved
{#if $selected === "Light"}
<slot name="light"/>
{:else if $selected === "Dark"}
<slot name="dark"/>
{/if}
</button>

<style>
button {
all: unset;
ggtylerr marked this conversation as resolved.
Show resolved Hide resolved
cursor: pointer;
}
button slot {
display: inline-block;
width: 1.5em;
height: 1.5em;
color: var(--fg0);
}
button slot {
color: var(--accent);
}
</style>
Loading