Skip to content

Commit

Permalink
fix: starry states
Browse files Browse the repository at this point in the history
  • Loading branch information
rxyhn committed Sep 11, 2024
1 parent affa278 commit a9b5c75
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/components/Starry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,31 @@ const Starry = (props: ParticlesProps) => {
setInit(true);
});

const isDarkMode = window.matchMedia(
"(prefers-color-scheme: dark)",
).matches;
setParticleColor(isDarkMode ? "#FFFFFF" : "#000000");

const handleThemeChange = (e: MediaQueryListEvent) => {
setParticleColor(e.matches ? "#FFFFFF" : "#000000");
const updateParticleColor = () => {
const isDarkMode = document.documentElement.classList.contains("dark");
setParticleColor(isDarkMode ? "#FFFFFF" : "#000000");
};

const darkModeMediaQuery = window.matchMedia(
"(prefers-color-scheme: dark)",
);
darkModeMediaQuery.addEventListener("change", handleThemeChange);
updateParticleColor();

const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (
mutation.type === "attributes" &&
mutation.attributeName === "class"
) {
updateParticleColor();
}
});
});

observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ["class"],
});

return () => {
darkModeMediaQuery.removeEventListener("change", handleThemeChange);
observer.disconnect();
};
}, []);

Expand Down Expand Up @@ -88,7 +97,6 @@ const Starry = (props: ParticlesProps) => {
enable: false,
zIndex: 1,
},

fpsLimit: 120,
interactivity: {
events: {
Expand Down

0 comments on commit a9b5c75

Please sign in to comment.