Skip to content

Commit 30a43b5

Browse files
committed
feat: Custom color-mode className
1 parent 13ec095 commit 30a43b5

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

Diff for: docs/.vuepress/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = {
3131
collapsable: false,
3232
children: [
3333
'/guide/',
34+
'/guide/class-naming.md',
3435
'/guide/modes.md',
3536
'/guide/storage.md',
3637
'/guide/meta-theme-color.md',

Diff for: docs/guide/class-naming.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Class naming
2+
3+
The class by default for deciding the color mode is inserted in the HTML tag. `${color}-mode`
4+
5+
e.g.
6+
7+
```html
8+
<!DOCTYPE html>
9+
<html lang="en" class="light-mode">
10+
<head>
11+
<meta charset="UTF-8">
12+
...
13+
```
14+
15+
## Custom
16+
17+
You can also customize the class name.
18+
19+
| prop | Type | default
20+
| -------------- | --------- | ----------------------------------------------------
21+
| `className` | String | `%cm-mode`
22+
23+
::: tip
24+
**`%cm`**: We use this placeholder to color mode that will be implemented
25+
:::
26+
27+
**e.g.**
28+
29+
```vue
30+
<VueDarkMode className="%cm-theme">
31+
<template v-slot="{ mode }">
32+
Color mode: {{ mode }}
33+
</template>
34+
</VueDarkMode>
35+
```
36+
37+
**Output:**
38+
39+
```html
40+
<!DOCTYPE html>
41+
<html lang="en" class="light-theme">
42+
<head>
43+
<meta charset="UTF-8">
44+
...
45+
```

Diff for: docs/howto/tailwind.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# TailwindCSS Dark Mode
22

33
By default, [tailwindcss-dark-mode](https://github.com/ChanceArthur/tailwindcss-dark-mode) uses the `.mode-dark` selector for dark mode.
4-
You can see more in the documentation section: [Changing the Selector](https://github.com/ChanceArthur/tailwindcss-dark-mode#changing-the-selector).
54

6-
The example below shows how you can modify the plugin selector to correctly serve the dark class generated by @vue-a11y/dark-mode.
5+
It is possible to [changing the Selector](https://github.com/ChanceArthur/tailwindcss-dark-mode#changing-the-selector) in "tailwindcss-dark-mode" plug-in, however, you can also adjust the class using the [className](/guide/class-naming.html) prop to generate the class compatible with the plugin.
76

8-
```javascript
9-
// tailwind.config.js
10-
module.exports = {
11-
theme: {
12-
darkSelector: '.dark-mode'
13-
},
14-
plugins: [
15-
require('tailwindcss-dark-mode')()
16-
]
17-
...
18-
}
19-
```
7+
e.g.
8+
9+
```vue
10+
<VueDarkMode className="mode-%cm">
11+
<template v-slot="{ mode }">
12+
Color mode: {{ mode }}
13+
</template>
14+
</VueDarkMode>
15+
```
16+
17+
```html
18+
<!DOCTYPE html>
19+
<html lang="en" class="mode-dark">
20+
<head>
21+
<meta charset="UTF-8">
22+
...
23+
```

Diff for: src/vue-dark-mode.vue

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export default {
2929
return ['light', 'dark', 'system']
3030
}
3131
},
32+
className: {
33+
type: String,
34+
default: '%cm-mode'
35+
},
3236
storage: {
3337
type: String,
3438
default: 'localStorage'
@@ -131,7 +135,7 @@ export default {
131135
},
132136
133137
handleColorModeClass (action) {
134-
return document.documentElement.classList[action](`${this.currentMode}-mode`)
138+
return document.documentElement.classList[action](`${this.className.replace(/%cm/g, this.currentMode)}`)
135139
},
136140
137141
handlePreferColorScheme (e) {

0 commit comments

Comments
 (0)