Files
rn-app/components/Themed.tsx

119 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-11-04 13:27:19 +08:00
/**
2025-11-06 16:37:01 +08:00
*
*
* Text View
* settingsStore
2025-11-04 13:27:19 +08:00
*/
2025-11-06 16:37:01 +08:00
import { Text as DefaultText, View as DefaultView, TextStyle } from 'react-native';
2025-11-04 13:27:19 +08:00
2025-11-12 00:13:26 +08:00
import { Colors } from '@/theme';
2025-11-06 16:37:01 +08:00
import { useColorScheme } from '@/hooks/useTheme';
2025-11-04 13:27:19 +08:00
type ThemeProps = {
lightColor?: string;
darkColor?: string;
};
export type TextProps = ThemeProps & DefaultText['props'];
export type ViewProps = ThemeProps & DefaultView['props'];
2025-11-06 16:37:01 +08:00
/**
*
*
* @param props - light dark
* @param colorName - Colors
* @returns
*/
2025-11-04 13:27:19 +08:00
export function useThemeColor(
props: { light?: string; dark?: string },
colorName: keyof typeof Colors.light & keyof typeof Colors.dark
) {
const theme = useColorScheme() ?? 'light';
const colorFromProps = props[theme];
if (colorFromProps) {
return colorFromProps;
} else {
return Colors[theme][colorName];
}
}
2025-11-06 16:37:01 +08:00
/**
* Text
*
*
*/
2025-11-04 13:27:19 +08:00
export function Text(props: TextProps) {
const { style, lightColor, darkColor, ...otherProps } = props;
const color = useThemeColor({ light: lightColor, dark: darkColor }, 'text');
return <DefaultText style={[{ color }, style]} {...otherProps} />;
}
2025-11-06 16:37:01 +08:00
/**
* View
*
*
*/
2025-11-04 13:27:19 +08:00
export function View(props: ViewProps) {
const { style, lightColor, darkColor, ...otherProps } = props;
const backgroundColor = useThemeColor({ light: lightColor, dark: darkColor }, 'background');
return <DefaultView style={[{ backgroundColor }, style]} {...otherProps} />;
}
2025-11-06 16:37:01 +08:00
/**
* Text
*
* title, subtitle, defaultSemiBold, link
*/
export type ThemedTextProps = TextProps & {
type?: 'default' | 'title' | 'defaultSemiBold' | 'subtitle' | 'link';
};
export function ThemedText({ style, type = 'default', ...rest }: ThemedTextProps) {
const color = useThemeColor({}, 'text');
const linkColor = useThemeColor({}, 'tint');
const typeStyles: Record<string, TextStyle> = {
default: {
fontSize: 16,
lineHeight: 24,
},
defaultSemiBold: {
fontSize: 16,
lineHeight: 24,
fontWeight: '600',
},
title: {
fontSize: 32,
fontWeight: 'bold',
lineHeight: 40,
},
subtitle: {
fontSize: 20,
fontWeight: '600',
lineHeight: 28,
},
link: {
fontSize: 16,
lineHeight: 24,
color: linkColor,
},
};
2025-11-13 16:47:10 +08:00
return <Text style={[{ color }, typeStyles[type], style]} {...rest} />;
2025-11-06 16:37:01 +08:00
}
/**
* View
*/
export type ThemedViewProps = ViewProps;
export function ThemedView({ style, ...otherProps }: ThemedViewProps) {
const backgroundColor = useThemeColor({}, 'background');
return <DefaultView style={[{ backgroundColor }, style]} {...otherProps} />;
}