Skip to content

Commit cd774d6

Browse files
committed
Respect site.json for darkmode config
darkmode is set to be opt in for now until mature However, if user's device is on dark mode, bootstrap will default to dark mode until javascript loads, then switch it to white mode
1 parent bf22300 commit cd774d6

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

packages/core/src/Page/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export class Page {
167167
title,
168168
enableSearch: this.siteConfig.enableSearch,
169169
codeTheme: this.siteConfig.style.codeTheme,
170+
darkMode: this.siteConfig.darkMode,
170171
};
171172
}
172173

packages/core/src/Page/page.njk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4+
{% if darkMode %}
45
<script>
56
(function() {
7+
window.__MARKBIND_DARK_MODE__ = true;
68
const storageKey = 'markbind-theme';
79
const darkTheme = 'dark';
810
const lightTheme = 'light';
@@ -27,6 +29,7 @@
2729
window.__MARKBIND_THEME__ = resolvedTheme;
2830
}());
2931
</script>
32+
{% endif %}
3033
{%- if asset.headTop %}
3134
{%- for headContent in asset.headTop %}
3235
{{ headContent }}
@@ -83,9 +86,11 @@
8386
<script src="{{ asset.vue }}"></script>
8487
<script src="{{ asset.markBindJs }}"></script>
8588
<script src="{{ asset.pageVueRenderJs }}"></script>
89+
{% if darkMode %}
8690
<script>
8791
document.body.setAttribute('data-code-theme', window.__MARKBIND_THEME__ || '{{ codeTheme }}');
8892
</script>
93+
{% endif %}
8994
<script>
9095
{% if enableSearch %}MarkBind.setupWithSearch(){% else %}MarkBind.setup(){% endif %}
9196
</script>

packages/core/src/Site/SiteConfig.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export class SiteConfig {
6464

6565
plantumlCheck: boolean;
6666

67+
darkMode: boolean;
68+
6769
/**
6870
* @param siteConfigJson The raw json read from the site configuration file
6971
* @param cliBaseUrl As read from the --baseUrl option
@@ -103,6 +105,8 @@ export class SiteConfig {
103105
// TODO this should probably be in pluginsContext
104106
this.plantumlCheck = siteConfigJson.plantumlCheck !== undefined
105107
? siteConfigJson.plantumlCheck : true; // check PlantUML's prerequisite by default
108+
109+
this.darkMode = siteConfigJson.darkMode === true;
106110
}
107111

108112
/**

packages/vue-components/src/DarkModeToggle.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<template>
22
<button
3+
v-if="enabled"
34
type="button"
45
class="btn btn-sm btn-outline-secondary dark-mode-toggle"
56
:aria-label="buttonLabel"
@@ -50,6 +51,7 @@ function isStoredTheme(theme) {
5051
export default {
5152
data() {
5253
return {
54+
enabled: typeof window !== 'undefined' && window.__MARKBIND_DARK_MODE__ === true,
5355
resolvedTheme: LIGHT_THEME,
5456
themePreference: null,
5557
mediaQueryList: null,
@@ -63,6 +65,9 @@ export default {
6365
},
6466
},
6567
mounted() {
68+
if (!this.enabled) {
69+
return;
70+
}
6671
this.themePreference = this.getStoredThemePreference();
6772
this.applyTheme(this.themePreference);
6873

0 commit comments

Comments
 (0)