import Styles from "@/constants/Styles"; import { useTheme } from "@/providers/ThemeProvider"; import { MaterialCommunityIcons } from "@expo/vector-icons"; import { useRef, useState, useEffect } from "react"; import { Animated, Pressable, View } from "react-native"; import Text from "./Text"; export default function TextExpandable({ content, maxLines }: { content: string, maxLines: number }) { const { colors } = useTheme(); const [isExpanded, setIsExpanded] = useState(false); const [shouldShowMore, setShouldShowMore] = useState(false); const [collapsedHeight, setCollapsedHeight] = useState(0); const [fullHeight, setFullHeight] = useState(0); const animatedHeight = useRef(new Animated.Value(0)).current; useEffect(() => { setCollapsedHeight(0); setFullHeight(0); setShouldShowMore(false); setIsExpanded(false); }, [content]); const measureCollapsed = (e: any) => { const h = e.nativeEvent.layout.height; setCollapsedHeight(h); animatedHeight.setValue(h); }; const measureFull = (e: any) => { setFullHeight(e.nativeEvent.layout.height); }; useEffect(() => { if (collapsedHeight > 0 && fullHeight > 0) { setShouldShowMore(fullHeight > collapsedHeight + 1); } }, [collapsedHeight, fullHeight]); const toggleExpand = () => { Animated.timing(animatedHeight, { toValue: isExpanded ? collapsedHeight : fullHeight, duration: 300, useNativeDriver: false, }).start(); setIsExpanded(!isExpanded); }; return ( {content} {content} {content} {shouldShowMore && ( {isExpanded ? 'Sembunyikan' : 'Lihat selengkapnya'} )} ); };