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

fix(darktheme): switch to mode-watcher #223

Merged
merged 4 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.36.0",
"mode-watcher": "^0.3.0",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
"prettier-plugin-svelte": "^3.2.2",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

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

35 changes: 7 additions & 28 deletions src/lib/components/DarkModeToggle.svelte
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
<script lang="ts">
// functions
import { onMount } from 'svelte';

// variables
let darkMode: boolean;

onMount(() => {
const theme: string | null = localStorage.getItem('theme');

if (theme === 'dark') {
darkMode = true;
} else if (theme === 'light') {
darkMode = false;
} else {
darkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
}
});

function toggleDarkMode() {
darkMode = !darkMode;
localStorage.setItem('theme', darkMode ? 'dark' : 'light');

if (darkMode) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
}
import { toggleMode } from 'mode-watcher';
</script>

<button class="absolute top-0 right-0 md:px-4 md:py-4" on:click={toggleDarkMode}>
<svelte:head>
<!-- prevent Dark Reader from inverting the page -->
<meta name="darkreader-lock" />
</svelte:head>

<button class="absolute top-0 right-0 md:px-4 md:py-4" on:click={toggleMode}>
<div class="h-10 w-10 object-cover hover:scale-110 duration-300 ease-in-out">
<svg
class="hidden dark:block fill-hearchco-primary dark:fill-hearchco-secondary stroke-hearchco-primary dark:stroke-hearchco-secondary"
Expand Down
17 changes: 2 additions & 15 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,12 @@
import '../app.css';

// components
import { ModeWatcher } from 'mode-watcher';
import DarkModeToggle from '$lib/components/DarkModeToggle.svelte';
import Footer from '$lib/components/Footer.svelte';
</script>

<svelte:head>
<!-- prevent Dark Reader from inverting the page -->
<meta name="darkreader-lock" />
<script type="text/javascript">
if (
localStorage.theme === 'dark' ||
(!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
</script>
</svelte:head>

<ModeWatcher />
<DarkModeToggle />
<slot />
<Footer />
Loading