import Styles from "@/constants/Styles"; 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 [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; const measureCollapsed = (e: any) => { if (collapsedHeight === 0) { setCollapsedHeight(e.nativeEvent.layout.height); animatedHeight.setValue(e.nativeEvent.layout.height); } }; const measureFull = (e: any) => { if (fullHeight === 0) { setFullHeight(e.nativeEvent.layout.height); } }; // Cek apakah memang perlu "View More" useEffect(() => { if (collapsedHeight > 0 && fullHeight > 0) { setShouldShowMore(fullHeight > collapsedHeight + 1); // +1 untuk toleransi float } }, [collapsedHeight, fullHeight]); const toggleExpand = () => { Animated.timing(animatedHeight, { toValue: isExpanded ? collapsedHeight : fullHeight, duration: 300, useNativeDriver: false, }).start(); setIsExpanded(!isExpanded); }; return ( {/* Hidden full text for measurement */} {content} {/* Collapsed text for measurement */} {content} {/* Animated visible text */} {content} {shouldShowMore && ( {isExpanded ? 'View Less' : 'View More'} )} ); };