1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-22 01:46:51 +01:00

theme toggle fixes

This commit is contained in:
Matthias Kretschmann 2024-03-12 22:44:24 +00:00
parent c21f6e2410
commit 57d03118b6
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 34 additions and 26 deletions

View File

@ -90,6 +90,12 @@ jobs:
- run: npm ci - run: npm ci
- run: npx playwright install --with-deps - run: npx playwright install --with-deps
- run: npm run test:e2e - run: npm run test:e2e
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report-${{ matrix.os }}-${{ matrix.node }}
path: playwright-report/
retention-days: 30
build: build:
strategy: strategy:

View File

@ -1,5 +1,4 @@
const htmlEl = document.documentElement const htmlEl = document.documentElement
const themeToggle = document.querySelector('#toggle')
const currentTheme = localStorage.getItem('@kremalicious/theme') const currentTheme = localStorage.getItem('@kremalicious/theme')
function getPreferTheme() { function getPreferTheme() {
@ -17,11 +16,6 @@ function getThemeColor(theme) {
let themeValue = getPreferTheme() let themeValue = getPreferTheme()
let themeColor = getThemeColor(themeValue) let themeColor = getThemeColor(themeValue)
function setPreference() {
localStorage.setItem('@kremalicious/theme', themeValue)
reflectPreference()
}
function reflectPreference() { function reflectPreference() {
const sun = document.querySelector('#sun') const sun = document.querySelector('#sun')
const moon = document.querySelector('#moon') const moon = document.querySelector('#moon')
@ -35,6 +29,7 @@ function reflectPreference() {
metaThemeColor?.setAttribute('content', themeColor) metaThemeColor?.setAttribute('content', themeColor)
metaThemeColorMs?.setAttribute('content', themeColor) metaThemeColorMs?.setAttribute('content', themeColor)
const themeToggle = document.querySelector('#theme-toggle')
themeToggle?.setAttribute('checked', `${themeValue === 'dark'}`) themeToggle?.setAttribute('checked', `${themeValue === 'dark'}`)
if (themeValue === 'dark') { if (themeValue === 'dark') {
@ -46,29 +41,34 @@ function reflectPreference() {
} }
} }
function themeInit() { function setPreference() {
// set early so no page flashes / CSS is made aware localStorage.setItem('@kremalicious/theme', themeValue)
reflectPreference()
}
// set early so no page flashes / CSS is made aware
reflectPreference()
window.onload = () => {
// set on load again so screen readers
// can get the latest value on the button
reflectPreference() reflectPreference()
window.onload = () => { const themeToggle = document.querySelector('#theme-toggle')
// set on load so screen readers can get the latest value on the button
reflectPreference()
themeToggle?.addEventListener('change', () => { themeToggle?.addEventListener('change', () => {
themeValue = themeValue === 'light' ? 'dark' : 'light' console.log('themeToggle change')
themeValue = themeValue === 'light' ? 'dark' : 'light'
themeColor = getThemeColor(themeValue)
setPreference()
})
// sync with system changes
window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', ({ matches: isDark }) => {
themeValue = isDark ? 'dark' : 'light'
themeColor = getThemeColor(themeValue) themeColor = getThemeColor(themeValue)
setPreference() setPreference()
}) })
// sync with system changes
window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', ({ matches: isDark }) => {
themeValue = isDark ? 'dark' : 'light'
themeColor = getThemeColor(themeValue)
setPreference()
})
}
} }
themeInit()

View File

@ -119,7 +119,9 @@ const faviconSvg = await getImage({ src: faviconSvgSrc, format: 'svg' })
) )
} }
<!-- ./theme.cjs is symlinked to public/theme.js before build --> {
/* src/components/ThemeSwitch/theme.cjs is symlinked to public/theme.js before build */
}
<script is:inline src="/theme.js"></script> <script is:inline src="/theme.js"></script>
{style && <link rel="stylesheet" href={style} />} {style && <link rel="stylesheet" href={style} />}