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