Skip to content

Instantly share code, notes, and snippets.

@anhtuank7c
Created March 14, 2023 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhtuank7c/bfc7e12b43aad1f83740815d7c7b13c0 to your computer and use it in GitHub Desktop.
Save anhtuank7c/bfc7e12b43aad1f83740815d7c7b13c0 to your computer and use it in GitHub Desktop.
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>
}
@anhtuank7c
Copy link
Author

anhtuank7c commented Mar 14, 2023

<Text preset="header">Text Header</Text>

@anhtuank7c
Copy link
Author

<Text preset="primary">Text Primary</Text>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment