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-hashtag

v2.1.2

Published

React hashtags parser with custom renderer and click action on each hashtag.

Downloads

538

Readme

React Hashtag

Enhance your strings with live components.

npm version codecov.io Code Coverage Build Status

Features:

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

Demo

React: https://codesandbox.io/s/qxow0z7v49

Preact: https://codesandbox.io/s/qv8qz89ll9

Quick example

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

// Will become
import ReactHashtag from "react-hashtag";

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

Install

The usual flow

npm install react-hashtag --save

Api

The component ReactHashtag 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 | ---- | ---- | ----------- | renderHashtag(value: String, onClick: Function) | function | Returns the custom element to be renderer instead of a <span>. You can go wild here. | onHashtagClick(value: String, e: Event) | function | The click handler for each hashtags. This will be called with the hashtag value that got clicked.

Examples

Custom renderer

const Card = (props) => (
    <p>
        <ReactHashtag
            renderHashtag={(hashtagValue) => (
                <div className="hashtag">{hashtagValue}</div>
            )}
        >
            {props.children}
        </ReactHashtag>
    </p>
);

With styled components


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

const Card = (props) => (
    <p>
        <ReactHashtag
            renderHashtag={(hashtagValue) => (
                <Hashtag>{hashtagValue}</Hashtag>
            )}
        >
            {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 Hashtags = (props) => (
    <ReactHashtag
        renderHashtag={(hashtagValue) => (
            <StyledHashtag
                href={`/search/${hashtagValue}`}
            >
                {hashtagValue}
            </StyledHashtag>
        )}
    >
        {props.children}
    </ReactHashtag>
);

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

Questions?

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