import React, { FC, useContext, useState } from 'react'; import { Text as RNText, TouchableOpacity, View, ViewStyle, } from 'react-native'; import { Icon } from '../Icon'; import { useScaleSize, Text } from '../Text'; import { useComponentId } from '../Application'; import { ApplicationContext, MiniAppContext } from '../Context'; import { Badge } from '../Badge'; import { Colors } from '../Consts'; import styles from './styles'; import { TitleProps } from './types'; import { Typography } from '../Text/types'; const Title: FC = ({ accessibilityLabel, title = 'Title', type = 'section', size = 'medium', icon, description, iconColor = null, iconAlign = 'top', showRightAction = false, showTrailingAction = false, badgeLabel, buttonTitle, onPressRightAction = () => {}, onPressTrailingAction = () => {}, buttonSize = 'small', textOnly = false, style, }) => { const { theme } = useContext(ApplicationContext); const context = useContext(MiniAppContext); const [badgeWidth, setBadgeWidth] = useState(0); const [contentWidth, setContentWidth] = useState(0); const showBaseLineDebug = context?.features?.showBaseLineDebug ?? false; const size18 = useScaleSize(18); const size22 = useScaleSize(22); // Dynamic styles using useScaleSize hook const dynamicStyles = { card_small: { fontSize: useScaleSize(14), lineHeight: useScaleSize(20), }, card_medium: { fontSize: useScaleSize(16), lineHeight: useScaleSize(22), }, card_large: { fontSize: useScaleSize(18), lineHeight: useScaleSize(26), }, section_medium: { fontSize: useScaleSize(20), lineHeight: useScaleSize(28), }, section_small: { fontSize: useScaleSize(16), lineHeight: useScaleSize(22), }, section_large: { fontSize: useScaleSize(24), lineHeight: useScaleSize(34), }, }; const styleSheet: { [key: string]: any; } = { ...styles, ...dynamicStyles }; const typography = `${type}_${size}`; const isSection = type === 'section'; const numberOfLines = showTrailingAction || !!badgeLabel ? 1 : 2; const buttonTypo: Typography = buttonSize === 'small' ? 'action_xs_bold' : 'action_s_bold'; const flexStyle: ViewStyle = showTrailingAction ? { maxWidth: '95%' } : {}; const componentName = 'Title'; const { componentId } = useComponentId( `${componentName}/${title}`, accessibilityLabel, ); const renderIcon = () => { if (!icon) return null; let iconStyle: ViewStyle = { justifyContent: 'flex-start', }; if (iconAlign === 'middle') { iconStyle = { justifyContent: 'center' }; } if (iconAlign === 'bottom') { iconStyle = { justifyContent: 'flex-end' }; } return ( ); }; const renderContent = () => { return ( { if (e.nativeEvent.layout.width !== contentWidth) { setContentWidth(e.nativeEvent.layout.width); } }} style={[styles.iconLeftView, flexStyle]} > 0 ? contentWidth - badgeWidth : undefined, }, ]} > {title} {badgeLabel && ( { if (e.nativeEvent.layout.width !== badgeWidth) { setBadgeWidth(e.nativeEvent.layout.width); } }} style={{ alignItems: 'center', }} > )} {renderActionLeft()} {description && ( {description} )} ); }; const renderActionLeft = () => { return ( {showTrailingAction && !showRightAction && ( )} ); }; const renderActionRight = () => { if (!showRightAction || showTrailingAction) return false; const lineHeight = styleSheet[typography].lineHeight; return ( {!buttonTitle ? ( ) : ( {buttonTitle} )} ); }; if (textOnly) { return ( {title} ); } return ( {renderIcon()} {renderContent()} {renderActionRight()} ); }; export { Title }; export type { TitleProps };