You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -18,11 +18,11 @@ Simple dark-mode/light-mode logic for any website
18
18
## ⚡ Features
19
19
20
20
- Easy to integrate with any site (via [script tag](#script-tag) or [NPM](#npm))
21
-
- Specify different styles for the dark and light mode with [CSS classes]()
22
-
- Automatically detect system theme
23
-
-Attach customizable theme toggle button to the page (or use it [programmatically]())
24
-
-Store users choice in local storage or optionally as a cookie
25
-
- Emits a `theme-change` event for advanced use cases like changing images
21
+
- Specify different styles for the dark and light mode with [CSS classes](#styling)
22
+
- Automatically detects system theme (and theme changes)
23
+
-Trigger a them change with the [default theme toggle](#default-theme-toggle), a [custom element](#custom-buttons) or even [programmatically](#programmatic)
24
+
-Stores users choice in local storage or [optionally](#%EF%B8%8F-options) as a cookie
25
+
- Emits a `theme-change`[event](#events) for advanced use cases like [changing images](#different-images-depending-on-the-theme)
26
26
27
27
## 👋 Introduction
28
28
@@ -37,11 +37,9 @@ The library will add the class `theme-dark`/`theme-light` to the body of the pag
> With `data-drkmd` the button will be attached automatically on load, leave it out if you want to attach it manually
44
-
45
43
### NPM
46
44
47
45
Install [drkmd.js](https://github.com/BetaHuhn/drkmd.js) using NPM
@@ -58,10 +56,16 @@ import Darkmode from 'drkmd-js';
58
56
newDarkmode().attach();
59
57
```
60
58
61
-
By default [drkmd.js](https://github.com/BetaHuhn/drkmd.js) will add the button to the bottom right corner and save the users choice in local storage, this can be configured using the [options](https://github.com/BetaHuhn/drkmd.js#options).
59
+
Both methods will add the [darkmode toggle](#default-theme-toggle) with all the default [options](#%EF%B8%8F-options) to your page.
60
+
61
+
The last step is to specify the [styling](#styling) for each theme and then you're done 🎉
62
+
63
+
Enjoy the dark side 🖤
62
64
63
65
## 📚 Usage
64
66
67
+
### Styling
68
+
65
69
There are multiple ways to specify the different styles for the dark and light mode when using [drkmd.js](https://github.com/BetaHuhn/drkmd.js).
66
70
67
71
[drkmd.js](https://github.com/BetaHuhn/drkmd.js) adds the class `theme-dark`/`theme-light` to the body element of your page, which can be used to specify different styles for each theme:
@@ -78,49 +82,43 @@ There are multiple ways to specify the different styles for the dark and light m
78
82
}
79
83
```
80
84
81
-
The easiest way is to specify different colors for each theme using cssvariables:
85
+
In most cases it is easier to specify [css-variables](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties)for different themes (See [below](#css-variables) for an example).
82
86
83
-
```css
84
-
/* Light Colors */
85
-
.theme-light {
86
-
--background: #fff;
87
-
--color: #000;
88
-
}
87
+
[drkmd.js](https://github.com/BetaHuhn/drkmd.js) also adds the attribute `data-theme` with either `light` or `dark` as the value to the html tag. With this the different themes can also be specified with the css selector `[data-theme="dark"]` and `[data-theme="dark"]`.
89
88
90
-
/* Dark Colors */
91
-
.theme-dark {
92
-
--background: #000;
93
-
--color: #fff;
94
-
}
89
+
### Default theme toggle
95
90
96
-
html,
97
-
body {
98
-
background: var(--background);
99
-
color: var(--color);
100
-
}
91
+
The easiest way to change the theme is to use the included theme toggle by either adding the `data-drkmd-attach` attribute to the script tag (or any other element):
[drkmd.js](https://github.com/BetaHuhn/drkmd.js) also adds the attribute `data-theme` with either `light`or `dark` as the value to the html tag. With this the different themes can also be specified with the css selector `[data-theme="dark"]` and `[data-theme="dark"]`.
97
+
or by calling `.attach()`:
104
98
105
-
### Events
99
+
```javascript
100
+
newDarkmode().attach()
101
+
```
106
102
107
-
By default [drkmd.js](https://github.com/BetaHuhn/drkmd.js) emits a `theme-change` event everytime the theme changes:
103
+
If you use any of the [options](#%EF%B8%8F-options) you can also set `attach: true` to achieve the same as the two methods above.
108
104
109
-
```js
110
-
importDarkmodefrom'drkmd-js';
105
+
By default the button will be added to the bottom right corner of the page and the users choice will be saved in local storage. This can be configured using the [options](#%EF%B8%8F-options).
111
106
112
-
newDarkmode();
107
+
### Custom buttons
113
108
114
-
window.addEventListener('theme-change', e=> {
115
-
console.log(e.detail.to); // will return 'light' or 'dark'
116
-
});
109
+
You can add the attribute `data-drkmd-toggle` to any element to transform it into a theme toggle:
110
+
111
+
```html
112
+
<spandata-drkmd-toggle>Toggle theme</span>
117
113
```
118
114
119
-
The `theme-change` event could be used to change the `src` attribute of an `<img>` tag depending on the theme ([more info](https://github.com/BetaHuhn/drkmd.js/discussions/11#discussioncomment-247341)) or modify the page in any other way with JavaScript when the theme changes.
115
+
When the element is clicked the current theme will be changed.
116
+
117
+
You can also use `data-drkmd-to-light` and `data-drkmd-to-dark` to switch to a specific theme.
120
118
121
-
### Programmatic Usage
119
+
### Programmatic
122
120
123
-
[drkmd.js](https://github.com/BetaHuhn/drkmd.js) can also be used programmatically if you don't want to show the button automatically on load or for other more advanced use cases.
121
+
[drkmd.js](https://github.com/BetaHuhn/drkmd.js) can also be used programmatically for more advanced use cases.
124
122
125
123
To enable/disable Darkmode you can use the method `toggle()`:
126
124
@@ -134,44 +132,69 @@ There are also other methods available:
134
132
```js
135
133
darkmode.attach() // Attach the default darkmode button to the page
136
134
darkmode.toggle() // Toggle the theme
137
-
darkmode.isActivated() // Returns true if the darkmode is active
138
135
darkmode.toLight() // Change theme to light
139
136
darkmode.toDark() // Change theme to dark
137
+
darkmode.currentTheme() // Returns the current theme as a string (dark/light)
138
+
darkmode.isDark() // Returns true if the current theme is dark
139
+
darkmode.isLight() // Returns true if the current theme is light
140
140
```
141
141
142
-
## ⚙️ Options
143
-
144
-
You can customize [drkmd.js](https://github.com/BetaHuhn/drkmd.js) in two different ways, depending on how you are including it in your site.
142
+
### Events
145
143
146
-
The main method is to pass a options object to `new Darkmode()`:
144
+
By default [drkmd.js](https://github.com/BetaHuhn/drkmd.js) emits a `theme-change` event everytime the theme changes:
147
145
148
146
```js
149
-
constoptions= {
150
-
top:'20px', // default: 'unset'
151
-
bottom:'unset', // default: '20px'
152
-
right:'unset', // default: '20px'
153
-
left:'32px', // default: 'unset'
154
-
buttonLight:'#fff', // default: '#fff'
155
-
buttonDark:'#000', // default: '#000'
156
-
events:false, // default: true
157
-
cookie:true, // default: false
158
-
localStorage:false, // default: true (will take precedence over cookie)
159
-
label:'', // default: '🌓'
160
-
autoMatchOsTheme:false, // default: true
161
-
defaultTheme:'dark', // default: 'light'
162
-
}
147
+
importDarkmodefrom'drkmd-js';
148
+
149
+
newDarkmode();
163
150
164
-
constdarkmode=newDarkmode(options);
165
-
darkmode.attach();
151
+
window.addEventListener('theme-change', e=> {
152
+
console.log(e.detail.to); // will return 'light' or 'dark'
153
+
});
166
154
```
167
155
168
-
The other method is to specify the options as the value for the `data-drkmd` attribute:
156
+
This can be turned off by setting the option `events: false`.
157
+
158
+
The `theme-change` event could be used to change the `src` attribute of an `<img>` tag depending on the theme ([more info](https://github.com/BetaHuhn/drkmd.js/discussions/11#discussioncomment-247341)) or modify the page in any other way with JavaScript when the theme changes.
159
+
160
+
## ⚙️ Options
161
+
162
+
You can customize the behaviour of [drkmd.js](https://github.com/BetaHuhn/drkmd.js) and the style of the included toggle with these options:
This works on any element, but it is recommended to only use it when you are loading drkmd via the [script tag](#script-tag).
186
+
> This works on any element, not just the script tag, so you can even use it when you are loading [drkmd.js](https://github.com/BetaHuhn/drkmd.js) via [NPM](#npm).
187
+
188
+
or you can pass them as a JS object to `new Darkmode()`:
189
+
190
+
```js
191
+
constoptions= {
192
+
cookie:true,
193
+
defaultTheme:'dark',
194
+
}
195
+
196
+
constdarkmode=newDarkmode(options)
197
+
```
175
198
176
199
## 📖 Examples
177
200
@@ -216,6 +239,34 @@ body {
216
239
217
240
---
218
241
242
+
### CSS variables
243
+
244
+
If you want to specify different colors for each theme, you can use [css-variables](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties):
0 commit comments