├── .npmignore ├── .babelrc ├── bin └── out.gif ├── example ├── babel.config.js ├── package.json ├── App.js ├── .gitignore └── README.md ├── package.json ├── LICENSE ├── .gitignore ├── README.md └── src └── index.js /.npmignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | example/ 3 | src/ 4 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["@babel/preset-env", "@babel/preset-react"] 3 | } 4 | -------------------------------------------------------------------------------- /bin/out.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cawfree/react-native-twitter-textview/HEAD/bin/out.gif -------------------------------------------------------------------------------- /example/babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = function(api) { 2 | api.cache(true); 3 | return { 4 | presets: ['babel-preset-expo'], 5 | }; 6 | }; 7 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "expo": "~39.0.0", 4 | "react": "~16.13.0", 5 | "react-dom": "~16.13.0", 6 | "react-native": "https://github.com/expo/react-native/archive/sdk-39.0.2.tar.gz", 7 | "react-native-twitter-textview": "../", 8 | "react-native-web": "0.13.13" 9 | }, 10 | "devDependencies": { 11 | "@babel/core": "7.9.0" 12 | }, 13 | "scripts": { 14 | "start": "expo start", 15 | "android": "expo start --android", 16 | "ios": "expo start --ios", 17 | "web": "expo web", 18 | "eject": "expo eject" 19 | }, 20 | "private": true 21 | } 22 | -------------------------------------------------------------------------------- /example/App.js: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import { View, Text, TextInput } from "react-native"; 3 | import TwitterTextView from "react-native-twitter-textview"; 4 | 5 | export default function App() { 6 | const [value, onChangeText] = React.useState(''); 7 | return ( 8 | 11 | 18 | 19 | {value} 20 | 21 | 22 | ); 23 | } 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-twitter-textview", 3 | "version": "1.0.7", 4 | "description": "A component for React Native built on top of twitter-text linkification.", 5 | "main": "dist", 6 | "repository": "https://github.com/cawfree/react-native-twitter-textview", 7 | "author": "cawfree ", 8 | "license": "MIT", 9 | "private": false, 10 | "scripts": { 11 | "build": "yarn && babel src --out-dir dist && rm -rf node_modules", 12 | "prettify": "yarn prettier ./src/* --write", 13 | "test": "jest" 14 | }, 15 | "devDependencies": { 16 | "@babel/cli": "^7.12.1", 17 | "@babel/core": "^7.12.3", 18 | "@babel/node": "^7.12.6", 19 | "@babel/polyfill": "^7.12.1", 20 | "@babel/preset-env": "^7.12.1", 21 | "@babel/preset-react": "^7.12.5", 22 | "husky": "^4.1.0", 23 | "jest": "^24.9.0", 24 | "prettier": "^1.19.1", 25 | "react": "^16.12.0", 26 | "react-native": "^0.61.5" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # Xcode 6 | # 7 | build/ 8 | *.pbxuser 9 | !default.pbxuser 10 | *.mode1v3 11 | !default.mode1v3 12 | *.mode2v3 13 | !default.mode2v3 14 | *.perspectivev3 15 | !default.perspectivev3 16 | xcuserdata 17 | *.xccheckout 18 | *.moved-aside 19 | DerivedData 20 | *.hmap 21 | *.ipa 22 | *.xcuserstate 23 | project.xcworkspace 24 | 25 | # Android/IntelliJ 26 | # 27 | build/ 28 | .idea 29 | .gradle 30 | local.properties 31 | *.iml 32 | 33 | # node.js 34 | # 35 | node_modules/ 36 | npm-debug.log 37 | yarn-error.log 38 | 39 | # BUCK 40 | buck-out/ 41 | \.buckd/ 42 | *.keystore 43 | 44 | # fastlane 45 | # 46 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 47 | # screenshots whenever they are needed. 48 | # For more information about the recommended setup visit: 49 | # https://docs.fastlane.tools/best-practices/source-control/ 50 | 51 | */fastlane/report.xml 52 | */fastlane/Preview.html 53 | */fastlane/screenshots 54 | 55 | # Bundle artifacts 56 | *.jsbundle 57 | 58 | # CocoaPods 59 | /ios/Pods/ 60 | 61 | # Expo 62 | .expo/* 63 | web-build/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Alexander Thomas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | # dist 64 | dist/ 65 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # My Universal React Project 2 | 3 |

4 | 5 | 6 | Supports Expo iOS 7 | 8 | 9 | 10 | Supports Expo Android 11 | 12 | 13 | 14 | Supports Expo Web 15 | 16 |

17 | 18 | ## 🚀 How to use 19 | 20 | - Install packages with `yarn` or `npm install`. 21 | - If you have native iOS code run `npx pod-install` 22 | - Run `yarn start` to start the bundler. 23 | - Open the project in a React runtime to try it: 24 | - iOS: [Client iOS](https://itunes.apple.com/app/apple-store/id982107779) 25 | - Android: [Client Android](https://play.google.com/store/apps/details?id=host.exp.exponent&referrer=blankexample) 26 | - Web: Any web browser 27 | 28 | ## Adding Native Code 29 | 30 | This project can be run from a web browser or the Expo client app. You may find that you want to add more native code later on. You can do this by ejecting the project and rebuilding it yourself. 31 | 32 | - Run `yarn eject` to create the native projects. 33 | - You can still run your project in the web browser or Expo client, you just won't be able to access any new native modules you add. 34 | 35 | ## Publishing 36 | 37 | - Deploy the native app to the App store and Play store using this guide: [Deployment](https://docs.expo.io/distribution/app-stores/). 38 | - Deploy the website using this guide: [Web deployment](https://docs.expo.io/distribution/publishing-websites/). 39 | 40 | ## 📝 Notes 41 | 42 | - Learn more about [Universal React](https://docs.expo.io/). 43 | - See what API and components are [available in the React runtimes](https://docs.expo.io/versions/latest/). 44 | - Find out more about developing apps and websites: [Guides](https://docs.expo.io/guides/). 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-twitter-textview 2 | A `` component for [React Native](https://facebook.github.io/react-native) and [React Native Web](https://github.com/necolas/react-native-web) that automatically detects **#hashtags** and **@mentions**. 3 | 4 | code style: prettier 5 | 6 |

7 | 8 |

9 | 10 | ## 🚀 Getting Started 11 | Using [`npm`](https://www.npmjs.com/package/react-native-twitter-textview): 12 | 13 | ``` 14 | npm install --save react-native-twitter-textview 15 | ``` 16 | 17 | Using [`yarn`](https://www.npmjs.com/package/react-native-twitter-textview): 18 | 19 | ``` 20 | yarn add react-native-twitter-textview 21 | ``` 22 | 23 | ## ✍️ Usage 24 | It's super easy; just replace your React Native [``](https://facebook.github.io/react-native/docs/text.html) component with a [``](./TwitterText/src/components/TwitterTextView), and there you go! 25 | 26 | ```javascript 27 | const App = () => { 28 | const [value, onChangeText] = useState(''); 29 | return ( 30 | 33 | 40 | 47 | {value} 48 | 49 | 50 | ); 51 | } 52 | ``` 53 | 54 | ## ✍️ Input 55 | 56 | Are you looking for a similar component for tagged ``? If so, please check out [**react-native-segmented-text-input**](https://github.com/cawfree/react-native-segmented-text-input). 57 | 58 | ## 📋 Props 59 | ----- 60 | Prop | Type | Default | Required | Description 61 | --------------------- | -------- | ------------------------- | -------- | ----------- 62 | children|string|''|No|The text to render. 63 | extractHashtags|bool|true|No|Whether you wish to support hashtags. 64 | onPressHashtag|func|(e, hashtag) => null|No|Called when a detected hashtag is clicked. 65 | hashtagStyle|shape[object Object]|styles.linkStyle|No|Hashtag style. 66 | extractMentions|bool|true|No|Whether you wish to support mentions. 67 | onPressMention|func|(e, hashtag) => null|No|Called when a detected mention is clicked. 68 | mentionStyle|shape[object Object]|styles.linkStyle|No|Mention style. 69 | extractLinks|bool|true|No|Whether you wish to support links. 70 | onPressLink|func|(e, link) => Linking.openURL(link)|No|Called when a detected link is clicked. 71 | linkStyle|shape[object Object]|styles.linkStyle|No|Link style. 72 | extractEmails|bool|true|No|Whether you wish to support emails. 73 | onPressEmail|func|(e, link) => Linking.openURL(link)|No|Called when a detected email is clicked. 74 | emailStyle|shape[object Object]|styles.linkStyle|No|Email style. 75 | 76 | ## ✌️ License 77 | [MIT](https://opensource.org/licenses/MIT) 78 | 79 |

80 | 81 | Buy @cawfree a coffee 82 | 83 |

84 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import PropTypes from "prop-types"; 3 | import { Linking, Platform, Text, StyleSheet, Alert } from "react-native"; 4 | 5 | const styles = StyleSheet.create({ 6 | linkStyle: { 7 | color: "#2980b9" 8 | } 9 | }); 10 | 11 | const PATTERN_HASHTAG = /(^|\s)(#[a-z\d-_]+)/gi; 12 | const PATTERN_MENTION = /(^|\s)(@[a-z\d-_]+)/gi; 13 | const PATTERN_EMAIL = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi; 14 | const PATTERN_URL = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi; 15 | 16 | const matchesWith = (str, pattern) => { 17 | let match = null; 18 | const arr = []; 19 | while ((match = pattern.exec(str)) != null) { 20 | arr.push([match, pattern]); 21 | } 22 | return arr; 23 | }; 24 | 25 | const splitStringByMatches = (str, matches) => { 26 | const arr = []; 27 | let o = 0; 28 | 29 | matches.forEach(([match, pattern]) => { 30 | const { index } = { ...match }; 31 | const text = match[match.length - 1]; 32 | arr.push([str.slice(o, index), null]); 33 | arr.push([str.slice(index, index + text.length + 1), pattern]); 34 | o = index + text.length + 1; 35 | }); 36 | 37 | arr.push([str.slice(o, str.length), null]); 38 | 39 | return arr.filter(([s]) => s.length > 0); 40 | }; 41 | 42 | const TwitterTextView = ({ 43 | children = "", 44 | extractHashtags, 45 | onPressHashtag, 46 | hashtagStyle, 47 | extractMentions, 48 | onPressMention, 49 | mentionStyle, 50 | extractLinks, 51 | onPressLink, 52 | linkStyle, 53 | extractEmails, 54 | onPressEmail, 55 | emailStyle, 56 | ...extraProps 57 | }) => { 58 | const str = (typeof children === "string" && children) || ""; 59 | 60 | const patterns = [ 61 | !!extractHashtags && PATTERN_HASHTAG, 62 | !!extractMentions && PATTERN_MENTION, 63 | !!extractEmails && PATTERN_EMAIL, 64 | !!extractLinks && PATTERN_URL 65 | ].filter(e => !!e); 66 | 67 | const matches = [] 68 | .concat(...patterns.map(pattern => matchesWith(str, pattern))) 69 | .filter(e => !!e) 70 | .sort(([a], [b]) => ({ ...a }.index - { ...b }.index)); 71 | 72 | const onPress = { 73 | [PATTERN_HASHTAG]: onPressHashtag, 74 | [PATTERN_MENTION]: onPressMention, 75 | [PATTERN_EMAIL]: onPressEmail, 76 | [PATTERN_URL]: onPressLink 77 | }; 78 | const style = { 79 | [PATTERN_HASHTAG]: hashtagStyle, 80 | [PATTERN_MENTION]: mentionStyle, 81 | [PATTERN_EMAIL]: emailStyle, 82 | [PATTERN_URL]: linkStyle 83 | }; 84 | 85 | return ( 86 | 87 | {splitStringByMatches(str, matches).map(([str, pattern], i) => { 88 | return ( 89 | { 93 | const handle = onPress[pattern]; 94 | if (handle) { 95 | return handle(e, str); 96 | } 97 | return undefined; 98 | }} 99 | children={str} 100 | /> 101 | ); 102 | })} 103 | 104 | ); 105 | }; 106 | 107 | const textStyleProps = PropTypes.oneOfType([ 108 | PropTypes.shape({}), 109 | PropTypes.number 110 | ]); 111 | 112 | TwitterTextView.propTypes = { 113 | children: PropTypes.string, 114 | extractHashtags: PropTypes.bool, 115 | onPressHashtag: PropTypes.func, 116 | hashtagStyle: textStyleProps, 117 | extractMentions: PropTypes.bool, 118 | onPressMention: PropTypes.func, 119 | mentionStyle: textStyleProps, 120 | extractLinks: PropTypes.bool, 121 | onPressLink: PropTypes.func, 122 | linkStyle: textStyleProps 123 | }; 124 | 125 | TwitterTextView.defaultProps = { 126 | children: "", 127 | extractHashtags: true, 128 | onPressHashtag: (e, hashtag) => { 129 | const msg = `Hashtag: "${hashtag}"`; 130 | if (Platform.OS !== "web") { 131 | Alert.alert(msg); 132 | } else { 133 | console.log(msg); 134 | } 135 | }, 136 | hashtagStyle: styles.linkStyle, 137 | extractMentions: true, 138 | onPressMention: (e, mention) => { 139 | const msg = `Mention: "${mention}"`; 140 | if (Platform.OS !== "web") { 141 | Alert.alert(msg); 142 | } else { 143 | console.log(msg); 144 | } 145 | }, 146 | mentionStyle: styles.linkStyle, 147 | extractLinks: true, 148 | onPressLink: (e, url) => 149 | Linking.canOpenURL(url).then(canOpen => !!canOpen && Linking.openURL(url)), 150 | linkStyle: styles.linkStyle, 151 | extractEmails: true, 152 | onPressEmail: (e, url) => 153 | Linking.canOpenURL(`mailto:${url}`).then( 154 | canOpen => !!canOpen && Linking.openURL(`mailto:${url}`) 155 | ), 156 | emailStyle: styles.linkStyle 157 | }; 158 | 159 | export default TwitterTextView; 160 | --------------------------------------------------------------------------------