Themed.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import * as React from 'react';
  2. import { Text as DefaultText, View as DefaultView } from 'react-native';
  3. import Colors from '../constants/Colors';
  4. import useColorScheme from '../hooks/useColorScheme';
  5. export function useThemeColor(
  6. props: { light?: string; dark?: string },
  7. colorName: keyof typeof Colors.light & keyof typeof Colors.dark
  8. ) {
  9. const theme = useColorScheme();
  10. const colorFromProps = props[theme];
  11. if (colorFromProps) {
  12. return colorFromProps;
  13. } else {
  14. return Colors[theme][colorName];
  15. }
  16. }
  17. interface ThemeProps {
  18. lightColor?: string;
  19. darkColor?: string;
  20. }
  21. export type TextProps = ThemeProps & DefaultText['props'];
  22. export type ViewProps = ThemeProps & DefaultView['props'];
  23. export function Text(props: TextProps) {
  24. const { style, lightColor, darkColor, ...otherProps } = props;
  25. const color = useThemeColor({ light: lightColor, dark: darkColor }, 'text');
  26. return <DefaultText style={[{ color }, style]} {...otherProps} />;
  27. }
  28. export function View(props: ViewProps) {
  29. const { style, lightColor, darkColor, ...otherProps } = props;
  30. const backgroundColor = useThemeColor({ light: lightColor, dark: darkColor }, 'background');
  31. return <DefaultView style={[{ backgroundColor }, style]} {...otherProps} />;
  32. }