docsify-dark-mode-toggle 1.0.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/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0] - 2026-05-28
9
+
10
+ ### Added
11
+
12
+ - Initial public release.
13
+ - Floating dark mode toggle button with sun/moon SVG icons.
14
+ - Respects `prefers-color-scheme: dark` for first-visit default.
15
+ - Persists user choice in `localStorage` (key configurable via `$docsify.darkMode.storageKey`).
16
+ - Built-in English and Polish labels; merge or override via `$docsify.darkMode.messages`.
17
+ - Customizable icons via `$docsify.darkMode.sunIcon` / `moonIcon` (SVG string or `SVGElement`).
18
+ - CSS custom properties on `:root` and `html.dark-mode` for easy palette overrides.
package/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # docsify-dark-mode-toggle
2
+
3
+ [![node version](https://img.shields.io/node/v/docsify-dark-mode-toggle.svg)](https://www.npmjs.com/package/docsify-dark-mode-toggle)
4
+ [![npm version](https://badge.fury.io/js/docsify-dark-mode-toggle.svg)](https://badge.fury.io/js/docsify-dark-mode-toggle)
5
+ [![downloads count](https://img.shields.io/npm/dt/docsify-dark-mode-toggle.svg)](https://www.npmjs.com/package/docsify-dark-mode-toggle)
6
+ [![size](https://packagephobia.com/badge?p=docsify-dark-mode-toggle)](https://packagephobia.com/result?p=docsify-dark-mode-toggle)
7
+ [![license](https://img.shields.io/npm/l/docsify-dark-mode-toggle.svg)](https://piecioshka.mit-license.org)
8
+
9
+ 🔨 A small, zero-dependency dark mode toggle for [Docsify](https://docsify.js.org/).
10
+ Adds a floating button that switches the site between light and dark palettes,
11
+ respects `prefers-color-scheme`, and persists the user's choice in `localStorage`.
12
+
13
+ Works just as well in any plain HTML page — Docsify is not required.
14
+
15
+ ## Installation
16
+
17
+ ### Via CDN (recommended for Docsify sites)
18
+
19
+ Add **after** `docsify.min.js` in your `index.html`:
20
+
21
+ ```html
22
+ <link
23
+ rel="stylesheet"
24
+ href="https://unpkg.com/docsify-dark-mode-toggle/index.css"
25
+ />
26
+ <!-- ...your docsify config and script... -->
27
+ <script src="https://unpkg.com/docsify-dark-mode-toggle/index.js"></script>
28
+ ```
29
+
30
+ Pin a version for reproducible builds:
31
+
32
+ ```html
33
+ <link
34
+ rel="stylesheet"
35
+ href="https://unpkg.com/docsify-dark-mode-toggle@1.0.0/index.css"
36
+ />
37
+ <script src="https://unpkg.com/docsify-dark-mode-toggle@1.0.0/index.js"></script>
38
+ ```
39
+
40
+ [jsDelivr](https://www.jsdelivr.com/) works too:
41
+ `https://cdn.jsdelivr.net/npm/docsify-dark-mode-toggle@1.0.0/index.js`
42
+
43
+ ### Via npm
44
+
45
+ ```bash
46
+ npm install docsify-dark-mode-toggle
47
+ ```
48
+
49
+ Then import the CSS and reference the JS however your bundler / setup expects.
50
+
51
+ ## Usage
52
+
53
+ No configuration is required — drop the two tags in and a toggle button appears
54
+ in the top-left corner of the page.
55
+
56
+ The script adds the class `dark-mode` to `<html>` when dark mode is active, so
57
+ your custom CSS can target either `html.dark-mode` or any of the variables
58
+ defined in `index.css`.
59
+
60
+ ## Configuration
61
+
62
+ All options are optional and read from `window.$docsify.darkMode`:
63
+
64
+ ```html
65
+ <script>
66
+ window.$docsify = {
67
+ // ...your other docsify options...
68
+ darkMode: {
69
+ storageKey: "color-scheme", // localStorage key (default: "color-scheme")
70
+ messages: {
71
+ // merged with built-in en/pl
72
+ de: {
73
+ enable: "Dunkles Design aktivieren",
74
+ disable: "Dunkles Design deaktivieren",
75
+ },
76
+ },
77
+ sunIcon: "<svg ...>...</svg>", // optional SVG string (or SVGElement)
78
+ moonIcon: "<svg ...>...</svg>",
79
+ },
80
+ };
81
+ </script>
82
+ ```
83
+
84
+ | Option | Type | Default | Description |
85
+ | ------------ | ------------------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------ |
86
+ | `storageKey` | `string` | `"color-scheme"` | `localStorage` key under which the user's choice is stored (`"dark"` / `"light"`). |
87
+ | `messages` | `Record<string, { enable, disable }>` | English + Polish | Per-locale labels for the button. Locale is taken from `<html lang>` (falling back to `navigator.language`). |
88
+ | `sunIcon` | `string \| SVGElement` | built-in sun | Icon shown when dark mode is **active** (click switches to light). |
89
+ | `moonIcon` | `string \| SVGElement` | built-in moon | Icon shown when dark mode is **inactive** (click switches to dark). |
90
+
91
+ ## Styling
92
+
93
+ `index.css` ships with CSS custom properties on `:root` for layout and on
94
+ `html.dark-mode` for the dark palette. Override either after loading the
95
+ stylesheet:
96
+
97
+ ```css
98
+ :root {
99
+ --dm-toggle-top: 12px;
100
+ --dm-toggle-left: auto;
101
+ --dm-toggle-right: 16px;
102
+ }
103
+
104
+ html.dark-mode {
105
+ --dm-bg: #001f3f;
106
+ --dm-code-bg: #002b5c;
107
+ }
108
+ ```
109
+
110
+ You can also place your own `<button class="dark-mode-toggle">` in the markup
111
+ — if one already exists, the script reuses it instead of creating a new one.
112
+
113
+ ## How it works
114
+
115
+ - On first visit, the initial mode is taken from `prefers-color-scheme: dark`.
116
+ - The user's explicit choice is saved as `"dark"` or `"light"` under
117
+ `storageKey` and wins over the system preference.
118
+ - The script reacts to `prefers-color-scheme` changes only while the user has
119
+ not made an explicit choice.
120
+
121
+ ## License
122
+
123
+ [The MIT License](https://piecioshka.mit-license.org) @ 2026
package/index.css ADDED
@@ -0,0 +1,227 @@
1
+ /*
2
+ * Dark mode palette and layout tokens.
3
+ *
4
+ * Override any value in a CSS file loaded after this one. Toggle layout tokens
5
+ * live on :root; the dark palette lives on html.dark-mode. Example:
6
+ *
7
+ * :root { --dm-toggle-left: 280px; }
8
+ * html.dark-mode { --dm-bg: #001f3f; --dm-code-bg: #002b5c; }
9
+ *
10
+ * Full variable list is in the :root and html.dark-mode blocks below.
11
+ */
12
+
13
+ :root {
14
+ --dm-toggle-color: #57606a;
15
+ --dm-toggle-top: 16px;
16
+ --dm-toggle-left: 316px;
17
+ --dm-toggle-right: auto;
18
+ --dm-toggle-size: 24px;
19
+ }
20
+
21
+ html.dark-mode {
22
+ --dm-bg: #0d1117;
23
+ --dm-bg-elevated: #161b22;
24
+ --dm-text: #c9d1d9;
25
+ --dm-text-strong: #f0f6fc;
26
+ --dm-text-muted: #8b949e;
27
+ --dm-border: #30363d;
28
+ --dm-border-strong: #21262d;
29
+ --dm-link: #58a6ff;
30
+ --dm-code: #d2a8ff;
31
+ --dm-summary-open-text: #fff;
32
+ --dm-img-brightness: 0.85;
33
+ --dm-toggle-color: var(--dm-text-strong);
34
+
35
+ --dm-code-bg: var(--dm-bg-elevated);
36
+ --dm-blockquote-bg: var(--dm-bg-elevated);
37
+ --dm-table-th-bg: var(--dm-bg-elevated);
38
+ --dm-table-row-alt-bg: var(--dm-bg-elevated);
39
+ --dm-tip-bg: var(--dm-bg-elevated);
40
+ --dm-summary-bg: var(--dm-bg-elevated);
41
+ --dm-search-input-bg: var(--dm-bg-elevated);
42
+
43
+ background: var(--dm-bg);
44
+ color: var(--dm-text);
45
+ }
46
+
47
+ /* Dark mode toggle (floating icon button) */
48
+ .dark-mode-toggle {
49
+ position: fixed;
50
+ top: var(--dm-toggle-top);
51
+ left: var(--dm-toggle-left);
52
+ right: var(--dm-toggle-right);
53
+ z-index: 1000;
54
+ width: var(--dm-toggle-size);
55
+ height: var(--dm-toggle-size);
56
+ padding: 0;
57
+ border: none;
58
+ background: transparent;
59
+ color: var(--dm-toggle-color);
60
+ cursor: pointer;
61
+ display: flex;
62
+ align-items: center;
63
+ justify-content: center;
64
+ transition: color 0.2s ease, opacity 0.2s ease;
65
+ }
66
+
67
+ .dark-mode-toggle:hover {
68
+ opacity: 0.7;
69
+ }
70
+
71
+ .dark-mode-toggle svg {
72
+ display: block;
73
+ width: 100%;
74
+ height: 100%;
75
+ }
76
+
77
+ /* Dark mode element styles */
78
+ html.dark-mode .sidebar,
79
+ html.dark-mode .sidebar-toggle,
80
+ html.dark-mode .content {
81
+ background: var(--dm-bg);
82
+ }
83
+
84
+ html.dark-mode .sidebar {
85
+ border-right: 1px solid var(--dm-border-strong);
86
+ }
87
+
88
+ html.dark-mode .sidebar ul li a {
89
+ color: var(--dm-text);
90
+ }
91
+
92
+ html.dark-mode .sidebar ul li.active > a,
93
+ html.dark-mode .sidebar ul li a:hover {
94
+ color: var(--theme-color, var(--dm-link));
95
+ }
96
+
97
+ html.dark-mode .app-name-link {
98
+ color: var(--dm-text);
99
+ }
100
+
101
+ html.dark-mode .markdown-section {
102
+ color: var(--dm-text);
103
+ }
104
+
105
+ html.dark-mode .markdown-section h1,
106
+ html.dark-mode .markdown-section h2,
107
+ html.dark-mode .markdown-section h3,
108
+ html.dark-mode .markdown-section h4,
109
+ html.dark-mode .markdown-section h5,
110
+ html.dark-mode .markdown-section h6 {
111
+ color: var(--dm-text-strong);
112
+ }
113
+
114
+ html.dark-mode .markdown-section a {
115
+ color: var(--dm-link);
116
+ }
117
+
118
+ html.dark-mode .markdown-section strong {
119
+ color: var(--dm-text-strong);
120
+ }
121
+
122
+ html.dark-mode .markdown-section blockquote {
123
+ color: var(--dm-text-muted);
124
+ border-left-color: var(--dm-border);
125
+ background: var(--dm-blockquote-bg);
126
+ }
127
+
128
+ html.dark-mode .markdown-section hr,
129
+ html.dark-mode .sidebar hr {
130
+ background: var(--dm-border);
131
+ border: 0;
132
+ height: 1px;
133
+ }
134
+
135
+ html.dark-mode .markdown-section code {
136
+ background: var(--dm-code-bg);
137
+ color: var(--dm-code);
138
+ }
139
+
140
+ html.dark-mode .markdown-section pre,
141
+ html.dark-mode .markdown-section pre > code {
142
+ background: var(--dm-code-bg);
143
+ color: var(--dm-text);
144
+ }
145
+
146
+ html.dark-mode .markdown-section pre::after {
147
+ color: var(--dm-text-muted);
148
+ }
149
+
150
+ html.dark-mode .markdown-section table tr {
151
+ background: var(--dm-bg);
152
+ border-top-color: var(--dm-border);
153
+ }
154
+
155
+ html.dark-mode .markdown-section table tr:nth-child(2n) {
156
+ background: var(--dm-table-row-alt-bg);
157
+ }
158
+
159
+ html.dark-mode .markdown-section table th,
160
+ html.dark-mode .markdown-section table td {
161
+ border-color: var(--dm-border);
162
+ }
163
+
164
+ html.dark-mode .markdown-section table th {
165
+ background: var(--dm-table-th-bg);
166
+ color: var(--dm-text-strong);
167
+ }
168
+
169
+ html.dark-mode .markdown-section tip,
170
+ html.dark-mode .markdown-section .tip {
171
+ background: var(--dm-tip-bg);
172
+ border-color: var(--dm-border);
173
+ }
174
+
175
+ html.dark-mode details > summary {
176
+ background-color: var(--dm-summary-bg);
177
+ color: var(--dm-text);
178
+ }
179
+
180
+ html.dark-mode details[open] > summary {
181
+ background-color: var(--theme-color, #1f6feb);
182
+ color: var(--dm-summary-open-text);
183
+ }
184
+
185
+ html.dark-mode .sidebar code {
186
+ background-color: var(--dm-code-bg);
187
+ color: var(--dm-code);
188
+ }
189
+
190
+ html.dark-mode .search input[type="search"] {
191
+ background: var(--dm-search-input-bg);
192
+ color: var(--dm-text);
193
+ border-color: var(--dm-border);
194
+ }
195
+
196
+ html.dark-mode .search .results-panel {
197
+ background: var(--dm-bg);
198
+ color: var(--dm-text);
199
+ }
200
+
201
+ html.dark-mode .search {
202
+ border-bottom: 1px solid var(--dm-border);
203
+ }
204
+
205
+ html.dark-mode .search .matching-post {
206
+ border-bottom: 1px solid var(--dm-border);
207
+ }
208
+
209
+ html.dark-mode .search .matching-post h2,
210
+ html.dark-mode .search .matching-post a {
211
+ color: var(--dm-text-strong);
212
+ }
213
+
214
+ html.dark-mode .search .matching-post em {
215
+ color: var(--theme-color, var(--dm-link));
216
+ }
217
+
218
+ html.dark-mode img:not([src*=".svg"]) {
219
+ filter: brightness(var(--dm-img-brightness));
220
+ }
221
+
222
+ @media (max-width: 768px) {
223
+ :root {
224
+ --dm-toggle-left: auto;
225
+ --dm-toggle-right: 16px;
226
+ }
227
+ }
package/index.js ADDED
@@ -0,0 +1,200 @@
1
+ /*
2
+ * Dark mode toggle.
3
+ *
4
+ * Optional configuration is read from window.$docsify.darkMode. All fields are
5
+ * optional; defaults are used when missing:
6
+ *
7
+ * window.$docsify = {
8
+ * // ...other docsify options...
9
+ * darkMode: {
10
+ * storageKey: "color-scheme", // localStorage key
11
+ * messages: { // merged with built-in en/pl
12
+ * pl: { enable: "...", disable: "..." },
13
+ * },
14
+ * sunIcon: "<svg ...>...</svg>", // SVG string (overrides default)
15
+ * moonIcon: "<svg ...>...</svg>",
16
+ * },
17
+ * };
18
+ */
19
+ (function () {
20
+ /**
21
+ * @typedef {{ enable: string; disable: string }} DarkModeMessages
22
+ * @typedef {{
23
+ * storageKey?: string;
24
+ * messages?: Record<string, Partial<DarkModeMessages>>;
25
+ * sunIcon?: string | SVGElement;
26
+ * moonIcon?: string | SVGElement;
27
+ * }} DarkModeConfig
28
+ */
29
+
30
+ const DEFAULTS = {
31
+ storageKey: "color-scheme",
32
+ /** @type {Record<string, DarkModeMessages>} */
33
+ messages: {
34
+ en: { enable: "Enable dark mode", disable: "Disable dark mode" },
35
+ pl: { enable: "Włącz tryb nocny", disable: "Wyłącz tryb nocny" },
36
+ },
37
+ };
38
+ const DARK_CLASS = "dark-mode";
39
+ const STORAGE_DARK = "dark";
40
+ const STORAGE_LIGHT = "light";
41
+ const SVG_NS = "http://www.w3.org/2000/svg";
42
+
43
+ const docsifyHost = /** @type {Window & { $docsify?: { darkMode?: DarkModeConfig } }} */ (
44
+ window
45
+ );
46
+ /** @type {DarkModeConfig} */
47
+ const userConfig = docsifyHost.$docsify?.darkMode || {};
48
+ const storageKey = userConfig.storageKey || DEFAULTS.storageKey;
49
+ /** @type {Record<string, DarkModeMessages>} */
50
+ const messages = { ...DEFAULTS.messages };
51
+ if (userConfig.messages) {
52
+ for (const [loc, msgs] of Object.entries(userConfig.messages)) {
53
+ messages[loc] = { ...(messages[loc] || messages.en), ...msgs };
54
+ }
55
+ }
56
+
57
+ const storage = {
58
+ get() {
59
+ try {
60
+ return localStorage.getItem(storageKey);
61
+ } catch {
62
+ return null;
63
+ }
64
+ },
65
+ set(/** @type {string} */ value) {
66
+ try {
67
+ localStorage.setItem(storageKey, value);
68
+ } catch {}
69
+ },
70
+ };
71
+
72
+ /**
73
+ * @param {string} tag
74
+ * @param {Record<string, string>} attrs
75
+ * @param {SVGElement[]} [children]
76
+ */
77
+ function svgEl(tag, attrs, children) {
78
+ const el = document.createElementNS(SVG_NS, tag);
79
+ for (const [name, value] of Object.entries(attrs)) {
80
+ el.setAttribute(name, value);
81
+ }
82
+ if (children) {
83
+ for (const child of children) el.appendChild(child);
84
+ }
85
+ return el;
86
+ }
87
+
88
+ /**
89
+ * @param {SVGElement[]} children
90
+ */
91
+ function buildIcon(children) {
92
+ return svgEl(
93
+ "svg",
94
+ {
95
+ viewBox: "0 0 24 24",
96
+ fill: "none",
97
+ stroke: "currentColor",
98
+ "stroke-width": "2",
99
+ "stroke-linecap": "round",
100
+ "stroke-linejoin": "round",
101
+ "aria-hidden": "true",
102
+ },
103
+ children,
104
+ );
105
+ }
106
+
107
+ const defaultSunIcon = buildIcon([
108
+ svgEl("circle", { cx: "12", cy: "12", r: "4", fill: "currentColor" }),
109
+ svgEl("line", { x1: "12", y1: "2", x2: "12", y2: "4" }),
110
+ svgEl("line", { x1: "12", y1: "20", x2: "12", y2: "22" }),
111
+ svgEl("line", { x1: "2", y1: "12", x2: "4", y2: "12" }),
112
+ svgEl("line", { x1: "20", y1: "12", x2: "22", y2: "12" }),
113
+ svgEl("line", { x1: "4.93", y1: "4.93", x2: "6.34", y2: "6.34" }),
114
+ svgEl("line", { x1: "17.66", y1: "17.66", x2: "19.07", y2: "19.07" }),
115
+ svgEl("line", { x1: "4.93", y1: "19.07", x2: "6.34", y2: "17.66" }),
116
+ svgEl("line", { x1: "17.66", y1: "6.34", x2: "19.07", y2: "4.93" }),
117
+ ]);
118
+
119
+ const defaultMoonIcon = buildIcon([
120
+ svgEl("path", { d: "M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" }),
121
+ ]);
122
+
123
+ /**
124
+ * @param {unknown} input
125
+ * @param {SVGElement} fallback
126
+ */
127
+ function parseIcon(input, fallback) {
128
+ if (input instanceof SVGElement) return input;
129
+ if (typeof input === "string" && input.trim()) {
130
+ const doc = new DOMParser().parseFromString(input.trim(), "image/svg+xml");
131
+ const root = doc.documentElement;
132
+ if (root && root.nodeName.toLowerCase() === "svg" && !doc.querySelector("parsererror")) {
133
+ return /** @type {SVGElement} */ (/** @type {unknown} */ (root));
134
+ }
135
+ }
136
+ return fallback;
137
+ }
138
+
139
+ const sunIcon = parseIcon(userConfig.sunIcon, defaultSunIcon);
140
+ const moonIcon = parseIcon(userConfig.moonIcon, defaultMoonIcon);
141
+
142
+ const rawLocale = document.documentElement.lang || navigator.language || "en";
143
+ const locale = rawLocale.toLowerCase().split("-")[0];
144
+ const i18n = messages[locale] || messages.en;
145
+ const darkQuery = window.matchMedia?.("(prefers-color-scheme: dark)");
146
+
147
+ function isDarkActive() {
148
+ return document.documentElement.classList.contains(DARK_CLASS);
149
+ }
150
+
151
+ function resolveInitialDark() {
152
+ const stored = storage.get();
153
+ if (stored !== null) return stored === STORAGE_DARK;
154
+ return !!darkQuery?.matches;
155
+ }
156
+
157
+ /**
158
+ * @param {HTMLButtonElement} button
159
+ * @param {boolean} isDark
160
+ */
161
+ function render(button, isDark) {
162
+ document.documentElement.classList.toggle(DARK_CLASS, isDark);
163
+ const label = isDark ? i18n.disable : i18n.enable;
164
+ button.replaceChildren(isDark ? sunIcon : moonIcon);
165
+ button.setAttribute("aria-label", label);
166
+ button.setAttribute("title", label);
167
+ }
168
+
169
+ function createButton() {
170
+ const btn = document.createElement("button");
171
+ btn.className = "dark-mode-toggle";
172
+ btn.type = "button";
173
+ return btn;
174
+ }
175
+
176
+ function init() {
177
+ const existing = document.querySelector(".dark-mode-toggle");
178
+ const button =
179
+ existing instanceof HTMLButtonElement ? existing : createButton();
180
+ if (!existing) document.body.appendChild(button);
181
+
182
+ render(button, resolveInitialDark());
183
+
184
+ button.addEventListener("click", () => {
185
+ const next = !isDarkActive();
186
+ storage.set(next ? STORAGE_DARK : STORAGE_LIGHT);
187
+ render(button, next);
188
+ });
189
+
190
+ darkQuery?.addEventListener("change", (event) => {
191
+ if (storage.get() === null) render(button, event.matches);
192
+ });
193
+ }
194
+
195
+ if (document.readyState === "loading") {
196
+ document.addEventListener("DOMContentLoaded", init);
197
+ } else {
198
+ init();
199
+ }
200
+ })();
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "docsify-dark-mode-toggle",
3
+ "description": "🔨 Dark mode toggle button for Docsify — vanilla JS, no dependencies, configurable storage key, i18n, custom SVG icons.",
4
+ "version": "1.0.0",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "Piotr Kowalski",
8
+ "email": "piecioshka@gmail.com",
9
+ "url": "https://piecioshka.pl/"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/piecioshka/docsify-dark-mode-toggle.git"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/piecioshka/docsify-dark-mode-toggle/issues"
17
+ },
18
+ "files": [
19
+ "index.js",
20
+ "index.css",
21
+ "CHANGELOG.md"
22
+ ],
23
+ "keywords": [
24
+ "docsify",
25
+ "docsify-plugin",
26
+ "dark-mode",
27
+ "dark-theme",
28
+ "toggle",
29
+ "button",
30
+ "theme-switcher",
31
+ "prefers-color-scheme"
32
+ ],
33
+ "main": "index.js",
34
+ "style": "index.css"
35
+ }