Skip to content

Instantly share code, notes, and snippets.

@intergalacticspacehighway
Last active May 17, 2024 15:08
Show Gist options
  • Save intergalacticspacehighway/5348b3461f269996681812ec5da821b5 to your computer and use it in GitHub Desktop.
Save intergalacticspacehighway/5348b3461f269996681812ec5da821b5 to your computer and use it in GitHub Desktop.
Collapsible text using reanimated
import { useEffect, useRef } from "react";
import { View } from "react-native";
import Animated, {
useAnimatedStyle,
useSharedValue,
} from "react-native-reanimated";
// Example
export default function App() {
return (
<CollapsibleText collapsedNumberOfLines={4}>
Lorem Ipsum is simply text of the printing and typesetting industry. Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a
type specimen book. It has survived not only five centuries, but also the
leap into electronic typesetting, remaining essentially unchanged. It was
popularised in the 1960s with the release of Letraset sheets containing
Lorem Ipsum passages, and more recently with desktop publishing software
like Aldus PageMaker including versions of Lorem Ipsum.
</CollapsibleText>
);
}
const CollapsibleText = (props: {
children: string;
collapsedNumberOfLines: number;
}) => {
const { children, collapsedNumberOfLines } = props;
const expandedTextHeight = useSharedValue(0);
const collapsedTextHeight = useSharedValue(0);
const containerViewHeight = useSharedValue(0);
const textStyle = useAnimatedStyle(() => {
return {
opacity: collapsedTextHeight.value ? 1 : 0,
flex: 1,
};
}, []);
const readMoreStyle = useAnimatedStyle(() => {
return {
opacity: containerViewHeight.value === collapsedTextHeight.value ? 1 : 0,
fontWeight: "bold",
};
}, []);
const readLessStyle = useAnimatedStyle(() => {
return {
opacity: containerViewHeight.value === expandedTextHeight.value ? 1 : 0,
fontWeight: "bold",
};
}, []);
const containerViewStyle = useAnimatedStyle(() => {
return {
height: containerViewHeight.value,
overflow: "hidden",
// Use flexWrap: "wrap" if you don't want words to be cut off
// flexWrap: "wrap",
flexDirection: "row",
};
}, []);
useEffect(() => {
// reset
containerViewHeight.value = 0;
expandedTextHeight.value = 0;
collapsedTextHeight.value = 0;
}, [
children,
collapsedNumberOfLines,
expandedTextHeight,
collapsedTextHeight,
containerViewHeight,
]);
return (
<Animated.View
style={containerViewStyle}
onTouchEnd={() => {
if (containerViewHeight.value === expandedTextHeight.value) {
containerViewHeight.value = collapsedTextHeight.value;
} else {
containerViewHeight.value = expandedTextHeight.value;
}
}}
>
<Animated.Text
style={textStyle}
onTextLayout={(e) => {
if (collapsedTextHeight.value === 0) {
let expandedHeight = 0;
let collapsedHeight = 0;
for (let i = 0; i < e.nativeEvent.lines.length; i++) {
expandedHeight += e.nativeEvent.lines[i].height;
if (
i <=
Math.min(
collapsedNumberOfLines - 1,
e.nativeEvent.lines.length - 1
)
) {
collapsedHeight += e.nativeEvent.lines[i].height;
}
}
expandedTextHeight.value = expandedHeight;
collapsedTextHeight.value = collapsedHeight;
containerViewHeight.value = collapsedHeight;
}
}}
>
{children}
<Animated.Text style={readLessStyle}> less</Animated.Text>
</Animated.Text>
<View
style={{
justifyContent: "flex-end",
height: "100%",
}}
>
<Animated.Text style={readMoreStyle}>... more</Animated.Text>
</View>
</Animated.View>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment