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 / components / DarkModeToggle.js

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

37 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __ } from "@wordpress/i18n";
2 import React, { useEffect, useState } from "react";
3 import { IconSun, IconMoon } from "../../icons/index";
4 import { setDarkMode as persistDarkMode, getInitialDarkMode } from "../utils/darkMode";
5
6 /* Shared dark/light mode switcher — the single source of truth for the admin
7 mode toggle, reused by the main header (Header.js) and the Analytics header
8 (analytics/components/Chrome.js) so they share one icon, markup, behaviour and
9 stylesheet. Persistence is the cookie-based helper in utils/darkMode (the same
10 one the Quick Setup header uses): PHP reads the `betterdocs_admin_dark_mode`
11 cookie on every admin screen and sets the body class server-side, so the choice
12 carries across full-page navigations with no flash and no DB write. State
13 initialises from the live body class / localized config, so a re-mounting host
14 (e.g. the Analytics route) never re-inits from a stale value. */
15 const DarkModeToggle = () => {
16 const [ dark, setDark ] = useState( getInitialDarkMode );
17
18 useEffect( () => {
19 persistDarkMode( dark );
20 }, [ dark ] );
21
22 const onClick = () => setDark( ( prev ) => ! prev );
23
24 return (
25 <button
26 type="button"
27 onClick={ onClick }
28 className="betterdocs-dashboard-header-action betterdocs-dashboard-header-action-mode"
29 aria-label={ __( "Toggle dark mode", "betterdocs" ) }
30 >
31 { dark ? <IconMoon /> : <IconSun /> }
32 </button>
33 );
34 };
35
36 export default DarkModeToggle;
37