Skip to content

Commit 811d333

Browse files
committed
Fix useColorMode hydration issues
1 parent 257cc5d commit 811d333

File tree

5 files changed

+45
-30
lines changed

5 files changed

+45
-30
lines changed

assets/css/tailwind.css

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
--colors-primary: 59 130 246;
1212
}
1313

14-
.dark {
14+
.dark,
15+
[data-appearance=dark] {
1516
--layer-0-bg: 18 18 18;
1617
--layer-0-bg-hover: 39 39 42;
1718
--layer-0-bg-active: 39 39 42;
@@ -80,12 +81,14 @@
8081
}
8182

8283
@media (prefers-color-scheme: dark) {
83-
.auto {
84+
.auto,
85+
[data-appearance=auto] {
8486
@apply dark;
8587
}
8688
}
8789

88-
.light {
90+
.light,
91+
[data-appearance=light] {
8992
--layer-0-bg: 244 244 245;
9093
--layer-0-bg-hover: 228 228 231;
9194
--layer-0-bg-active: 228 228 231;
@@ -154,12 +157,14 @@
154157
}
155158

156159
@media (prefers-color-scheme: light) {
157-
.auto {
160+
.auto,
161+
[data-appearance=auto] {
158162
@apply light;
159163
}
160164
}
161165

162-
.custom {
166+
.custom,
167+
[data-appearance=custom] {
163168
--layer-0-bg: 10 0 31;
164169
--layer-0-bg-hover: 21 0 44;
165170
--layer-0-bg-active: 32 2 58;

components/settings/Appearance.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { computed } from 'vue'
33
44
const { store } = useVuex()
5-
const { store: appearance } = useAppearance()
5+
const { storedValue: appearance } = useAppearance()
66
77
const experimental = computed(() => store.state.settings.experimental)
88
</script>

composables/useAppearance.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
1+
const valueToSystem = {
2+
auto: 'auto',
3+
custom: 'dark',
4+
dark: 'dark',
5+
light: 'light',
6+
} as const
7+
8+
export type AppearanceValue = keyof typeof valueToSystem
9+
110
const buildAppearance = () => {
2-
const { store, system: resolved } = useColorMode({
11+
const storedValue = useLocalStorage<AppearanceValue>('vueuse-color-scheme', 'auto', {
312
initOnMounted: true,
4-
modes: {
5-
auto: 'auto',
6-
october: 'dark october',
7-
custom: 'custom',
13+
})
14+
15+
watch(storedValue, () => {
16+
if (!Object.keys(valueToSystem).includes(storedValue.value)) {
17+
storedValue.value = 'auto'
18+
}
19+
})
20+
21+
useHead({
22+
htmlAttrs: {
23+
'data-appearance': storedValue,
824
},
925
})
1026

1127
const system = computed(() => {
12-
if (store.value === 'custom') {
13-
// Todo: Remove this extra step by making the editor use CSS variables all the time.
28+
// Todo: Remove this extra step by making the editor use CSS variables all the time.
29+
if (storedValue.value === 'custom') {
1430
return 'dark'
1531
}
1632

17-
if (store.value === 'october') {
33+
if (storedValue.value === 'dark') {
1834
return 'dark'
1935
}
2036

21-
return store.value
37+
if (storedValue.value === 'light') {
38+
return 'light'
39+
}
40+
41+
return 'auto'
2242
})
43+
2344
const isAuto = computed(() => system.value === 'auto')
2445
const isDark = computed(() => system.value === 'dark')
2546
const isLight = computed(() => system.value === 'light')
@@ -28,8 +49,7 @@ const buildAppearance = () => {
2849
isAuto,
2950
isDark,
3051
isLight,
31-
resolved,
32-
store,
52+
storedValue,
3353
system,
3454
}
3555
}
@@ -40,15 +60,5 @@ export type SystemAppearance = 'auto' | 'dark' | 'light'
4060
export const appearanceKey = Symbol('appearance') as InjectionKey<Appearance>
4161

4262
export const useAppearance = () => {
43-
const injected = inject(appearanceKey, undefined)
44-
45-
if (injected) {
46-
return injected
47-
}
48-
49-
const appearance = buildAppearance()
50-
51-
provide(appearanceKey, appearance)
52-
53-
return appearance
63+
return injectOrProvide(appearanceKey, buildAppearance)
5464
}

layouts/dashboard.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { bindGlobal } from '#root/src/common/keybindings'
1010
const { doc } = useDocs()
1111
const { isZen, toggleZen } = useZen()
1212
const { showMenu, showMeta, toggleMenu, toggleMeta } = useLayout()
13-
const { store: appearance, isAuto, isDark, isLight } = useAppearance()
13+
const { storedValue: appearance, isAuto, isDark, isLight } = useAppearance()
1414
const isPrimaryGutterShowing = computed(() => !isZen.value && showMenu.value)
1515
const isSecondaryGutterShowing = computed(() => !isZen.value && showMeta.value && !!doc.value)
1616

nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default defineNuxtConfig({
3030
app: {
3131
head: {
3232
htmlAttrs: {
33-
class: 'auto scroll-smooth scroll-pt-4 layer-0',
33+
class: 'scroll-smooth scroll-pt-4 layer-0',
3434
lang: 'en',
3535
},
3636
link: [

0 commit comments

Comments
 (0)