Skip to content

Commit

Permalink
Dark Mode fix
Browse files Browse the repository at this point in the history
  • Loading branch information
NDruce committed Apr 18, 2024
1 parent e31fe3e commit 4edc091
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
%sveltekit.head%
<script>
var ui_theme = localStorage.getItem("ui_theme");
if (ui_theme && ui_theme !== "default") {
document.querySelector("html").dataset.theme = ui_theme;
}
</script>
</head>
<body
data-sveltekit-preload-data="hover"
Expand Down
26 changes: 5 additions & 21 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import { PRIVATE_SUPABASE_SERVICE_ROLE } from "$env/static/private"
import { createSupabaseServerClient } from "@supabase/auth-helpers-sveltekit"
import { createClient } from "@supabase/supabase-js"
import type { Handle } from "@sveltejs/kit"
import { sequence } from "@sveltejs/kit/hooks"

const setSession: Handle = async ({ event, resolve }) => {
export const handle: Handle = async ({ event, resolve }) => {
event.locals.supabase = createSupabaseServerClient({
supabaseUrl: PUBLIC_SUPABASE_URL,
supabaseKey: PUBLIC_SUPABASE_ANON_KEY,
event,
})

event.locals.supabaseServiceRole = createClient(
PUBLIC_SUPABASE_URL,
PRIVATE_SUPABASE_SERVICE_ROLE,
{ auth: { persistSession: false } },
PUBLIC_SUPABASE_URL,
PRIVATE_SUPABASE_SERVICE_ROLE,
{ auth: { persistSession: false } },
)

/**
Expand All @@ -37,19 +36,4 @@ const setSession: Handle = async ({ event, resolve }) => {
return name === "content-range"
},
})
}

const insertTheme: Handle = async ({ event, resolve }) => {
const theme = event.cookies.get("theme")

return await resolve(event, {
transformPageChunk: ({ html }) => {
if (theme) {
html = html.replace('data-theme="default"', `data-theme="${theme}"`)
}
return html
},
})
}

export const handle = sequence(setSession, insertTheme)
}
6 changes: 5 additions & 1 deletion src/routes/(admin)/account/(menu)/theme.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
? "dark"
: "default"
document.querySelector("html")?.setAttribute("data-theme", theme)
document.cookie = `theme=${theme}; expires=Thu, 1 Dec 2050 12:00:00 UTC`
if (theme !== "default") {
localStorage.setItem("ui_theme", theme)
} else {
localStorage.removeItem("ui_theme")
}
}
</script>

Expand Down

0 comments on commit 4edc091

Please sign in to comment.