import type { ColorValue, StyleProp, TextStyle } from 'react-native'; import { useColors } from '../../hook'; import { usePaletteContext } from '../../theme'; import { splitStringWithDelimiter } from '../../utils'; import { Text, TextProps } from './Text'; export type HighTextProps = Omit & { keyword: string; content: string; highColors?: ColorValue[]; textColors?: ColorValue[]; containerStyle?: StyleProp; numberOfLines?: number | undefined; }; /** * Highlight keywords. * * **Note** Exceeding the width is not considered. */ export function HighText(props: HighTextProps) { const { containerStyle, numberOfLines } = props; const { getContent } = useHighText2(props); return ( {getContent()} ); } export function useHighText(props: HighTextProps) { const { keyword, content, style, highColors, textColors, ...others } = props; const list = content.split(keyword); const { colors } = usePaletteContext(); const { getColor } = useColors({ high: { light: highColors?.[0] ?? colors.primary[5], dark: highColors?.[1] ?? colors.primary[6], }, text: { light: textColors?.[0] ?? colors.neutral[1], dark: textColors?.[1] ?? colors.primary[98], }, }); const getContent = () => { return list.map((item, index) => { if (item.length === 0) { if (index !== list.length - 1) { return ( {keyword} ); } else { return null; } } else { if (index === list.length - 1) { return ( {item} ); } else { return ( {item} {keyword} ); } } }); }; return { getContent, }; } export function useHighText2(props: HighTextProps) { const { keyword, content, style, highColors, textColors, ...others } = props; const list = splitStringWithDelimiter(content, keyword); const { colors } = usePaletteContext(); const { getColor } = useColors({ high: { light: highColors?.[0] ?? colors.primary[5], dark: highColors?.[1] ?? colors.primary[6], }, text: { light: textColors?.[0] ?? colors.neutral[1], dark: textColors?.[1] ?? colors.primary[98], }, }); const getContent = () => { return list.map((item, index) => { if (item === keyword) { return ( {keyword} ); } else { return ( {item} ); } }); }; return { getContent, }; }