Skip to content

Commit a0bab57

Browse files
author
reco_luan
committed
feat: add dark mode(part.1)
1. Complete mode switch methods 2. Adapt dark theme to theme elements.
1 parent 9ae5f15 commit a0bab57

19 files changed

+260
-129
lines changed

components/AlgoliaSearchBox.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default {
8383
margin 6px 0 0
8484
padding 4px
8585
text-align left
86-
box-shadow $boxShadow
86+
box-shadow var(--box-shadow)
8787
&:before
8888
display none
8989
[class*=ds-dataset-]

components/DropdownLink.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export default {
161161
// make the arrow always down at desktop
162162
border-left 4px solid transparent
163163
border-right 4px solid transparent
164-
border-top 6px solid $arrowBgColor
164+
border-top 6px solid $textColorSub
165165
border-bottom 0
166166
.nav-dropdown
167167
display none
@@ -173,9 +173,9 @@ export default {
173173
position absolute
174174
top 100%
175175
right 0
176-
background-color #fff
176+
background-color var(--background-color)
177177
padding 0.6rem 0
178-
box-shadow: $boxShadow;
178+
box-shadow: var(--box-shadow);
179179
text-align left
180180
border-radius $borderRadius
181181
white-space nowrap

components/Footer.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default {
3737
<style lang="stylus" scoped>
3838
.footer-wrapper {
3939
padding: 2.5rem;
40-
border-top: 1px solid $borderColor;
40+
border-top: 1px solid var(--border-color);
4141
text-align: center;
4242
color: lighten($textColor, 25%);
4343
> span {
@@ -58,4 +58,4 @@ export default {
5858
}
5959
}
6060
}
61-
</style>
61+
</style>

components/FriendLink.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export default {
129129
border-radius: $borderRadius
130130
background: #fff;
131131
font-size: 13px;
132-
box-shadow $boxShadow
132+
box-shadow var(--box-shadow)
133133
transition: all .5s
134134
.list-style
135135
position absolute
@@ -149,7 +149,7 @@ export default {
149149
position absolute
150150
display flex
151151
background #ffffff
152-
box-shadow $boxShadow
152+
box-shadow var(--box-shadow)
153153
border-radius $borderRadius
154154
box-sizing border-box
155155
padding .8rem 1rem

components/HomeBlog.vue

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ export default {
168168
margin: 1.8rem auto;
169169
font-size: 1.6rem;
170170
line-height: 1.3;
171-
color: lighten($textColor, 20%);
172171
}
173172
}
174173
.home-blog-wrapper {
@@ -192,12 +191,12 @@ export default {
192191
margin-left 15px;
193192
flex 0 0 300px
194193
height auto;
195-
box-shadow $boxShadow;
194+
box-shadow var(--box-shadow);
196195
border-radius $borderRadius
197196
box-sizing border-box
198197
padding 0 15px
199198
&:hover {
200-
box-shadow: $boxShadowHover;
199+
box-shadow: var(--box-shadow-hover);
201200
}
202201
.personal-img {
203202
display block
@@ -207,6 +206,7 @@ export default {
207206
}
208207
.name {
209208
text-align center
209+
color var(--text-color)
210210
}
211211
.num {
212212
display flex
@@ -221,14 +221,18 @@ export default {
221221
h3 {
222222
line-height auto
223223
margin 0 0 .6rem
224-
color $textColor
224+
color var(--text-color)
225225
}
226226
h6 {
227227
line-height auto
228+
color var(--text-color)
228229
margin 0
229230
}
230231
}
231232
}
233+
h4 {
234+
color var(--text-color)
235+
}
232236
.category-wrapper {
233237
list-style none
234238
padding-left 0
@@ -237,7 +241,7 @@ export default {
237241
padding: .4rem .8rem;
238242
transition: all .5s
239243
border-radius $borderRadius
240-
box-shadow $boxShadow
244+
box-shadow var(--box-shadow)
241245
&:hover {
242246
transform scale(1.04)
243247
}

components/Mode/ModeOptions.vue

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</template>
1414

1515
<script>
16+
import setMode, { activateMode } from './setMode'
1617
1718
export default {
1819
name: 'ModeOptions',
@@ -28,9 +29,28 @@ export default {
2829
}
2930
},
3031
32+
mounted () {
33+
const mode = localStorage.getItem('mode')
34+
this.currentMode = mode === null ? 'auto' : mode
35+
if (mode === 'dark') {
36+
activateMode('dark')
37+
} else if (mode === 'light') {
38+
activateMode('light')
39+
}
40+
},
41+
3142
methods: {
3243
selectMode (mode) {
33-
if (mode.mode === this.currentMode) return
44+
if (mode.mode === this.currentMode) {
45+
return
46+
} else if (mode.mode === 'dark') {
47+
activateMode('dark')
48+
} else if (mode.mode === 'light') {
49+
activateMode('light')
50+
} else if (mode.mode === 'auto') {
51+
setMode()
52+
}
53+
localStorage.setItem('mode', mode.mode)
3454
this.currentMode = mode.mode
3555
},
3656
getClass (mode) {
@@ -44,22 +64,29 @@ export default {
4464
@require '../../styles/recoConfig.styl'
4565
4666
.mode-options
47-
.title
48-
margin-top 0
49-
margin-bottom .6rem
50-
font-weight bold
67+
background-color var(--background-color)
68+
min-width: 125px;
69+
margin: 0;
70+
padding: 1em;
71+
box-shadow var(--box-shadow);
72+
border-radius: $borderRadius;
73+
.title
74+
margin-top 0
75+
margin-bottom .6rem
76+
font-weight bold
77+
color var(--text-color)
5178
.color-mode-options
5279
display: flex;
5380
flex-wrap wrap
5481
li
5582
text-align: center;
5683
font-size 12px
57-
color #666
84+
color var(--text-color)
5885
line-height 18px
5986
padding 3px 6px
6087
border-top 1px solid #666
6188
border-bottom 1px solid #666
62-
background-color: #fff;
89+
background-color var(--background-color)
6390
cursor pointer
6491
&.dark
6592
border-radius: $borderRadius 0 0 $borderRadius

components/Mode/index.vue

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,9 @@ export default {
5757
}
5858
5959
.color-picker-menu {
60-
background-color: #fff;
6160
position: absolute;
6261
top: 40px;
6362
left: 50%;
64-
min-width: 125px;
65-
margin: 0;
66-
padding: 1em;
67-
box-shadow $boxShadow;
68-
border-radius: $borderRadius;
6963
transform: translateX(-50%);
7064
z-index: 150;
7165
&::before {

components/Mode/modeOptions.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const modeOptions = {
2+
light: {
3+
'--background-color': '#fff',
4+
'--box-shadow': '0 1px 6px 0 rgba(0, 0, 0, 0.2)',
5+
'--box-shadow-hover': '0 2px 16px 0 rgba(0, 0, 0, 0.2)',
6+
'--text-color': '#2c3e50',
7+
'--border-color': '#cfd4db'
8+
},
9+
dark: {
10+
'--background-color': '#25272a',
11+
'--box-shadow': '0 1px 6px 0 rgba(0, 0, 0, .9)',
12+
'--box-shadow-hover': '0 2px 26px 0 rgba(0, 0, 0, .9)',
13+
'--text-color': '#aaa',
14+
'--border-color': 'rgba(0, 0, 0, .9)'
15+
}
16+
}
17+
18+
export default modeOptions

components/Mode/setMode.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import modeOptions from './modeOptions'
2+
3+
export function activateMode (mode) {
4+
console.log(mode)
5+
const rootElement = document.querySelector(':root')
6+
const options = modeOptions[mode]
7+
8+
for (const k in options) {
9+
rootElement.style.setProperty(k, options[k])
10+
}
11+
}
12+
13+
export function activateDarkMode () {
14+
const rootElement = document.querySelector(':root')
15+
const darkTheme = {
16+
'--background-color': '#25272a',
17+
'--box-shadow': '0 1px 6px 0 rgba(0, 0, 0, .9)',
18+
'--box-shadow-hover': '0 2px 26px 0 rgba(0, 0, 0, .9)'
19+
}
20+
for (const k in darkTheme) {
21+
rootElement.style.setProperty(k, darkTheme[k])
22+
}
23+
}
24+
25+
export function activateLightMode () {
26+
const rootElement = document.querySelector(':root')
27+
const lightTheme = {
28+
'--background-color': '#fff',
29+
'--box-shadow': '0 1px 6px 0 rgba(0, 0, 0, 0.2)',
30+
'--box-shadow-hover': '0 2px 16px 0 rgba(0, 0, 0, 0.2)'
31+
}
32+
for (const k in lightTheme) {
33+
rootElement.style.setProperty(k, lightTheme[k])
34+
}
35+
}
36+
/**
37+
* Sets a color scheme for the website.
38+
* If browser supports "prefers-color-scheme" it will respect the setting for light or dark mode
39+
* otherwise it will set a dark theme during night time
40+
*/
41+
export default function setMode () {
42+
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
43+
const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
44+
const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
45+
const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
46+
47+
window.matchMedia('(prefers-color-scheme: dark)').addListener(e => e.matches && activateMode('dark'))
48+
window.matchMedia('(prefers-color-scheme: light)').addListener(e => e.matches && activateMode('light'))
49+
50+
if (isDarkMode) activateMode('dark')
51+
if (isLightMode) activateMode('light')
52+
if (isNotSpecified || hasNoSupport) {
53+
console.log('You specified no preference for a color scheme or your browser does not support it. I schedule dark mode during night time.')
54+
const now = new Date()
55+
const hour = now.getHours()
56+
if (hour < 4 || hour >= 16) {
57+
activateMode('dark')
58+
}
59+
}
60+
}

components/NavLinks.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
class="repo-link"
2424
target="_blank"
2525
rel="noopener noreferrer">
26+
<i :class="`iconfont reco-${repoLabel.toLowerCase()}`"></i>
2627
{{ repoLabel }}
2728
<OutboundLink/>
2829
</a>
@@ -159,7 +160,7 @@ export default {
159160
display inline-block
160161
a
161162
line-height 1.4rem
162-
color inherit
163+
color var(--text-color)
163164
&:hover, &.router-link-active
164165
color $accentColor
165166
.iconfont
@@ -180,9 +181,6 @@ export default {
180181
margin-left 0
181182
182183
@media (min-width: $MQMobile)
183-
.nav-links a
184-
&:hover, &.router-link-active
185-
color $textColor
186184
.nav-item > a:not(.external)
187185
&:hover, &.router-link-active
188186
margin-bottom -2px

0 commit comments

Comments
 (0)