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

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-hashtag's Issues

Linting error, can't find "preact"

I know it's not a big deal, but I can't clear all my linting errors because I'm getting the following:

Module not found: Can't resolve 'preact' in '{my path}/node_modules/react-hashtag/dist'

Not sure what if anything that can be done.

Encountered two children with the same key, `#hashtag`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

Error message: Encountered two children with the same key, #hashtag. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

Note #hashtag #hashtag in index.js.
image

Feature Request: Allow taking input

This package works perfectly fine when rendering hashtags from a given string.
However, it would be great if it could be used for taking inputs as well! (I wish to use this package to allow the user to add hashtags in a textfield input area).

Not working. Wont render hashtags, just plain #text

`import React from 'react'
import ReactHashtag from "react-hashtag";

export default function Post(props) {

if (props.data.feedImgURL) {
    
    return (
        <MDBJumbotron>
            <div className='mb-4' style={{ display: 'flex', flexDirection: 'row-reverse', marginTop: '-10px' }}>
                <Hamburger key={props.data.docId}/>
            </div>
            <div className='news'>
                <div className="excerpt ml-4 d-flex justify-content-between">
                    <div className='label m-auto align-items-start h-100 mt-0 col-4'>
                        <img
                            src={props.data.Avatar ? props.data.Avatar : defaultDogImg}
                            alt=""
                            className="rounded-circle z-depth-1-half"
                            style={{ width: '75px', height: '75px', objectFit: 'cover', margin: '0 auto' }}
                        />
                        <div className="brief">
                            <Link to={`/user/${props.data.DogID}`} className="name">
                                {props.data.SenderName}
                            </Link> posted a new photo
                    <div className="date">- {moment(props.data.timestamp.toDate()).fromNow()}</div>
                        </div>
                    </div>
                    <div className="added-text my-2 m-auto col-8 align-items-center">
                        <h6><strong><ReactHashtag>{props.data.Content}</ReactHashtag></strong></h6>
                        {<ModalImage small={props.data.feedImgURL} large={props.data.feedImgURL} style={{ width: '350px', borderRadius: '25px' }} />}
                        <div className="feed-footer">
                            <button onClick={handleIncrement} style={{ border: 'none', color: 'red' }} className="like mt-2">
                                <MDBIcon icon="heart" />
                                <span> {props.data.Likes} </span> likes
                            </button>
                        </div>
                        <MDBContainer className='mt-2'>
                            <FacebookShareButton url={`https://www.socialhound.co/user/${props.data.DogID}`} quote={props.data.content}>
                                <FacebookIcon className='mr-1' size={32} round />
                            </FacebookShareButton>
                            <TwitterShareButton url={`localhost:3000/user/${props.data.DogID}`} title={props.data.content}>
                                <TwitterIcon className='mr-1' size={32} round />
                            </TwitterShareButton>
                            <EmailShareButton url={`localhost:3000/user/${props.data.DogID}`} subject={`Email from ${props.data.SenderName}`}>
                                <EmailIcon className='mr-1' size={32} round />
                            </EmailShareButton>
                        </MDBContainer>
                    </div>
                </div>
            </div>
        </MDBJumbotron>`

Feature request: Support react children elements

Your library perfectly fits into one of my needs and renders hashtags from a string.
But in other place I need to "hashtagify" html string and basically what I need it to be able somehow to parse not only a string child.

Example:

const text = "<p>Here is some content with a #hashtag</p>";

<ReactHashtag renderHashtag={hashtag => <Hashtag hashtag={hashtag} />}>
	<div
        dangerouslySetInnerHTML={{
          __html: text
        }}
      />
</ReactHashtag>

I am wondering is this somehow possible to be added to this package?

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.