1.0.7 • Published 1 month ago

react-native-highlighter v1.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

react-native-highlighter

react-native-highlighter

A library for highlight what you want and interact with them

Installation

npm install react-native-highlighter

OR

yarn add react-native-highlighter

Basic Usage

import HighlightedText, { Highlight } from 'react-native-highlighter';

export default function App() {
  const text = `React Native combines the best parts of native development with React, a best-in-class JavaScript library for building user interfaces. You can use React Native today in your existing Android and iOS projects or you can create a whole new app from scratch.`;

  const mainKeywords = new Highlight({
    keywords: ['react native', 'javascript'],
    style: { color: '#6C00FF', fontWeight: 'bold' },
  });

  const extraMarkers = new Highlight({
    keywords: ['user interface', 'highlight'],
    style: { backgroundColor: '#F7DB6A' },
  });

  return (
    <HighlightedText highlights={[mainKeywords, extraMarkers]}>
      {text}
    </HighlightedText>
  );
}
// ...

Full Example

import { StyleSheet, Linking } from 'react-native';
import HighlightedText, { Highlight } from 'react-native-highlighter';

export default function App() {
  const text = `React Native combines the best parts of native development with React, a best-in-class JavaScript library for building user interfaces.
Use a little—or a lot. You can use React Native today in your existing Android and iOS projects or you can create a whole new app from scratch.
For more please visit https://reactnative.dev or read latest posts from @reactnative.

#react #reactnative #javascript`;

  const mainKeywords = new Highlight({
    keywords: ['react native', 'javascript'],
    style: { color: '#6C00FF', fontWeight: 'bold' },
    onPress: (keyword) => Alert.alert(keyword),
  });

  const extraMarkers = new Highlight({
    keywords: ['user interface', 'highlight'],
    style: { backgroundColor: '#F7DB6A' },
    onPress: () => {},
  });

  return (
    <HighlightedText
      highlights={[mainKeywords, extraMarkers]}
      caseSensitive={false}
      hashtags={true}
      hashtagStyle={styles.hashtagStyle}
      mentions={true}
      mentionStyle={styles.mentionStyle}
      onMentionPress={(mention) =>
        Linking.openURL(`https://twitter.com/${mention.replace('@', '')}`)
      }
      emails={true}
      emailStyle={styles.emailStyle}
      onEmailPress={(email) => Linking.openURL(`mailto:${email}`)}
      links={true}
      onLinkPress={(url) => Linking.openURL(url)}
    >
      {text}
    </HighlightedText>
  );
}

const styles = StyleSheet.create({
  hashtagStyle: { color: '#F54291' },
  mentionStyle: { color: '#379237', fontWeight: 'bold' },
  emailStyle: { color: '#FF6D28', fontWeight: 'bold' },
});
// ...

Available props

NameTypeDefaultDescription
highlightsHighlight[]nullArray of Highlight object
caseSensitivebooleanfalseDefines that uppercase and lowercase letters are treated as distinct
styleTextStylenullDefines the style of text
hashtagsbooleanfalseDefines that all hashtags within the text are specified
hashtagStyleTextStyle{ color: 'blue' }Defines the style of hashtags
onHashtagPressfunc() => {}Defines what action to take when pressing on the hashtags
mentionsbooleanfalseDefines that all mentions within the text are specified
mentionStyleTextStyle{ color: 'blue' }Defines the style of mentions
onMentionPressfunc() => {}Defines what action to take when pressing on the mentions
emailsbooleanfalseDefines that all emails within the text are specified
emailStyleTextStyle{ color: 'blue' }Defines the style of emails
onEmailPressfunc() => {}Defines what action to take when pressing on the emails
linksbooleanfalseDefines that all links within the text are specified
linkStyleTextStyle{ color: 'blue' }Defines the style of links
onLinkPressfunc() => {}Defines what action to take when pressing on the links

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

1.0.7

1 month ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago