Skip to content

Commit a7089b8

Browse files
committed
feat: allow user to force colorSchema in frontmatter
1 parent 2c38860 commit a7089b8

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

docs/custom/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ download: true
1818
highlighter: 'prism'
1919
# enable monaco editor, default to dev only
2020
monaco: 'dev'
21+
# force color schema for the slides (could be 'auto', 'light', or 'dark')
22+
colorSchema: 'auto'
2123
# information for your slides, can be a markdown string
2224
info: |
2325
## Slidev

packages/client/internals/NavControls.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { computed, defineProps, ref } from 'vue'
3-
import { isDark, toggleDark } from '../logic/dark'
3+
import { isDark, toggleDark, isColorSchemaAuto } from '../logic/dark'
44
import { hasNext, hasPrev, prev, next, total, isPresenter, currentPage, downloadPDF } from '../logic/nav'
55
import { toggleOverview, showEditor, showInfoDialog, fullscreen, breakpoints, activeElement } from '../state'
66
import { configs } from '../env'
@@ -46,7 +46,7 @@ const onMouseLeave = () => {
4646
<carbon:apps />
4747
</button>
4848

49-
<button class="icon-btn" title="Toggle dark mode" @click="toggleDark">
49+
<button v-if="isColorSchemaAuto" class="icon-btn" title="Toggle dark mode" @click="toggleDark">
5050
<carbon-moon v-if="isDark" />
5151
<carbon-sun v-else />
5252
</button>

packages/client/logic/dark.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
import { useDark, useToggle } from '@vueuse/core'
1+
import { isClient, useStorage, useToggle } from '@vueuse/core'
2+
import { computed, watch } from 'vue'
3+
import { configs } from '../env'
24

3-
export const isDark = useDark({
4-
valueDark: 'dark',
5-
valueLight: 'light',
5+
const _isDark = useStorage('slidev-color-schema', false)
6+
7+
export const isColorSchemaAuto = computed(() => configs.colorSchema === 'auto')
8+
export const isDark = computed<boolean>({
9+
get() {
10+
return isColorSchemaAuto.value
11+
? _isDark.value
12+
: configs.colorSchema === 'dark'
13+
},
14+
set(v) {
15+
if (isColorSchemaAuto.value)
16+
_isDark.value = v
17+
},
618
})
719
export const toggleDark = useToggle(isDark)
20+
21+
if (isClient) {
22+
watch(
23+
isDark,
24+
(v) => {
25+
const html = document.querySelector('html')!
26+
html.classList.toggle('dark', v)
27+
html.classList.toggle('light', !v)
28+
},
29+
{ immediate: true },
30+
)
31+
}

packages/parser/src/core.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,15 @@ export function parse(
143143
download: false,
144144
info: false,
145145
highlighter: 'prism',
146+
colorSchema: 'auto',
146147
}
147148
const config: SlidevConfig = Object.assign(
148149
defaultConfig,
149150
headmatter.config || {},
150151
objectPick(headmatter, Object.keys(defaultConfig)),
151152
)
153+
if (config.colorSchema !== 'dark' && config.colorSchema !== 'light')
154+
config.colorSchema = 'auto'
152155

153156
return {
154157
raw: markdown,

packages/types/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ export interface SlidevConfig {
4545
* @default prism
4646
*/
4747
highlighter: 'prism' | 'shiki'
48+
/**
49+
* @default 'auto'
50+
*/
51+
colorSchema: 'dark' | 'light' | 'all' | 'auto'
4852
}
4953

5054
export interface SlidevFeatureFlags {

0 commit comments

Comments
 (0)