Skip to content

Commit b21c48f

Browse files
committed
refactor(projects): refactor themeSettings
1 parent 43a0999 commit b21c48f

File tree

6 files changed

+44
-34
lines changed

6 files changed

+44
-34
lines changed

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/features/themeSchema/ThemeProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const DARK_MODE_MEDIA_QUERY = '(prefers-color-scheme: dark)';
1010
const ThemeProvider: FC<PropsWithChildren> = ({ children }) => {
1111
const [themeMode, setThemeMode] = useState<ThemeModeType>((localStg.get('themeMode') as ThemeModeType) || 'light');
1212

13-
const darkMode = useMemo(() => themeMode === 'dark', [themeMode]);
13+
const darkMode = themeMode === 'dark';
1414

1515
function changeThemeMode(mode: ThemeModeType) {
1616
setThemeMode(mode);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { ThemeMode } from 'ahooks/lib/useTheme';
2+
import type { ThemeModeType } from 'ahooks/lib/useTheme';
3+
import type { SegmentedOptions } from 'antd/es/segmented';
4+
import { useContext } from 'react';
5+
6+
import { ThemeContext, icons } from './themeContext';
7+
8+
const ThemeSchemaSegmented = () => {
9+
const { setThemeScheme, themeScheme } = useContext(ThemeContext);
10+
11+
const OPTIONS = Object.values(ThemeMode).map(item => {
12+
const key = item as ThemeModeType;
13+
return {
14+
label: (
15+
<div className="w-[70px] flex justify-center">
16+
<SvgIcon
17+
className="h-28px text-icon-small"
18+
icon={icons[key]}
19+
/>
20+
</div>
21+
),
22+
value: item
23+
};
24+
}) satisfies SegmentedOptions;
25+
26+
return (
27+
<ASegmented
28+
className="bg-layout"
29+
options={OPTIONS}
30+
value={themeScheme}
31+
onChange={setThemeScheme}
32+
/>
33+
);
34+
};
35+
36+
export default ThemeSchemaSegmented;

src/features/themeSchema/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ export * from './themeContext';
22

33
export { default as ThemeProvider } from './ThemeProvider';
44

5+
export { default as ThemeSchemaSegmented } from './ThemeSchemaSegmented';
6+
57
export { default as ThemeSchemaSwitch } from './ThemeSchemaSwitch';

src/layouts/modules/theme-drawer/modules/DarkMode.tsx

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { ThemeMode } from 'ahooks/lib/useTheme';
2-
import type { ThemeModeType } from 'ahooks/lib/useTheme';
3-
import { Segmented, Switch } from 'antd';
4-
import type { SegmentedOptions } from 'antd/es/segmented';
1+
import { Switch } from 'antd';
52

6-
import { ThemeContext, icons } from '@/features';
3+
import { ThemeSchemaSegmented } from '@/features';
74
import {
85
getThemeSettings,
96
setColourWeakness,
@@ -14,33 +11,12 @@ import {
1411
import SettingItem from '../components/SettingItem';
1512
import '@/styles/css/darkMode.css';
1613

17-
const OPTIONS: SegmentedOptions = Object.values(ThemeMode).map(item => {
18-
const key = item as ThemeModeType;
19-
return {
20-
label: (
21-
<div className="w-[70px] flex justify-center">
22-
<SvgIcon
23-
className="h-28px text-icon-small"
24-
icon={icons[key]}
25-
/>
26-
</div>
27-
),
28-
value: item
29-
};
30-
});
31-
3214
const DarkMode = () => {
3315
const { t } = useTranslation();
3416
const dispatch = useAppDispatch();
3517

36-
const { setThemeScheme, themeScheme } = useContext(ThemeContext);
37-
3818
const themeSettings = useAppSelector(getThemeSettings);
3919

40-
function handleSegmentChange(value: string | number) {
41-
setThemeScheme(value as ThemeModeType);
42-
}
43-
4420
function handleGrayscaleChange(value: boolean) {
4521
dispatch(setGrayscale(value));
4622
}
@@ -55,12 +31,7 @@ const DarkMode = () => {
5531
return (
5632
<div className="flex-col-stretch gap-16px">
5733
<div className="i-flex-center">
58-
<Segmented
59-
className="bg-layout"
60-
options={OPTIONS}
61-
value={themeScheme}
62-
onChange={handleSegmentChange}
63-
/>
34+
<ThemeSchemaSegmented />
6435
</div>
6536

6637
<SettingItem

src/types/auto-imports.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ declare global {
2929
const APopover: typeof import('antd')['Popover']
3030
const ARadio: typeof import('antd')['Radio']
3131
const ARow: typeof import('antd')['Row']
32+
const ASegmented: typeof import('antd')['Segmented']
3233
const ASelect: typeof import('antd')['Select']
3334
const ASpace: typeof import('antd')['Space']
3435
const AStatistic: typeof import('antd')['Statistic']

0 commit comments

Comments
 (0)