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

Dark Mode fix #59

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 1 addition & 17 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ 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,
Expand Down Expand Up @@ -38,18 +37,3 @@ const setSession: Handle = async ({ event, resolve }) => {
},
})
}

const insertTheme: Handle = async ({ event, resolve }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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