|
1 | 1 | import React from "react";
|
2 |
| -import { useMemo } from "react"; |
| 2 | +import { useMemo, useLayoutEffect, useState, useCallback } from "react"; |
3 | 3 | import { useLocation } from "react-router-dom";
|
4 | 4 | import logoWide from "src/assets/svg/logo-wide.svg";
|
5 | 5 | import logoSquare from "src/assets/svg/logo-square.svg";
|
@@ -41,6 +41,26 @@ export function TopBar({ version, links }: TopBarProps): JSX.Element {
|
41 | 41 |
|
42 | 42 | const { localize } = useLocale();
|
43 | 43 |
|
| 44 | + const [isDark, setIsDark] = useState(() => { |
| 45 | + const savedTheme = localStorage.getItem("theme"); |
| 46 | + const isDark = savedTheme === "dark"; |
| 47 | + return savedTheme ? isDark : true; // default should be dark |
| 48 | + }); |
| 49 | + |
| 50 | + useLayoutEffect(() => { |
| 51 | + const theme = localStorage.getItem("theme"); |
| 52 | + if (theme) { |
| 53 | + document.documentElement.setAttribute("data-theme", theme); |
| 54 | + } |
| 55 | + }, []); |
| 56 | + |
| 57 | + const toggleTheme = useCallback(() => { |
| 58 | + const newTheme = isDark ? "light" : "dark"; |
| 59 | + setIsDark(!isDark); |
| 60 | + localStorage.setItem("theme", newTheme); |
| 61 | + document.documentElement.setAttribute("data-theme", newTheme); |
| 62 | + }, [isDark, setIsDark]); |
| 63 | + |
44 | 64 | return (
|
45 | 65 | <div className="bg-neutral">
|
46 | 66 | <div className="m-auto flex max-w-7xl flex-row gap-4 p-4 items-center">
|
@@ -113,10 +133,12 @@ export function TopBar({ version, links }: TopBarProps): JSX.Element {
|
113 | 133 | <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
|
114 | 134 | </svg>
|
115 | 135 | <input
|
| 136 | + onClick={toggleTheme} |
116 | 137 | id="theme-toggle"
|
117 | 138 | type="checkbox"
|
118 | 139 | value="dzcodeLight"
|
119 | 140 | className="theme-controller toggle"
|
| 141 | + checked={!isDark} |
120 | 142 | />
|
121 | 143 | <svg
|
122 | 144 | xmlns="http://www.w3.org/2000/svg"
|
|
0 commit comments