Skip to content

Commit

Permalink
fix: improve dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jrson83 committed Jul 15, 2024
1 parent 546fc48 commit 91244bc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
6 changes: 5 additions & 1 deletion src/_includes/layouts/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default (
{/* deno-fmt-ignore */}
{/* deno-lint-ignore ban-ts-comment */}
{/* @ts-ignore */}
<script type="module" nonce="CSP_NONCE" src={urlFilter!(`/scripts/main.js`)} inline />
<script nonce="CSP_NONCE" src={urlFilter!(`/scripts/critical.js`)} inline />
<head>
<meta charSet='utf-8' />
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
Expand Down Expand Up @@ -163,6 +163,10 @@ export default (
{children}
</main>
<comp.layout.footer />
{/* deno-fmt-ignore */}
{/* deno-lint-ignore ban-ts-comment */}
{/* @ts-ignore */}
<script nonce="CSP_NONCE" src={urlFilter!(`/scripts/main.js`)} inline />
{importJs && (
<script
type='module'
Expand Down
10 changes: 10 additions & 0 deletions src/scripts/critical.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'
const e = document.documentElement
const currentMode =
globalThis.matchMedia('(prefers-color-scheme: dark)').matches
const storedTheme = ('theme' in localStorage)
? localStorage.getItem('theme')
: currentMode
? 'dark'
: 'light'
if (storedTheme !== 'light') e.setAttribute('data-theme', 'dark')
45 changes: 16 additions & 29 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
'use strict'

const root = document.documentElement
const mediaQuery = '(prefers-color-scheme: dark)'
const mediaMatch = globalThis.matchMedia
const currentMode = mediaMatch(mediaQuery).matches

const storeTheme = (targetTheme) => {
if ('boolean' === typeof targetTheme) {
targetTheme = targetTheme ? 'dark' : 'light'
}
root.setAttribute('data-theme', targetTheme)
localStorage.setItem('theme', targetTheme)
}

const storedTheme = ('theme' in localStorage)
? localStorage.getItem('theme')
: currentMode

storedTheme && storeTheme(storedTheme)

document.getElementById('theme-toggle').addEventListener('click', () => {
{/* deno-fmt-ignore */}
{/* deno-lint-ignore ban-ts-comment */}
{/* @ts-ignore */}
const currentTheme =
getComputedStyle(root).getPropertyValue('color-scheme') == 'light'
storeTheme(!!currentTheme)
const nextTheme = (getComputedStyle(e).getPropertyValue('color-scheme') ===
'light')
? 'dark'
: 'light'
localStorage.setItem('theme', nextTheme)
e.setAttribute('data-theme', nextTheme)
})

mediaMatch(mediaQuery).addEventListener('change', (event) => {
storeTheme(event.matches)
})
globalThis.matchMedia('(prefers-color-scheme: dark)').addEventListener(
'change',
(event) => {
localStorage.removeItem('theme')
e.setAttribute(
'data-theme',
event.matches ? 'dark' : 'light',
)
},
)

0 comments on commit 91244bc

Please sign in to comment.