62 lines
1.9 KiB
TypeScript
Executable file
62 lines
1.9 KiB
TypeScript
Executable file
import React from 'react';
|
|
import { Text as PaperText } from 'react-native-paper';
|
|
import { TextProps as RNTextProps } from 'react-native';
|
|
import { StyleSheet } from 'react-native';
|
|
import { interFamilyFor } from '../lib/fonts';
|
|
import { useTheme } from "common-ui";
|
|
import type { Theme } from "common-ui";
|
|
// import { useTheme } from '../hooks/theme-context';
|
|
|
|
// All color keys from theme.colors
|
|
// import type { Theme } from '../hooks/theme-context';
|
|
type ThemeColor = keyof Theme['colors'];
|
|
|
|
type FontWeight = 'light' | 'regular' | 'medium' | 'semibold' | 'bold';
|
|
|
|
/** MyText's `weight` prop -> a flexbox font weight. */
|
|
const WEIGHT_PROP: Record<FontWeight, '300' | '400' | '500' | '600' | '700'> = {
|
|
light: '300',
|
|
regular: '400',
|
|
medium: '500',
|
|
semibold: '600',
|
|
bold: '700',
|
|
};
|
|
|
|
interface Props extends RNTextProps {
|
|
children: React.ReactNode;
|
|
color?: ThemeColor;
|
|
weight?: FontWeight;
|
|
}
|
|
|
|
function MyText({ style, children, color, weight = 'regular', ...rest }: Props) {
|
|
const { colors } = useTheme().theme;
|
|
|
|
let textColor: string | undefined;
|
|
if (color && colors[color]) textColor = colors[color];
|
|
|
|
// The family is picked from whichever weight wins: the `weight` prop or a
|
|
// tw class (`font-bold`) in `style`. The weight itself is removed from the
|
|
// applied style - on Android a custom family plus fontWeight falls back to
|
|
// synthesised/system bold instead of the Inter file we loaded.
|
|
const flat = StyleSheet.flatten(style) || {};
|
|
const { fontWeight: styleWeight, ...styleWithoutWeight } = flat as {
|
|
fontWeight?: string | number;
|
|
[key: string]: unknown;
|
|
};
|
|
const fontFamily = interFamilyFor(styleWeight ?? WEIGHT_PROP[weight]);
|
|
|
|
return (
|
|
<PaperText
|
|
{...rest}
|
|
style={[
|
|
{ color: textColor, fontFamily },
|
|
styleWithoutWeight,
|
|
]}
|
|
allowFontScaling={false}
|
|
>
|
|
{children}
|
|
</PaperText>
|
|
);
|
|
}
|
|
|
|
export default MyText;
|