Skip to content

Commit

Permalink
feat: 'change-mode' event when chosen a new color mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ktquez committed Jun 9, 2020
1 parent 130f96f commit 8fa7193
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Expand Up @@ -39,6 +39,7 @@ module.exports = {
'/guide/',
'/guide/class-naming.md',
'/guide/modes.md',
'/guide/events.md',
'/guide/storage.md',
'/guide/meta-theme-color.md',
'/guide/accessibility.md',
Expand Down
29 changes: 29 additions & 0 deletions docs/guide/events.md
@@ -0,0 +1,29 @@
# Events

## change-mode

Event fired every color mode change.


| Event | value |
| -------------- | ------------------------------------------------------------ |
| `change-mode` | color mode token. (`light` \| `dark` \| `system` \| `myCustomMode`) |

```vue
<VueDarkMode @change-mode="myColorModeHandler">
<template v-slot="{ mode }">
Color mode: {{ mode }}
</template>
</VueDarkMode>
```

```js
export default {
// ...
methods: {
myColorModeHandler (mode) {
// action here
}
}
}
```
3 changes: 3 additions & 0 deletions src/VueDarkMode.vue
Expand Up @@ -62,6 +62,7 @@ export default {
computed: {
getPrefersColorScheme () {
if (this.$isServer) return false
const colorSchemeTypes = ['dark', 'light']
let colorScheme = null
colorSchemeTypes.forEach(type => {
Expand Down Expand Up @@ -120,6 +121,7 @@ export default {
window[this.storage].setItem('colorMode', this.chosenMode)
this.handleColorModeClass('add')
if (Object.keys(this.metaThemeColor).length) this.setMetaThemeColor(this.metaThemeColor[this.currentMode] || this.metaThemeColor[this.getPrefersColorScheme])
this.$emit('change-mode', this.chosenMode)
},
getMediaQueryList (type) {
Expand All @@ -135,6 +137,7 @@ export default {
},
handleColorModeClass (action) {
if (this.$isServer) return
return document.documentElement.classList[action](`${this.className.replace(/%cm/g, this.currentMode)}`)
},
Expand Down

0 comments on commit 8fa7193

Please sign in to comment.