Skip to content

Commit a08574a

Browse files
committed
feat: disable transitions on toggling color modes
1 parent 93145d8 commit a08574a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

packages/client/logic/dark.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@ export const isDark = computed<boolean>({
2626
export const toggleDark = useToggle(isDark)
2727

2828
if (isClient) {
29+
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}'
30+
2931
watch(
3032
isDark,
3133
(v) => {
34+
const style = window!.document.createElement('style')
35+
style.appendChild(document.createTextNode(CSS_DISABLE_TRANS))
36+
window!.document.head.appendChild(style)
37+
3238
const html = document.querySelector('html')!
3339
html.classList.toggle('dark', v)
3440
html.classList.toggle('light', !v)
41+
42+
// Calling getComputedStyle forces the browser to redraw
43+
// @ts-expect-error unused variable
44+
const _ = window!.getComputedStyle(style!).opacity
45+
document.head.removeChild(style!)
3546
},
3647
{ immediate: true },
3748
)

0 commit comments

Comments
 (0)