Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an invisible text to get the full height instead of 2 renders #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { StyleSheet, Text, View } from "react-native";

export default class ReadMore extends React.Component {
state = {
measured: false,
shouldShowReadMore: false,
showAllText: false
};
Expand All @@ -17,13 +16,7 @@ export default class ReadMore extends React.Component {
}

// Get the height of the text with no restriction on number of lines
const fullHeight = await measureHeightAsync(this._text);
this.setState({ measured: true });
await nextFrameAsync();

if (!this._isMounted) {
return;
}
const fullHeight = await measureHeightAsync(this._invisibleText);

// Get the height of the text now that number of lines has been set
const limitedHeight = await measureHeightAsync(this._text);
Expand All @@ -42,14 +35,23 @@ export default class ReadMore extends React.Component {
}

render() {
let { measured, showAllText } = this.state;
let { showAllText } = this.state;

let { numberOfLines } = this.props;

return (
<View>
<Text
numberOfLines={measured && !showAllText ? numberOfLines : 0}
style={styles.invisible}
ref={text => {
this._invisibleText = text;
}}
>
{this.props.children}
</Text>

<Text
numberOfLines={!showAllText ? numberOfLines : 0}
ref={text => {
this._text = text;
}}
Expand Down Expand Up @@ -110,6 +112,10 @@ function nextFrameAsync() {
}

const styles = StyleSheet.create({
invisible: {
opacity: 0,
position: "absolute"
},
button: {
color: "#888",
marginTop: 5
Expand Down