Skip to content

Commit 1e8bb48

Browse files
committedJun 9, 2024·
feat: support force-auto as an option for appearance
closes #3946
1 parent e24899a commit 1e8bb48

File tree

8 files changed

+50
-14
lines changed

8 files changed

+50
-14
lines changed
 

‎docs/reference/site-config.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,16 @@ When set to `true`, the production app will be built in [MPA Mode](../guide/mpa-
489489

490490
### appearance
491491

492-
- Type: `boolean | 'dark' | 'force-dark' | import('@vueuse/core').UseDarkOptions`
492+
- Type: `boolean | 'dark' | 'force-dark' | 'force-auto' | import('@vueuse/core').UseDarkOptions`
493493
- Default: `true`
494494

495495
Whether to enable dark mode (by adding the `.dark` class to the `<html>` element).
496496

497497
- If the option is set to `true`, the default theme will be determined by the user's preferred color scheme.
498498
- If the option is set to `dark`, the theme will be dark by default, unless the user manually toggles it.
499499
- If the option is set to `false`, users will not be able to toggle the theme.
500+
- If the option is set to `'force-dark'`, the theme will always be dark and users will not be able to toggle it.
501+
- If the option is set to `'force-auto'`, the theme will always be determined by the user's preferred color scheme and users will not be able to toggle it.
500502

501503
This option injects an inline script that restores users settings from local storage using the `vitepress-theme-appearance` key. This ensures the `.dark` class is applied before the page is rendered to avoid flickering.
502504

‎src/client/app/data.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ export function initData(route: Route): VitePressData {
8282
: appearance
8383
? useDark({
8484
storageKey: APPEARANCE_KEY,
85-
initialValue: () =>
86-
typeof appearance === 'string' ? appearance : 'auto',
85+
initialValue: () => (appearance === 'dark' ? 'dark' : 'auto'),
8786
...(typeof appearance === 'object' ? appearance : {})
8887
})
8988
: ref(false)

‎src/client/theme-default/components/VPNavBarAppearance.vue

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ const { site } = useData()
66
</script>
77

88
<template>
9-
<div v-if="site.appearance && site.appearance !== 'force-dark'" class="VPNavBarAppearance">
9+
<div
10+
v-if="
11+
site.appearance &&
12+
site.appearance !== 'force-dark' &&
13+
site.appearance !== 'force-auto'
14+
"
15+
class="VPNavBarAppearance"
16+
>
1017
<VPSwitchAppearance />
1118
</div>
1219
</template>

‎src/client/theme-default/components/VPNavBarExtra.vue

+17-3
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,30 @@ const hasExtraContent = computed(
1919
</script>
2020

2121
<template>
22-
<VPFlyout v-if="hasExtraContent" class="VPNavBarExtra" label="extra navigation">
23-
<div v-if="localeLinks.length && currentLang.label" class="group translations">
22+
<VPFlyout
23+
v-if="hasExtraContent"
24+
class="VPNavBarExtra"
25+
label="extra navigation"
26+
>
27+
<div
28+
v-if="localeLinks.length && currentLang.label"
29+
class="group translations"
30+
>
2431
<p class="trans-title">{{ currentLang.label }}</p>
2532

2633
<template v-for="locale in localeLinks" :key="locale.link">
2734
<VPMenuLink :item="locale" />
2835
</template>
2936
</div>
3037

31-
<div v-if="site.appearance && site.appearance !== 'force-dark'" class="group">
38+
<div
39+
v-if="
40+
site.appearance &&
41+
site.appearance !== 'force-dark' &&
42+
site.appearance !== 'force-auto'
43+
"
44+
class="group"
45+
>
3246
<div class="item appearance">
3347
<p class="label">
3448
{{ theme.darkModeSwitchLabel || 'Appearance' }}

‎src/client/theme-default/components/VPNavScreenAppearance.vue

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ const { site, theme } = useData()
66
</script>
77

88
<template>
9-
<div v-if="site.appearance && site.appearance !== 'force-dark'" class="VPNavScreenAppearance">
9+
<div
10+
v-if="
11+
site.appearance &&
12+
site.appearance !== 'force-dark' &&
13+
site.appearance !== 'force-auto'
14+
"
15+
class="VPNavScreenAppearance"
16+
>
1017
<p class="text">
1118
{{ theme.darkModeSwitchLabel || 'Appearance' }}
1219
</p>

‎src/node/config.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,17 @@ function resolveSiteDataHead(userConfig?: UserConfig): HeadConfig[] {
278278
{ id: 'check-dark-mode' },
279279
fallbackPreference === 'force-dark'
280280
? `document.documentElement.classList.add('dark')`
281-
: `;(() => {
282-
const preference = localStorage.getItem('${APPEARANCE_KEY}') || '${fallbackPreference}'
283-
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
284-
if (!preference || preference === 'auto' ? prefersDark : preference === 'dark')
285-
document.documentElement.classList.add('dark')
286-
})()`
281+
: fallbackPreference === 'force-auto'
282+
? `;(() => {
283+
if (window.matchMedia('(prefers-color-scheme: dark)').matches)
284+
document.documentElement.classList.add('dark')
285+
})()`
286+
: `;(() => {
287+
const preference = localStorage.getItem('${APPEARANCE_KEY}') || '${fallbackPreference}'
288+
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
289+
if (!preference || preference === 'auto' ? prefersDark : preference === 'dark')
290+
document.documentElement.classList.add('dark')
291+
})()`
287292
])
288293
}
289294

‎src/node/siteConfig.ts

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export interface UserConfig<ThemeConfig = any>
7777
| boolean
7878
| 'dark'
7979
| 'force-dark'
80+
| 'force-auto'
8081
| (Omit<UseDarkOptions, 'initialValue'> & { initialValue?: 'dark' })
8182
lastUpdated?: boolean
8283
contentProps?: Record<string, any>

‎types/shared.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export interface SiteData<ThemeConfig = any> {
120120
| boolean
121121
| 'dark'
122122
| 'force-dark'
123+
| 'force-auto'
123124
| (Omit<UseDarkOptions, 'initialValue'> & { initialValue?: 'dark' })
124125
themeConfig: ThemeConfig
125126
scrollOffset:

0 commit comments

Comments
 (0)
Please sign in to comment.