react-native-read-more

repository·master·Indexed 43 days ago

https://github.com/fawaz-ahmed/react-native-read-more

A React Native library for displaying large blocks of text in a condensed format with 'See more' and 'See less' toggles. Version 3.0.4 provides a pure JS solution compatible with React Native CLI and Expo across all platforms, featuring a ReadMore component that calculates positioning to keep toggles within the same paragraph.

Tokens
1.4K
Snippets
5
Records
8
Agent score
5%

What's inside react-native-read-more

  1. Install @fawazahmed/react-native-read-more

    master

    Install the library using npm or yarn. This is a pure JS solution that works with React Native (CLI or Expo) on all platforms and does not require native dependencies.

    npm i @fawazahmed/react-native-read-more --save
    # or
    yarn add @fawazahmed/react-native-read-more
  2. Troubleshoot Android text overlap and scaling

    master

    If you encounter issues on Android, try the following:

    • See More always visible: If See more is always shown on Android, pass the prop allowFontScaling={false}.
    • Text Overlap (numberOfLines={1}): On Android, passing numberOfLines={1} causes text to overlap. To fix this, use a numberOfLines value greater than 1.
    • Nested Components: If you have nested components other than Text, ensure you are handling layout correctly (refer to library issues for specific edge cases).
  3. Basic Usage of ReadMore component

    master

    Import ReadMore from @fawazahmed/react-native-read-more and wrap your text content. You can specify the number of lines to show before the text is condensed using the numberOfLines prop.

    import React from 'react';
    import {SafeAreaView, StyleSheet, View} from 'react-native';
    import ReadMore from '@fawazahmed/react-native-read-more';
    
    const Home = () => {
      return (
        <SafeAreaView style={styles.safe}>
          <View style={styles.root}>
            <ReadMore numberOfLines={3} style={styles.textStyle}>
              {
                "Lorem Ipsum is simply dummy text of the printing and typesetting industry..."
              }
            </ReadMore>
          </View>
        </SafeAreaView>
      );
    };
    
    const styles = StyleSheet.create({
      safe: { flex: 1 },
      root: { flex: 1, padding: 16 },
      textStyle: { fontSize: 14 },
    });
    
    export default Home;
  4. Configure Metro transformer options

    master

    The metro.config.js file in the example project defines how the Metro bundler transforms files. It specifically configures getTransformOptions to set inlineRequires to true, which can help improve startup performance by delaying the execution of modules until they are actually required.

    module.exports = {
      transformer: {
        getTransformOptions: async () => ({
          transform: {
            experimentalImportSupport: false,
            inlineRequires: true,
          },
        }),
      },
    };
  5. Reference the ReadMore props

    master

    The ReadMore component accepts several props to customize the behavior and appearance of the expandable text:

    PropTypeDefaultDescription
    styleobject or array-Text style
    seeMoreTextstring'See more'Text for the expansion trigger
    seeMoreStyleobject or array-Text style for seeMoreText
    seeLessTextstring'See less'Text for the collapse trigger
    seeLessStyleobject or array-Text style for seeLessText
    ellipsisstring'...'Ellipsis character
    wrapperStyleobject or array-Style for the wrapper View
    numberOfLinesnumber3Number of lines to show before condensing
    animatebooltrueApplies subtle animation to the toggle text
    customTextComponentReact componentTextCustom component to use for rendering text
    expandOnlyboolfalseIf true, hides the 'See less' option (LinkedIn style)
    onExpandfunc-Callback executed when expanded
    onCollapsefunc-Callback executed when collapsed
    onReadyfunc-Callback when measurements are complete. Receives ({ canExpand, expandedLinesCount })
    collapsedbool-Programmatically control collapsed state
    debugbool-Print debug logs
    seeMoreContainerStyleSecondaryobject-Use { position: 'relative' } to prevent text overlap
    onSeeMorefunc-Custom callback for 'See More' (disables default toggle)
    onSeeLessfunc-Custom callback for 'See Less' (disables default toggle)

    Note: Any additional props are passed down to the underlying Text component.

  6. Use the ReadMore component

    master

    The ReadMore component is the primary entry point for this library. It is exported as a default export and can be used to render text that can be expanded or collapsed based on a specified number of lines.

    import ReadMore from './ReadMore';
    
    // Usage within a component
    <ReadMore>
      Your long text content goes here...
    </ReadMore>