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-mention-parser

v1.1.9

Published

React Mention parser with custom renderer and click action on each mention.

Downloads

8

Readme

React Mention Parser

Enhance your strings with live mention components.

Features:

  • Super small ~430 B
  • Available for React and Preact
  • Custom renderer for each mention
  • Custom 'click' handler for each mention
  • Generic output
  • Drop-in and use it. Your code will not have to adapt to anything.

Quick example

// Your typical 'component'
const Card = () => (
    <p>
        Here goes my card contents with #static text inside
    </p>
);

// Will become
import ReactMentionParser from "react-mention-parser";

const Card = () => (
    <p>
        <ReactMentionParser>
            Here goes my card contents with #static text inside
        </ReactMentionParser>
    </p>
);

Install

The usual flow

npm install react-mention-parser --save

Api

The component ReactMentionParser is actually pretty generic. Is not something that someone can't do in half an hour. But, this one has some generic API that could make you turn.

| Name | Type | Description | ---- | ---- | ----------- | renderMention(value: String, onClick: Function) | function | Returns the custom element to be renderer instead of a <span>. You can go wild here. | onMentionClick(value: String, e: Event) | function | The click handler for each mentions. This will be called with the mention value that got clicked.

Examples

Custom renderer

const Card = (props) => (
    <p>
        <ReactMentionParser
            renderMention={(mentionValue) => (
                <div className="mention">{mentionValue}</div>
            )}
        >
            {props.children}
        </ReactMentionParser>
    </p>
);

With styled components


const Mention = styled.span`
    color: tomato;
`;

const Card = (props) => (
    <p>
        <ReactMentionParser
            renderHashtag={(mentionValue) => (
                <Mention>{mentionValue}</Mention>
            )}
        >
            {props.children}
        </ReactHashtag>
    </p>
);

Reusable or composition

You could reuse the same definition, if that's something you're looking for. The following example uses the anchor and defines a component that will redirect to certain hashtag pages.

const StyledHashtag = styled.a`
    color: tomato;
`;

/**
* Custom component to render the hashtags with a custom renderer
*/
const Mentions = (props) => (
    <ReactMentionParser
        renderMention={(mentionValue) => (
            <StyledHashtag
                href={`/search/${mentionValue}`}
            >
                {mentionValue}
            </StyledHashtag>
        )}
    >
        {props.children}
    </ReactMentionParser>
);

const Card = (props) => (
    <p>
        <ReactMentionParser>
            {props.children}
        </ReactMentionParser>
    </p>
);

Questions?

Feel free to file an issue if you have any questions.