|
| 1 | +import { useDirection, useScreenInfo } from '@/hooks' |
| 2 | +import { RootState } from '@/store' |
| 3 | +import React from 'react' |
| 4 | +import { Text, TextProps } from 'react-native' |
| 5 | +import { useSelector } from 'react-redux' |
| 6 | + |
| 7 | +interface StyledTextProps extends TextProps { |
| 8 | + variant?: 'h1' | 'h2' | 'body' | 'button' |
| 9 | + color?: string |
| 10 | + textAlign?: 'left' | 'center' | 'right' |
| 11 | +} |
| 12 | + |
| 13 | +export const StyledText: React.FC<StyledTextProps> = ({ |
| 14 | + children, |
| 15 | + variant = 'body', |
| 16 | + color = 'text', |
| 17 | + textAlign, |
| 18 | + style, |
| 19 | + className = '', |
| 20 | + ...props |
| 21 | +}) => { |
| 22 | + const { isSmallScreen } = useScreenInfo() |
| 23 | + const { isRTL } = useDirection() |
| 24 | + const theme = useSelector((state: RootState) => state.theme.theme) |
| 25 | + |
| 26 | + const getTextAlign = () => { |
| 27 | + if (textAlign) return textAlign |
| 28 | + |
| 29 | + return isSmallScreen ? 'center' : isRTL ? 'right' : 'left' |
| 30 | + } |
| 31 | + |
| 32 | + const getColor = () => { |
| 33 | + switch (color) { |
| 34 | + case 'primary': |
| 35 | + return theme.primaryColor |
| 36 | + case 'link': |
| 37 | + return theme.linkColor |
| 38 | + case 'primaryButton': |
| 39 | + return theme.buttonPrimaryColor |
| 40 | + case 'secondaryButton': |
| 41 | + return theme.buttonSecondaryColor |
| 42 | + case 'text': |
| 43 | + return theme.textColor |
| 44 | + case 'yellow': |
| 45 | + return theme.yellowColor |
| 46 | + default: |
| 47 | + return theme.textColor |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + const getVariantClasses = () => { |
| 52 | + switch (variant) { |
| 53 | + case 'h1': |
| 54 | + return `${isSmallScreen ? 'text-[18px] leading-[21px]' : 'text-[42px] leading-[50px]'}` |
| 55 | + case 'h2': |
| 56 | + return 'text-[24px] leading-[29px]' |
| 57 | + default: |
| 58 | + return '' |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return ( |
| 63 | + <Text |
| 64 | + className={`font-inter ${getVariantClasses()} ${className}`} |
| 65 | + style={[ |
| 66 | + { |
| 67 | + fontFamily: 'Inter', |
| 68 | + textAlign: getTextAlign(), |
| 69 | + color: getColor(), |
| 70 | + }, |
| 71 | + style, |
| 72 | + ]} |
| 73 | + {...props} |
| 74 | + > |
| 75 | + {children} |
| 76 | + </Text> |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +export default StyledText |
0 commit comments