Skip to content

Commit

Permalink
fix: comment tags still show a light color when page is first loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
hakadao committed Jul 14, 2024
1 parent df1701c commit 2d47811
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/composables/useDark.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { usePreferredDark } from '@vueuse/core'

import { settings } from '~/logic'
import { setCookie } from '~/utils/main'
import { runWhenIdle } from '~/utils/lazyLoad'
import { delay, setCookie } from '~/utils/main'

Check failure on line 5 in src/composables/useDark.ts

View workflow job for this annotation

GitHub Actions / Test (lts/*, ubuntu-latest)

'delay' is defined but never used

Check failure on line 5 in src/composables/useDark.ts

View workflow job for this annotation

GitHub Actions / Test (lts/-1, ubuntu-latest)

'delay' is defined but never used
import { executeTimes } from '~/utils/timer'

export function useDark() {
Expand All @@ -26,6 +27,25 @@ export function useDark() {
{ immediate: true },
)

onMounted(() => {
// Because some shadow dom may not be loaded when the page has already loaded, we need to wait until the page is idle
runWhenIdle(() => {
if (isDark.value) {
setCookie('theme_style', 'dark', 365 * 10)
// TODO: find a better way implement this
themeChangeTimer = executeTimes(() => {
window.dispatchEvent(new CustomEvent('global.themeChange', { detail: 'dark' }))
}, 10, 500)
}
else {
setCookie('theme_style', 'light', 365 * 10)
themeChangeTimer = executeTimes(() => {
window.dispatchEvent(new CustomEvent('global.themeChange', { detail: 'light' }))
}, 10, 500)
}
})
})

/**
* Watch for changes in the 'settings.value.theme' variable and add the 'dark' class to the 'mainApp' element
* to prevent some Unocss dark-specific styles from failing to take effect
Expand All @@ -37,19 +57,16 @@ export function useDark() {
if (isDark.value) {
document.querySelector('#bewly')?.classList.add('dark')
document.documentElement.classList.add('dark')
// TODO: find a better way implement this
themeChangeTimer = executeTimes(() => {
setCookie('theme_style', 'dark', 365 * 10)
window.dispatchEvent(new CustomEvent('global.themeChange', { detail: 'dark' }))
}, 10, 200)

setCookie('theme_style', 'dark', 365 * 10)
window.dispatchEvent(new CustomEvent('global.themeChange', { detail: 'dark' }))
}
else {
document.querySelector('#bewly')?.classList.remove('dark')
document.documentElement.classList.remove('dark')
themeChangeTimer = executeTimes(() => {
setCookie('theme_style', 'light', 365 * 10)
window.dispatchEvent(new CustomEvent('global.themeChange', { detail: 'light' }))
}, 10, 200)

setCookie('theme_style', 'light', 365 * 10)
window.dispatchEvent(new CustomEvent('global.themeChange', { detail: 'light' }))
}
}

Expand Down

0 comments on commit 2d47811

Please sign in to comment.