Created
September 12, 2023 00:14
-
-
Save dombarnes/91e37b3774dc3eb919da801949c02386 to your computer and use it in GitHub Desktop.
Dark Mode Toggle Vue 3 Component with System-based Option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div class="flex flex-col"> | |
<div class="flex items-center"> | |
<label class="switch"> | |
<input ref="darkModeToggle" type="checkbox" name="dark-mode" v-model="darkMode" @change="toggleMode" :disabled="useSystemSetting"> | |
<span class="slider round"></span> | |
</label> | |
<label>Dark Mode</label> | |
</div> | |
<label class="m-1"> | |
<input type="checkbox" v-model="useSystemSetting" :value="useSystemSetting" @change="systemSettingToggled"/> | |
Use System setting | |
</label> | |
</div> | |
</template> | |
<script setup lang="ts"> | |
import { onMounted, ref } from 'vue' | |
const useSystemSetting = ref<Boolean>(false) | |
const darkMode = ref<Boolean>(false) | |
const selectedTheme = ref<string>('light') | |
function systemSettingToggled() { | |
if (useSystemSetting.value) { | |
if (localStorage.getItem('theme')) { | |
localStorage.removeItem('theme') | |
} | |
const dark = window.matchMedia('(prefers-color-scheme: dark)').matches | |
setTheme(dark ? 'dark' : 'light', false) | |
// add an event listener to toggle the color scheme as changes are made, as long as the user hasn't already set their mode with the button | |
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', setSystemDarkMode) | |
} else { | |
window.matchMedia('(prefers-color-scheme: dark)').removeEventListener('change', setSystemDarkMode) | |
} | |
} | |
function setSystemDarkMode(event) { | |
if(!localStorage.getItem('theme')) { | |
const new_color_scheme = event.matches ? "dark" : "light" | |
// console.log(`scheme change to ${new_color_scheme}`) | |
setTheme(new_color_scheme) | |
} | |
} | |
function toggleMode() { | |
setTheme(darkMode.value ? 'dark' : 'light', true) | |
} | |
function setTheme(theme: string, save = false) : void { | |
// console.log(`setting dark mode ${theme}`) | |
selectedTheme.value = theme | |
if (save) localStorage.setItem('theme', selectedTheme.value) | |
const body = document.body | |
body.classList.add('dark') | |
document.documentElement.setAttribute('data-theme', selectedTheme.value) | |
darkMode.value = theme == 'dark' ? true : false | |
} | |
onMounted(() => { | |
// console.log(selectedTheme.value, useSystemSetting.value, darkMode.value) | |
if (localStorage.getItem('theme')) { | |
setTheme(localStorage.getItem('theme'), true) | |
return | |
} | |
if(window.matchMedia('(prefers-color-scheme)').media !== 'not all') { | |
// the browser is indicating that it supports prefers-color-scheme | |
if(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { | |
// the browser prefers dark mode | |
setTheme('dark') | |
} else { | |
// the browser doesn’t prefer dark mode | |
setTheme('light') | |
} | |
} else { | |
// browser does not support prefers-color-scheme | |
setTheme('light', false) | |
} | |
}) | |
</script> | |
<style scoped> | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This component offers two modes:
Use in combination with CSS variables on root and data-theme attribute to override.
You may also want to add this to your
<head>
to ensure the theme is set before the page renders.