Skip to content

Commit 3843aa3

Browse files
committed
fix: mode listeners not removed
1 parent c820b79 commit 3843aa3

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

components/Mode/ModePicker.vue

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
v-for="(mode, index) in modeOptions"
77
:key="index"
88
:class="getClass(mode.mode)"
9-
@click="selectMode(mode)"
9+
@click="selectMode(mode.mode)"
1010
>{{ mode.title }}</li>
1111
</ul>
1212
</div>
1313
</template>
1414

1515
<script>
16-
import setMode, { activateMode } from './setMode'
16+
import setMode from './setMode'
1717
1818
export default {
1919
name: 'ModeOptions',
@@ -32,21 +32,17 @@ export default {
3232
mounted () {
3333
const mode = localStorage.getItem('mode')
3434
const { mode: customizeMode } = this.$themeConfig
35-
this.currentMode = mode === null ? customizeMode === undefined ? 'auto' : customizeMode : mode
36-
activateMode(this.currentMode)
35+
this.currentMode = mode ?? (customizeMode === null || customizeMode === undefined ? 'auto' : customizeMode)
36+
setMode(this.currentMode)
3737
},
3838
3939
methods: {
4040
selectMode (mode) {
41-
if (mode.mode === this.currentMode) {
42-
return
43-
} else if (mode.mode === 'auto') {
44-
setMode()
45-
} else {
46-
activateMode(mode.mode)
41+
if (mode !== this.currentMode) {
42+
this.currentMode = mode
43+
setMode(mode)
44+
localStorage.setItem('mode', mode)
4745
}
48-
localStorage.setItem('mode', mode.mode)
49-
this.currentMode = mode.mode
5046
},
5147
getClass (mode) {
5248
return mode !== this.currentMode ? mode : `${mode} active`

components/Mode/setMode.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import modeOptions from './modeOptions'
22

3-
export function activateMode (mode) {
3+
function activateMode (mode) {
44
const rootElement = document.querySelector(':root')
55
const options = modeOptions[mode]
66

@@ -9,19 +9,33 @@ export function activateMode (mode) {
99
}
1010
}
1111

12+
// Dark and Light autoswitches
13+
const onDark = (e) => e.matches && activateMode('dark')
14+
const onLight = (e) => e.matches && activateMode('light')
15+
16+
const darkScheme = window.matchMedia('(prefers-color-scheme: dark)')
17+
const lightScheme = window.matchMedia('(prefers-color-scheme: light)')
18+
1219
/**
1320
* Sets a color scheme for the website.
1421
* If browser supports "prefers-color-scheme" it will respect the setting for light or dark mode
1522
* otherwise it will set a dark theme during night time
1623
*/
17-
export default function setMode () {
18-
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
19-
const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
24+
export default function setMode (mode = 'auto') {
25+
if (mode !== 'auto') {
26+
darkScheme.removeListener(onDark)
27+
lightScheme.removeListener(onLight)
28+
activateMode(mode)
29+
return
30+
}
31+
32+
const isDarkMode = darkScheme.matches
33+
const isLightMode = lightScheme.matches
2034
const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
2135
const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
2236

23-
window.matchMedia('(prefers-color-scheme: dark)').addListener(e => e.matches && activateMode('dark'))
24-
window.matchMedia('(prefers-color-scheme: light)').addListener(e => e.matches && activateMode('light'))
37+
darkScheme.addListener(onDark)
38+
lightScheme.addListener(onLight)
2539

2640
if (isDarkMode) activateMode('dark')
2741
if (isLightMode) activateMode('light')

0 commit comments

Comments
 (0)