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-highlightable-input

v1.1.7

Published

A custom react input component that allows highlighting or styling of specific text as it is typed. For example, you can highlight mentions starting with @ symbol in a text input as the user types them.

Downloads

53

Readme

react-highlightable-input NPM Version NPM Downloads GitHub License

A custom react input component that allows highlighting or styling of specific text as it is typed. For example, you can highlight mentions starting with @ symbol in a text input as the user types them.

Install

npm install react-highlightable-input

Usage

import {
 HighlightableTextInput,
 highlightMentions,
 cleanHtml,
} from "react-highlightable-input";
import { useRef, useState } from "react";
import "./App.css";

function App() {
 const inputRef = useRef<HTMLDivElement>(null);
 const shadowRef = useRef<HTMLDivElement>(null);
 const [highlightedContent, setHiglightedContent] = useState<string>("");

 const onInput = () => {
  if (inputRef.current) {
   const value = inputRef.current.innerHTML;
   const highlighted = highlightMentions(value, /@[\w]+/g, "#1d9bf0");
   setHiglightedContent(highlighted);
  }
 };

 const onSend = () => {
  console.log("Send message:", cleanHtml(highlightedContent));
 };

 return (
  <div>
   <div
    style={{
     border: "1px solid black",
     padding: "5px",
    }}>
    <HighlightableTextInput
     inputRef={inputRef}
     shadowRef={shadowRef}
     highlightedContent={highlightedContent}
     setHiglightedContent={setHiglightedContent}
     onInput={onInput}
     placeholderText="Highlight mentions with @ symbol"
    />
   </div>
   <button
    onClick={onSend}
    style={{
     backgroundColor: "#4caf50",
     color: "white",
     padding: "5px 10px",
     border: "none",
     cursor: "pointer",
     alignSelf: "flex-start",
     marginTop: "10px",
    }}>
    Send
   </button>
  </div>
 );
}

export default App;

A closer look at the HighlightableTextInput component

<HighlightableTextInput
 inputRef={inputRef} // Ref to the input element
 shadowRef={shadowRef} // Ref to the shadow element
 highlightedContent={highlightedContent} // The content to be highlighted
 setHiglightedContent={setHiglightedContent} // Function to set the highlighted content
 onInput={onInput} // Function to be called when the input changes
/>

Props

| Prop Name | Type | Description | | ---------------------- | -------------------------------------------------- | -------------------------------------------- | | inputRef | React.RefObject<HTMLDivElement> | A reference to the input element. | | shadowRef | React.RefObject<HTMLDivElement> | A reference to the shadow element. | | highlightedContent | string | The content that should be highlighted. | | placeholderText | string (optional) | The placeholder text for the input element. | | onInput | (event: React.FormEvent<HTMLDivElement>) => void | The event handler for the input event. | | setHiglightedContent | (highlighted: string) => void | The function to set the highlighted content. | | style | React.CSSProperties (optional) | The CSS styles for the input element. |

export interface HighlightableTextInputProps {
 inputRef: React.RefObject<HTMLDivElement>;
 shadowRef: React.RefObject<HTMLDivElement>;
 highlightedContent: string;
 placeholderText?: string;
 onInput: (event: React.FormEvent<HTMLDivElement>) => void;
 setHiglightedContent: (highlighted: string) => void;
 style?: React.CSSProperties;
}

Highlight Functions

highlightMentions

function highlightMentions(
 text: string,
 mentionPattern: RegExp,
 highlightColor: string
): string;

This function takes a string of text, a regular expression pattern to match mentions, and a color to highlight the mentions. It returns a new string where the mentions are wrapped in a span tag with the specified color.

Parameters:

  • text: The input text.
  • mentionPattern: The regular expression pattern to match mentions.
  • highlightColor: The color to highlight the mentions.

Returns:

A new string where the mentions are highlighted with the specified color.


highlightText

function highlightText(
 text: string,
 pattern: RegExp,
 style: React.CSSProperties
): string;

This function takes a string of text, a regular expression pattern to match mentions, and a style object to style the mentions. It returns a new string where the mentions are wrapped in a span tag with the specified styles.

Parameters:

  • text: The input text.
  • mentionPattern: The regular expression pattern to match mentions.
  • style: The style object to style the mentions. This should be a React.CSSProperties object.

Returns:

A new string where the mentions are styled with the specified styles.

Utility Functions

cleanHtml

function cleanHtml(html: string): string

Parameters:

  • html: The input HTML string.

Returns:

A new string where all HTML tags are removed, <div> tags are replaced with newlines, and &lt; and &gt; entities are replaced with < and > characters, respectively.

Usage Instructions for the HighlightableTextInput component

1. Setting border and padding

If you want to add the border to the react-highlightable-input then wrap the HighlightableTextInput component with a div and add the border to the div element. Same goes for other styles like padding margin. Otherwise some unexpected styling issues may occur.

<div style={{ border: "1px solid black", padding: "5px" }}>
 <HighlightableTextInput
  inputRef={inputRef}
  shadowRef={shadowRef}
  highlightedContent={highlightedContent}
  setHiglightedContent={setHiglightedContent}
  onInput={onInput}
 />
</div>

2. Setting height and width

Width and height of the input div can be set in two ways

1. Via style props

<div style={{ border: "1px solid black", padding: "5px" }}>
 <HighlightableTextInput
  inputRef={inputRef}
  shadowRef={shadowRef}
  highlightedContent={highlightedContent}
  setHiglightedContent={setHiglightedContent}
  onInput={onInput}
  style={{
   width: "500px",
   height: "300px",
  }}
 />
</div>

2. Via HighlightableTextInput div id (highlightableTextInput-container)

#highlightableTextInput-container {
 width: 500px;
 height: 300px;
}
<div style={{ border: "1px solid black", padding: "5px", display: "inline-block" }}>
  <HighlightableTextInput
    inputRef={inputRef}
    shadowRef={shadowRef}
    highlightedContent={highlightedContent}
    setHiglightedContent={setHiglightedContent}
    onInput={onInput}
</div>

Note: The display: inline-block is used to make the div element take the width and height of the child element.

Code Sandbox

Edit react-highlightable-input-demo