Skip to content
This repository was archived by the owner on Oct 1, 2023. It is now read-only.

Commit ddc651c

Browse files
committed
feat : Add defaultTheme to control default theme
1 parent 26b359f commit ddc651c

File tree

8 files changed

+141
-15
lines changed

8 files changed

+141
-15
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
3+
[README](README.md) | [CHANGELOG](CHANGELOG.md)
4+
5+
## 1.0.1
6+
7+
- Add `defaultTheme` to control default theme color

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
> add prefers-color-scheme for vuepress default theme
44
5+
[README](README.md) | [CHANGELOG](CHANGELOG.md)
6+
57
**This theme for Vuepress 1.x, Works only on supported operating systems and browsers**
68

79
[prefers-color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme)
@@ -36,6 +38,37 @@ module.exports = {
3638
}
3739
```
3840

41+
## Options
42+
43+
### defaultTheme
44+
- Type: `string`, `object`
45+
- Default: `undefined`
46+
- Required: `false`
47+
48+
By default, light or dark themes are displayed by [prefers-color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme), You can change it by `defaultTheme`
49+
50+
support `light`, `dark` or `{ theme: [begin hours, end hours] }`
51+
52+
``` js
53+
module.exports = {
54+
theme: 'default-prefers-color-scheme',
55+
themeConfig: {
56+
defaultTheme: 'dark',
57+
// or
58+
defaultTheme: { dark: [18, 6] },
59+
// or
60+
defaultTheme: { light: [6, 18], dark: [18, 6] },
61+
},
62+
// When using `light theme` or `dark theme`, you need to add a postcss plugins to your config.js
63+
postcss: {
64+
plugins: [
65+
require('css-prefers-color-scheme/postcss'),
66+
require('autoprefixer')
67+
]
68+
}
69+
}
70+
```
71+
3972
[Theme Config](https://v1.vuepress.vuejs.org/theme/default-theme-config.html)
4073

4174
## Styling

examples/.vuepress/config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ module.exports = {
66
evergreen: true,
77
theme: require.resolve('../../'),
88
themeConfig: {
9+
defaultTheme: { dark: [18, 6] },
910
sidebar: [
1011
'/started'
1112
]
13+
},
14+
postcss: {
15+
plugins: [
16+
require('css-prefers-color-scheme/postcss'),
17+
require('autoprefixer')
18+
]
1219
}
1320
}

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tagline: add prefers-color-scheme for vuepress default theme
55
actionText: Get Started →
66
actionLink: /started
77
features:
8-
- title: Inheritance on the default theme
8+
- title: Inheritance default theme
99
details: Seamlessly add prefers-color-scheme to the default theme
1010
- title: Enhanced display
1111
details: Works only on supported operating systems and browsers

examples/started.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ npm i vuepress-theme-default-prefers-color-scheme
88

99
## Usage
1010

11-
``` js
11+
``` js {3}
1212
// .vuepress -> config.js
1313
module.exports = {
1414
theme: 'default-prefers-color-scheme',
@@ -18,6 +18,39 @@ module.exports = {
1818
}
1919
```
2020

21+
## Options
22+
23+
### defaultTheme
24+
- Type: `string`, `object`
25+
- Default: `undefined`
26+
- Required: `false`
27+
28+
::: tip
29+
By default, light or dark themes are displayed by [prefers-color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme), You can change it by `defaultTheme`
30+
:::
31+
32+
support `light`, `dark` or `{ theme: [begin hours, end hours] }`
33+
34+
``` js {4,6,8}
35+
module.exports = {
36+
theme: 'default-prefers-color-scheme',
37+
themeConfig: {
38+
defaultTheme: 'dark',
39+
// or
40+
defaultTheme: { dark: [18, 6] },
41+
// or
42+
defaultTheme: { light: [6, 18], dark: [18, 6] },
43+
},
44+
// When using `light theme` or `dark theme`, you need to add a postcss plugins to your config.js
45+
postcss: {
46+
plugins: [
47+
require('css-prefers-color-scheme/postcss'),
48+
require('autoprefixer')
49+
]
50+
}
51+
}
52+
```
53+
2154
[Theme Config](https://v1.vuepress.vuejs.org/theme/default-theme-config.html)
2255

2356
## Styling

layouts/Layout.vue

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,51 @@ import ParentLayout from '@parent-theme/layouts/Layout.vue'
88
export default {
99
components: {
1010
ParentLayout
11+
},
12+
data() {
13+
return {
14+
colorScheme: {}
15+
}
16+
},
17+
computed: {
18+
defaultTheme() {
19+
const _defaultTheme = this.$themeConfig.defaultTheme
20+
if (typeof _defaultTheme === 'object') {
21+
const hours = new Date().getHours()
22+
let _key = false
23+
for (const key in _defaultTheme) {
24+
const value = _defaultTheme[key]
25+
if (value[0] <= value[1]) {
26+
if (value[0] <= hours && hours < value[1]) {
27+
_key = key
28+
break
29+
}
30+
} else {
31+
if ((value[0] <= hours && hours < 24) || (0 <= hours && hours < value[1])) {
32+
_key = key
33+
break
34+
}
35+
}
36+
}
37+
return _key
38+
} else {
39+
return _defaultTheme || false
40+
}
41+
}
42+
},
43+
beforeMount() {
44+
if (this.defaultTheme) {
45+
const prefersColorScheme = require('css-prefers-color-scheme').default
46+
this.colorScheme = prefersColorScheme(this.defaultTheme)
47+
}
48+
},
49+
mounted() {
50+
// Prevent styles in index.styl not work
51+
if (this.defaultTheme) {
52+
window.onload = function() {
53+
this.colorScheme.scheme = this.defaultTheme
54+
}.bind(this)
55+
}
1156
}
1257
}
1358
</script>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuepress-theme-default-prefers-color-scheme",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"main": "index.js",
55
"description": "add prefers-color-scheme for vuepress default theme",
66
"author": "tolking <qw13131wang@gmail.com>",
@@ -26,6 +26,7 @@
2626
"dark-theme"
2727
],
2828
"devDependencies": {
29+
"css-prefers-color-scheme": "^3.1.1",
2930
"vuepress": "^1.0.0"
3031
}
3132
}

styles/index.styl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,26 +194,26 @@ div[class*="language-"].line-numbers-mode .line-numbers-wrapper
194194
border-color var(--searchBorderColor)
195195
.ds-suggestion
196196
border-bottom-color var(--borderColor)
197-
.algolia-docsearch-suggestion--highlight
198-
color #2c815b
197+
// .algolia-docsearch-suggestion--highlight
198+
// color #2c815b
199199
.algolia-docsearch-suggestion
200200
border-color var(--borderColor)
201201
.algolia-docsearch-suggestion--category-header
202202
background var(--accentColor)
203-
color #fff
204-
.algolia-docsearch-suggestion--highlight
205-
background rgba(255, 255, 255, 0.6)
203+
// color #fff
204+
// .algolia-docsearch-suggestion--highlight
205+
// background rgba(255, 255, 255, 0.6)
206206
.algolia-docsearch-suggestion--title
207207
color var(--textColor)
208208
.algolia-docsearch-suggestion--subcategory-column
209209
border-color var(--borderColor)
210-
background #f1f3f5
211-
.algolia-docsearch-suggestion--subcategory-column-text
212-
color #555
210+
// background #f1f3f5
211+
// .algolia-docsearch-suggestion--subcategory-column-text
212+
// color #555
213213
.algolia-docsearch-footer
214214
border-color var(--borderColor)
215215
.ds-cursor .algolia-docsearch-suggestion--content
216-
background-color #e7edf3 !important
216+
// background-color #e7edf3 !important
217217
color var(--textColor)
218218

219219
.dropdown-wrapper
@@ -235,8 +235,8 @@ div[class*="language-"].line-numbers-mode .line-numbers-wrapper
235235

236236
.dropdown-wrapper .nav-dropdown
237237
background-color var(--bgColor)
238-
border-color #ddd
239-
border-bottom-color #ccc
238+
// border-color #ddd
239+
// border-bottom-color #ccc
240240

241241

242242
// home
@@ -245,7 +245,7 @@ div[class*="language-"].line-numbers-mode .line-numbers-wrapper
245245
.description
246246
color var(--lighten40TextColor)
247247
.action-button
248-
color #fff
248+
// color #fff
249249
background-color var(--accentColor)
250250
border-bottom-color var(--darken10AccentColor)
251251
&:hover

0 commit comments

Comments
 (0)