Skip to content

Commit

Permalink
feat: disable transitions on toggling color modes
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 18, 2024
1 parent 93145d8 commit a08574a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/client/logic/dark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,23 @@ export const isDark = computed<boolean>({
export const toggleDark = useToggle(isDark)

if (isClient) {
const CSS_DISABLE_TRANS = '*,*::before,*::after{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}'

watch(
isDark,
(v) => {
const style = window!.document.createElement('style')
style.appendChild(document.createTextNode(CSS_DISABLE_TRANS))
window!.document.head.appendChild(style)

const html = document.querySelector('html')!
html.classList.toggle('dark', v)
html.classList.toggle('light', !v)

// Calling getComputedStyle forces the browser to redraw
// @ts-expect-error unused variable
const _ = window!.getComputedStyle(style!).opacity
document.head.removeChild(style!)
},
{ immediate: true },
)
Expand Down

0 comments on commit a08574a

Please sign in to comment.