PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.5
4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 All 203 releases
betterdocs / react-src / admin / utils / darkMode.js

darkMode.js in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.5, at react-src/admin/utils/darkMode.js

33 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Shared admin dark/light mode helper.
3 *
4 * The theme is a CLIENT-SIDE preference stored in the `betterdocs_admin_dark_mode`
5 * cookie — never the database. PHP (Admin::is_dark_mode + body_classes) reads the
6 * same cookie on every BetterDocs admin screen and adds the `betterdocs-dark-mode`
7 * body class server-side, so the choice carries across full-page navigations with
8 * no flash and without touching `betterdocs_settings`.
9 *
10 * Both the dashboard header and the Quick Setup header use this, so the two
11 * switchers behave identically.
12 */
13
14 export const DARK_CLASS = "betterdocs-dark-mode";
15 export const DARK_COOKIE = "betterdocs_admin_dark_mode";
16
17 /** Apply the mode now (this page) and persist it for other admin pages. */
18 export const setDarkMode = (dark) => {
19 document.body.classList.toggle(DARK_CLASS, !!dark);
20 // path=/ so PHP receives it on every wp-admin request; 1-year max-age.
21 document.cookie = `${DARK_COOKIE}=${dark ? "1" : "0"};path=/;max-age=31536000;samesite=lax`;
22 };
23
24 /**
25 * Initial mode for a freshly-mounted switcher. PHP already rendered the body class
26 * (and localized window.betterdocs_admin.dark_mode) from the cookie, so we read
27 * whichever is available — no localStorage, no DB.
28 */
29 export const getInitialDarkMode = () =>
30 Boolean(window.betterdocs_admin?.dark_mode) ||
31 (typeof document !== "undefined" &&
32 document.body.classList.contains(DARK_CLASS));
33