docsify-dark-mode-toggle 1.0.4 → 1.1.1

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.
Files changed (5) hide show
  1. package/README.md +60 -28
  2. package/boot.js +28 -0
  3. package/index.css +10 -0
  4. package/index.js +34 -12
  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 — Docsify is not required.
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,77 @@ 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 **after** `docsify.min.js` in your `index.html`:
33
+ Add the stylesheet and the boot script to `<head>`, and the plugin **after**
34
+ `docsify.min.js`:
34
35
 
35
36
  ```html
36
- <link
37
- rel="stylesheet"
38
- href="https://unpkg.com/docsify-dark-mode-toggle/index.css"
39
- />
40
- <!-- ...your docsify config and script... -->
41
- <script src="https://unpkg.com/docsify-dark-mode-toggle/index.js"></script>
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
- Pin a version for reproducible builds:
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
- <link
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 import the CSS and reference the JS however your bundler / setup expects.
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 — drop the two tags in and a toggle button appears
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) and a constant accessible
103
+ name (`label`, e.g. "Dark mode"); the tooltip says what a click will do.
73
104
 
74
105
  ## Configuration
75
106
 
@@ -84,6 +115,7 @@ All options are optional and read from `window.$docsify.darkMode`:
84
115
  messages: {
85
116
  // merged with built-in en/pl
86
117
  de: {
118
+ label: "Dunkles Design",
87
119
  enable: "Dunkles Design aktivieren",
88
120
  disable: "Dunkles Design deaktivieren",
89
121
  },
@@ -95,12 +127,12 @@ All options are optional and read from `window.$docsify.darkMode`:
95
127
  </script>
96
128
  ```
97
129
 
98
- | Option | Type | Default | Description |
99
- | ------------ | ------------------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------ |
100
- | `storageKey` | `string` | `"color-scheme"` | `localStorage` key under which the user's choice is stored (`"dark"` / `"light"`). |
101
- | `messages` | `Record<string, { enable, disable }>` | English + Polish | Per-locale labels for the button. Locale is taken from `<html lang>` (falling back to `navigator.language`). |
102
- | `sunIcon` | `string \| SVGElement` | built-in sun | Icon shown when dark mode is **active** (click switches to light). |
103
- | `moonIcon` | `string \| SVGElement` | built-in moon | Icon shown when dark mode is **inactive** (click switches to dark). |
130
+ | Option | Type | Default | Description |
131
+ | ------------ | -------------------------------------------- | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
132
+ | `storageKey` | `string` | `"color-scheme"` | `localStorage` key under which the user's choice is stored (`"dark"` / `"light"`). |
133
+ | `messages` | `Record<string, { label, enable, disable }>` | English + Polish | Per-locale texts: `label` is the button's accessible name (falls back to `enable`), `enable`/`disable` are the tooltips. Locale is taken from `<html lang>` (falling back to `navigator.language`). |
134
+ | `sunIcon` | `string \| SVGElement` | built-in sun | Icon shown when dark mode is **active** (click switches to light). |
135
+ | `moonIcon` | `string \| SVGElement` | built-in moon | Icon shown when dark mode is **inactive** (click switches to dark). |
104
136
 
105
137
  ## Styling
106
138
 
@@ -121,8 +153,8 @@ html.dark-mode {
121
153
  }
122
154
  ```
123
155
 
124
- You can also place your own `<button class="dark-mode-toggle">` in the markup
125
- — if one already exists, the script reuses it instead of creating a new one.
156
+ You can also place your own `<button class="dark-mode-toggle">` in the markup -
157
+ if one already exists, the script reuses it instead of creating a new one.
126
158
 
127
159
  ## How it works
128
160
 
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
@@ -85,8 +85,11 @@ html.dark-mode .content {
85
85
  background: var(--dm-bg);
86
86
  }
87
87
 
88
+ /* vue.css sets .sidebar { color: #364149 }, which covers plain (unlinked)
89
+ group names in the sidebar. */
88
90
  html.dark-mode .sidebar {
89
91
  border-right: 1px solid var(--dm-border-strong);
92
+ color: var(--dm-text);
90
93
  }
91
94
 
92
95
  html.dark-mode .sidebar ul li a {
@@ -115,6 +118,13 @@ html.dark-mode .markdown-section h6 {
115
118
  color: var(--dm-text-strong);
116
119
  }
117
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
+
118
128
  html.dark-mode .markdown-section a {
119
129
  color: var(--dm-link);
120
130
  }
package/index.js CHANGED
@@ -9,7 +9,7 @@
9
9
  * darkMode: {
10
10
  * storageKey: "color-scheme", // localStorage key
11
11
  * messages: { // merged with built-in en/pl
12
- * pl: { enable: "...", disable: "..." },
12
+ * pl: { label: "...", enable: "...", disable: "..." },
13
13
  * },
14
14
  * sunIcon: "<svg ...>...</svg>", // SVG string (overrides default)
15
15
  * moonIcon: "<svg ...>...</svg>",
@@ -18,7 +18,7 @@
18
18
  */
19
19
  (function () {
20
20
  /**
21
- * @typedef {{ enable: string; disable: string }} DarkModeMessages
21
+ * @typedef {{ label?: string; enable: string; disable: string }} DarkModeMessages
22
22
  * @typedef {{
23
23
  * storageKey?: string;
24
24
  * messages?: Record<string, Partial<DarkModeMessages>>;
@@ -31,8 +31,16 @@
31
31
  storageKey: "color-scheme",
32
32
  /** @type {Record<string, DarkModeMessages>} */
33
33
  messages: {
34
- en: { enable: "Enable dark mode", disable: "Disable dark mode" },
35
- pl: { enable: "Włącz tryb nocny", disable: "Wyłącz tryb nocny" },
34
+ en: {
35
+ label: "Dark mode",
36
+ enable: "Enable dark mode",
37
+ disable: "Disable dark mode",
38
+ },
39
+ pl: {
40
+ label: "Tryb nocny",
41
+ enable: "Włącz tryb nocny",
42
+ disable: "Wyłącz tryb nocny",
43
+ },
36
44
  },
37
45
  };
38
46
  const DARK_CLASS = "dark-mode";
@@ -40,9 +48,10 @@
40
48
  const STORAGE_LIGHT = "light";
41
49
  const SVG_NS = "http://www.w3.org/2000/svg";
42
50
 
43
- const docsifyHost = /** @type {Window & { $docsify?: { darkMode?: DarkModeConfig } }} */ (
44
- window
45
- );
51
+ const docsifyHost =
52
+ /** @type {Window & { $docsify?: { darkMode?: DarkModeConfig } }} */ (
53
+ window
54
+ );
46
55
  /** @type {DarkModeConfig} */
47
56
  const userConfig = docsifyHost.$docsify?.darkMode || {};
48
57
  const storageKey = userConfig.storageKey || DEFAULTS.storageKey;
@@ -127,9 +136,16 @@
127
136
  function parseIcon(input, fallback) {
128
137
  if (input instanceof SVGElement) return input;
129
138
  if (typeof input === "string" && input.trim()) {
130
- const doc = new DOMParser().parseFromString(input.trim(), "image/svg+xml");
139
+ const doc = new DOMParser().parseFromString(
140
+ input.trim(),
141
+ "image/svg+xml",
142
+ );
131
143
  const root = doc.documentElement;
132
- if (root && root.nodeName.toLowerCase() === "svg" && !doc.querySelector("parsererror")) {
144
+ if (
145
+ root &&
146
+ root.nodeName.toLowerCase() === "svg" &&
147
+ !doc.querySelector("parsererror")
148
+ ) {
133
149
  return /** @type {SVGElement} */ (/** @type {unknown} */ (root));
134
150
  }
135
151
  }
@@ -160,10 +176,12 @@
160
176
  */
161
177
  function render(button, isDark) {
162
178
  document.documentElement.classList.toggle(DARK_CLASS, isDark);
163
- const label = isDark ? i18n.disable : i18n.enable;
179
+ // A toggle button keeps a constant name; its state is in aria-pressed.
180
+ // The tooltip still says what a click will do.
164
181
  button.replaceChildren(isDark ? sunIcon : moonIcon);
165
- button.setAttribute("aria-label", label);
166
- button.setAttribute("title", label);
182
+ button.setAttribute("aria-label", i18n.label || i18n.enable);
183
+ button.setAttribute("title", isDark ? i18n.disable : i18n.enable);
184
+ button.setAttribute("aria-pressed", String(isDark));
167
185
  }
168
186
 
169
187
  function createButton() {
@@ -192,6 +210,10 @@
192
210
  });
193
211
  }
194
212
 
213
+ // Apply the class as early as possible. boot.js in <head> does this before
214
+ // the first paint; without it, this still beats waiting for DOMContentLoaded.
215
+ document.documentElement.classList.toggle(DARK_CLASS, resolveInitialDark());
216
+
195
217
  if (document.readyState === "loading") {
196
218
  document.addEventListener("DOMContentLoaded", init);
197
219
  } 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",
4
+ "version": "1.1.1",
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
  }