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-smart-text

v1.1.0

Published

Replace meaningful substrings with React components.

Downloads

35

Readme

react-smart-text

Replace meaningful substrings with components.

Build Status

This is a complete example of replacing email addresses with mailto anchors.

import React from 'react'
import SmartText from 'react-smart-text'

// This is what we're hunting in the text.
const emailRegex = /\w+@.+?\.(com)/g

// This is what we want to replace matches with.
const Email = (props) =>
  <a href={`mailto:${props.text}`}>{props.text}</a>

const App = () =>
  <SmartText regex={emailRegex} component={Email}>
    My email address is [email protected]. Yours is [email protected].
  </SmartText>

export default App

Result

Usage

Render a plain string within <SmartText /> and substrings matching a pattern will be replaced by a custom component.

Props

regex - Provide a regular expression describing the substring(s) you wish to replace.

component - The component regex matches will be replaced with. Instances will be passed the props

  • result - RegExp.exec result
  • text - the plain text of the match
  • extra props you need passed down (like event handlers), see "componentProps"

outerComponent (optional) - The outer component all other nodes will be contained within. Thid defaults to a plain <div />.

Installation

yarn add react-smart-text

Examples

Using componentProps

const barRegex = (/bar/g)
const Bar = (props) => (
  <div onClick={props.onClick}>
    {props.text}
  </div>
)

class Demo extends React.Component {
  handleBarClick = () => {
    console.log('Bar was clicked')
  }

  render () {
    const barProps = {
      onClick: this.handleBarClick,
    }
    return (
      <SmartText regex={barRegex} component={Bar} componentProps={barProps}>
        foo bar baz
      </SmartText>
    )
  }
}

You can also include componentProps when using multiple replacements.

Multiple Replacement Types

If you want to replace multiple types of strings, provide an array of replacments.

import React from 'react'
import SmartText from 'react-smart-text'

const emailRegex = /\w+@.+?\.(com)/g
const Email = (props) =>
  <a href={`mailto:${props.text}`}>{props.text}</a>

const vowelRegex = /[aeiou]/gi
const Vowel = (props) =>
  <span className="vowel">*</span>

const replacements = [
  {
    regex: emailRegex,
    component: Email,
  },
  {
    regex: vowelRegex,
    component: Vowel,
    componentProps: {
      onClick: () => console.log('AEIOU'),
    },
  },
]

const App = () =>
  <SmartText replacements={replacements}>
    My email address is [email protected]. Yours is [email protected].
  </SmartText>

export default App

Result

Note that replacements only happen on text nodes. If a replacment has already happened for a section of text, it will not be processed again. This is why the vowels are visible in the emails above. This may change in a future version.

Test

yarn test


kickstarted by npm-boom