Skip to content

Instantly share code, notes, and snippets.

View ipuppyyt's full-sized avatar
🎯
Focused

Dhanush S ipuppyyt

🎯
Focused
View GitHub Profile
@ipuppyyt
ipuppyyt / devicethemeselector.jsx
Last active September 19, 2023 18:58
A React JSX device theme selector function that updates itself upon device theme update. Updates Realtime.
const useThemeDetector = () => {
const getCurrentTheme = () => {
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
return "dark";
}
return "light";
}
const [theme, setTheme] = useState (getCurrentTheme());
document.querySelector("body").setAttribute("data-theme", theme);
localStorage.setItem("theme", theme);