Skip to content

Commit

Permalink
feat: save data-theme state in localstorage (#850)
Browse files Browse the repository at this point in the history
* feat: save data-theme state in localstorage

* fix: prefer the state in localstorage than the scheme preference

* do not save state when using default theme

* fix: determine the correct theme mode
  • Loading branch information
yin1999 authored Sep 10, 2023
1 parent fb931d8 commit 0e620fe
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions web/writing.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<strong>DDNS-GO</strong>
</a>
<button class="btn btn-info btn-sm" id="logsBtn" onclick="showHideLogs()">日志</button>
<span class="theme-button gg-dark-mode" onclick="toggleTheme()"></span>
<span class="theme-button gg-dark-mode" onclick="toggleTheme(true)"></span>
<span class="badge badge-secondary">{{.Version}}</span>
</div>
</div>
Expand Down Expand Up @@ -797,9 +797,13 @@ <h5 class="portlet__head">Webhook</h5>
</script>
<script>
window.addEventListener('load', () => {
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
const theme = localStorage.getItem('theme') ??
(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')

if (theme === 'dark') {
toggleTheme()
}

document.getElementById('webhookTestBtn').addEventListener('click', e => {
e.preventDefault();
const formData = new FormData();
Expand All @@ -823,12 +827,15 @@ <h5 class="portlet__head">Webhook</h5>
</script>

<script>
function toggleTheme() {
function toggleTheme(write = false) {
const docEle = document.documentElement
if (docEle.getAttribute('data-theme') === 'dark')
if (docEle.getAttribute('data-theme') === 'dark') {
docEle.removeAttribute('data-theme')
else
write && localStorage.setItem('theme', 'light')
} else {
docEle.setAttribute('data-theme', 'dark')
write && localStorage.setItem('theme', 'dark')
}
}

function showHideLogs() {
Expand Down

0 comments on commit 0e620fe

Please sign in to comment.