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-transform-words

v0.4.0

Published

A React component that transforms individual words in a block of text with css classes and actions.

Downloads

297

Readme

react-transform-words

NPM LICENSE

A React component that transforms individual words in a block of text with css classes and actions.

Install

yarn add react-transform-words

or

npm install --save react-transform-words

Usage

Check out the demo for some simple examples.

import React, { Component } from 'react'

import Transformer from 'react-transform-words'

class ExampleTransformer extends Component {
  matchWords = [
    {
      word: 'fat',
      caseSensitive: true,
    },
    {
      word: 'cultivating mass', // can be a phrase
      action: 'click',
      className: 'clicky-word', // set a custom css class
      actionCallback: () => { console.log('clicked!!') } // captures action (on click)
    }
  ]
  render () {
    return (
       <Transformer
        matchWords={this.matchWords}
        displayText={"I’m not fat. I’m cultivating mass. -Fat Mac"} />
    )
  }
}

The above would result in this:

Example Render

So, we're passing in an array with one word and one phrase to transform.

  • We didn't provide a className for fat, so it used the built-in default.
  • Notice that "fat" isn't matching both occurances because we've passed in caseSensitive: true for that word.

Props

| Property | Type | Required | Default | Description | | :----------- | :------------- | :------: | :----------------- | :----------------------------------------------------------------------------- | | matchWords | Array<Object> | No | | Array of word objects that the transformer will match (see below) | | displayText | String | Yes | | The text to match words within | | defaultClass | String | No | built-in highlight | The class applied to matched words that don't have a class provided themselves |

Search Word Objects

An array of word objects are passed to the Transfomer are used by the component to search for and apply class changes. Each word can have its own options, allowing you to customize each with CSS, actions and more. These are the word object options:

| Option | Type | Options | Default | Description | | :------------- | :------- | :-------------------------------------------: | :----------------- | :-------------------------------------------------------------------------------------------------------------- | | word | String | | | The word to match and transform | | className | String | | props.defaultClass | Applies the className to the word | | action | String | 'click', 'doubleclick', 'mouseover', 'change' | | Adds the action to the word (use with actionCallback or replaceText) | | actionCallback | Function | | | The function to be called when the user triggers the action (ie, clicks the word) | | replaceText | String | | | The text to replace the word with if using the 'change' action | | format | String | 'regex', 'string' | 'string' | If set to 'regex', regular expression characters will not be escaped. This treats the string as a normal regex' | | extraProps | Object | | | Each key/value will be added as attributes to the highlighted word's <span> tag |

Word Object Examples

[
  {
    word: "search for me" // at a minimum, can accept just a word/phrase and the defaultClassName will be used
  },
  {
    word: "search for me", // word to search for, can be a phrase or a single word
    className: "my-custom-class", // class to apply to the word
    action: "doubleclick", // adds onDoubleClick to the word
    actionCallback: handleDoubleClicked, // function will be called when double-clicked
  },
  {
    word: "me too", // word to search for, can be a phrase or a single word
    className: "my-custom-class", // class to apply to the word
    action: "change", // changes the word to replaceText property
    replaceText: "new text" // text that will be used with action 'change'
  }
  {
    word: "\\bexact\\b", // text with regex that matches only the full word 'exact'. Using double-slash to escape
    format: 'regex', // tells the transformer to use the string as regex instead of a normal string, which has regex characters escaped by default
    extraProps: {
      title: "A title for the highlight",
      lang: 'en'
    },
  }
]

License

MIT © codyparker | codyparker.com