docsify-dark-mode-toggle 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -22
- package/boot.js +28 -0
- package/index.css +26 -1
- package/index.js +18 -5
- package/package.json +13 -2
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
Adds a floating button that switches the site between light and dark palettes,
|
|
11
11
|
respects `prefers-color-scheme`, and persists the user's choice in `localStorage`.
|
|
12
12
|
|
|
13
|
-
Works just as well in any plain HTML page
|
|
13
|
+
Works just as well in any plain HTML page - Docsify is not required.
|
|
14
14
|
|
|
15
15
|
| Light | Dark |
|
|
16
16
|
| -------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
|
|
@@ -30,46 +30,76 @@ Works just as well in any plain HTML page — Docsify is not required.
|
|
|
30
30
|
|
|
31
31
|
### Via CDN (recommended for Docsify sites)
|
|
32
32
|
|
|
33
|
-
Add
|
|
33
|
+
Add the stylesheet and the boot script to `<head>`, and the plugin **after**
|
|
34
|
+
`docsify.min.js`:
|
|
34
35
|
|
|
35
36
|
```html
|
|
36
|
-
<
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
<head>
|
|
38
|
+
<!-- ...your docsify theme... -->
|
|
39
|
+
<link
|
|
40
|
+
rel="stylesheet"
|
|
41
|
+
href="https://unpkg.com/docsify-dark-mode-toggle@1/index.css"
|
|
42
|
+
/>
|
|
43
|
+
<script src="https://unpkg.com/docsify-dark-mode-toggle@1/boot.js"></script>
|
|
44
|
+
</head>
|
|
45
|
+
<body>
|
|
46
|
+
<!-- ...your docsify config and script... -->
|
|
47
|
+
<script src="https://unpkg.com/docsify-dark-mode-toggle@1/index.js"></script>
|
|
48
|
+
</body>
|
|
42
49
|
```
|
|
43
50
|
|
|
44
|
-
|
|
51
|
+
The URLs use the `@1` range on purpose: you get every fix released in the 1.x
|
|
52
|
+
line (no breaking changes by semver) without editing the URL, while a future
|
|
53
|
+
2.0 never lands on your site by surprise. For fully reproducible builds pin an
|
|
54
|
+
exact version from the
|
|
55
|
+
[npm versions list](https://www.npmjs.com/package/docsify-dark-mode-toggle?activeTab=versions),
|
|
56
|
+
e.g. `docsify-dark-mode-toggle@1.1.0`.
|
|
57
|
+
|
|
58
|
+
[jsDelivr](https://www.jsdelivr.com/) works the same way:
|
|
59
|
+
`https://cdn.jsdelivr.net/npm/docsify-dark-mode-toggle@1/index.js` (and
|
|
60
|
+
`index.css`, `boot.js`).
|
|
61
|
+
|
|
62
|
+
### Avoiding the light flash (`boot.js`)
|
|
63
|
+
|
|
64
|
+
`index.js` is loaded at the end of the page, so on a dark site the browser
|
|
65
|
+
could paint the light theme for a moment before the plugin runs. `boot.js` is
|
|
66
|
+
a tiny script that, loaded synchronously in `<head>` (no `async` or `defer`),
|
|
67
|
+
reads the saved choice from `localStorage` (falling back to
|
|
68
|
+
`prefers-color-scheme`) and sets the `dark-mode` class on `<html>` right away.
|
|
69
|
+
|
|
70
|
+
It is optional: without it the plugin works as before, only the first paint
|
|
71
|
+
may be light. If you use a custom `storageKey`, pass the same key to the boot
|
|
72
|
+
script, because it runs before your `window.$docsify` config:
|
|
45
73
|
|
|
46
74
|
```html
|
|
47
|
-
<
|
|
48
|
-
rel="stylesheet"
|
|
49
|
-
href="https://unpkg.com/docsify-dark-mode-toggle@1.0.0/index.css"
|
|
50
|
-
/>
|
|
51
|
-
<script src="https://unpkg.com/docsify-dark-mode-toggle@1.0.0/index.js"></script>
|
|
75
|
+
<script src="boot.js" data-storage-key="my-docs-theme"></script>
|
|
52
76
|
```
|
|
53
77
|
|
|
54
|
-
[jsDelivr](https://www.jsdelivr.com/) works too:
|
|
55
|
-
`https://cdn.jsdelivr.net/npm/docsify-dark-mode-toggle@1.0.0/index.js`
|
|
56
|
-
|
|
57
78
|
### Via npm
|
|
58
79
|
|
|
59
80
|
```bash
|
|
60
81
|
npm install docsify-dark-mode-toggle
|
|
61
82
|
```
|
|
62
83
|
|
|
63
|
-
Then
|
|
84
|
+
Then serve the files from `node_modules/docsify-dark-mode-toggle/` (or copy
|
|
85
|
+
them into your site) and reference them locally:
|
|
86
|
+
|
|
87
|
+
```html
|
|
88
|
+
<link rel="stylesheet" href="vendor/docsify-dark-mode-toggle/index.css" />
|
|
89
|
+
<script src="vendor/docsify-dark-mode-toggle/boot.js"></script>
|
|
90
|
+
<!-- ...at the end of <body>, after docsify.min.js... -->
|
|
91
|
+
<script src="vendor/docsify-dark-mode-toggle/index.js"></script>
|
|
92
|
+
```
|
|
64
93
|
|
|
65
94
|
## Usage
|
|
66
95
|
|
|
67
|
-
No configuration is required
|
|
96
|
+
No configuration is required - drop the tags in and a toggle button appears
|
|
68
97
|
in the top-left corner of the page.
|
|
69
98
|
|
|
70
99
|
The script adds the class `dark-mode` to `<html>` when dark mode is active, so
|
|
71
100
|
your custom CSS can target either `html.dark-mode` or any of the variables
|
|
72
|
-
defined in `index.css`.
|
|
101
|
+
defined in `index.css`. The button exposes its state to assistive technology
|
|
102
|
+
with `aria-pressed` (`"true"` while dark mode is on).
|
|
73
103
|
|
|
74
104
|
## Configuration
|
|
75
105
|
|
|
@@ -121,8 +151,8 @@ html.dark-mode {
|
|
|
121
151
|
}
|
|
122
152
|
```
|
|
123
153
|
|
|
124
|
-
You can also place your own `<button class="dark-mode-toggle">` in the markup
|
|
125
|
-
|
|
154
|
+
You can also place your own `<button class="dark-mode-toggle">` in the markup -
|
|
155
|
+
if one already exists, the script reuses it instead of creating a new one.
|
|
126
156
|
|
|
127
157
|
## How it works
|
|
128
158
|
|
package/boot.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* docsify-dark-mode-toggle boot script.
|
|
3
|
+
*
|
|
4
|
+
* Load it synchronously in <head> (no async/defer) to set the dark-mode class
|
|
5
|
+
* on <html> before the page is painted, so a dark page never flashes light.
|
|
6
|
+
* It reads the same localStorage key as index.js; a custom key can be given
|
|
7
|
+
* with data-storage-key on the script tag or window.$docsify.darkMode.
|
|
8
|
+
*/
|
|
9
|
+
(function () {
|
|
10
|
+
var script = document.currentScript;
|
|
11
|
+
var docsify = window.$docsify;
|
|
12
|
+
var key =
|
|
13
|
+
(script && script.getAttribute("data-storage-key")) ||
|
|
14
|
+
(docsify && docsify.darkMode && docsify.darkMode.storageKey) ||
|
|
15
|
+
"color-scheme";
|
|
16
|
+
var stored = null;
|
|
17
|
+
try {
|
|
18
|
+
stored = localStorage.getItem(key);
|
|
19
|
+
} catch (e) {}
|
|
20
|
+
var dark =
|
|
21
|
+
stored !== null
|
|
22
|
+
? stored === "dark"
|
|
23
|
+
: !!(
|
|
24
|
+
window.matchMedia &&
|
|
25
|
+
window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
26
|
+
);
|
|
27
|
+
document.documentElement.classList.toggle("dark-mode", dark);
|
|
28
|
+
})();
|
package/index.css
CHANGED
|
@@ -39,6 +39,8 @@ html.dark-mode {
|
|
|
39
39
|
--dm-tip-bg: var(--dm-bg-elevated);
|
|
40
40
|
--dm-summary-bg: var(--dm-bg-elevated);
|
|
41
41
|
--dm-search-input-bg: var(--dm-bg-elevated);
|
|
42
|
+
--dm-search-clear-bg: var(--dm-border);
|
|
43
|
+
--dm-search-clear-icon: var(--dm-text);
|
|
42
44
|
|
|
43
45
|
background: var(--dm-bg);
|
|
44
46
|
color: var(--dm-text);
|
|
@@ -61,7 +63,9 @@ html.dark-mode {
|
|
|
61
63
|
display: flex;
|
|
62
64
|
align-items: center;
|
|
63
65
|
justify-content: center;
|
|
64
|
-
transition:
|
|
66
|
+
transition:
|
|
67
|
+
color 0.2s ease,
|
|
68
|
+
opacity 0.2s ease;
|
|
65
69
|
}
|
|
66
70
|
|
|
67
71
|
.dark-mode-toggle:hover {
|
|
@@ -81,8 +85,11 @@ html.dark-mode .content {
|
|
|
81
85
|
background: var(--dm-bg);
|
|
82
86
|
}
|
|
83
87
|
|
|
88
|
+
/* vue.css sets .sidebar { color: #364149 }, which covers plain (unlinked)
|
|
89
|
+
group names in the sidebar. */
|
|
84
90
|
html.dark-mode .sidebar {
|
|
85
91
|
border-right: 1px solid var(--dm-border-strong);
|
|
92
|
+
color: var(--dm-text);
|
|
86
93
|
}
|
|
87
94
|
|
|
88
95
|
html.dark-mode .sidebar ul li a {
|
|
@@ -111,6 +118,13 @@ html.dark-mode .markdown-section h6 {
|
|
|
111
118
|
color: var(--dm-text-strong);
|
|
112
119
|
}
|
|
113
120
|
|
|
121
|
+
/* Docsify wraps heading text in <a class="anchor"><span>, and vue.css gives
|
|
122
|
+
.anchor span its own dark color, so the heading rule above does not reach
|
|
123
|
+
the visible text. */
|
|
124
|
+
html.dark-mode .markdown-section .anchor span {
|
|
125
|
+
color: var(--dm-text-strong);
|
|
126
|
+
}
|
|
127
|
+
|
|
114
128
|
html.dark-mode .markdown-section a {
|
|
115
129
|
color: var(--dm-link);
|
|
116
130
|
}
|
|
@@ -193,6 +207,17 @@ html.dark-mode .search input[type="search"] {
|
|
|
193
207
|
border-color: var(--dm-border);
|
|
194
208
|
}
|
|
195
209
|
|
|
210
|
+
/* Clear (x) button of the search input: Docsify draws it as an inline SVG
|
|
211
|
+
with a light gray circle and a white cross, which is barely visible on a
|
|
212
|
+
dark sidebar. CSS overrides the SVG presentation attributes. */
|
|
213
|
+
html.dark-mode .search .clear-button circle {
|
|
214
|
+
fill: var(--dm-search-clear-bg);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
html.dark-mode .search .clear-button path {
|
|
218
|
+
stroke: var(--dm-search-clear-icon);
|
|
219
|
+
}
|
|
220
|
+
|
|
196
221
|
html.dark-mode .search .results-panel {
|
|
197
222
|
background: var(--dm-bg);
|
|
198
223
|
color: var(--dm-text);
|
package/index.js
CHANGED
|
@@ -40,9 +40,10 @@
|
|
|
40
40
|
const STORAGE_LIGHT = "light";
|
|
41
41
|
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
42
42
|
|
|
43
|
-
const docsifyHost =
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
const docsifyHost =
|
|
44
|
+
/** @type {Window & { $docsify?: { darkMode?: DarkModeConfig } }} */ (
|
|
45
|
+
window
|
|
46
|
+
);
|
|
46
47
|
/** @type {DarkModeConfig} */
|
|
47
48
|
const userConfig = docsifyHost.$docsify?.darkMode || {};
|
|
48
49
|
const storageKey = userConfig.storageKey || DEFAULTS.storageKey;
|
|
@@ -127,9 +128,16 @@
|
|
|
127
128
|
function parseIcon(input, fallback) {
|
|
128
129
|
if (input instanceof SVGElement) return input;
|
|
129
130
|
if (typeof input === "string" && input.trim()) {
|
|
130
|
-
const doc = new DOMParser().parseFromString(
|
|
131
|
+
const doc = new DOMParser().parseFromString(
|
|
132
|
+
input.trim(),
|
|
133
|
+
"image/svg+xml",
|
|
134
|
+
);
|
|
131
135
|
const root = doc.documentElement;
|
|
132
|
-
if (
|
|
136
|
+
if (
|
|
137
|
+
root &&
|
|
138
|
+
root.nodeName.toLowerCase() === "svg" &&
|
|
139
|
+
!doc.querySelector("parsererror")
|
|
140
|
+
) {
|
|
133
141
|
return /** @type {SVGElement} */ (/** @type {unknown} */ (root));
|
|
134
142
|
}
|
|
135
143
|
}
|
|
@@ -164,6 +172,7 @@
|
|
|
164
172
|
button.replaceChildren(isDark ? sunIcon : moonIcon);
|
|
165
173
|
button.setAttribute("aria-label", label);
|
|
166
174
|
button.setAttribute("title", label);
|
|
175
|
+
button.setAttribute("aria-pressed", String(isDark));
|
|
167
176
|
}
|
|
168
177
|
|
|
169
178
|
function createButton() {
|
|
@@ -192,6 +201,10 @@
|
|
|
192
201
|
});
|
|
193
202
|
}
|
|
194
203
|
|
|
204
|
+
// Apply the class as early as possible. boot.js in <head> does this before
|
|
205
|
+
// the first paint; without it, this still beats waiting for DOMContentLoaded.
|
|
206
|
+
document.documentElement.classList.toggle(DARK_CLASS, resolveInitialDark());
|
|
207
|
+
|
|
195
208
|
if (document.readyState === "loading") {
|
|
196
209
|
document.addEventListener("DOMContentLoaded", init);
|
|
197
210
|
} else {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "docsify-dark-mode-toggle",
|
|
3
3
|
"description": "Dark mode toggle button for Docsify - vanilla JS, no dependencies, configurable storage key, i18n, custom SVG icons.",
|
|
4
|
-
"version": "1.0
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Piotr Kowalski",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"files": [
|
|
22
22
|
"index.js",
|
|
23
23
|
"index.css",
|
|
24
|
+
"boot.js",
|
|
24
25
|
"LICENSE"
|
|
25
26
|
],
|
|
26
27
|
"keywords": [
|
|
@@ -34,5 +35,15 @@
|
|
|
34
35
|
"prefers-color-scheme"
|
|
35
36
|
],
|
|
36
37
|
"main": "index.js",
|
|
37
|
-
"style": "index.css"
|
|
38
|
+
"style": "index.css",
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "playwright test",
|
|
41
|
+
"format": "prettier --write .",
|
|
42
|
+
"format:check": "prettier --check .",
|
|
43
|
+
"ci": "npm run format:check && npm test"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@playwright/test": "1.65.1",
|
|
47
|
+
"prettier": "3.10.1"
|
|
48
|
+
}
|
|
38
49
|
}
|