Files
rn-app/hooks/useTheme.ts

103 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-11-06 16:37:01 +08:00
/**
* Hooks
2025-11-12 00:13:26 +08:00
*
2025-11-06 16:37:01 +08:00
* 访
*/
import { useMemo } from 'react';
import { useColorScheme as useSystemColorScheme } from 'react-native';
import { useTheme as useThemeStore } from '@/stores';
2025-11-12 00:13:26 +08:00
import { Colors } from '@/theme';
import { ThemeEnum } from '@/constants/theme';
2025-11-06 16:37:01 +08:00
/**
2025-11-12 00:13:26 +08:00
* light | dark | orange
*
2025-11-06 16:37:01 +08:00
* settingsStore
2025-11-12 00:13:26 +08:00
* 'light' | 'dark' | 'orange' | 'auto'
2025-11-06 16:37:01 +08:00
*/
2025-11-12 00:13:26 +08:00
export function useColorScheme(): ThemeEnum {
2025-11-06 16:37:01 +08:00
const userTheme = useThemeStore();
const systemTheme = useSystemColorScheme();
// 如果用户选择了 'auto',则使用系统主题
if (userTheme === 'auto') {
2025-11-12 00:13:26 +08:00
return systemTheme === 'dark' ? ThemeEnum.DARK : ThemeEnum.LIGHT;
2025-11-06 16:37:01 +08:00
}
// 否则使用用户选择的主题
2025-11-12 00:13:26 +08:00
return userTheme as ThemeEnum;
2025-11-06 16:37:01 +08:00
}
/**
*
2025-11-12 00:13:26 +08:00
*
* @param props - { light?: string; dark?: string; orange?: string }
2025-11-06 16:37:01 +08:00
* @param colorName - Colors
* @returns
2025-11-12 00:13:26 +08:00
*
2025-11-06 16:37:01 +08:00
* @example
* ```tsx
* const textColor = useThemeColor({}, 'text');
2025-11-12 00:13:26 +08:00
* const customColor = useThemeColor({ light: '#000', dark: '#fff', orange: '#f90' }, 'text');
2025-11-06 16:37:01 +08:00
* ```
*/
export function useThemeColor(
2025-11-12 00:13:26 +08:00
props: { light?: string; dark?: string; orange?: string },
colorName: keyof typeof Colors.light & keyof typeof Colors.dark & keyof typeof Colors.orange
2025-11-06 16:37:01 +08:00
): string {
const theme = useColorScheme();
const colorFromProps = props[theme];
if (colorFromProps) {
return colorFromProps;
} else {
return Colors[theme][colorName];
}
}
/**
*
2025-11-12 00:13:26 +08:00
*
2025-11-06 16:37:01 +08:00
* @returns
2025-11-12 00:13:26 +08:00
*
2025-11-06 16:37:01 +08:00
* @example
* ```tsx
* const colors = useThemeColors();
* <View style={{ backgroundColor: colors.background }}>
* <Text style={{ color: colors.text }}>Hello</Text>
* </View>
* ```
*/
export function useThemeColors() {
const theme = useColorScheme();
2025-11-12 00:13:26 +08:00
2025-11-06 16:37:01 +08:00
return useMemo(() => {
return Colors[theme];
}, [theme]);
}
/**
*
2025-11-12 00:13:26 +08:00
*
2025-11-06 16:37:01 +08:00
* @returns
2025-11-12 00:13:26 +08:00
*
2025-11-06 16:37:01 +08:00
* @example
* ```tsx
2025-11-12 00:13:26 +08:00
* const { theme, colors, isDark, isLight, isOrange } = useThemeInfo();
2025-11-06 16:37:01 +08:00
* ```
*/
export function useThemeInfo() {
const theme = useColorScheme();
const colors = useThemeColors();
2025-11-12 00:13:26 +08:00
2025-11-06 16:37:01 +08:00
return useMemo(() => ({
theme,
colors,
2025-11-12 00:13:26 +08:00
isDark: theme === ThemeEnum.DARK,
isLight: theme === ThemeEnum.LIGHT,
isOrange: theme === ThemeEnum.ORANGE,
2025-11-06 16:37:01 +08:00
}), [theme, colors]);
}