Skip to content

Commit ff9d6c4

Browse files
committed
chore: format code
1 parent b239d0c commit ff9d6c4

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

packages/react/src/components/theme.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@ import { useEffect, useState } from "react";
66
const storageKey = "theme-index";
77
const darkThemeMatch = window.matchMedia("(prefers-color-scheme: dark)");
88
const list = [<IconDesktop />, <IconSun />, <IconMoon />];
9+
910
const handler = (e: MediaQueryListEvent) => {
10-
if (e.matches) document.body.setAttribute("arco-theme", "dark");
11-
else document.body.removeAttribute("arco-theme");
11+
if (e.matches) {
12+
document.body.setAttribute("arco-theme", "dark");
13+
} else {
14+
document.body.removeAttribute("arco-theme");
15+
}
1216
};
17+
1318
export const ThemeAction: FC = () => {
14-
const [index, setIndex] = useState(Storage.local.get<number>(storageKey) || 0);
19+
const [index, setIndex] = useState(() => Storage.local.get<number>(storageKey) || 0);
1520

1621
useEffect(() => {
1722
switch (index) {
1823
case 0: {
19-
if (darkThemeMatch.matches) document.body.setAttribute("arco-theme", "dark");
20-
else document.body.removeAttribute("arco-theme");
24+
if (darkThemeMatch.matches) {
25+
document.body.setAttribute("arco-theme", "dark");
26+
} else {
27+
document.body.removeAttribute("arco-theme");
28+
}
2129
darkThemeMatch.onchange = handler;
2230
break;
2331
}
@@ -34,11 +42,11 @@ export const ThemeAction: FC = () => {
3442
}
3543
}, [index]);
3644

37-
const changeTheme = () => {
45+
const onChangeTheme = () => {
3846
const nextIndex = (index + 1) % list.length;
3947
setIndex(nextIndex);
4048
Storage.local.set(storageKey, nextIndex);
4149
};
4250

43-
return <div onClick={changeTheme}>{list[index]}</div>;
51+
return <div onClick={onChangeTheme}>{list[index]}</div>;
4452
};

0 commit comments

Comments
 (0)