aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-11-16 21:19:34 -0800
committerAdam Soltys <[email protected]>2024-11-16 21:19:34 -0800
commitb3653c0a6d448d215c96511ae88c5f3bc741e52f (patch)
tree89dbc4e3f01be23883fb542df18b275d3454db33 /src
parent1b6ac8df832ad6f4b469b2197c6b529f8d9d757f (diff)
downloadcoinos-ui-b3653c0a6d448d215c96511ae88c5f3bc741e52f.tar.gz
coinos-ui-b3653c0a6d448d215c96511ae88c5f3bc741e52f.zip
get system theme
Diffstat (limited to 'src')
-rw-r--r--src/components/DarkToggle.svelte2
-rw-r--r--src/routes/+layout.server.ts1
-rw-r--r--src/routes/+layout.ts18
3 files changed, 20 insertions, 1 deletions
diff --git a/src/components/DarkToggle.svelte b/src/components/DarkToggle.svelte
index be1fe858..904df022 100644
--- a/src/components/DarkToggle.svelte
+++ b/src/components/DarkToggle.svelte
@@ -3,7 +3,7 @@
import { theme } from "$lib/store";
let dark = () => {
$theme = $theme === "dark" ? "light" : "dark";
- document.cookie = `theme=${$theme}; path=/; max-age=86400`;
+ document.cookie = `theme=${$theme}; path=/; max-age=21000000`;
};
</script>
diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts
index 7fa95931..584e836c 100644
--- a/src/routes/+layout.server.ts
+++ b/src/routes/+layout.server.ts
@@ -37,6 +37,7 @@ export const load = async ({ cookies, request, url }) => {
}
await loadTranslations(locale, pathname); // keep this just before the `return`
+
const theme = cookies.get("theme") || "light";
return {
diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts
index 0de57279..5f480c5a 100644
--- a/src/routes/+layout.ts
+++ b/src/routes/+layout.ts
@@ -1,5 +1,7 @@
+import { browser } from "$app/environment";
import { addTranslations, setLocale, setRoute } from "$lib/translations/index";
import { addIcon } from "iconify-icon";
+import cookies from "js-cookie";
/** @type {import('@sveltejs/kit').Load} */
export const load = async ({ data }) => {
@@ -10,6 +12,22 @@ export const load = async ({ data }) => {
await setRoute(route);
await setLocale(locale);
+
+ if (browser) {
+ let theme = cookies.get("theme");
+
+ if (!theme) {
+ theme = matchMedia("(prefers-color-scheme: dark)").matches
+ ? "dark"
+ : "light";
+
+ const expires = new Date();
+ expires.setSeconds(expires.getSeconds() + 21000000);
+
+ cookies.set("theme", theme, { path: "/", expires });
+ if (theme === "dark") location.reload();
+ }
+ }
};
addIcon("coinos:logo", {