HyperlinkedText
Text component for React Native with regex defined hyperlinks.
Heavily inspired from react-native-hyperlink.
The difference is that with react-native-hyperlink you use linkify which I couldn't configure to detect arbitrary regex without prefix (e.g. '1:00'). If you only need to detect regexs with prefixes (e.g. mentions with '@' or links with 'schema://') then use hyperlink. react-native-hyperlink also supports nesting <Text>
components.
Important - put only strings inside a <HyperlinkedText>
component. There is no way to nest components right now.
Installation
npm install --save react-native-hyperlinked-text
or
yarn add react-native-hyperlinked-text
Usage Examples
The default behavior is to identify URLs and open web browser when they are clicked:
Configure the default link style and on press behavior:
Pass in linkDefs
array to configure custom regex and behavior:
Props
Prop | Description | Example | Default |
---|---|---|---|
style |
The style of the entire component | style={{backgroundColor:'blue'}} |
None |
linkStyle |
Default style for links. Can be overriden in linkDef.style |
linkStyle={{color: 'purple'}} |
{{color:'#0000EE'}} |
onLinkPress |
Default handler for link presses | onLinkPress={text=>window.alert(text)} |
Open browser |
linkDefs |
Array of linkDef definitions. See below. |
linkDefs=[{regex:/\d+/mgi}] |
[] |
LinkDef
Each link definition is an object with the following properties: