Skip to content

Commit

Permalink
style: extract logic
Browse files Browse the repository at this point in the history
  • Loading branch information
StarHeartHunt committed Dec 3, 2023
1 parent d151140 commit 0d1856d
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/components/ThemeSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ type Props = {
export default function ThemeSwitcher({ dayIcon, nightIcon }: Props) {
const [theme, setTheme] = useState("light");

function updateClass(mode: string) {
if (mode === "light") document.documentElement.classList.remove("dark");
function updateClass(toTheme: string) {
if (toTheme === "light") document.documentElement.classList.remove("dark");
else document.documentElement.classList.add("dark");
}

const reverseTheme = () => (theme === "dark" ? "light" : "dark");

function syncTheme() {
const toTheme = reverseTheme();
setTheme(toTheme);
localStorage.setItem("starheart-color-scheme", toTheme);
updateClass(toTheme);
}

useEffect(() => {
const toTheme = localStorage.getItem("starheart-color-scheme") || "light";
setTheme(toTheme);
Expand All @@ -28,9 +37,7 @@ export default function ThemeSwitcher({ dayIcon, nightIcon }: Props) {
!window.matchMedia("(prefers-reduced-motion: reduce)").matches;

if (!isAppearanceTransition) {
const toTheme = theme === "dark" ? "light" : "dark";
setTheme(toTheme);
updateClass(toTheme);
syncTheme();
return;
}

Expand All @@ -41,12 +48,7 @@ export default function ThemeSwitcher({ dayIcon, nightIcon }: Props) {
Math.max(y, innerHeight - y),
);
// @ts-expect-error: Transition API
const transition = document.startViewTransition(() => {
const toTheme = theme === "dark" ? "light" : "dark";
setTheme(toTheme);
localStorage.setItem("starheart-color-scheme", toTheme);
updateClass(toTheme);
});
const transition = document.startViewTransition(() => syncTheme());
transition.ready.then(() => {
const clipPath = [
`circle(0px at ${x}px ${y}px)`,
Expand Down

0 comments on commit 0d1856d

Please sign in to comment.