diff options
| author | Adam Soltys <[email protected]> | 2024-11-16 21:19:34 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-11-16 21:19:34 -0800 |
| commit | b3653c0a6d448d215c96511ae88c5f3bc741e52f (patch) | |
| tree | 89dbc4e3f01be23883fb542df18b275d3454db33 /src/routes | |
| parent | 1b6ac8df832ad6f4b469b2197c6b529f8d9d757f (diff) | |
| download | coinos-ui-b3653c0a6d448d215c96511ae88c5f3bc741e52f.tar.gz coinos-ui-b3653c0a6d448d215c96511ae88c5f3bc741e52f.zip | |
get system theme
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/+layout.server.ts | 1 | ||||
| -rw-r--r-- | src/routes/+layout.ts | 18 |
2 files changed, 19 insertions, 0 deletions
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", { |
