├── .github └── FUNDING.yml ├── .gitignore ├── LICENSE ├── README.md ├── android.gif ├── index.d.ts ├── index.js ├── ios.gif └── package.json /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [nlt2390] 4 | buy_me_a_coffee: nlt23 5 | 6 | 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | npm-debug.log 2 | Example/ 3 | .eslintrc 4 | node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## react-native-view-more-text 2 | 3 | A super lightweight plugin to expand/collapse text in React-Native. Truncated text is ended with dotdotdot. 4 | 5 | Working on IOS/Android 6 | 7 | ![ios](https://raw.githubusercontent.com/nlt2390/react-native-view-more-text/master/ios.gif) 8 | ![android](https://raw.githubusercontent.com/nlt2390/react-native-view-more-text/master/android.gif) 9 | 10 | ### Installation 11 | 12 | ``` 13 | npm install --save react-native-view-more-text || yarn add react-native-view-more-text 14 | 15 | ``` 16 | 17 | ### Usage 18 | 19 | - **numberOfLines**(number)(*required): Number of lines to be displayed. 20 | - **renderViewMore**(object): Render view-more component 21 | - **renderViewLess**(object): Render view-less component 22 | - **afterCollapse**(func): Callback after collapsing 23 | - **afterExpand**(func): Callback after expanding 24 | 25 | - **onTextLayout**(func): onTextLayout function is passed to `Text` inside `ViewMoreText` 26 | (Refer to this [PR#56](https://github.com/nlt2390/react-native-view-more-text/pull/56)) 27 | - **textStyle**([object, array]): Styles is passed to `Text` inside `ViewMoreText` 28 | (Refer to this [PR#8](https://github.com/nlt2390/react-native-view-more-text/pull/8)) 29 | 30 | ```jsx 31 | import ViewMoreText from 'react-native-view-more-text'; 32 | 33 | const Screen = () => { 34 | const renderViewMore = (onPress) => View more; 35 | const renderViewLess = (onPress) => View less; 36 | 37 | return ( 38 | //... 39 | 45 | 46 | Lorem ipsum dolor sit amet, in quo dolorum ponderum, nam veri molestie constituto eu. Eum enim tantas sadipscing ne, ut omnes malorum nostrum cum. Errem populo qui ne, ea ipsum antiopam definitionem eos. 47 | 48 | 49 | //... 50 | ); 51 | } 52 | ``` 53 | 54 | ### Support 55 | Discord: [lelabrador/react-native-view-more-text](https://discord.com/channels/1233795861582975026/1233795861582975029) 56 | -------------------------------------------------------------------------------- /android.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nlt2390/react-native-view-more-text/a28c4cb03008ca26ce0dabdd930e091906dbf12a/android.gif -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import { ViewProperties, StyleProp, TextStyle } from 'react-native'; 3 | 4 | declare module 'react-native-view-more-text' { 5 | 6 | export interface ViewMoreTextProps extends ViewProperties { 7 | numberOfLines: number; 8 | renderViewMore?: (handlePress: () => void) => JSX.Element; 9 | renderViewLess?: (handlePress: () => void) => JSX.Element; 10 | afterCollapse?: () => void; 11 | afterExpand?: () => void; 12 | onTextLayout?: () => void; 13 | textStyle?: StyleProp; 14 | } 15 | 16 | export default class ViewMoreText extends React.Component {} 17 | } 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Text, View, StyleSheet } from 'react-native'; 3 | import PropTypes from 'prop-types'; 4 | 5 | const styles = StyleSheet.create({ 6 | fullTextWrapper: { 7 | opacity: 0, 8 | position: 'absolute', 9 | left: 0, 10 | top: 0, 11 | }, 12 | viewMoreText: { 13 | color: 'blue', 14 | }, 15 | transparent: { 16 | opacity: 0, 17 | }, 18 | }); 19 | 20 | class ViewMoreText extends React.Component { 21 | trimmedTextHeight = null; 22 | fullTextHeight = null; 23 | shouldShowMore = false; 24 | 25 | state = { 26 | isFulltextShown: true, 27 | numberOfLines: this.props.numberOfLines, 28 | } 29 | 30 | hideFullText = () => { 31 | if ( 32 | this.state.isFulltextShown && 33 | this.trimmedTextHeight && 34 | this.fullTextHeight 35 | ) { 36 | this.shouldShowMore = this.trimmedTextHeight < this.fullTextHeight; 37 | this.setState({ 38 | isFulltextShown: false, 39 | }); 40 | } 41 | } 42 | 43 | onLayoutTrimmedText = (event) => { 44 | const { 45 | height, 46 | } = event.nativeEvent.layout; 47 | 48 | this.trimmedTextHeight = height; 49 | this.hideFullText(); 50 | } 51 | 52 | onLayoutFullText = (event) => { 53 | const { 54 | height, 55 | } = event.nativeEvent.layout; 56 | 57 | this.fullTextHeight = height; 58 | this.hideFullText(); 59 | } 60 | 61 | onPressMore = () => { 62 | this.setState({ 63 | numberOfLines: null, 64 | }, () => { 65 | this.props.afterExpand(); 66 | }); 67 | } 68 | 69 | onPressLess = () => { 70 | this.setState({ 71 | numberOfLines: this.props.numberOfLines, 72 | }, () => { 73 | this.props.afterCollapse(); 74 | }); 75 | } 76 | 77 | getWrapperStyle = () => { 78 | if (this.state.isFulltextShown) { 79 | return styles.transparent; 80 | } 81 | return {}; 82 | } 83 | 84 | renderViewMore = () => ( 85 | 89 | View More 90 | 91 | ) 92 | 93 | renderViewLess = () => ( 94 | 98 | View Less 99 | 100 | ) 101 | 102 | renderFooter = () => { 103 | const { 104 | numberOfLines, 105 | } = this.state; 106 | 107 | if (this.shouldShowMore === true) { 108 | if (numberOfLines > 0) { 109 | return (this.props.renderViewMore || this.renderViewMore)(this.onPressMore); 110 | } 111 | return (this.props.renderViewLess || this.renderViewLess)(this.onPressLess); 112 | } 113 | return null; 114 | } 115 | 116 | renderFullText = () => { 117 | if (this.state.isFulltextShown) { 118 | return ( 119 | 120 | {this.props.children} 121 | 122 | ); 123 | } 124 | return null; 125 | } 126 | 127 | render() { 128 | return ( 129 | 130 | 131 | 136 | {this.props.children} 137 | 138 | {this.renderFooter()} 139 | 140 | 141 | {this.renderFullText()} 142 | 143 | ); 144 | } 145 | } 146 | 147 | ViewMoreText.propTypes = { 148 | onTextLayout: PropTypes.func, 149 | renderViewMore: PropTypes.func, 150 | renderViewLess: PropTypes.func, 151 | afterCollapse: PropTypes.func, 152 | afterExpand: PropTypes.func, 153 | numberOfLines: PropTypes.number.isRequired, 154 | textStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]), 155 | }; 156 | 157 | ViewMoreText.defaultProps = { 158 | afterCollapse: () => {}, 159 | afterExpand: () => {}, 160 | textStyle: {}, 161 | }; 162 | 163 | export default ViewMoreText; 164 | -------------------------------------------------------------------------------- /ios.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nlt2390/react-native-view-more-text/a28c4cb03008ca26ce0dabdd930e091906dbf12a/ios.gif -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-view-more-text", 3 | "version": "2.2.0", 4 | "description": "Expand/Collapse long text.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "homepage": "https://github.com/nlt2390/react-native-view-more-text", 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/nlt2390/react-native-view-more-text" 13 | }, 14 | "keywords": [ 15 | "react", 16 | "native", 17 | "view", 18 | "more", 19 | "text", 20 | "react-native-view-more-text" 21 | ], 22 | "author": "nlt2390", 23 | "dependencies": { 24 | "prop-types": "^15.5.7" 25 | }, 26 | "peerDependencies": { 27 | "react": "*", 28 | "react-native": "*", 29 | "prop-types": "^15.5.7" 30 | } 31 | } 32 | --------------------------------------------------------------------------------