npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-mention-hashtag

v1.0.15

Published

React Native Mention and Hashtag Text Component

Downloads

45

Readme

React Native Mention Hashtag

This package allows you to capture mention (tagging) and hashtag elements within text in React Native applications. When a mention or hashtag is detected within the text, it calls the specified callback functions, providing customized functionality to the user.

React Native Mention Hashtag Screenshot

How to Use

Installation

You can install the package in your project using npm or yarn:

npm install react-native-mention-hashtag

or

yarn add react-native-mention-hashtag

Usage

This package provides two components named MentionHashtagText and MentionHashtagProvider. The provider component provides necessary properties such as onMentionPress and onHashtagPress, and these properties are passed to all MentionHashtagText components. However, the provider component is optional, and the MentionHashtagText component can be used directly without any component.

import React from "react";
import { View } from "react-native";
import {
  MentionHashtagProvider,
  MentionHashtagText,
} from "react-native-mention-hashtag";

const MyComponent = () => {
  const handleMentionPress = (mention: string) => {
    console.log("Mention pressed:", mention);
  };

  const handleHashtagPress = (hashtag: string) => {
    console.log("Hashtag pressed:", hashtag);
  };

  return (
    <MentionHashtagProvider
      onMentionPress={handleMentionPress}
      onHashtagPress={handleHashtagPress}
    >
      <View>
        <MentionHashtagText>Hello @world #reactnative</MentionHashtagText>
      </View>
    </MentionHashtagProvider>
  );
};

export default MyComponent;

Props


| Prop | Description | Default | Example Usage | | ------------------ | -------------------------------------------------- | ----------- | ------------------------------------------- | | onMentionPress | Callback function called when a mention is pressed | undefined | (mention: string) => console.log(mention) | | onHashtagPress | Callback function called when a hashtag is pressed | undefined | (hashtag: string) => console.log(hashtag) | | minHashtagLength | Minimum length of a hashtag | 1 | 3 | | minMentionLength | Minimum length of a mention | 1 | 5 | | mentionTextStyle | Style object for mention text | {} | { color: 'blue' } | | hashtagTextStyle | Style object for hashtag text | {} | { color: 'green' } |

The MentionHashtagText component inherits all props of the Text component in React Native, such as style, numberOfLines, onLayout, etc.

Examples

Basic Usage

<MentionHashtagText
  onMentionPress={(mention) => console.log("Mention pressed:", mention)}
  onHashtagPress={(hashtag) => console.log("Hashtag pressed:", hashtag)}
>
  Hello @world #reactnative
</MentionHashtagText>

Applying Styles

<MentionHashtagText
  onMentionPress={(mention) => console.log("Mention pressed:", mention)}
  onHashtagPress={(hashtag) => console.log("Hashtag pressed:", hashtag)}
  mentionTextStyle={{ fontWeight: "bold", color: "blue" }}
  hashtagTextStyle={{ fontStyle: "italic", color: "green" }}
>
  Hello @world #reactnative
</MentionHashtagText>

Setting Minimum Lengths

<MentionHashtagText
  onMentionPress={(mention) => console.log("Mention pressed:", mention)}
  onHashtagPress={(hashtag) => console.log("Hashtag pressed:", hashtag)}
  minMentionLength={3}
  minHashtagLength={2}
>
  Hello @world #reactnative
</MentionHashtagText>

The code of the example in the simulator image above:

import { StyleSheet, View } from "react-native";
import {
  MentionHashtagProvider,
  MentionHashtagText,
} from "react-native-mention-hashtag";

export default function App() {
  return (
    <View style={styles.container}>
      <MentionHashtagProvider
        onHashtagPress={(hashtag) => console.log(hashtag)}
        onMentionPress={(mention) => console.log(mention)}
        /**
         * Custom styles for MentionHashtagText.
         */
        style={{ color: "gray", fontWeight: "400" }}
        /**
         * Custom styles for Mention.
         */
        mentionTextStyle={{
          fontWeight: "500",
          color: "#4073ff",
        }}
        /**
         * Custom styles for Hashtag.
         */
        hashtagTextStyle={{
          fontWeight: "500",
          color: "#181818",
        }}
        minHashtagLength={5}
        minMentionLength={5}
      >
        <MentionHashtagText>
          Hello from #react-native-mention-hashtag! This is a #hashtag and this
          is a @mention.
        </MentionHashtagText>
        <MentionHashtagText>
          Invalid hash#tag. Because minHashtagLength=5.
        </MentionHashtagText>
        <MentionHashtagText>
          Invalid ment@ion. Because minMentionLength=5.
        </MentionHashtagText>
        <MentionHashtagText>
          #combined#hashtag but can be clicked separately.
        </MentionHashtagText>
        <MentionHashtagText>
          @combined@mention but can be clicked separately.
        </MentionHashtagText>
      </MentionHashtagProvider>
      <MentionHashtagText>
        #hashtag and @mention used outside the provider. These are clickable,
        but because they are outside the provider, common styles are not
        applied.
      </MentionHashtagText>
      <MentionHashtagText
        mentionTextStyle={{
          fontWeight: "500",
          color: "red",
        }}
        hashtagTextStyle={{
          color: "green",
        }}
        style={{
          fontWeight: "200",
        }}
      >
        However, you can also apply custom styles for @mention and #hashtag,
        which are here.
      </MentionHashtagText>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
    alignItems: "flex-start",
    paddingHorizontal: 20,
    gap: 16,
  },
});

These examples demonstrate different usage scenarios and features of the package. Feel free to apply these examples to your own project to better understand the package.