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-substring-styler

v0.0.2

Published

React text component to style substrings based on patterns provided.

Downloads

7

Readme

The react-substring-styler is a package designed to simplify the styling of substrings within a larger string in React applications. It enables developers to apply styles to specific substrings without breaking the structure of the overall text or resorting to manual numerous HTML text tags. It is also possible to apply any desired attribute from the HTML text element <span> to any of the substrings.

Installation

Install the package using npm:

npm install react-substring-styler

or using yarn:

yarn add react-substring-styler

Usage

The package provides a <ParsedText> component that accepts a pattern prop to specify the type of pattern, its associated styling, testID and any <span> default property for targeted substrings.

Default patterns example

Code

import React from "react";
import { ParsedText, ParsedTextProps } from "react-substring-styler";

const DEFAULT_TEXT_VALUE =
  "Make sure to send an email to [email protected] or access our website https://www.testing.com if have any {trouble} using our [code].";
const MOCK_ON_PRESS = () => console.log("onPress");
const DEFAULT_TEXT_TEST_ID = "DEFAULT_TEXT_TEST_ID";

const defaultProps: ParsedTextProps = {
  patterns: [
    { type: "email", style: { color: "blue" }, testID: "emailTestID" },
    {
      type: "url",
      style: { color: "green" },
      onClick: MOCK_ON_PRESS,
      testID: "urlTestID",
    },
    {
      type: "curlyBrackets",
      style: { fontSize: 16, fontWeight: "bold" },
      testID: "curlyBracketsTestID",
    },
    {
      type: "squareBrackets",
      style: {
        fontWeight: "bold",
        fontStyle: "italic",
        color: "purple",
        fontSize: 24,
      },
      testID: "squareBracketsTestID",
    },
  ],
  testID: DEFAULT_TEXT_TEST_ID,
};

const DefaultPatternsExample = () => {
  return <ParsedText {...defaultProps}>{DEFAULT_TEXT_VALUE}</ParsedText>;
};

Preview

image

Custom pattern example

Developers can create custom patterns using the <ParsedText> component by defining a new pattern, its corresponding styling, and rendering behavior.

Guidelines for Custom Patterns:

  1. Define Pattern: Use a regular expression RegExp to define the pattern that identifies the substring to be styled;
  2. Render Text: Implement the renderText function to modify the matched string as you wish before rendering;
  3. Styling: Specify the desired style object to be applied to the matched substring.

Feel free to create multiple custom patterns by adding more objects to the patterns prop within the <ParsedText> component, allowing for versatile styling options within your strings.

Code

import React from "react";
import { ParsedText, ParsedTextProps } from "react-substring-styler";

const DEFAULT_TEXT_VALUE = "this text is using the |custom pattern| to style";

const defaultProps: ParsedTextProps = {
  patterns: [
    {
      pattern: /\|(.*?)\|/, // Define the regex pattern to match
      renderText: (matchingString: string) =>
        matchingString.replace(/\|/g, "").replace(/}/g, ""), // Define how the matched text should be rendered
      style: { color: "cyan", fontWeight: "bolder", fontSize: 24 }, // Define the style for the matched text
    },
  ],
};

const CustomPatternExample = () => {
  return <ParsedText {...defaultProps}>{DEFAULT_TEXT_VALUE}</ParsedText>;
};

Preview

image

Props

| Prop | Description | Default | | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- | | patterns | Array of patterns, corresponding styles, testID or any attribute from the <span> element that will be applied to the corresponding substrings | [ ] | | testID | Value that will be set for the whole string | undefined | | ...rest | You can set any attribute from the <span> element for the whole string |

<span>

If you want to know more about the <span> element and its attributes, make sure to check the Mozilla Doc.

Contributions

Contributions and feedback are welcome! Feel free to open issues or submit pull requests on the GitHub repository for this project.