| 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 |
|