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

v1.0.1

Published

Render placeholders from text with custom React components

Downloads

7

Readme

react-text-subst

NPM latest NPM next Build Status

How will you add i18n support in following cases?

<span>Hello <span>{username}</span>!</span>
<span>
    By clicking “Sign up”, you agree
    to our <a href="...">terms of service</a> and...
</span>

Sure, you just can split texts in parts, but that parts would be translated not properly.

Here is a solution:

import TextSubst from 'react-text-subst';

return (
    <TextSubst user={<span>{username}</span>}>
        Hello @[user]!
    </TextSubst>
);
<TextSubst link={({children}) => <a href="...">{children}</a>}>
    By clicking “Sign up”, you agree to our @[link[terms of service]] and...
</TextSubst>

Now you can simply add i18n to patterns and translate them.

Install

npm i --save react-text-subst

Pattern syntax

Pattern text has following special tokens:

  • @[foo] - inline node. Render corresponding standalone value, element or component.
  • @[foo[ - start block node. Render a component wrapped around block's content.
  • ]] - end corresponding block node.

Notice: Since React has special props key and ref, you shouldn't use such names in pattern.

Nodes

So, there are few types of nodes. Nodes of any type can repeat or share its' names - everything is up to you.

Inline @[foo]

Inline node can render anything what React can render as a node, or component. The last will receive following property:

  • name - corresponding node name (foo in example).
<TextSubst
    bar={something}
    baz={<b>{something}</b>}
    qux={({name}) => <b>value of {name}</b>}
>
    foo @[bar] lol @[baz]@[qux].
</TextSubst>
// <>
//     foo {something} lol <b>{something}</b><b>value of qux</b>
// </>

Block @[foo[...]]

Block node can only render a component. It will receive following properties:

  • name - corresponding node name (foo in example).
  • children - rendered content of the block.

Blocks can be nested.

<TextSubst
    b={({children}) => <b>{children}</b>}
    i={({children}) => <i>{children}</i>}
>
    lorem @[b[ipsum @[i[dolor]] sit]] amet @[i[consectep@[b[ture]]]]
</TextSubst>
// <>
//     lorem <b>ipsum <i>dolor</i> sit</b> amet <i>consectep<b>ture</b></i>
// </>
<TextSubst foo={({children}) => <span>{children}</span>}>
    lorem @[foo[ipsum @[foo[dolor]] sit]] amet
</TextSubst>
// <>
//     lorem <span>ipsum <span>dolor</span> sit</span> amet
// </>
const Link = ({name, children}) => <a href={URL[name]}>{children}</a>;
return (
    <TextSubst foo={Link} bar={Link}>
        lorem @[foo[ipsum]] dolor @[bar[sit]] amet
    </TextSubst>
);
// <>
//     lorem <a href={URL.foo}>ipsum</a> dolor <a href={URL.bar}>sit</a> amet
// </>

API

Properties

  • children - pattern string to render, only single string is acceptable:

    <TextSubst>Lorem ipsum dolor</TextSubst>
    <TextSubst>{'Lorem ipsum dolor'}</TextSubst>
    <TextSubst>{i18n('Lorem ipsum dolor')}</TextSubst>
  • ... rest - value, element or component to render nodes with corresponding name.

    Notice: You cannot use React specific props like key or ref for values.

License

This package is under MIT License