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:
parent
c21f6e2410
commit
57d03118b6
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
@ -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:
|
||||||
|
@ -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,15 +41,23 @@ 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', () => {
|
||||||
|
console.log('themeToggle change')
|
||||||
themeValue = themeValue === 'light' ? 'dark' : 'light'
|
themeValue = themeValue === 'light' ? 'dark' : 'light'
|
||||||
themeColor = getThemeColor(themeValue)
|
themeColor = getThemeColor(themeValue)
|
||||||
setPreference()
|
setPreference()
|
||||||
@ -68,7 +71,4 @@ function themeInit() {
|
|||||||
themeColor = getThemeColor(themeValue)
|
themeColor = getThemeColor(themeValue)
|
||||||
setPreference()
|
setPreference()
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
themeInit()
|
|
||||||
|
@ -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} />}
|
||||||
|
Loading…
Reference in New Issue
Block a user