Skip to content

Commit

Permalink
Use OS color scheme to init light/dark mode (#2611)
Browse files Browse the repository at this point in the history
Closes #2591
  • Loading branch information
pt2121 committed Aug 9, 2022
1 parent efed96e commit d3ff629
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Expand Up @@ -31,9 +31,10 @@ window.addEventListener('load', () => {
const darkModeSwitch = () => {
const localStorageKey = "dokka-dark-mode"
const storage = localStorage.getItem(localStorageKey)
const savedDarkMode = storage ? JSON.parse(storage) : false
const osDarkSchemePreferred = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
const darkModeEnabled = storage ? JSON.parse(storage) : osDarkSchemePreferred
const element = document.getElementById("theme-toggle-button")
initPlayground(savedDarkMode ? samplesDarkThemeName : samplesLightThemeName)
initPlayground(darkModeEnabled ? samplesDarkThemeName : samplesLightThemeName)

element.addEventListener('click', () => {
const enabledClasses = document.getElementsByTagName("html")[0].classList
Expand Down
16 changes: 12 additions & 4 deletions plugins/base/src/main/resources/dokka/templates/base.ftl
Expand Up @@ -12,10 +12,18 @@
<#-- This script doesn't need to be there but it is nice to have
since app in dark mode doesn't 'blink' (class is added before it is rendered) -->
<script>const storage = localStorage.getItem("dokka-dark-mode")
const savedDarkMode = storage ? JSON.parse(storage) : false
if(savedDarkMode === true){
document.getElementsByTagName("html")[0].classList.add("theme-dark")
}</script>
if (storage == null) {
const osDarkSchemePreferred = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
if (osDarkSchemePreferred === true) {
document.getElementsByTagName("html")[0].classList.add("theme-dark")
}
} else {
const savedDarkMode = JSON.parse(storage)
if(savedDarkMode === true) {
document.getElementsByTagName("html")[0].classList.add("theme-dark")
}
}
</script>
<#-- Resources (scripts, stylesheets) are handled by Dokka.
Use customStyleSheets and customAssets to change them. -->
<@resources/>
Expand Down

0 comments on commit d3ff629

Please sign in to comment.