Code Monkey home page Code Monkey logo

react-hashtag's Introduction

React Hashtag

Enhance your strings with live hashtag components.

npm version codecov.io Code Coverage Build Status gzip size

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.

react-hashtag's People

Contributors

cristianbote avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.