Created
March 14, 2023 09:52
-
-
Save anhtuank7c/bfc7e12b43aad1f83740815d7c7b13c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { TranslateOptions } from 'i18n-js/typings' | |
import {StyleProp, Text as RNText, TextProps as RNTextProps, TextStyle} from 'react-native' | |
import { TxKeyPath, useI18n } from '../hooks/useI18n' | |
import { ThemeProps, useThemeColor } from './Themed' | |
const presets = { | |
default: {fontSize: 17, lineHeight: 24, fontWeight: 'normal'} as StyleProp<TextStyle>, | |
primary: {fontSize: 17, lineHeight: 24, fontWeight: '600'} as StyleProp<TextStyle>, | |
secondary: {fontSize: 17, lineHeight: 24, fontWeight: 'normal'} as StyleProp<TextStyle>, | |
header: {fontSize: 27, lineHeight: 34, fontWeight: '600' } as StyleProp<TextStyle>, | |
placeholder: {fontSize: 15, lineHeight: 20, fontWeight: '300' } as StyleProp<TextStyle>, | |
}; | |
export type Presets = keyof typeof presets; | |
export type TextProps = RNTextProps & ThemeProps & { | |
tx?: TxKeyPath | |
txOptions?: TranslateOptions | |
preset?: Presets | |
} | |
export function Text(props: TextProps) { | |
const {tx, txOptions, children, darkColor, lightColor, style, ...textProps} = props | |
const i18n = useI18n() | |
const text = typeof tx === 'string' ? i18n.t(tx, txOptions) : children | |
const color = useThemeColor({ light: lightColor, dark: darkColor }, 'text'); | |
const preset: StyleProp<TextStyle> = presets[textProps.preset || 'default']; | |
const textStyle = [{color}, preset, style] | |
return <RNText {...textProps} style={textStyle}>{text}</RNText> | |
} |
<Text preset="primary">Text Primary</Text>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<Text preset="header">Text Header</Text>