First dark theme commit for command bar

This commit is contained in:
Sakshi Gupta
2025-04-17 19:26:25 +05:30
parent 32576f50d3
commit f2d6bbf54e
9 changed files with 273 additions and 63 deletions

51
src/hooks/useTheme.tsx Normal file
View File

@@ -0,0 +1,51 @@
import { useFluent } from "@fluentui/react-components";
import React, { createContext, FC, ReactNode, useEffect, useState } from "react";
interface ThemeContextType {
theme: "Light" | "Dark";
isDarkMode: boolean;
}
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
interface ThemeProviderProps {
children: ReactNode;
theme: "Light" | "Dark";
}
export const CustomThemeProvider: FC<ThemeProviderProps> = ({ children, theme }) => {
const isDarkMode = theme === "Dark";
return <ThemeContext.Provider value={{ theme, isDarkMode }}>{children}</ThemeContext.Provider>;
};
export const useTheme = () => {
const { targetDocument } = useFluent();
const [isDarkMode, setIsDarkMode] = useState(() => {
const hasDarkMode = targetDocument?.body.classList.contains("isDarkMode") ?? true;
return hasDarkMode;
});
useEffect(() => {
if (!targetDocument) return;
const checkTheme = () => {
const hasDarkMode = targetDocument.body.classList.contains("isDarkMode");
setIsDarkMode(hasDarkMode);
};
// Initial check
checkTheme();
// Create a MutationObserver to watch for class changes
const observer = new MutationObserver((mutations) => {
checkTheme();
});
observer.observe(targetDocument.body, { attributes: true, attributeFilter: ["class"] });
return () => observer.disconnect();
}, [targetDocument]);
return {
isDarkMode
};
};